1 package org.apache.maven.index.updater.fixtures;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.mortbay.jetty.Connector;
23 import org.mortbay.jetty.Handler;
24 import org.mortbay.jetty.Server;
25 import org.mortbay.jetty.handler.DefaultHandler;
26 import org.mortbay.jetty.handler.HandlerCollection;
27 import org.mortbay.jetty.nio.SelectChannelConnector;
28 import org.mortbay.jetty.security.Constraint;
29 import org.mortbay.jetty.security.ConstraintMapping;
30 import org.mortbay.jetty.security.HashUserRealm;
31 import org.mortbay.jetty.security.SecurityHandler;
32 import org.mortbay.jetty.servlet.AbstractSessionManager;
33 import org.mortbay.jetty.servlet.SessionHandler;
34 import org.mortbay.jetty.webapp.WebAppContext;
35
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.OutputStream;
41 import java.net.URISyntaxException;
42 import java.net.URL;
43
44 import javax.servlet.ServletException;
45 import javax.servlet.http.HttpServlet;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
49 public class ServerTestFixture
50 {
51
52 private static final String SERVER_ROOT_RESOURCE_PATH = "index-updater/server-root";
53
54 private static final String SIXTY_TWO_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
55
56 public static final String LONG_PASSWORD = SIXTY_TWO_CHARS + SIXTY_TWO_CHARS;
57
58 private final Server server;
59
60 public ServerTestFixture( final int port )
61 throws Exception
62 {
63 server = new Server();
64
65 Connector connector = new SelectChannelConnector();
66 connector.setPort( port );
67
68 server.setConnectors( new Connector[] { connector } );
69
70 Constraint constraint = new Constraint();
71 constraint.setName( Constraint.__BASIC_AUTH );
72
73 constraint.setRoles( new String[] { "allowed" } );
74 constraint.setAuthenticate( true );
75
76 ConstraintMapping cm = new ConstraintMapping();
77 cm.setConstraint( constraint );
78 cm.setPathSpec( "/protected/*" );
79
80 SecurityHandler sh = new SecurityHandler();
81
82 HashUserRealm realm = new HashUserRealm( "POC Server" );
83 realm.put( "user", "password" );
84 realm.put( "longuser", LONG_PASSWORD );
85 realm.addUserToRole( "user", "allowed" );
86 realm.addUserToRole( "longuser", "allowed" );
87
88 sh.setUserRealm( realm );
89 sh.setConstraintMappings( new ConstraintMapping[] { cm } );
90
91 WebAppContext ctx = new WebAppContext();
92 ctx.setContextPath( "/" );
93
94 File base = getBase();
95 ctx.setWar( base.getAbsolutePath() );
96 ctx.addHandler( sh );
97
98 ctx.getServletHandler().addServletWithMapping( TimingServlet.class, "/slow/*" );
99 ctx.getServletHandler().addServletWithMapping( InfiniteRedirectionServlet.class, "/redirect-trap/*" );
100
101 SessionHandler sessionHandler = ctx.getSessionHandler();
102 ( (AbstractSessionManager) sessionHandler.getSessionManager() ).setUsingCookies( false );
103
104 HandlerCollection handlers = new HandlerCollection();
105 handlers.setHandlers( new Handler[] { ctx, new DefaultHandler() } );
106
107 server.setHandler( handlers );
108 server.start();
109 }
110
111 private static File getBase()
112 throws URISyntaxException
113 {
114 URL resource = Thread.currentThread().getContextClassLoader().getResource( SERVER_ROOT_RESOURCE_PATH );
115 if ( resource == null )
116 {
117 throw new IllegalStateException( "Cannot find classpath resource: " + SERVER_ROOT_RESOURCE_PATH );
118 }
119
120 return new File( resource.toURI().normalize() );
121 }
122
123 public void stop()
124 throws Exception
125 {
126 server.stop();
127 server.join();
128 }
129
130 public static final class TimingServlet
131 extends HttpServlet
132 {
133 private static final long serialVersionUID = 1L;
134
135 @Override
136 protected void doGet( final HttpServletRequest req, final HttpServletResponse resp )
137 throws ServletException, IOException
138 {
139 String basePath = req.getServletPath();
140 String subPath = req.getRequestURI().substring( basePath.length() );
141
142 File base;
143 try
144 {
145 base = getBase();
146 }
147 catch ( URISyntaxException e )
148 {
149 resp.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
150 "Cannot find server document root in classpath: " + SERVER_ROOT_RESOURCE_PATH );
151 return;
152 }
153
154 File f = new File( base, "slow" + subPath );
155 InputStream in = null;
156 try
157 {
158 in = new FileInputStream( f );
159 OutputStream out = resp.getOutputStream();
160
161 int read = -1;
162 byte[] buf = new byte[64];
163 while ( ( read = in.read( buf ) ) > -1 )
164 {
165 System.out.println( "Sending " + read + " bytes (after pausing 1 seconds)..." );
166 try
167 {
168 Thread.sleep( 1000 );
169 }
170 catch ( InterruptedException e )
171 {
172 }
173
174 out.write( buf, 0, read );
175 }
176
177 out.flush();
178 }
179 finally
180 {
181 if ( in != null )
182 {
183 try
184 {
185 in.close();
186 }
187 catch ( IOException e )
188 {
189 }
190 }
191 }
192 }
193 }
194
195 public static final class InfiniteRedirectionServlet
196 extends HttpServlet
197 {
198 private static final long serialVersionUID = 1L;
199
200 static int redirCount = 0;
201
202 @Override
203 protected void doGet( final HttpServletRequest req, final HttpServletResponse resp )
204 throws ServletException, IOException
205 {
206 String path = req.getServletPath();
207 String subPath = req.getRequestURI().substring( path.length() );
208
209 path += subPath + "-" + ( ++redirCount );
210 resp.sendRedirect( path );
211 }
212 }
213
214 }