1 | |
package org.apache.maven.wagon.tck.http.fixture; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import static org.apache.maven.wagon.tck.http.util.TestUtil.getResource; |
23 | |
|
24 | |
import org.mortbay.jetty.Connector; |
25 | |
import org.mortbay.jetty.Handler; |
26 | |
import org.mortbay.jetty.Server; |
27 | |
import org.mortbay.jetty.handler.DefaultHandler; |
28 | |
import org.mortbay.jetty.handler.HandlerCollection; |
29 | |
import org.mortbay.jetty.nio.SelectChannelConnector; |
30 | |
import org.mortbay.jetty.security.Constraint; |
31 | |
import org.mortbay.jetty.security.ConstraintMapping; |
32 | |
import org.mortbay.jetty.security.HashUserRealm; |
33 | |
import org.mortbay.jetty.security.SecurityHandler; |
34 | |
import org.mortbay.jetty.security.SslSocketConnector; |
35 | |
import org.mortbay.jetty.servlet.AbstractSessionManager; |
36 | |
import org.mortbay.jetty.servlet.FilterHolder; |
37 | |
import org.mortbay.jetty.servlet.FilterMapping; |
38 | |
import org.mortbay.jetty.servlet.ServletHolder; |
39 | |
import org.mortbay.jetty.servlet.SessionHandler; |
40 | |
import org.mortbay.jetty.webapp.WebAppContext; |
41 | |
|
42 | |
import java.io.File; |
43 | |
import java.io.IOException; |
44 | |
import java.net.URISyntaxException; |
45 | |
|
46 | |
import javax.servlet.Filter; |
47 | |
import javax.servlet.Servlet; |
48 | |
|
49 | |
public class ServerFixture |
50 | |
{ |
51 | |
public static final String SERVER_ROOT_RESOURCE_PATH = "default-server-root"; |
52 | |
|
53 | |
public static final String SERVER_SSL_KEYSTORE_RESOURCE_PATH = "ssl/keystore"; |
54 | |
|
55 | |
public static final String SERVER_SSL_KEYSTORE_PASSWORD = "password"; |
56 | |
|
57 | |
public static final String SERVER_HOST = "localhost"; |
58 | |
|
59 | |
private final Server server; |
60 | |
|
61 | |
private final WebAppContext webappContext; |
62 | |
|
63 | |
private final HashUserRealm securityRealm; |
64 | |
|
65 | |
private final SecurityHandler securityHandler; |
66 | |
|
67 | 0 | private int filterCount = 0;; |
68 | |
|
69 | |
public ServerFixture( final int port, final boolean ssl ) |
70 | |
throws URISyntaxException, IOException |
71 | 0 | { |
72 | 0 | server = new Server(); |
73 | 0 | if ( ssl ) |
74 | |
{ |
75 | 0 | SslSocketConnector connector = new SslSocketConnector(); |
76 | 0 | String keystore = getResource( SERVER_SSL_KEYSTORE_RESOURCE_PATH ).getAbsolutePath(); |
77 | |
|
78 | |
|
79 | 0 | connector.setPort( port ); |
80 | 0 | connector.setKeystore( keystore ); |
81 | 0 | connector.setPassword( SERVER_SSL_KEYSTORE_PASSWORD ); |
82 | 0 | connector.setKeyPassword( SERVER_SSL_KEYSTORE_PASSWORD ); |
83 | |
|
84 | 0 | server.addConnector( connector ); |
85 | 0 | } |
86 | |
else |
87 | |
{ |
88 | 0 | Connector connector = new SelectChannelConnector(); |
89 | 0 | connector.setHost( "localhost" ); |
90 | 0 | connector.setPort( port ); |
91 | 0 | server.addConnector( connector ); |
92 | |
} |
93 | |
|
94 | 0 | Constraint constraint = new Constraint(); |
95 | 0 | constraint.setName( Constraint.__BASIC_AUTH ); |
96 | |
|
97 | 0 | constraint.setRoles( new String[] { "allowed" } ); |
98 | 0 | constraint.setAuthenticate( true ); |
99 | |
|
100 | 0 | ConstraintMapping cm = new ConstraintMapping(); |
101 | 0 | cm.setConstraint( constraint ); |
102 | 0 | cm.setPathSpec( "/protected/*" ); |
103 | |
|
104 | 0 | securityHandler = new SecurityHandler(); |
105 | |
|
106 | 0 | securityRealm = new HashUserRealm( "Test Server" ); |
107 | |
|
108 | 0 | securityHandler.setUserRealm( securityRealm ); |
109 | 0 | securityHandler.setConstraintMappings( new ConstraintMapping[] { cm } ); |
110 | |
|
111 | 0 | webappContext = new WebAppContext(); |
112 | 0 | webappContext.setContextPath( "/" ); |
113 | |
|
114 | 0 | File base = getResource( SERVER_ROOT_RESOURCE_PATH ); |
115 | 0 | System.out.println( "docroot: " + base ); |
116 | 0 | webappContext.setWar( base.getAbsolutePath() ); |
117 | 0 | webappContext.addHandler( securityHandler ); |
118 | |
|
119 | 0 | SessionHandler sessionHandler = webappContext.getSessionHandler(); |
120 | 0 | ( (AbstractSessionManager) sessionHandler.getSessionManager() ).setUsingCookies( false ); |
121 | |
|
122 | 0 | HandlerCollection handlers = new HandlerCollection(); |
123 | 0 | handlers.setHandlers( new Handler[] { webappContext, new DefaultHandler() } ); |
124 | |
|
125 | 0 | server.setHandler( handlers ); |
126 | 0 | } |
127 | |
|
128 | |
public void addFilter( final String pathSpec, final Filter filter ) |
129 | |
{ |
130 | 0 | String name = "filter" + filterCount++; |
131 | |
|
132 | 0 | FilterMapping fm = new FilterMapping(); |
133 | 0 | fm.setPathSpec( pathSpec ); |
134 | 0 | fm.setFilterName( name ); |
135 | |
|
136 | 0 | FilterHolder fh = new FilterHolder( filter ); |
137 | 0 | fh.setName( name ); |
138 | |
|
139 | 0 | webappContext.getServletHandler().addFilter( fh, fm ); |
140 | 0 | } |
141 | |
|
142 | |
public void addServlet( final String pathSpec, final Servlet servlet ) |
143 | |
{ |
144 | 0 | webappContext.getServletHandler().addServletWithMapping( new ServletHolder( servlet ), pathSpec ); |
145 | 0 | } |
146 | |
|
147 | |
public void addUser( final String user, final String password ) |
148 | |
{ |
149 | 0 | securityRealm.put( user, password ); |
150 | 0 | securityRealm.addUserToRole( user, "allowed" ); |
151 | 0 | } |
152 | |
|
153 | |
public Server getServer() |
154 | |
{ |
155 | 0 | return server; |
156 | |
} |
157 | |
|
158 | |
public WebAppContext getWebappContext() |
159 | |
{ |
160 | 0 | return webappContext; |
161 | |
} |
162 | |
|
163 | |
public void stop() |
164 | |
throws Exception |
165 | |
{ |
166 | 0 | if ( server != null ) |
167 | |
{ |
168 | 0 | server.stop(); |
169 | |
} |
170 | 0 | } |
171 | |
|
172 | |
public void start() |
173 | |
throws Exception |
174 | |
{ |
175 | 0 | server.start(); |
176 | |
|
177 | 0 | int total = 0; |
178 | 0 | while ( total < 3000 && !server.isStarted() ) |
179 | |
{ |
180 | 0 | server.wait( 10 ); |
181 | 0 | total += 10; |
182 | |
} |
183 | |
|
184 | 0 | if ( !server.isStarted() ) |
185 | |
{ |
186 | 0 | throw new IllegalStateException( "Server didn't start in: " + total + "ms." ); |
187 | |
} |
188 | 0 | } |
189 | |
|
190 | |
} |