1 | |
package org.apache.maven.wagon.http; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.wagon.FileTestUtils; |
23 | |
import org.apache.maven.wagon.ResourceDoesNotExistException; |
24 | |
import org.apache.maven.wagon.StreamingWagon; |
25 | |
import org.apache.maven.wagon.StreamingWagonTestCase; |
26 | |
import org.apache.maven.wagon.TransferFailedException; |
27 | |
import org.apache.maven.wagon.Wagon; |
28 | |
import org.apache.maven.wagon.authentication.AuthenticationInfo; |
29 | |
import org.apache.maven.wagon.authorization.AuthorizationException; |
30 | |
import org.apache.maven.wagon.proxy.ProxyInfo; |
31 | |
import org.apache.maven.wagon.repository.Repository; |
32 | |
import org.apache.maven.wagon.resource.Resource; |
33 | |
import org.codehaus.plexus.util.FileUtils; |
34 | |
import org.codehaus.plexus.util.IOUtil; |
35 | |
import org.codehaus.plexus.util.StringOutputStream; |
36 | |
import org.mortbay.jetty.Handler; |
37 | |
import org.mortbay.jetty.HttpConnection; |
38 | |
import org.mortbay.jetty.Request; |
39 | |
import org.mortbay.jetty.Response; |
40 | |
import org.mortbay.jetty.Server; |
41 | |
import org.mortbay.jetty.handler.AbstractHandler; |
42 | |
import org.mortbay.jetty.handler.HandlerCollection; |
43 | |
import org.mortbay.jetty.security.Constraint; |
44 | |
import org.mortbay.jetty.security.ConstraintMapping; |
45 | |
import org.mortbay.jetty.security.HashUserRealm; |
46 | |
import org.mortbay.jetty.security.SecurityHandler; |
47 | |
import org.mortbay.jetty.servlet.Context; |
48 | |
import org.mortbay.jetty.servlet.DefaultServlet; |
49 | |
import org.mortbay.jetty.servlet.ServletHolder; |
50 | |
|
51 | |
import javax.servlet.ServletException; |
52 | |
import javax.servlet.ServletInputStream; |
53 | |
import javax.servlet.http.HttpServletRequest; |
54 | |
import javax.servlet.http.HttpServletResponse; |
55 | |
import java.io.File; |
56 | |
import java.io.FileOutputStream; |
57 | |
import java.io.IOException; |
58 | |
import java.io.OutputStream; |
59 | |
import java.lang.reflect.Method; |
60 | |
import java.net.URLDecoder; |
61 | |
import java.util.ArrayList; |
62 | |
import java.util.Collections; |
63 | |
import java.util.Enumeration; |
64 | |
import java.util.HashMap; |
65 | |
import java.util.List; |
66 | |
import java.util.Map; |
67 | |
import java.util.Properties; |
68 | |
import java.util.zip.GZIPOutputStream; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | 0 | public abstract class HttpWagonTestCase |
74 | |
extends StreamingWagonTestCase |
75 | |
{ |
76 | |
private Server server; |
77 | |
|
78 | |
protected void setupWagonTestingFixtures() |
79 | |
throws Exception |
80 | |
{ |
81 | |
|
82 | |
|
83 | 0 | File file = FileTestUtils.createUniqueFile( "local-repository", "test-resource" ); |
84 | |
|
85 | 0 | file.delete(); |
86 | |
|
87 | 0 | file.getParentFile().mkdirs(); |
88 | |
|
89 | 0 | File repositoryDirectory = getRepositoryDirectory(); |
90 | 0 | FileUtils.deleteDirectory( repositoryDirectory ); |
91 | 0 | repositoryDirectory.mkdirs(); |
92 | |
|
93 | 0 | server = new Server( 0 ); |
94 | |
|
95 | 0 | PutHandler putHandler = new PutHandler( repositoryDirectory ); |
96 | 0 | server.addHandler( putHandler ); |
97 | |
|
98 | 0 | createContext( server, repositoryDirectory ); |
99 | |
|
100 | 0 | addConnectors( server ); |
101 | |
|
102 | 0 | server.start(); |
103 | |
|
104 | 0 | testRepository.setUrl( getTestRepositoryUrl() ); |
105 | 0 | } |
106 | |
|
107 | |
@Override |
108 | |
protected final int getTestRepositoryPort() |
109 | |
{ |
110 | 0 | if ( server == null ) |
111 | |
{ |
112 | 0 | return 0; |
113 | |
} |
114 | 0 | return server.getConnectors()[0].getLocalPort(); |
115 | |
} |
116 | |
|
117 | |
protected void createContext( Server server, File repositoryDirectory ) |
118 | |
throws IOException |
119 | |
{ |
120 | 0 | Context root = new Context( server, "/", Context.SESSIONS ); |
121 | 0 | root.setResourceBase( repositoryDirectory.getAbsolutePath() ); |
122 | 0 | ServletHolder servletHolder = new ServletHolder( new DefaultServlet() ); |
123 | 0 | root.addServlet( servletHolder, "/*" ); |
124 | 0 | } |
125 | |
|
126 | |
protected void tearDownWagonTestingFixtures() |
127 | |
throws Exception |
128 | |
{ |
129 | 0 | server.stop(); |
130 | 0 | } |
131 | |
|
132 | |
public void testWagonGetFileList() |
133 | |
throws Exception |
134 | |
{ |
135 | 0 | File dir = getRepositoryDirectory(); |
136 | 0 | FileUtils.deleteDirectory( dir ); |
137 | |
|
138 | 0 | File f = new File( dir, "file-list" ); |
139 | 0 | f.mkdirs(); |
140 | |
|
141 | 0 | super.testWagonGetFileList(); |
142 | 0 | } |
143 | |
|
144 | |
public void testHttpHeaders() |
145 | |
throws Exception |
146 | |
{ |
147 | 0 | Properties properties = new Properties(); |
148 | 0 | properties.setProperty( "User-Agent", "Maven-Wagon/1.0" ); |
149 | |
|
150 | 0 | StreamingWagon wagon = (StreamingWagon) getWagon(); |
151 | |
|
152 | 0 | setHttpHeaders( wagon, properties ); |
153 | |
|
154 | 0 | Server server = new Server( 0 ); |
155 | 0 | TestHeaderHandler handler = new TestHeaderHandler(); |
156 | 0 | server.setHandler( handler ); |
157 | 0 | addConnectors( server ); |
158 | 0 | server.start(); |
159 | |
|
160 | 0 | wagon.connect( |
161 | |
new Repository( "id", getProtocol() + "://localhost:" + server.getConnectors()[0].getLocalPort() ) ); |
162 | |
|
163 | 0 | wagon.getToStream( "resource", new StringOutputStream() ); |
164 | |
|
165 | 0 | wagon.disconnect(); |
166 | |
|
167 | 0 | server.stop(); |
168 | |
|
169 | 0 | assertEquals( "Maven-Wagon/1.0", handler.headers.get( "User-Agent" ) ); |
170 | 0 | } |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public void testHttpHeadersWithCommonMethods() |
176 | |
throws Exception |
177 | |
{ |
178 | 0 | Properties properties = new Properties(); |
179 | 0 | properties.setProperty( "User-Agent", "Maven-Wagon/1.0" ); |
180 | |
|
181 | 0 | StreamingWagon wagon = (StreamingWagon) getWagon(); |
182 | |
|
183 | 0 | Method setHttpHeaders = wagon.getClass().getMethod( "setHttpHeaders", Properties.class ); |
184 | 0 | setHttpHeaders.invoke( wagon, properties ); |
185 | |
|
186 | 0 | Server server = new Server( 0 ); |
187 | 0 | TestHeaderHandler handler = new TestHeaderHandler(); |
188 | 0 | server.setHandler( handler ); |
189 | 0 | addConnectors( server ); |
190 | 0 | server.start(); |
191 | |
|
192 | 0 | wagon.connect( |
193 | |
new Repository( "id", getProtocol() + "://localhost:" + server.getConnectors()[0].getLocalPort() ) ); |
194 | |
|
195 | 0 | wagon.getToStream( "resource", new StringOutputStream() ); |
196 | |
|
197 | 0 | wagon.disconnect(); |
198 | |
|
199 | 0 | server.stop(); |
200 | |
|
201 | 0 | assertEquals( "Maven-Wagon/1.0", handler.headers.get( "User-Agent" ) ); |
202 | 0 | } |
203 | |
|
204 | |
protected abstract void setHttpHeaders( StreamingWagon wagon, Properties properties ); |
205 | |
|
206 | |
protected void addConnectors( Server server ) |
207 | |
{ |
208 | 0 | } |
209 | |
|
210 | |
protected String getRepositoryUrl( Server server ) |
211 | |
{ |
212 | 0 | int localPort = server.getConnectors()[0].getLocalPort(); |
213 | 0 | return getProtocol() + "://localhost:" + localPort; |
214 | |
} |
215 | |
|
216 | |
public void testGetForbidden() |
217 | |
throws Exception |
218 | |
{ |
219 | |
try |
220 | |
{ |
221 | 0 | runTestGet( HttpServletResponse.SC_FORBIDDEN ); |
222 | 0 | fail(); |
223 | |
} |
224 | 0 | catch ( AuthorizationException e ) |
225 | |
{ |
226 | 0 | assertTrue( true ); |
227 | 0 | } |
228 | 0 | } |
229 | |
|
230 | |
public void testGet404() |
231 | |
throws Exception |
232 | |
{ |
233 | |
try |
234 | |
{ |
235 | 0 | runTestGet( HttpServletResponse.SC_NOT_FOUND ); |
236 | 0 | fail(); |
237 | |
} |
238 | 0 | catch ( ResourceDoesNotExistException e ) |
239 | |
{ |
240 | 0 | assertTrue( true ); |
241 | 0 | } |
242 | 0 | } |
243 | |
|
244 | |
public void testGet500() |
245 | |
throws Exception |
246 | |
{ |
247 | |
try |
248 | |
{ |
249 | 0 | runTestGet( HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); |
250 | 0 | fail(); |
251 | |
} |
252 | 0 | catch ( TransferFailedException e ) |
253 | |
{ |
254 | 0 | assertTrue( true ); |
255 | 0 | } |
256 | 0 | } |
257 | |
|
258 | |
private void runTestGet( int status ) |
259 | |
throws Exception |
260 | |
{ |
261 | 0 | StreamingWagon wagon = (StreamingWagon) getWagon(); |
262 | |
|
263 | 0 | Server server = new Server( 0 ); |
264 | 0 | StatusHandler handler = new StatusHandler(); |
265 | 0 | handler.setStatusToReturn( status ); |
266 | 0 | server.setHandler( handler ); |
267 | 0 | addConnectors( server ); |
268 | 0 | server.start(); |
269 | |
|
270 | 0 | wagon.connect( new Repository( "id", getRepositoryUrl( server ) ) ); |
271 | |
|
272 | |
try |
273 | |
{ |
274 | 0 | wagon.getToStream( "resource", new StringOutputStream() ); |
275 | 0 | fail(); |
276 | |
} |
277 | |
finally |
278 | |
{ |
279 | 0 | wagon.disconnect(); |
280 | |
|
281 | 0 | server.stop(); |
282 | 0 | } |
283 | 0 | } |
284 | |
|
285 | |
public void testResourceExistsForbidden() |
286 | |
throws Exception |
287 | |
{ |
288 | |
try |
289 | |
{ |
290 | 0 | runTestResourceExists( HttpServletResponse.SC_FORBIDDEN ); |
291 | 0 | fail(); |
292 | |
} |
293 | 0 | catch ( AuthorizationException e ) |
294 | |
{ |
295 | 0 | assertTrue( true ); |
296 | 0 | } |
297 | 0 | } |
298 | |
|
299 | |
public void testResourceExists404() |
300 | |
throws Exception |
301 | |
{ |
302 | |
try |
303 | |
{ |
304 | 0 | assertFalse( runTestResourceExists( HttpServletResponse.SC_NOT_FOUND ) ); |
305 | |
} |
306 | 0 | catch ( ResourceDoesNotExistException e ) |
307 | |
{ |
308 | 0 | assertTrue( true ); |
309 | 0 | } |
310 | 0 | } |
311 | |
|
312 | |
public void testResourceExists500() |
313 | |
throws Exception |
314 | |
{ |
315 | |
try |
316 | |
{ |
317 | 0 | runTestResourceExists( HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); |
318 | 0 | fail(); |
319 | |
} |
320 | 0 | catch ( TransferFailedException e ) |
321 | |
{ |
322 | 0 | assertTrue( true ); |
323 | 0 | } |
324 | 0 | } |
325 | |
|
326 | |
private boolean runTestResourceExists( int status ) |
327 | |
throws Exception |
328 | |
{ |
329 | 0 | StreamingWagon wagon = (StreamingWagon) getWagon(); |
330 | |
|
331 | 0 | Server server = new Server( 0 ); |
332 | 0 | StatusHandler handler = new StatusHandler(); |
333 | 0 | handler.setStatusToReturn( status ); |
334 | 0 | server.setHandler( handler ); |
335 | 0 | addConnectors( server ); |
336 | 0 | server.start(); |
337 | |
|
338 | 0 | wagon.connect( new Repository( "id", getRepositoryUrl( server ) ) ); |
339 | |
|
340 | |
try |
341 | |
{ |
342 | 0 | return wagon.resourceExists( "resource" ); |
343 | |
} |
344 | |
finally |
345 | |
{ |
346 | 0 | wagon.disconnect(); |
347 | |
|
348 | 0 | server.stop(); |
349 | |
} |
350 | |
} |
351 | |
|
352 | |
protected long getExpectedLastModifiedOnGet( Repository repository, Resource resource ) |
353 | |
{ |
354 | 0 | File file = new File( getRepositoryDirectory(), resource.getName() ); |
355 | 0 | return ( file.lastModified() / 1000 ) * 1000; |
356 | |
} |
357 | |
|
358 | |
protected File getRepositoryDirectory() |
359 | |
{ |
360 | 0 | return getTestFile( "target/test-output/http-repository" ); |
361 | |
} |
362 | |
|
363 | |
public void testGzipGet() |
364 | |
throws Exception |
365 | |
{ |
366 | 0 | Server server = new Server( getTestRepositoryPort() ); |
367 | |
|
368 | 0 | String localRepositoryPath = FileTestUtils.getTestOutputDir().toString(); |
369 | 0 | Context root = new Context( server, "/", Context.SESSIONS ); |
370 | 0 | root.setResourceBase( localRepositoryPath ); |
371 | 0 | ServletHolder servletHolder = new ServletHolder( new DefaultServlet() ); |
372 | 0 | servletHolder.setInitParameter( "gzip", "true" ); |
373 | 0 | root.addServlet( servletHolder, "/*" ); |
374 | 0 | addConnectors( server ); |
375 | 0 | server.start(); |
376 | |
|
377 | |
try |
378 | |
{ |
379 | 0 | Wagon wagon = getWagon(); |
380 | |
|
381 | 0 | Repository testRepository = new Repository( "id", getRepositoryUrl( server ) ); |
382 | |
|
383 | 0 | File sourceFile = new File( localRepositoryPath + "/gzip" ); |
384 | |
|
385 | 0 | sourceFile.deleteOnExit(); |
386 | |
|
387 | 0 | String resName = "gzip-res.txt"; |
388 | 0 | String sourceContent = writeTestFileGzip( sourceFile, resName ); |
389 | |
|
390 | 0 | wagon.connect( testRepository ); |
391 | |
|
392 | 0 | File destFile = FileTestUtils.createUniqueFile( getName(), getName() ); |
393 | |
|
394 | 0 | destFile.deleteOnExit(); |
395 | |
|
396 | 0 | wagon.get( "gzip/" + resName, destFile ); |
397 | |
|
398 | 0 | wagon.disconnect(); |
399 | |
|
400 | 0 | String destContent = FileUtils.fileRead( destFile ); |
401 | |
|
402 | 0 | assertEquals( sourceContent, destContent ); |
403 | |
} |
404 | |
finally |
405 | |
{ |
406 | 0 | server.stop(); |
407 | 0 | } |
408 | 0 | } |
409 | |
|
410 | |
public void testProxiedRequest() |
411 | |
throws Exception |
412 | |
{ |
413 | 0 | ProxyInfo proxyInfo = createProxyInfo(); |
414 | 0 | TestHeaderHandler handler = new TestHeaderHandler(); |
415 | |
|
416 | 0 | runTestProxiedRequest( proxyInfo, handler ); |
417 | 0 | } |
418 | |
|
419 | |
public void testProxiedRequestWithAuthentication() |
420 | |
throws Exception |
421 | |
{ |
422 | 0 | ProxyInfo proxyInfo = createProxyInfo(); |
423 | 0 | proxyInfo.setUserName( "user" ); |
424 | 0 | proxyInfo.setPassword( "secret" ); |
425 | 0 | TestHeaderHandler handler = new AuthorizingProxyHandler(); |
426 | |
|
427 | 0 | runTestProxiedRequest( proxyInfo, handler ); |
428 | |
|
429 | 0 | assertTrue( handler.headers.containsKey( "Proxy-Authorization" ) ); |
430 | 0 | } |
431 | |
|
432 | |
private void runTestProxiedRequest( ProxyInfo proxyInfo, TestHeaderHandler handler ) |
433 | |
throws Exception |
434 | |
{ |
435 | |
|
436 | |
|
437 | |
|
438 | 0 | Thread.sleep( 5001L ); |
439 | |
|
440 | 0 | Server proxyServer = new Server( 0 ); |
441 | |
|
442 | 0 | proxyServer.setHandler( handler ); |
443 | |
|
444 | 0 | proxyServer.start(); |
445 | |
|
446 | 0 | proxyInfo.setPort( proxyServer.getConnectors()[0].getLocalPort() ); |
447 | |
|
448 | 0 | System.out.println( |
449 | |
"start proxy on host/port " + proxyInfo.getHost() + "/" + proxyInfo.getPort() + " with non proxyHosts " |
450 | |
+ proxyInfo.getNonProxyHosts() ); |
451 | |
|
452 | 0 | while ( !proxyServer.isRunning() || !proxyServer.isStarted() ) |
453 | |
{ |
454 | 0 | Thread.sleep( 10 ); |
455 | |
} |
456 | |
|
457 | |
try |
458 | |
{ |
459 | 0 | StreamingWagon wagon = (StreamingWagon) getWagon(); |
460 | |
|
461 | 0 | System.out.println( " wagon hashCode " + wagon.hashCode() ); |
462 | |
|
463 | 0 | Repository testRepository = new Repository( "id", "http://www.example.com/" ); |
464 | |
|
465 | 0 | String localRepositoryPath = FileTestUtils.getTestOutputDir().toString(); |
466 | 0 | File sourceFile = new File( localRepositoryPath, "test-proxied-resource" ); |
467 | 0 | FileUtils.fileWrite( sourceFile.getAbsolutePath(), "content" ); |
468 | |
|
469 | 0 | wagon.connect( testRepository, proxyInfo ); |
470 | |
|
471 | 0 | StringOutputStream out = new StringOutputStream(); |
472 | |
try |
473 | |
{ |
474 | 0 | wagon.getToStream( "test-proxied-resource", out ); |
475 | |
|
476 | 0 | assertTrue( handler.headers.containsKey( "Proxy-Connection" ) ); |
477 | |
} |
478 | |
finally |
479 | |
{ |
480 | 0 | System.setProperty( "http.proxyHost", "" ); |
481 | 0 | System.setProperty( "http.proxyPort", "" ); |
482 | 0 | wagon.disconnect(); |
483 | 0 | } |
484 | |
} |
485 | |
finally |
486 | |
{ |
487 | 0 | proxyServer.stop(); |
488 | 0 | } |
489 | 0 | } |
490 | |
|
491 | |
private ProxyInfo createProxyInfo() |
492 | |
{ |
493 | 0 | ProxyInfo proxyInfo = new ProxyInfo(); |
494 | 0 | proxyInfo.setHost( "localhost" ); |
495 | 0 | proxyInfo.setNonProxyHosts( null ); |
496 | 0 | proxyInfo.setType( "http" ); |
497 | 0 | return proxyInfo; |
498 | |
} |
499 | |
|
500 | |
public void testSecuredGetUnauthorized() |
501 | |
throws Exception |
502 | |
{ |
503 | |
try |
504 | |
{ |
505 | 0 | runTestSecuredGet( null ); |
506 | 0 | fail(); |
507 | |
} |
508 | 0 | catch ( AuthorizationException e ) |
509 | |
{ |
510 | 0 | assertTrue( true ); |
511 | 0 | } |
512 | 0 | } |
513 | |
|
514 | |
public void testSecuredGetWrongPassword() |
515 | |
throws Exception |
516 | |
{ |
517 | |
try |
518 | |
{ |
519 | 0 | AuthenticationInfo authInfo = new AuthenticationInfo(); |
520 | 0 | authInfo.setUserName( "user" ); |
521 | 0 | authInfo.setPassword( "admin" ); |
522 | 0 | runTestSecuredGet( authInfo ); |
523 | 0 | fail(); |
524 | |
} |
525 | 0 | catch ( AuthorizationException e ) |
526 | |
{ |
527 | 0 | assertTrue( true ); |
528 | 0 | } |
529 | 0 | } |
530 | |
|
531 | |
public void testSecuredGet() |
532 | |
throws Exception |
533 | |
{ |
534 | 0 | AuthenticationInfo authInfo = new AuthenticationInfo(); |
535 | 0 | authInfo.setUserName( "user" ); |
536 | 0 | authInfo.setPassword( "secret" ); |
537 | 0 | runTestSecuredGet( authInfo ); |
538 | 0 | } |
539 | |
|
540 | |
public void runTestSecuredGet( AuthenticationInfo authInfo ) |
541 | |
throws Exception |
542 | |
{ |
543 | 0 | String localRepositoryPath = FileTestUtils.getTestOutputDir().toString(); |
544 | 0 | Server server = createSecurityServer( localRepositoryPath ); |
545 | 0 | server.start(); |
546 | |
|
547 | |
try |
548 | |
{ |
549 | 0 | StreamingWagon wagon = (StreamingWagon) getWagon(); |
550 | |
|
551 | 0 | Repository testRepository = new Repository( "id", getRepositoryUrl( server ) ); |
552 | |
|
553 | 0 | File sourceFile = new File( localRepositoryPath, "test-secured-resource" ); |
554 | 0 | FileUtils.fileWrite( sourceFile.getAbsolutePath(), "top secret" ); |
555 | |
|
556 | 0 | wagon.connect( testRepository, authInfo ); |
557 | |
|
558 | 0 | StringOutputStream out = new StringOutputStream(); |
559 | |
try |
560 | |
{ |
561 | 0 | wagon.getToStream( "test-secured-resource", out ); |
562 | |
} |
563 | |
finally |
564 | |
{ |
565 | 0 | wagon.disconnect(); |
566 | 0 | } |
567 | |
|
568 | 0 | assertEquals( "top secret", out.toString() ); |
569 | |
} |
570 | |
finally |
571 | |
{ |
572 | 0 | server.stop(); |
573 | 0 | } |
574 | 0 | } |
575 | |
|
576 | |
public void testSecuredResourceExistsUnauthorized() |
577 | |
throws Exception |
578 | |
{ |
579 | |
try |
580 | |
{ |
581 | 0 | runTestSecuredResourceExists( null ); |
582 | 0 | fail(); |
583 | |
} |
584 | 0 | catch ( AuthorizationException e ) |
585 | |
{ |
586 | 0 | assertTrue( true ); |
587 | 0 | } |
588 | 0 | } |
589 | |
|
590 | |
public void testSecuredResourceExistsWrongPassword() |
591 | |
throws Exception |
592 | |
{ |
593 | |
try |
594 | |
{ |
595 | 0 | AuthenticationInfo authInfo = new AuthenticationInfo(); |
596 | 0 | authInfo.setUserName( "user" ); |
597 | 0 | authInfo.setPassword( "admin" ); |
598 | 0 | runTestSecuredResourceExists( authInfo ); |
599 | |
} |
600 | 0 | catch ( AuthorizationException e ) |
601 | |
{ |
602 | 0 | assertTrue( true ); |
603 | 0 | } |
604 | 0 | } |
605 | |
|
606 | |
public void testSecuredResourceExists() |
607 | |
throws Exception |
608 | |
{ |
609 | 0 | AuthenticationInfo authInfo = new AuthenticationInfo(); |
610 | 0 | authInfo.setUserName( "user" ); |
611 | 0 | authInfo.setPassword( "secret" ); |
612 | 0 | runTestSecuredResourceExists( authInfo ); |
613 | 0 | } |
614 | |
|
615 | |
public void runTestSecuredResourceExists( AuthenticationInfo authInfo ) |
616 | |
throws Exception |
617 | |
{ |
618 | 0 | String localRepositoryPath = FileTestUtils.getTestOutputDir().toString(); |
619 | 0 | Server server = createSecurityServer( localRepositoryPath ); |
620 | 0 | server.start(); |
621 | |
|
622 | |
try |
623 | |
{ |
624 | 0 | StreamingWagon wagon = (StreamingWagon) getWagon(); |
625 | |
|
626 | 0 | Repository testRepository = new Repository( "id", getRepositoryUrl( server ) ); |
627 | |
|
628 | 0 | File sourceFile = new File( localRepositoryPath, "test-secured-resource-exists" ); |
629 | 0 | FileUtils.fileWrite( sourceFile.getAbsolutePath(), "top secret" ); |
630 | |
|
631 | 0 | wagon.connect( testRepository, authInfo ); |
632 | |
|
633 | |
try |
634 | |
{ |
635 | 0 | assertTrue( wagon.resourceExists( "test-secured-resource-exists" ) ); |
636 | |
|
637 | 0 | assertFalse( wagon.resourceExists( "test-secured-resource-not-exists" ) ); |
638 | |
} |
639 | |
finally |
640 | |
{ |
641 | 0 | wagon.disconnect(); |
642 | 0 | } |
643 | |
} |
644 | |
finally |
645 | |
{ |
646 | 0 | server.stop(); |
647 | 0 | } |
648 | 0 | } |
649 | |
|
650 | |
private Server createSecurityServer( String localRepositoryPath ) |
651 | |
{ |
652 | 0 | Server server = new Server( 0 ); |
653 | |
|
654 | 0 | SecurityHandler sh = createSecurityHandler(); |
655 | |
|
656 | 0 | Context root = new Context( Context.SESSIONS ); |
657 | 0 | root.setContextPath( "/" ); |
658 | 0 | root.addHandler( sh ); |
659 | 0 | root.setResourceBase( localRepositoryPath ); |
660 | 0 | ServletHolder servletHolder = new ServletHolder( new DefaultServlet() ); |
661 | 0 | root.addServlet( servletHolder, "/*" ); |
662 | |
|
663 | 0 | server.setHandler( root ); |
664 | 0 | addConnectors( server ); |
665 | 0 | return server; |
666 | |
} |
667 | |
|
668 | |
|
669 | |
private String writeTestFileGzip( File parent, String child ) |
670 | |
throws IOException |
671 | |
{ |
672 | 0 | File file = new File( parent, child ); |
673 | 0 | file.getParentFile().mkdirs(); |
674 | 0 | file.deleteOnExit(); |
675 | 0 | OutputStream out = new FileOutputStream( file ); |
676 | |
try |
677 | |
{ |
678 | 0 | out.write( child.getBytes() ); |
679 | |
} |
680 | |
finally |
681 | |
{ |
682 | 0 | out.close(); |
683 | 0 | } |
684 | |
|
685 | 0 | file = new File( parent, child + ".gz" ); |
686 | 0 | file.deleteOnExit(); |
687 | |
String content; |
688 | 0 | out = new FileOutputStream( file ); |
689 | 0 | out = new GZIPOutputStream( out ); |
690 | |
try |
691 | |
{ |
692 | |
|
693 | |
|
694 | 0 | content = file.getAbsolutePath(); |
695 | 0 | out.write( content.getBytes() ); |
696 | |
} |
697 | |
finally |
698 | |
{ |
699 | 0 | out.close(); |
700 | 0 | } |
701 | |
|
702 | 0 | return content; |
703 | |
} |
704 | |
|
705 | |
public void testPutForbidden() |
706 | |
throws Exception |
707 | |
{ |
708 | |
try |
709 | |
{ |
710 | 0 | runTestPut( HttpServletResponse.SC_FORBIDDEN ); |
711 | 0 | fail(); |
712 | |
} |
713 | 0 | catch ( AuthorizationException e ) |
714 | |
{ |
715 | 0 | assertTrue( true ); |
716 | 0 | } |
717 | 0 | } |
718 | |
|
719 | |
public void testPut404() |
720 | |
throws Exception |
721 | |
{ |
722 | |
try |
723 | |
{ |
724 | 0 | runTestPut( HttpServletResponse.SC_NOT_FOUND ); |
725 | 0 | fail(); |
726 | |
} |
727 | 0 | catch ( ResourceDoesNotExistException e ) |
728 | |
{ |
729 | 0 | assertTrue( true ); |
730 | 0 | } |
731 | 0 | } |
732 | |
|
733 | |
public void testPut500() |
734 | |
throws Exception |
735 | |
{ |
736 | |
try |
737 | |
{ |
738 | 0 | runTestPut( HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); |
739 | 0 | fail(); |
740 | |
} |
741 | 0 | catch ( TransferFailedException e ) |
742 | |
{ |
743 | 0 | assertTrue( true ); |
744 | 0 | } |
745 | 0 | } |
746 | |
|
747 | |
private void runTestPut( int status ) |
748 | |
throws Exception |
749 | |
{ |
750 | 0 | StreamingWagon wagon = (StreamingWagon) getWagon(); |
751 | |
|
752 | 0 | Server server = new Server( 0 ); |
753 | 0 | StatusHandler handler = new StatusHandler(); |
754 | 0 | handler.setStatusToReturn( status ); |
755 | 0 | server.setHandler( handler ); |
756 | 0 | addConnectors( server ); |
757 | 0 | server.start(); |
758 | |
|
759 | 0 | wagon.connect( new Repository( "id", getRepositoryUrl( server ) ) ); |
760 | |
|
761 | 0 | File tempFile = File.createTempFile( "wagon", "tmp" ); |
762 | 0 | tempFile.deleteOnExit(); |
763 | 0 | FileUtils.fileWrite( tempFile.getAbsolutePath(), "content" ); |
764 | |
|
765 | |
try |
766 | |
{ |
767 | 0 | wagon.put( tempFile, "resource" ); |
768 | 0 | fail(); |
769 | |
} |
770 | |
finally |
771 | |
{ |
772 | 0 | wagon.disconnect(); |
773 | |
|
774 | 0 | server.stop(); |
775 | |
|
776 | 0 | tempFile.delete(); |
777 | 0 | } |
778 | 0 | } |
779 | |
|
780 | |
public void testSecuredPutUnauthorized() |
781 | |
throws Exception |
782 | |
{ |
783 | |
try |
784 | |
{ |
785 | 0 | runTestSecuredPut( null ); |
786 | 0 | fail(); |
787 | |
} |
788 | 0 | catch ( TransferFailedException e ) |
789 | |
{ |
790 | 0 | assertTrue( true ); |
791 | 0 | } |
792 | 0 | } |
793 | |
|
794 | |
public void testSecuredPutWrongPassword() |
795 | |
throws Exception |
796 | |
{ |
797 | |
try |
798 | |
{ |
799 | 0 | AuthenticationInfo authInfo = new AuthenticationInfo(); |
800 | 0 | authInfo.setUserName( "user" ); |
801 | 0 | authInfo.setPassword( "admin" ); |
802 | 0 | runTestSecuredPut( authInfo ); |
803 | 0 | fail(); |
804 | |
} |
805 | 0 | catch ( TransferFailedException e ) |
806 | |
{ |
807 | 0 | assertTrue( true ); |
808 | 0 | } |
809 | 0 | } |
810 | |
|
811 | |
public void testSecuredPut() |
812 | |
throws Exception |
813 | |
{ |
814 | 0 | AuthenticationInfo authInfo = new AuthenticationInfo(); |
815 | 0 | authInfo.setUserName( "user" ); |
816 | 0 | authInfo.setPassword( "secret" ); |
817 | 0 | runTestSecuredPut( authInfo ); |
818 | 0 | } |
819 | |
|
820 | |
public void runTestSecuredPut( AuthenticationInfo authInfo ) |
821 | |
throws Exception |
822 | |
{ |
823 | 0 | runTestSecuredPut( authInfo, 1 ); |
824 | 0 | } |
825 | |
|
826 | |
public void runTestSecuredPut( AuthenticationInfo authInfo, int putNumber ) |
827 | |
throws Exception |
828 | |
{ |
829 | 0 | String localRepositoryPath = FileTestUtils.getTestOutputDir().toString(); |
830 | 0 | Server server = new Server( 0 ); |
831 | |
|
832 | 0 | TestSecurityHandler sh = createSecurityHandler(); |
833 | |
|
834 | 0 | PutHandler putHandler = new PutHandler( new File( localRepositoryPath ) ); |
835 | |
|
836 | 0 | HandlerCollection handlers = new HandlerCollection(); |
837 | 0 | handlers.setHandlers( new Handler[]{ sh, putHandler } ); |
838 | |
|
839 | 0 | server.setHandler( handlers ); |
840 | 0 | addConnectors( server ); |
841 | 0 | server.start(); |
842 | |
|
843 | 0 | StreamingWagon wagon = (StreamingWagon) getWagon(); |
844 | 0 | Repository testRepository = new Repository( "id", getRepositoryUrl( server ) ); |
845 | 0 | wagon.connect( testRepository, authInfo ); |
846 | |
try |
847 | |
{ |
848 | 0 | for ( int i = 0; i < putNumber; i++ ) |
849 | |
{ |
850 | 0 | File sourceFile = new File( localRepositoryPath, "test-secured-put-resource" ); |
851 | 0 | sourceFile.delete(); |
852 | 0 | assertFalse( sourceFile.exists() ); |
853 | |
|
854 | 0 | File tempFile = File.createTempFile( "wagon", "tmp" ); |
855 | 0 | tempFile.deleteOnExit(); |
856 | 0 | FileUtils.fileWrite( tempFile.getAbsolutePath(), "put top secret" ); |
857 | |
|
858 | |
try |
859 | |
{ |
860 | 0 | wagon.put( tempFile, "test-secured-put-resource" ); |
861 | |
} |
862 | |
finally |
863 | |
{ |
864 | 0 | tempFile.delete(); |
865 | 0 | } |
866 | |
|
867 | 0 | assertEquals( "put top secret", FileUtils.fileRead( sourceFile.getAbsolutePath() ) ); |
868 | |
} |
869 | |
} |
870 | |
finally |
871 | |
{ |
872 | 0 | wagon.disconnect(); |
873 | 0 | server.stop(); |
874 | 0 | } |
875 | 0 | assertEquals( putNumber, putHandler.putCallNumber ); |
876 | 0 | testPreemptiveAuthentication( sh ); |
877 | 0 | } |
878 | |
|
879 | |
protected abstract boolean supportPreemptiveAuthentication(); |
880 | |
|
881 | |
protected void testPreemptiveAuthentication( TestSecurityHandler sh ) |
882 | |
{ |
883 | |
|
884 | 0 | if ( supportPreemptiveAuthentication() ) |
885 | |
{ |
886 | 0 | assertEquals( "not 1 security handler use " + sh.securityHandlerRequestReponses, 1, |
887 | |
sh.securityHandlerRequestReponses.size() ); |
888 | 0 | assertEquals( 200, sh.securityHandlerRequestReponses.get( 0 ).responseCode ); |
889 | |
} |
890 | |
else |
891 | |
{ |
892 | 0 | assertEquals( "not 2 security handler use " + sh.securityHandlerRequestReponses, 2, |
893 | |
sh.securityHandlerRequestReponses.size() ); |
894 | 0 | assertEquals( 401, sh.securityHandlerRequestReponses.get( 0 ).responseCode ); |
895 | 0 | assertEquals( 200, sh.securityHandlerRequestReponses.get( 1 ).responseCode ); |
896 | |
|
897 | |
} |
898 | 0 | } |
899 | |
|
900 | 0 | static class StatusHandler |
901 | |
extends AbstractHandler |
902 | |
{ |
903 | |
private int status; |
904 | |
|
905 | |
public void setStatusToReturn( int status ) |
906 | |
{ |
907 | 0 | this.status = status; |
908 | 0 | } |
909 | |
|
910 | |
public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch ) |
911 | |
throws IOException, ServletException |
912 | |
{ |
913 | 0 | if ( status != 0 ) |
914 | |
{ |
915 | 0 | response.setStatus( status ); |
916 | 0 | ( (Request) request ).setHandled( true ); |
917 | |
} |
918 | 0 | } |
919 | |
} |
920 | |
|
921 | |
static class PutHandler |
922 | |
extends AbstractHandler |
923 | |
{ |
924 | |
private final File resourceBase; |
925 | |
|
926 | 0 | public int putCallNumber = 0; |
927 | |
|
928 | |
public PutHandler( File repositoryDirectory ) |
929 | 0 | { |
930 | 0 | this.resourceBase = repositoryDirectory; |
931 | 0 | } |
932 | |
|
933 | |
public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch ) |
934 | |
throws IOException, ServletException |
935 | |
{ |
936 | 0 | Request base_request = |
937 | |
request instanceof Request ? (Request) request : HttpConnection.getCurrentConnection().getRequest(); |
938 | |
|
939 | 0 | if ( base_request.isHandled() || !"PUT".equals( base_request.getMethod() ) ) |
940 | |
{ |
941 | 0 | return; |
942 | |
} |
943 | |
|
944 | 0 | base_request.setHandled( true ); |
945 | |
|
946 | 0 | File file = new File( resourceBase, URLDecoder.decode( request.getPathInfo() ) ); |
947 | 0 | file.getParentFile().mkdirs(); |
948 | 0 | FileOutputStream out = new FileOutputStream( file ); |
949 | 0 | ServletInputStream in = request.getInputStream(); |
950 | |
try |
951 | |
{ |
952 | 0 | IOUtil.copy( in, out ); |
953 | |
} |
954 | |
finally |
955 | |
{ |
956 | 0 | in.close(); |
957 | 0 | out.close(); |
958 | 0 | } |
959 | 0 | System.out.println( "put file " + request.getPathInfo() ); |
960 | 0 | putCallNumber++; |
961 | 0 | response.setStatus( HttpServletResponse.SC_CREATED ); |
962 | 0 | } |
963 | |
} |
964 | |
|
965 | 0 | private static class AuthorizingProxyHandler |
966 | |
extends TestHeaderHandler |
967 | |
{ |
968 | |
|
969 | |
public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch ) |
970 | |
throws IOException, ServletException |
971 | |
{ |
972 | 0 | System.out.println( " handle proxy request" ); |
973 | 0 | if ( request.getHeader( "Proxy-Authorization" ) == null ) |
974 | |
{ |
975 | 0 | response.setStatus( 407 ); |
976 | 0 | response.addHeader( "Proxy-Authenticate", "Basic realm=\"Squid proxy-caching web server\"" ); |
977 | |
|
978 | 0 | ( (Request) request ).setHandled( true ); |
979 | 0 | return; |
980 | |
} |
981 | 0 | super.handle( target, request, response, dispatch ); |
982 | 0 | } |
983 | |
} |
984 | |
|
985 | 0 | private static class TestHeaderHandler |
986 | |
extends AbstractHandler |
987 | |
{ |
988 | 0 | private Map headers = Collections.EMPTY_MAP; |
989 | |
|
990 | |
public TestHeaderHandler() |
991 | 0 | { |
992 | 0 | } |
993 | |
|
994 | |
public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch ) |
995 | |
throws IOException, ServletException |
996 | |
{ |
997 | 0 | headers = new HashMap(); |
998 | 0 | for ( Enumeration e = request.getHeaderNames(); e.hasMoreElements(); ) |
999 | |
{ |
1000 | 0 | String name = (String) e.nextElement(); |
1001 | 0 | headers.put( name, request.getHeader( name ) ); |
1002 | 0 | } |
1003 | |
|
1004 | 0 | response.setContentType( "text/plain" ); |
1005 | 0 | response.setStatus( HttpServletResponse.SC_OK ); |
1006 | 0 | response.getWriter().println( "Hello, World!" ); |
1007 | |
|
1008 | 0 | ( (Request) request ).setHandled( true ); |
1009 | 0 | } |
1010 | |
|
1011 | |
} |
1012 | |
|
1013 | |
protected TestSecurityHandler createSecurityHandler() |
1014 | |
{ |
1015 | 0 | Constraint constraint = new Constraint(); |
1016 | 0 | constraint.setName( Constraint.__BASIC_AUTH ); |
1017 | 0 | constraint.setRoles( new String[]{ "admin" } ); |
1018 | 0 | constraint.setAuthenticate( true ); |
1019 | |
|
1020 | 0 | ConstraintMapping cm = new ConstraintMapping(); |
1021 | 0 | cm.setConstraint( constraint ); |
1022 | 0 | cm.setPathSpec( "/*" ); |
1023 | |
|
1024 | 0 | TestSecurityHandler sh = new TestSecurityHandler(); |
1025 | 0 | HashUserRealm hashUserRealm = new HashUserRealm( "MyRealm" ); |
1026 | 0 | hashUserRealm.put( "user", "secret" ); |
1027 | 0 | hashUserRealm.addUserToRole( "user", "admin" ); |
1028 | 0 | sh.setUserRealm( hashUserRealm ); |
1029 | 0 | sh.setConstraintMappings( new ConstraintMapping[]{ cm } ); |
1030 | 0 | return sh; |
1031 | |
} |
1032 | |
|
1033 | 0 | public static class TestSecurityHandler |
1034 | |
extends SecurityHandler |
1035 | |
{ |
1036 | |
|
1037 | 0 | public List<SecurityHandlerRequestReponse> securityHandlerRequestReponses = |
1038 | |
new ArrayList<SecurityHandlerRequestReponse>(); |
1039 | |
|
1040 | |
@Override |
1041 | |
public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch ) |
1042 | |
throws IOException, ServletException |
1043 | |
{ |
1044 | 0 | String method = request.getMethod(); |
1045 | 0 | super.handle( target, request, response, dispatch ); |
1046 | 0 | System.out.println( "method in SecurityHandler: " + method ); |
1047 | |
|
1048 | 0 | securityHandlerRequestReponses.add( |
1049 | |
new SecurityHandlerRequestReponse( method, ( (Response) response ).getStatus() ) ); |
1050 | 0 | } |
1051 | |
|
1052 | |
} |
1053 | |
|
1054 | 0 | public static class SecurityHandlerRequestReponse |
1055 | |
{ |
1056 | |
public String method; |
1057 | |
|
1058 | |
public int responseCode; |
1059 | |
|
1060 | |
private SecurityHandlerRequestReponse( String method, int responseCode ) |
1061 | 0 | { |
1062 | 0 | this.method = method; |
1063 | 0 | this.responseCode = responseCode; |
1064 | 0 | } |
1065 | |
|
1066 | |
@Override |
1067 | |
public String toString() |
1068 | |
{ |
1069 | 0 | final StringBuilder sb = new StringBuilder(); |
1070 | 0 | sb.append( "SecurityHandlerRequestReponse" ); |
1071 | 0 | sb.append( "{method='" ).append( method ).append( '\'' ); |
1072 | 0 | sb.append( ", responseCode=" ).append( responseCode ); |
1073 | 0 | sb.append( '}' ); |
1074 | 0 | return sb.toString(); |
1075 | |
} |
1076 | |
} |
1077 | |
} |