[MRM-412] support legacy layout requests

Submitted by: nicolas de loof

git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@562356 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2007-08-03 06:54:05 +00:00
parent 59cd555432
commit f8e12362f6
6 changed files with 139 additions and 65 deletions

View File

@ -40,6 +40,12 @@ public interface BidirectionalRepositoryLayout
*/ */
public String getId(); public String getId();
/**
* Given a repository relative path, return <code>true</code> if the path is valid
* according to the repository layout.
*/
public boolean isValidPath( String path );
/** /**
* Given an ArchivaArtifact, return the relative path to the artifact. * Given an ArchivaArtifact, return the relative path to the artifact.
* *
@ -80,7 +86,8 @@ public interface BidirectionalRepositoryLayout
* an {@link ArchivaArtifact}) * an {@link ArchivaArtifact})
* @throws LayoutException if there was a problem converting the path to an artifact. * @throws LayoutException if there was a problem converting the path to an artifact.
*/ */
public ArchivaArtifact toArtifact( String path ) throws LayoutException; public ArchivaArtifact toArtifact( String path )
throws LayoutException;
/** /**
* Given a repository relative path to a filename, return the {@link ProjectReference} object suitable for the path. * Given a repository relative path to a filename, return the {@link ProjectReference} object suitable for the path.
@ -90,7 +97,8 @@ public interface BidirectionalRepositoryLayout
* a {@link ProjectReference}) * a {@link ProjectReference})
* @throws LayoutException if there was a problem converting the path to an artifact. * @throws LayoutException if there was a problem converting the path to an artifact.
*/ */
public ProjectReference toProjectReference( String path ) throws LayoutException; public ProjectReference toProjectReference( String path )
throws LayoutException;
/** /**
* Given a repository relative path to a filename, return the {@link VersionedReference} object suitable for the path. * Given a repository relative path to a filename, return the {@link VersionedReference} object suitable for the path.
@ -100,7 +108,8 @@ public interface BidirectionalRepositoryLayout
* a {@link VersionedReference}) * a {@link VersionedReference})
* @throws LayoutException if there was a problem converting the path to an artifact. * @throws LayoutException if there was a problem converting the path to an artifact.
*/ */
public VersionedReference toVersionedReference( String path ) throws LayoutException; public VersionedReference toVersionedReference( String path )
throws LayoutException;
/** /**
* Given a repository relative path to a filename, return the {@link VersionedReference} object suitable for the path. * Given a repository relative path to a filename, return the {@link VersionedReference} object suitable for the path.
@ -110,5 +119,6 @@ public interface BidirectionalRepositoryLayout
* a {@link ArtifactReference}) * a {@link ArtifactReference})
* @throws LayoutException if there was a problem converting the path to an artifact. * @throws LayoutException if there was a problem converting the path to an artifact.
*/ */
public ArtifactReference toArtifactReference( String path ) throws LayoutException; public ArtifactReference toArtifactReference( String path )
throws LayoutException;
} }

View File

@ -31,6 +31,7 @@ import org.codehaus.plexus.registry.Registry;
import org.codehaus.plexus.registry.RegistryListener; import org.codehaus.plexus.registry.RegistryListener;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
/** /**
@ -38,7 +39,6 @@ import java.util.Map;
* *
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$ * @version $Id$
*
* @plexus.component role="org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayoutFactory" * @plexus.component role="org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayoutFactory"
*/ */
public class BidirectionalRepositoryLayoutFactory public class BidirectionalRepositoryLayoutFactory
@ -62,13 +62,28 @@ public class BidirectionalRepositoryLayoutFactory
{ {
if ( !layouts.containsKey( type ) ) if ( !layouts.containsKey( type ) )
{ {
throw new LayoutException( "Layout type [" + type + "] does not exist. " + "Available types [" throw new LayoutException(
+ layouts.keySet() + "]" ); "Layout type [" + type + "] does not exist. " + "Available types [" + layouts.keySet() + "]" );
} }
return (BidirectionalRepositoryLayout) layouts.get( type ); return (BidirectionalRepositoryLayout) layouts.get( type );
} }
public BidirectionalRepositoryLayout getLayoutForPath( String path )
throws LayoutException
{
for ( Iterator iter = layouts.values().iterator(); iter.hasNext(); )
{
BidirectionalRepositoryLayout layout = (BidirectionalRepositoryLayout) iter.next();
if ( layout.isValidPath( path ) )
{
return layout;
}
}
throw new LayoutException( "No valid layout was found for path [" + path + "]" );
}
public BidirectionalRepositoryLayout getLayout( ArchivaArtifact artifact ) public BidirectionalRepositoryLayout getLayout( ArchivaArtifact artifact )
throws LayoutException throws LayoutException
{ {

View File

@ -32,7 +32,6 @@ import org.apache.maven.archiva.repository.content.DefaultArtifactExtensionMappi
* *
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$ * @version $Id$
*
* @plexus.component role-hint="default" * @plexus.component role-hint="default"
*/ */
public class DefaultBidirectionalRepositoryLayout public class DefaultBidirectionalRepositoryLayout
@ -134,7 +133,8 @@ public class DefaultBidirectionalRepositoryLayout
path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR ); path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
path.append( reference.getArtifactId() ).append( PATH_SEPARATOR ); path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
if( reference.getVersion() != null ) { if ( reference.getVersion() != null )
{
// add the version only if it is present // add the version only if it is present
path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR ); path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
} }
@ -148,8 +148,8 @@ public class DefaultBidirectionalRepositoryLayout
{ {
if ( !path.endsWith( "/maven-metadata.xml" ) ) if ( !path.endsWith( "/maven-metadata.xml" ) )
{ {
throw new LayoutException( "Only paths ending in '/maven-metadata.xml' can be " throw new LayoutException(
+ "converted to a ProjectReference." ); "Only paths ending in '/maven-metadata.xml' can be " + "converted to a ProjectReference." );
} }
PathReferences pathrefs = toPathReferences( path, false ); PathReferences pathrefs = toPathReferences( path, false );
@ -165,8 +165,8 @@ public class DefaultBidirectionalRepositoryLayout
{ {
if ( !path.endsWith( "/maven-metadata.xml" ) ) if ( !path.endsWith( "/maven-metadata.xml" ) )
{ {
throw new LayoutException( "Only paths ending in '/maven-metadata.xml' can be " throw new LayoutException(
+ "converted to a VersionedReference." ); "Only paths ending in '/maven-metadata.xml' can be " + "converted to a VersionedReference." );
} }
PathReferences pathrefs = toPathReferences( path, false ); PathReferences pathrefs = toPathReferences( path, false );
@ -211,6 +211,19 @@ public class DefaultBidirectionalRepositoryLayout
return path.toString(); return path.toString();
} }
public boolean isValidPath( String path )
{
try
{
toPathReferences( path, false );
return true;
}
catch ( LayoutException e )
{
return false;
}
}
private PathReferences toPathReferences( String path, boolean parseFilename ) private PathReferences toPathReferences( String path, boolean parseFilename )
throws LayoutException throws LayoutException
{ {
@ -232,8 +245,8 @@ public class DefaultBidirectionalRepositoryLayout
if ( pathParts.length < 4 ) if ( pathParts.length < 4 )
{ {
// Illegal Path Parts Length. // Illegal Path Parts Length.
throw new LayoutException( "Not enough parts to the path [" + path throw new LayoutException( "Not enough parts to the path [" + path +
+ "] to construct an ArchivaArtifact from. (Requires at least 4 parts)" ); "] to construct an ArchivaArtifact from. (Requires at least 4 parts)" );
} }
// Maven 2.x path. // Maven 2.x path.

View File

@ -34,7 +34,6 @@ import java.util.Map;
* *
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$ * @version $Id$
*
* @plexus.component role-hint="legacy" * @plexus.component role-hint="legacy"
*/ */
public class LegacyBidirectionalRepositoryLayout public class LegacyBidirectionalRepositoryLayout
@ -63,8 +62,8 @@ public class LegacyBidirectionalRepositoryLayout
public String toPath( ArchivaArtifact artifact ) public String toPath( ArchivaArtifact artifact )
{ {
return toPath( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), return toPath( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getClassifier(),
artifact.getClassifier(), artifact.getType() ); artifact.getType() );
} }
public String toPath( ProjectReference reference ) public String toPath( ProjectReference reference )
@ -177,8 +176,8 @@ public class LegacyBidirectionalRepositoryLayout
if ( pathParts.length != 3 ) if ( pathParts.length != 3 )
{ {
// Illegal Path Parts Length. // Illegal Path Parts Length.
throw new LayoutException( "Invalid number of parts to the path [" + path throw new LayoutException( "Invalid number of parts to the path [" + path +
+ "] to construct an ArchivaArtifact from. (Required to be 3 parts)" ); "] to construct an ArchivaArtifact from. (Required to be 3 parts)" );
} }
// The Group ID. // The Group ID.
@ -204,9 +203,9 @@ public class LegacyBidirectionalRepositoryLayout
if ( !prefs.type.equals( prefs.fileParts.extension ) ) if ( !prefs.type.equals( prefs.fileParts.extension ) )
{ {
throw new LayoutException( "Invalid artifact, mismatch on extension <" + prefs.fileParts.extension throw new LayoutException( "Invalid artifact, mismatch on extension <" + prefs.fileParts.extension +
+ "> and expected layout specified type <" + prefs.pathType + "> (mapped type: <" + prefs.type "> and expected layout specified type <" + prefs.pathType + "> (mapped type: <" + prefs.type +
+ ">) on path <" + path + ">" ); ">) on path <" + path + ">" );
} }
} }
@ -219,6 +218,19 @@ public class LegacyBidirectionalRepositoryLayout
throw new LayoutException( "Cannot parse legacy paths to a Project Reference." ); throw new LayoutException( "Cannot parse legacy paths to a Project Reference." );
} }
public boolean isValidPath( String path )
{
try
{
toPathReferences( path, false );
return true;
}
catch ( LayoutException e )
{
return false;
}
}
public ArchivaArtifact toArtifact( String path ) public ArchivaArtifact toArtifact( String path )
throws LayoutException throws LayoutException
{ {

View File

@ -74,4 +74,15 @@ public class BidirectionalRepositoryLayoutFactoryTest
/* expected path */ /* expected path */
} }
} }
public void testFindLayoutForPath()
throws LayoutException
{
BidirectionalRepositoryLayout layout =
factory.getLayoutForPath( "javax/servlet/servlet-api/2.3/servlet-api-2.3.jar" );
assertEquals( "default", layout.getId() );
layout = factory.getLayoutForPath( "javax.servlet/jars/servlet-api-2.3.jar" );
assertEquals( "legacy", layout.getId() );
}
} }

View File

@ -38,12 +38,11 @@ import org.codehaus.plexus.webdav.DavServerException;
import org.codehaus.plexus.webdav.servlet.DavServerRequest; import org.codehaus.plexus.webdav.servlet.DavServerRequest;
import org.codehaus.plexus.webdav.util.WebdavMethodUtil; import org.codehaus.plexus.webdav.util.WebdavMethodUtil;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletConfig; import javax.servlet.ServletConfig;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
/** /**
* ProxiedDavServer * ProxiedDavServer
@ -154,13 +153,25 @@ public class ProxiedDavServer
ProjectReference project; ProjectReference project;
VersionedReference versioned; VersionedReference versioned;
ArtifactReference artifact; ArtifactReference artifact;
BidirectionalRepositoryLayout resourceLayout;
try try
{ {
artifact = layout.toArtifactReference( resource ); resourceLayout = layoutFactory.getLayoutForPath( resource );
}
catch ( LayoutException e )
{
/* invalid request - eat it */
return;
}
try
{
artifact = resourceLayout.toArtifactReference( resource );
if ( artifact != null ) if ( artifact != null )
{ {
connectors.fetchFromProxies( managedRepository, artifact ); connectors.fetchFromProxies( managedRepository, artifact );
request.getRequest().setPathInfo( layout.toPath( artifact ) );
return; return;
} }
} }
@ -171,10 +182,11 @@ public class ProxiedDavServer
try try
{ {
versioned = layout.toVersionedReference( resource ); versioned = resourceLayout.toVersionedReference( resource );
if ( versioned != null ) if ( versioned != null )
{ {
connectors.fetchFromProxies( managedRepository, versioned ); connectors.fetchFromProxies( managedRepository, versioned );
request.getRequest().setPathInfo( layout.toPath( versioned ) );
return; return;
} }
} }
@ -185,10 +197,11 @@ public class ProxiedDavServer
try try
{ {
project = layout.toProjectReference( resource ); project = resourceLayout.toProjectReference( resource );
if ( project != null ) if ( project != null )
{ {
connectors.fetchFromProxies( managedRepository, project ); connectors.fetchFromProxies( managedRepository, project );
request.getRequest().setPathInfo( layout.toPath( project ) );
return; return;
} }
} }