]> source.dussan.org Git - archiva.git/commitdiff
[MRM-884] network proxy doesn't work
authorBrett Porter <brett@apache.org>
Sun, 27 Jul 2008 15:23:30 +0000 (15:23 +0000)
committerBrett Porter <brett@apache.org>
Sun, 27 Jul 2008 15:23:30 +0000 (15:23 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@680127 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-proxy/pom.xml
archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultRepositoryProxyConnectors.java
archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/AbstractProxyTestCase.java
archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/HttpProxyTransferTest.java [new file with mode: 0644]
archiva-modules/archiva-base/archiva-proxy/src/test/resources/org/apache/maven/archiva/proxy/HttpProxyTransferTest.xml [new file with mode: 0644]
pom.xml

index b800c65a2edf5479cdd75a2bb97892ecf33ad215..f4b39942a4058729c3f856bbcddcef8461269482 100644 (file)
       <artifactId>xmlunit</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mortbay.jetty</groupId>
+      <artifactId>jetty</artifactId>
+      <version>6.1.11</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.wagon</groupId>
+      <artifactId>wagon-http-lightweight</artifactId>
+      <version>${wagon.version}</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
index 3604bc20b9a1db17eb94fe64faad53b92da286ae..f8cfca84fc8c3ee3c7a409f553eb843d5fe53e61 100644 (file)
@@ -19,6 +19,17 @@ package org.apache.maven.archiva.proxy;
  * under the License.
  */
 
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Map.Entry;
+
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.StringUtils;
@@ -65,17 +76,6 @@ import org.codehaus.plexus.util.SelectorUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Properties;
-
 /**
  * DefaultRepositoryProxyConnectors
  *
@@ -938,7 +938,7 @@ public class DefaultRepositoryProxyConnectors
     {
         boolean connected = false;
 
-        ProxyInfo networkProxy = null;
+        final ProxyInfo networkProxy;
         synchronized ( this.networkProxyMap )
         {
             networkProxy = (ProxyInfo) this.networkProxyMap.get( connector.getProxyId() );
index e71ea65b011b2ca94cf7bd02f6430e5c35592606..344e5fc102b9d90d7f50c7c693533e5111baca22 100644 (file)
@@ -549,17 +549,19 @@ public abstract class AbstractProxyTestCase
             // This is just a warning.
             System.err.println( "[WARN] Skipping setup of testable managed repository, source dir does not exist: "
                 + sourceDir );
-            return;
         }
-
-        // Test that the source is a dir.
-        if ( !sourceDir.isDirectory() )
+        else
         {
-            fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
-        }
 
-        // Copy directory structure.
-        copyDirectoryStructure( sourceDir, destDir );
+            // Test that the source is a dir.
+            if ( !sourceDir.isDirectory() )
+            {
+                fail( "Unable to setup testable managed repository, source is not a directory: " + sourceDir );
+            }
+
+            // Copy directory structure.
+            copyDirectoryStructure( sourceDir, destDir );
+        }
     }
 
     protected void setManagedNewerThanRemote( File managedFile, File remoteFile )
diff --git a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/HttpProxyTransferTest.java b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/HttpProxyTransferTest.java
new file mode 100644 (file)
index 0000000..63285c6
--- /dev/null
@@ -0,0 +1,210 @@
+package org.apache.maven.archiva.proxy;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Enumeration;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.maven.archiva.configuration.ArchivaConfiguration;
+import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
+import org.apache.maven.archiva.configuration.NetworkProxyConfiguration;
+import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
+import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
+import org.apache.maven.archiva.model.ArtifactReference;
+import org.apache.maven.archiva.policies.CachedFailuresPolicy;
+import org.apache.maven.archiva.policies.ChecksumPolicy;
+import org.apache.maven.archiva.policies.PropagateErrorsDownloadPolicy;
+import org.apache.maven.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
+import org.apache.maven.archiva.policies.ReleasesPolicy;
+import org.apache.maven.archiva.policies.SnapshotsPolicy;
+import org.apache.maven.archiva.repository.ManagedRepositoryContent;
+import org.codehaus.plexus.spring.PlexusInSpringTestCase;
+import org.mortbay.jetty.Handler;
+import org.mortbay.jetty.Request;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.handler.AbstractHandler;
+
+/**
+ * Integration test for connecting over a HTTP proxy.
+ * 
+ * @author Brett Porter
+ * @version $Id: ManagedDefaultTransferTest.java 677852 2008-07-18 08:16:24Z brett $
+ */
+public class HttpProxyTransferTest
+    extends PlexusInSpringTestCase
+{
+    private static final String PROXY_ID = "proxy";
+
+    private static final String MANAGED_ID = "default-managed-repository";
+
+    private static final String PROXIED_ID = "proxied1";
+
+    private static final String PROXIED_BASEDIR = "src/test/repositories/proxied1";
+
+    private RepositoryProxyConnectors proxyHandler;
+
+    private ArchivaConfiguration config;
+
+    private ManagedRepositoryContent managedDefaultRepository;
+
+    private Server server;
+
+    @Override
+    protected void setUp()
+        throws Exception
+    {
+        super.setUp();
+
+        // Setup source repository (using default layout)
+        String repoPath = "target/test-repository/managed/" + getName();
+
+        File destRepoDir = new File( repoPath );
+
+        // Cleanout destination dirs.
+        if ( destRepoDir.exists() )
+        {
+            FileUtils.deleteDirectory( destRepoDir );
+        }
+
+        // Make the destination dir.
+        destRepoDir.mkdirs();
+
+        ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
+        repo.setId( MANAGED_ID );
+        repo.setName( "Default Managed Repository" );
+        repo.setLocation( repoPath );
+        repo.setLayout( "default" );
+
+        ManagedRepositoryContent repoContent =
+            (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
+        repoContent.setRepository( repo );
+        managedDefaultRepository = repoContent;
+
+        config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
+        config.getConfiguration().addManagedRepository( repo );
+
+        Handler handler = new AbstractHandler()
+        {
+            public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
+                throws IOException, ServletException
+            {
+                response.setContentType( "text/plain" );
+                response.setStatus( HttpServletResponse.SC_OK );
+                response.getWriter().println( "get-default-layout-1.0.jar" );
+                response.getWriter().println();
+                assertNotNull( request.getHeader( "Proxy-Connection" ) );
+                
+                ( (Request) request ).setHandled( true );
+            }
+        };
+
+        server = new Server( 0 );
+        server.setHandler( handler );
+        server.start();
+
+        int port = server.getConnectors()[0].getLocalPort();
+
+        NetworkProxyConfiguration proxyConfig = new NetworkProxyConfiguration();
+        proxyConfig.setHost( "localhost" );
+        proxyConfig.setPort( port );
+        proxyConfig.setProtocol( "http" );
+        proxyConfig.setId( PROXY_ID );
+        config.getConfiguration().addNetworkProxy( proxyConfig );
+        
+        // Setup target (proxied to) repository.
+        RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
+
+        repoConfig.setId( PROXIED_ID );
+        repoConfig.setName( "Proxied Repository 1" );
+        repoConfig.setLayout( "default" );
+        repoConfig.setUrl( "http://www.example.com/" );
+
+        config.getConfiguration().addRemoteRepository( repoConfig );
+
+        // Setup the proxy handler.
+        proxyHandler = (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
+    }
+
+    @Override
+    protected void tearDown()
+        throws Exception
+    {
+        super.tearDown();
+        
+        server.stop();
+    }
+
+    public void testGetOverHttpProxy()
+        throws Exception
+    {
+        String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
+
+        // Configure Connector (usually done within archiva.xml configuration)
+        addConnector();
+
+        File expectedFile = new File( new File( managedDefaultRepository.getRepoRoot() ), path );
+        ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
+
+        // Attempt the proxy fetch.
+        File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
+
+        File sourceFile = new File( PROXIED_BASEDIR, path );
+        assertNotNull( "Expected File should not be null.", expectedFile );
+        assertNotNull( "Actual File should not be null.", downloadedFile );
+
+        assertTrue( "Check actual file exists.", downloadedFile.exists() );
+        assertEquals( "Check filename path is appropriate.", expectedFile.getCanonicalPath(),
+                      downloadedFile.getCanonicalPath() );
+        assertEquals( "Check file path matches.", expectedFile.getAbsolutePath(), downloadedFile.getAbsolutePath() );
+
+        String expectedContents = FileUtils.readFileToString( sourceFile, null );
+        String actualContents = FileUtils.readFileToString( downloadedFile, null );
+        assertEquals( "Check file contents.", expectedContents, actualContents );
+    }
+
+    private void addConnector()
+    {
+        ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
+        connectorConfig.setProxyId( PROXY_ID );
+        connectorConfig.setSourceRepoId( MANAGED_ID );
+        connectorConfig.setTargetRepoId( PROXIED_ID );
+        connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.FIX );
+        connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ONCE );
+        connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ONCE );
+        connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
+        connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS,
+                                   PropagateErrorsDownloadPolicy.QUEUE );
+        connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE,
+                                   PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
+
+        int count = config.getConfiguration().getProxyConnectors().size();
+        config.getConfiguration().addProxyConnector( connectorConfig );
+
+        // Proper Triggering ...
+        String prefix = "proxyConnectors.proxyConnector(" + count + ")";
+        ( (MockConfiguration) config ).triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
+    }
+}
diff --git a/archiva-modules/archiva-base/archiva-proxy/src/test/resources/org/apache/maven/archiva/proxy/HttpProxyTransferTest.xml b/archiva-modules/archiva-base/archiva-proxy/src/test/resources/org/apache/maven/archiva/proxy/HttpProxyTransferTest.xml
new file mode 100644 (file)
index 0000000..9c3909d
--- /dev/null
@@ -0,0 +1,102 @@
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~
+  ~   http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
+  -->
+
+<component-set>
+  <components>
+    <component>
+      <role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role>
+      <role-hint>mock</role-hint>
+      <implementation>org.apache.maven.archiva.proxy.MockConfiguration</implementation>
+    </component>
+    <component>
+      <role>org.apache.maven.archiva.repository.RepositoryContentFactory</role>
+      <role-hint>mocked</role-hint>
+      <implementation>org.apache.maven.archiva.repository.RepositoryContentFactory</implementation>
+      <description>RepositoryContentRequest</description>
+      <requirements>
+        <requirement>
+          <role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role>
+          <role-hint>mock</role-hint>
+          <field-name>archivaConfiguration</field-name>
+        </requirement>
+      </requirements>
+    </component>
+    <component>
+      <role>org.apache.maven.archiva.proxy.RepositoryProxyConnectors</role>
+      <role-hint>default</role-hint>
+      <implementation>org.apache.maven.archiva.proxy.DefaultRepositoryProxyConnectors</implementation>
+      <description>DefaultRepositoryProxyConnectors</description>
+      <requirements>
+        <requirement>
+          <role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role>
+          <role-hint>mock</role-hint>
+          <field-name>archivaConfiguration</field-name>
+        </requirement>
+        <requirement>
+          <role>org.apache.maven.wagon.Wagon</role>
+          <field-name>wagons</field-name>
+        </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.repository.RepositoryContentFactory</role>
+          <role-hint>mocked</role-hint>
+        </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.repository.metadata.MetadataTools</role>
+        </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.policies.PreDownloadPolicy</role>
+          <field-name>preDownloadPolicies</field-name>
+        </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.policies.PostDownloadPolicy</role>
+          <field-name>postDownloadPolicies</field-name>
+        </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers</role>
+          <field-name>consumers</field-name>
+        </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.policies.urlcache.UrlFailureCache</role>
+          <field-name>urlFailureCache</field-name>
+        </requirement>
+      </requirements>
+    </component>
+
+    <component>
+      <role>org.codehaus.plexus.cache.Cache</role>
+      <role-hint>url-failures-cache</role-hint>
+      <implementation>org.codehaus.plexus.cache.ehcache.EhcacheCache</implementation>
+      <description>URL Failure Cache</description>
+      <configuration>
+        <disk-expiry-thread-interval-seconds>600</disk-expiry-thread-interval-seconds>
+        <disk-persistent>false</disk-persistent> <!--disabling disk persistence for unit testing. -->
+        <disk-store-path>${java.io.tmpdir}/archiva/urlcache</disk-store-path>
+        <eternal>false</eternal>
+        <max-elements-in-memory>1000</max-elements-in-memory>
+        <memory-eviction-policy>LRU</memory-eviction-policy>
+        <name>url-failures-cache</name>
+        <overflow-to-disk>false</overflow-to-disk>
+        <!-- 45 minutes = 2700 seconds -->
+        <time-to-idle-seconds>2700</time-to-idle-seconds>
+        <!-- 30 minutes = 1800 seconds  -->
+        <time-to-live-seconds>1800</time-to-live-seconds>
+      </configuration>
+    </component>
+  </components>
+</component-set>
diff --git a/pom.xml b/pom.xml
index d3f439c5b41a5192b4c0491b4491e0cb7fe3227a..284e85b82ec1f78d517b5b13e638846d0c31c4b2 100644 (file)
--- a/pom.xml
+++ b/pom.xml
   </dependencyManagement>
   <properties>
     <maven.version>2.0.8</maven.version>
-    <wagon.version>1.0-beta-3</wagon.version>
+    <wagon.version>1.0-SNAPSHOT</wagon.version>
     <redback.version>1.0.3-SNAPSHOT</redback.version>
     <jetty.version>6.1.6</jetty.version>
   </properties>