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