]> source.dussan.org Git - archiva.git/commitdiff
[MRM-138] add proxy to the webapp
authorBrett Porter <brett@apache.org>
Tue, 15 Aug 2006 02:59:06 +0000 (02:59 +0000)
committerBrett Porter <brett@apache.org>
Tue, 15 Aug 2006 02:59:06 +0000 (02:59 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@431499 13f79535-47bb-0310-9956-ffa450edef68

maven-repository-core/src/main/java/org/apache/maven/repository/DefaultRepositoryManager.java
maven-repository-core/src/main/java/org/apache/maven/repository/configuration/ConfiguredRepositoryFactory.java
maven-repository-core/src/main/java/org/apache/maven/repository/configuration/DefaultConfiguredRepositoryFactory.java
maven-repository-core/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java
maven-repository-webapp/pom.xml
maven-repository-webapp/src/main/java/org/apache/maven/repository/manager/web/action/ProxyAction.java [new file with mode: 0644]
maven-repository-webapp/src/main/java/org/apache/maven/repository/manager/web/mapper/RepositoryActionMapper.java
maven-repository-webapp/src/main/resources/xwork.xml

index c9676a486acc75841f377d0a797ae769bc98be23..e753c0d3f2df971041b61a97bfff6fb9a5f962dc 100644 (file)
@@ -21,32 +21,32 @@ public class DefaultRepositoryManager
     implements RepositoryManager
 {
     /**
-     * @plexus.requirement role="org.apache.maven.artifact.repository.discovery.ArtifactDiscoverer" role-hint="legacy"
+     * @plexus.requirement role-hint="legacy"
      */
     private ArtifactDiscoverer artifactDiscoverer;
 
     /**
-     * @plexus.requirement role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout" role-hint="legacy"
+     * @plexus.requirement role-hint="legacy"
      */
     private ArtifactRepositoryLayout legacyLayout;
 
     /**
-     * @plexus.requirement role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout" role-hint="default"
+     * @plexus.requirement role-hint="default"
      */
     private ArtifactRepositoryLayout defaultLayout;
 
     /**
-     * @plexus.requirement role="org.apache.maven.artifact.repository.ArtifactRepositoryFactory"
+     * @plexus.requirement
      */
     private ArtifactRepositoryFactory artifactRepositoryFactory;
 
     /**
-     * @plexus.requirement role="org.apache.maven.repository.converter.ArtifactRepositoryFactory"
+     * @plexus.requirement
      */
     private RepositoryConverter repositoryConverter;
 
     /**
-     * @plexus.requirement role="org.apache.maven.artifact.repository.reporter.ArtifactReporter" role-hint="default"
+     * @plexus.requirement role-hint="default"
      */
     private ArtifactReporter reporter;
 
index 2dfd10a995deca92d4e1be78fad9fed09f7f6add..568e436da0a3213a00d6b6a0f108e95cc3e6a8d3 100644 (file)
@@ -17,6 +17,7 @@ package org.apache.maven.repository.configuration;
  */
 
 import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.repository.proxy.ProxiedArtifactRepository;
 
 import java.util.List;
 
@@ -59,7 +60,7 @@ public interface ConfiguredRepositoryFactory
      * @param configuration the configuration
      * @return the artifact repository
      */
-    ArtifactRepository createProxiedRepository( ProxiedRepositoryConfiguration configuration );
+    ProxiedArtifactRepository createProxiedRepository( ProxiedRepositoryConfiguration configuration );
 
     /**
      * Create artifact repositories from the given proxy repository configurations.
index 4db9b49d9ea4761851cba95ec8aa48c3bd571550..290a523012d75cec8a0a891c5c5b89b85980f975 100644 (file)
@@ -20,6 +20,7 @@ import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.maven.repository.proxy.ProxiedArtifactRepository;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -55,7 +56,7 @@ public class DefaultConfiguredRepositoryFactory
         return repoFactory.createArtifactRepository( configuration.getId(), repoDir, layout, null, null );
     }
 
-    public ArtifactRepository createProxiedRepository( ProxiedRepositoryConfiguration configuration )
+    public ProxiedArtifactRepository createProxiedRepository( ProxiedRepositoryConfiguration configuration )
     {
         boolean enabled = isEnabled( configuration.getSnapshotsPolicy() );
         String updatePolicy =
@@ -69,8 +70,15 @@ public class DefaultConfiguredRepositoryFactory
             new ArtifactRepositoryPolicy( enabled, updatePolicy, ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL );
 
         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( configuration.getLayout() );
-        return repoFactory.createArtifactRepository( configuration.getId(), configuration.getUrl(), layout,
-                                                     snapshotsPolicy, releasesPolicy );
+        ArtifactRepository artifactRepository = repoFactory.createArtifactRepository( configuration.getId(),
+                                                                                      configuration.getUrl(), layout,
+                                                                                      snapshotsPolicy, releasesPolicy );
+        ProxiedArtifactRepository repository = new ProxiedArtifactRepository( artifactRepository );
+        repository.setCacheFailures( configuration.isCacheFailures() );
+        repository.setHardFail( configuration.isHardFail() );
+        repository.setName( configuration.getName() );
+        repository.setUseNetworkProxy( configuration.isUseNetworkProxy() );
+        return repository;
     }
 
     public List createRepositories( Configuration configuration )
index 562e3550a95a66bb52082a693bb1172585eca771..c0d78b41d9e14ebcfe94803f7173df1fd7cdb661 100644 (file)
@@ -51,7 +51,8 @@ public class DefaultProxyManager
     private ConfigurationStore configurationStore;
 
     /**
-     * @plexus.requirement
+     * @plexus.requirement role="org.apache.maven.repository.proxy.ProxyRequestHandler"
+     * @todo seems to be a bug in qdox that the role above is required
      */
     private ProxyRequestHandler requestHandler;
 
@@ -61,22 +62,40 @@ public class DefaultProxyManager
     private ConfiguredRepositoryFactory repositoryFactory;
 
     /**
-     * The proxy handlers for each managed repository.
+     * The proxy groups for each managed repository.
      */
     private Map/*<String,ProxiedRepositoryGroup>*/ proxyGroups;
 
+    /**
+     * The default proxy group/managed repository.
+     */
+    private ProxiedRepositoryGroup defaultProxyGroup;
+
     public File get( String path )
         throws ProxyException, ResourceDoesNotExistException
     {
         assert path.startsWith( "/" );
 
-        Map groups = getProxyRepositoryHandlers();
+        Map groups = getProxyGroups();
 
-        String id = parseRepositoryId( path, groups );
+        ProxiedRepositoryGroup proxyGroup = parseRepositoryId( path, groups );
 
-        String repositoryPath = path.substring( id.length() + 2 );
-
-        ProxiedRepositoryGroup proxyGroup = (ProxiedRepositoryGroup) groups.get( id );
+        String repositoryPath = path;
+        if ( proxyGroup == null )
+        {
+            if ( defaultProxyGroup != null )
+            {
+                proxyGroup = defaultProxyGroup;
+            }
+            else
+            {
+                throw new ResourceDoesNotExistException( "No repositories exist under the path: " + path );
+            }
+        }
+        else
+        {
+            repositoryPath = repositoryPath.substring( proxyGroup.getManagedRepository().getId().length() + 2 );
+        }
 
         return requestHandler.get( repositoryPath, proxyGroup.getProxiedRepositories(),
                                    proxyGroup.getManagedRepository(), proxyGroup.getWagonProxy() );
@@ -87,13 +106,26 @@ public class DefaultProxyManager
     {
         assert path.startsWith( "/" );
 
-        Map groups = getProxyRepositoryHandlers();
-
-        String id = parseRepositoryId( path, groups );
+        Map groups = getProxyGroups();
 
-        String repositoryPath = path.substring( id.length() + 2 );
+        ProxiedRepositoryGroup proxyGroup = parseRepositoryId( path, groups );
 
-        ProxiedRepositoryGroup proxyGroup = (ProxiedRepositoryGroup) groups.get( id );
+        String repositoryPath = path;
+        if ( proxyGroup == null )
+        {
+            if ( defaultProxyGroup != null )
+            {
+                proxyGroup = defaultProxyGroup;
+            }
+            else
+            {
+                throw new ResourceDoesNotExistException( "No repositories exist under the path: " + path );
+            }
+        }
+        else
+        {
+            repositoryPath = repositoryPath.substring( proxyGroup.getManagedRepository().getId().length() + 2 );
+        }
 
         return requestHandler.getAlways( repositoryPath, proxyGroup.getProxiedRepositories(),
                                          proxyGroup.getManagedRepository(), proxyGroup.getWagonProxy() );
@@ -115,7 +147,7 @@ public class DefaultProxyManager
         return configuration;
     }
 
-    private Map getProxyRepositoryHandlers()
+    private Map getProxyGroups()
         throws ProxyException
     {
         if ( proxyGroups == null )
@@ -137,6 +169,13 @@ public class DefaultProxyManager
                             new ProxiedRepositoryGroup( proxiedRepositories, managedRepository, wagonProxy ) );
             }
 
+            // TODO: ability to configure default proxy separately
+
+            if ( groups.size() == 1 )
+            {
+                defaultProxyGroup = (ProxiedRepositoryGroup) groups.values().iterator().next();
+            }
+
             proxyGroups = groups;
         }
         return proxyGroups;
@@ -157,19 +196,22 @@ public class DefaultProxyManager
         return repositories;
     }
 
-    private static String parseRepositoryId( String path, Map handlers )
+    private static ProxiedRepositoryGroup parseRepositoryId( String path, Map groups )
         throws ProxyException, ResourceDoesNotExistException
     {
-        for ( Iterator i = handlers.keySet().iterator(); i.hasNext(); )
+        ProxiedRepositoryGroup group = null;
+
+        for ( Iterator i = groups.entrySet().iterator(); i.hasNext() && group == null; )
         {
-            String id = (String) i.next();
+            Map.Entry entry = (Map.Entry) i.next();
 
-            if ( path.startsWith( "/" + id + "/" ) )
+            if ( path.startsWith( "/" + entry.getKey() + "/" ) )
             {
-                return id;
+                group = (ProxiedRepositoryGroup) entry.getValue();
             }
         }
-        throw new ResourceDoesNotExistException( "No repositories exist under the path: " + path );
+
+        return group;
     }
 
     private static ProxyInfo createWagonProxy( Proxy proxy )
index 99253be1cb512f52b4c68fc40be077e2d4ed83a5..934c2ce0c440935e18f3c0a6cbaa4ee9e5296c8e 100644 (file)
       <artifactId>plexus-log4j-logging</artifactId>
       <version>1.1-alpha-2</version>
     </dependency>
+    <dependency>
+      <groupId>org.apache.maven.wagon</groupId>
+      <artifactId>wagon-http-lightweight</artifactId>
+      <version>1.0-beta-1</version>
+      <scope>runtime</scope>
+    </dependency>
     <dependency>
       <groupId>org.apache.maven.wagon</groupId>
       <artifactId>wagon-file</artifactId>
diff --git a/maven-repository-webapp/src/main/java/org/apache/maven/repository/manager/web/action/ProxyAction.java b/maven-repository-webapp/src/main/java/org/apache/maven/repository/manager/web/action/ProxyAction.java
new file mode 100644 (file)
index 0000000..246463d
--- /dev/null
@@ -0,0 +1,104 @@
+package org.apache.maven.repository.manager.web.action;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 com.opensymphony.xwork.ActionSupport;
+import org.apache.maven.repository.proxy.ProxyException;
+import org.apache.maven.repository.proxy.ProxyManager;
+import org.apache.maven.wagon.ResourceDoesNotExistException;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+
+/**
+ * Proxy functionality.
+ *
+ * @plexus.component role="com.opensymphony.xwork.Action" role-hint="proxyAction"
+ */
+public class ProxyAction
+    extends ActionSupport
+{
+    /**
+     * @plexus.requirement
+     */
+    private ProxyManager proxyManager;
+
+    private String path;
+
+    private String filename;
+
+    private String contentType;
+
+    private static final String NOT_FOUND = "notFound";
+
+    private InputStream artifactStream;
+
+    public String execute()
+        throws ProxyException
+    {
+        try
+        {
+            File file = proxyManager.get( path );
+
+            artifactStream = new FileInputStream( file );
+
+            // TODO: could be better
+            contentType = "application/octet-stream";
+
+            filename = file.getName();
+        }
+        catch ( ResourceDoesNotExistException e )
+        {
+            // TODO: set message?
+            return NOT_FOUND;
+        }
+        catch ( FileNotFoundException e )
+        {
+            // TODO: set message?
+            return NOT_FOUND;
+        }
+
+        return SUCCESS;
+    }
+
+    public String getPath()
+    {
+        return path;
+    }
+
+    public void setPath( String path )
+    {
+        this.path = path;
+    }
+
+    public String getFilename()
+    {
+        return filename;
+    }
+
+    public String getContentType()
+    {
+        return contentType;
+    }
+
+    public InputStream getArtifactStream()
+    {
+        return artifactStream;
+    }
+}
index 9447fd23c08a0a30c9699de01088dc91529ee208..ae89c37047865786184947fba060e717b6df5fd9 100644 (file)
@@ -33,6 +33,8 @@ public class RepositoryActionMapper
 {
     private static final String BROWSE_PREFIX = "/browse/";
 
+    private static final String PROXY_PREFIX = "/proxy/";
+
     public String getUriFromActionMapping( ActionMapping actionMapping )
     {
         Map params = actionMapping.getParams();
@@ -49,6 +51,10 @@ public class RepositoryActionMapper
             return BROWSE_PREFIX + params.remove( "groupId" ) + "/" + params.remove( "artifactId" ) + "/" +
                 params.remove( "version" );
         }
+        else if ( "proxy".equals( actionMapping.getName() ) )
+        {
+            return PROXY_PREFIX + params.remove( "path" );
+        }
 
         return super.getUriFromActionMapping( actionMapping );
     }
@@ -89,6 +95,15 @@ public class RepositoryActionMapper
                 }
             }
         }
+        else if ( path.startsWith( PROXY_PREFIX ) )
+        {
+            // retain the leading /
+            path = path.substring( PROXY_PREFIX.length() - 1 );
+
+            Map params = new HashMap();
+            params.put( "path", path );
+            return new ActionMapping( "proxy", "/", "", params );
+        }
 
         return super.getMapping( httpServletRequest );
     }
index 0ae8a0ad2132cd03a463b5a234639d0c17fc378f..db1d796a7c3a044206fe0eff572fb6229dde5d01 100644 (file)
       <result>/WEB-INF/jsp/showArtifact.jsp</result>
     </action>
 
-    <!-- TODO! old actions
-    <action name="proxy" class="org.apache.maven.repository.proxy.web.action.RepositoryProxyAction">
-      <result name="success" type="stream">
-        <param name="contentType">application/octet-stream</param>
+    <action name="proxy" class="proxyAction">
+      <result type="stream">
+        <param name="contentType">${contentType}</param>
+        <param name="contentDisposition">filename="${filename}"</param>
         <param name="inputName">artifactStream</param>
         <param name="bufferSize">1024</param>
       </result>
-      <result name="notFound" type="dispatcher">notFoundError</result>
-      <result name="proxyError" type="dispatcher">proxyError</result>
+      <result name="notFound" type="httpheader">404</result>
     </action>
-    -->
   </package>
 
   <!-- Configuration for the admin package. -->