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