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