View Javadoc

1   package org.apache.maven.it;
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 org.apache.maven.it.Verifier;
23  import org.apache.maven.it.util.ResourceExtractor;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.PrintWriter;
28  import java.util.List;
29  import java.util.Properties;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  import org.mortbay.jetty.Connector;
35  import org.mortbay.jetty.Handler;
36  import org.mortbay.jetty.Request;
37  import org.mortbay.jetty.Server;
38  import org.mortbay.jetty.handler.AbstractHandler;
39  import org.mortbay.jetty.security.SslSocketConnector;
40  
41  /**
42   * This is a test set for <a href="http://jira.codehaus.org/browse/MNG-2305">MNG-2305</a>.
43   * 
44   * @author Benjamin Bentmann
45   */
46  public class MavenITmng2305MultipleProxiesTest
47      extends AbstractMavenIntegrationTestCase
48  {
49  
50      public MavenITmng2305MultipleProxiesTest()
51      {
52          super( "[3.0-alpha-3,)" );
53      }
54  
55      /**
56       * Verify that proxies can be setup for multiple protocols, in this case HTTP and HTTPS. As a nice side effect,
57       * this checks HTTPS tunneling over a web proxy.
58       */
59      public void testit()
60          throws Exception
61      {
62          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2305" );
63  
64          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
65  
66          // NOTE: trust store cannot be reliably configured for the current JVM
67          verifier.setForkJvm( true );
68  
69          // keytool -genkey -alias https.mngit -keypass key-passwd -keystore keystore -storepass store-passwd \
70          //   -validity 4096 -dname "cn=https.mngit, ou=None, L=Seattle, ST=Washington, o=ExampleOrg, c=US" -keyalg RSA
71          String storePath = new File( testDir, "keystore" ).getAbsolutePath();
72          String storePwd = "store-passwd";
73          String keyPwd = "key-passwd";
74  
75          Server server = new Server( 0 );
76          server.addConnector( newHttpsConnector( storePath, storePwd, keyPwd ) );
77          server.setHandler( new RepoHandler() );
78          server.start();
79          int httpPort = server.getConnectors()[0].getLocalPort();
80          int httpsPort = server.getConnectors()[1].getLocalPort();
81  
82          TunnelingProxyServer proxy = new TunnelingProxyServer( 0, "localhost", httpsPort, "https.mngit:443" );
83          proxy.start();
84          int proxyPort = proxy.getPort();
85  
86          try
87          {
88              verifier.setAutoclean( false );
89              verifier.deleteDirectory( "target" );
90              verifier.deleteArtifacts( "org.apache.maven.its.mng2305" );
91              Properties filterProps = verifier.newDefaultFilterProperties();
92              filterProps.setProperty( "@proxy.http@", Integer.toString( httpPort ) );
93              filterProps.setProperty( "@proxy.https@", Integer.toString( proxyPort ) );
94              verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
95              verifier.getCliOptions().add( "--settings" );
96              verifier.getCliOptions().add( "settings.xml" );
97              verifier.setSystemProperty( "javax.net.ssl.trustStore", storePath );
98              verifier.setSystemProperty( "javax.net.ssl.trustStorePassword", storePwd );
99              // disable concurrent downloading as not all wagons (e.g. wagon-lightweight-http) are thread-safe regarding proxy settings
100             verifier.setSystemProperty( "maven.artifact.threads", "1" );
101             verifier.executeGoal( "validate" );
102             verifier.verifyErrorFreeLog();
103             verifier.resetStreams();
104         }
105         finally
106         {
107             proxy.stop();
108             server.stop();
109         }
110 
111         List cp = verifier.loadLines( "target/classpath.txt", "UTF-8" );
112         assertTrue( cp.toString(), cp.contains( "http-0.1.jar" ) );
113         assertTrue( cp.toString(), cp.contains( "https-0.1.jar" ) );
114     }
115 
116     private Connector newHttpsConnector( String keystore, String storepwd, String keypwd )
117     {
118         SslSocketConnector connector = new SslSocketConnector();
119         connector.setPort( 0 );
120         connector.setKeystore( keystore );
121         connector.setPassword( storepwd );
122         connector.setKeyPassword( keypwd );
123         return connector;
124     }
125 
126     static class RepoHandler extends AbstractHandler
127     {
128 
129         public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
130             throws IOException
131         {
132             PrintWriter writer = response.getWriter();
133 
134             String uri = request.getRequestURI();
135 
136             if ( !uri.startsWith( "/repo/org/apache/maven/its/mng2305/" + request.getScheme() + "/" ) )
137             {
138                 // HTTP connector serves only http-0.1.jar and HTTPS connector serves only https-0.1.jar
139                 response.setStatus( HttpServletResponse.SC_NOT_FOUND );
140             }
141             else if ( uri.endsWith( ".pom" ) )
142             {
143                 writer.println( "<project>" );
144                 writer.println( "  <modelVersion>4.0.0</modelVersion>" );
145                 writer.println( "  <groupId>org.apache.maven.its.mng2305</groupId>" );
146                 writer.println( "  <artifactId>" + request.getScheme() + "</artifactId>" );
147                 writer.println( "  <version>0.1</version>" );
148                 writer.println( "</project>" );
149                 response.setStatus( HttpServletResponse.SC_OK );
150             }
151             else if ( uri.endsWith( ".jar" ) )
152             {
153                 writer.println( "empty" );
154                 response.setStatus( HttpServletResponse.SC_OK );
155             }
156             else
157             {
158                 response.setStatus( HttpServletResponse.SC_NOT_FOUND );
159             }
160 
161             ( (Request) request ).setHandled( true );
162         }
163 
164     }
165 
166 }