From: Brett Porter Date: Tue, 15 Aug 2006 02:59:06 +0000 (+0000) Subject: [MRM-138] add proxy to the webapp X-Git-Tag: archiva-0.9-alpha-1~683 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c9e6033906d481917fa077a207afaca30f6f7fa7;p=archiva.git [MRM-138] add proxy to the webapp git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@431499 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/maven-repository-core/src/main/java/org/apache/maven/repository/DefaultRepositoryManager.java b/maven-repository-core/src/main/java/org/apache/maven/repository/DefaultRepositoryManager.java index c9676a486..e753c0d3f 100644 --- a/maven-repository-core/src/main/java/org/apache/maven/repository/DefaultRepositoryManager.java +++ b/maven-repository-core/src/main/java/org/apache/maven/repository/DefaultRepositoryManager.java @@ -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; diff --git a/maven-repository-core/src/main/java/org/apache/maven/repository/configuration/ConfiguredRepositoryFactory.java b/maven-repository-core/src/main/java/org/apache/maven/repository/configuration/ConfiguredRepositoryFactory.java index 2dfd10a99..568e436da 100644 --- a/maven-repository-core/src/main/java/org/apache/maven/repository/configuration/ConfiguredRepositoryFactory.java +++ b/maven-repository-core/src/main/java/org/apache/maven/repository/configuration/ConfiguredRepositoryFactory.java @@ -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. diff --git a/maven-repository-core/src/main/java/org/apache/maven/repository/configuration/DefaultConfiguredRepositoryFactory.java b/maven-repository-core/src/main/java/org/apache/maven/repository/configuration/DefaultConfiguredRepositoryFactory.java index 4db9b49d9..290a52301 100644 --- a/maven-repository-core/src/main/java/org/apache/maven/repository/configuration/DefaultConfiguredRepositoryFactory.java +++ b/maven-repository-core/src/main/java/org/apache/maven/repository/configuration/DefaultConfiguredRepositoryFactory.java @@ -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 ) diff --git a/maven-repository-core/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java b/maven-repository-core/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java index 562e3550a..c0d78b41d 100644 --- a/maven-repository-core/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java +++ b/maven-repository-core/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java @@ -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/**/ 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 ) diff --git a/maven-repository-webapp/pom.xml b/maven-repository-webapp/pom.xml index 99253be1c..934c2ce0c 100644 --- a/maven-repository-webapp/pom.xml +++ b/maven-repository-webapp/pom.xml @@ -57,6 +57,12 @@ plexus-log4j-logging 1.1-alpha-2 + + org.apache.maven.wagon + wagon-http-lightweight + 1.0-beta-1 + runtime + org.apache.maven.wagon wagon-file 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 index 000000000..246463d5d --- /dev/null +++ b/maven-repository-webapp/src/main/java/org/apache/maven/repository/manager/web/action/ProxyAction.java @@ -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; + } +} diff --git a/maven-repository-webapp/src/main/java/org/apache/maven/repository/manager/web/mapper/RepositoryActionMapper.java b/maven-repository-webapp/src/main/java/org/apache/maven/repository/manager/web/mapper/RepositoryActionMapper.java index 9447fd23c..ae89c3704 100644 --- a/maven-repository-webapp/src/main/java/org/apache/maven/repository/manager/web/mapper/RepositoryActionMapper.java +++ b/maven-repository-webapp/src/main/java/org/apache/maven/repository/manager/web/mapper/RepositoryActionMapper.java @@ -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 ); } diff --git a/maven-repository-webapp/src/main/resources/xwork.xml b/maven-repository-webapp/src/main/resources/xwork.xml index 0ae8a0ad2..db1d796a7 100644 --- a/maven-repository-webapp/src/main/resources/xwork.xml +++ b/maven-repository-webapp/src/main/resources/xwork.xml @@ -111,17 +111,15 @@ /WEB-INF/jsp/showArtifact.jsp -