You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by oc...@apache.org on 2007/09/16 07:18:23 UTC

svn commit: r576032 - /maven/archiva/trunk/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/ProxiedDavServer.java

Author: oching
Date: Sat Sep 15 22:18:18 2007
New Revision: 576032

URL: http://svn.apache.org/viewvc?rev=576032&view=rev
Log:
[MRM-153]
Submitted by: nicolas de loof

- Added method in ProxiedDavServer that allows to get a relocated artifact (either client-side relocation or server-side 
relocation)


Modified:
    maven/archiva/trunk/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/ProxiedDavServer.java

Modified: maven/archiva/trunk/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/ProxiedDavServer.java
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/ProxiedDavServer.java?rev=576032&r1=576031&r2=576032&view=diff
==============================================================================
--- maven/archiva/trunk/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/ProxiedDavServer.java (original)
+++ maven/archiva/trunk/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/ProxiedDavServer.java Sat Sep 15 22:18:18 2007
@@ -34,6 +34,11 @@
 import org.apache.maven.archiva.repository.layout.LayoutException;
 import org.apache.maven.archiva.repository.metadata.MetadataTools;
 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
+import org.apache.maven.model.DistributionManagement;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Relocation;
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
+import org.apache.maven.wagon.ResourceDoesNotExistException;
 import org.codehaus.plexus.webdav.AbstractDavServerComponent;
 import org.codehaus.plexus.webdav.DavServerComponent;
 import org.codehaus.plexus.webdav.DavServerException;
@@ -44,6 +49,8 @@
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
 import java.io.IOException;
 
 /**
@@ -52,8 +59,7 @@
  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
  * @version $Id$
  * @plexus.component role="org.codehaus.plexus.webdav.DavServerComponent"
- * role-hint="proxied"
- * instantiation-strategy="per-lookup"
+ * role-hint="proxied" instantiation-strategy="per-lookup"
  */
 public class ProxiedDavServer
     extends AbstractDavServerComponent
@@ -118,7 +124,8 @@
 
         repositoryConfiguration = config.findManagedRepositoryById( getPrefix() );
 
-        managedRepository = ArchivaConfigurationAdaptor.toArchivaRepository( repositoryConfiguration );
+        managedRepository =
+            ArchivaConfigurationAdaptor.toArchivaRepository( repositoryConfiguration );
 
         try
         {
@@ -135,16 +142,18 @@
     {
         if ( WebdavMethodUtil.isReadMethod( request.getRequest().getMethod() ) )
         {
-            if ( !hasResource( request.getLogicalResource() ) )
-            {
-                fetchContentFromProxies( request );
-            }
+            // if ( !hasResource( request.getLogicalResource() ) )
+            // {
+            fetchContentFromProxies( request );
+            // }
         }
         else
         {
             // Create parent directories that don't exist when writing a file
-            // This actually makes this implementation not compliant to the WebDAV RFC - but we have enough knowledge
-            // about how the collection is being used to do this reasonably and some versions of Maven's WebDAV don't
+            // This actually makes this implementation not compliant to the
+            // WebDAV RFC - but we have enough knowledge
+            // about how the collection is being used to do this reasonably and
+            // some versions of Maven's WebDAV don't
             // correctly create the collections themselves.
             File rootDirectory = getRootDirectory();
             if ( rootDirectory != null )
@@ -231,6 +240,8 @@
             artifact = resourceLayout.toArtifactReference( resource );
             if ( artifact != null )
             {
+                applyServerSideRelocation( artifact );
+
                 connectors.fetchFromProxies( managedRepository, artifact );
                 request.getRequest().setPathInfo( layout.toPath( artifact ) );
                 return;
@@ -243,6 +254,71 @@
         catch ( ProxyException e )
         {
             throw new ServletException( "Unable to fetch artifact resource.", e );
+        }
+    }
+
+    /**
+     * A relocation capable client will request the POM prior to the artifact,
+     * and will then read meta-data and do client side relocation. A simplier
+     * client (like maven 1) will only request the artifact and not use the
+     * metadatas.
+     * <p>
+     * For such clients, archiva does server-side relocation by reading itself
+     * the &lt;relocation&gt; element in metadatas and serving the expected
+     * artifact.
+     */
+    protected void applyServerSideRelocation( ArtifactReference artifact )
+        throws ProxyException
+    {
+        if ( "pom".equals( artifact.getType() ) )
+        {
+            return;
+        }
+
+        // Build the artifact POM reference
+        ArtifactReference pomReference = new ArtifactReference();
+        pomReference.setGroupId( artifact.getGroupId() );
+        pomReference.setArtifactId( artifact.getArtifactId() );
+        pomReference.setVersion( artifact.getVersion() );
+        pomReference.setType( "pom" );
+
+        // Get the artifact POM from proxied repositories if needed
+        connectors.fetchFromProxies( managedRepository, pomReference );
+
+        // Open and read the POM from the managed repo
+        File pom = new File( getRootDirectory(), layout.toPath( pomReference ) );
+        try
+        {
+            Model model = new MavenXpp3Reader().read( new FileReader( pom ) );
+            DistributionManagement dist = model.getDistributionManagement();
+            if ( dist != null )
+            {
+                Relocation relocation = dist.getRelocation();
+                if ( relocation != null )
+                {
+                    // artifact is relocated : update the repositoryPath
+                    if ( relocation.getGroupId() != null )
+                    {
+                        artifact.setGroupId( relocation.getGroupId() );
+                    }
+                    if ( relocation.getArtifactId() != null )
+                    {
+                        artifact.setArtifactId( relocation.getArtifactId() );
+                    }
+                    if ( relocation.getVersion() != null )
+                    {
+                        artifact.setVersion( relocation.getVersion() );
+                    }
+                }
+            }
+        }
+        catch ( FileNotFoundException e )
+        {
+            // Artifact has no POM in repo : ignore
+        }
+        catch ( Exception e )
+        {
+            // invalid POM : ignore
         }
     }