add validation on adding LegacyPath rest method

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1297637 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-03-06 18:30:59 +00:00
parent 0acf0c9e11
commit 09a3de2d63
3 changed files with 84 additions and 17 deletions

View File

@ -18,9 +18,6 @@ package org.apache.archiva.admin.model.beans;
* under the License.
*/
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
@ -43,6 +40,16 @@ public class LegacyArtifactPath
*/
private String artifact;
private String groupId;
private String artifactId;
private String version;
private String classifier;
private String type;
public LegacyArtifactPath()
{
// no op
@ -51,7 +58,25 @@ public class LegacyArtifactPath
public LegacyArtifactPath( String path, String artifact )
{
this.path = path;
this.artifact = artifact;
initValues( this.artifact );
}
private void initValues( String artifact )
{
String[] splitted = artifact.split( ":" );
if ( splitted.length < 4 )
{
throw new IllegalArgumentException( "artifact value '" + artifact + "' is not correct" );
}
this.groupId = splitted[0];// artifact.split( ":" )[0];
this.artifactId = splitted[1];// artifact.split( ":" )[1];
this.version = splitted[2];// artifact.split( ":" )[2];
String classifier = splitted.length >= 4 ? splitted[3] : null;// artifact.split( ":" )[3];
this.classifier = classifier.length() > 0 ? classifier : null;
String type = splitted.length >= 5 ? splitted[4] : null;
this.type = type.length() > 0 ? artifact.split( ":" )[4] : null;
}
public String getPath()
@ -72,6 +97,7 @@ public class LegacyArtifactPath
public void setArtifact( String artifact )
{
this.artifact = artifact;
initValues( this.artifact );
}
public boolean match( String path )
@ -79,35 +105,42 @@ public class LegacyArtifactPath
return path.equals( this.path );
}
@JsonIgnore
public void setGroupId( String groupId )
{
this.groupId = groupId;
}
public String getGroupId()
{
return artifact.split( ":" )[0];
return this.groupId;// artifact.split( ":" )[0];
}
@JsonIgnore
public String getArtifactId()
{
return artifact.split( ":" )[1];
return this.artifactId;// artifact.split( ":" )[1];
}
public void setArtifactId( String artifactId )
{
this.artifactId = artifactId;
}
@JsonIgnore
public String getVersion()
{
return artifact.split( ":" )[2];
return this.version;// artifact.split( ":" )[2];
}
@JsonIgnore
public String getClassifier()
{
String classifier = artifact.split( ":" )[3];
return classifier.length() > 0 ? classifier : null;
//String classifier = artifact.split( ":" )[3];
//return classifier.length() > 0 ? classifier : null;
return this.classifier;
}
@JsonIgnore
public String getType()
{
return artifact.split( ":" )[4];
return this.type;// artifact.split( ":" )[4];
}
@Override
@ -145,6 +178,11 @@ public class LegacyArtifactPath
sb.append( "LegacyArtifactPath" );
sb.append( "{path='" ).append( path ).append( '\'' );
sb.append( ", artifact='" ).append( artifact ).append( '\'' );
sb.append( ", groupId='" ).append( groupId ).append( '\'' );
sb.append( ", artifactId='" ).append( artifactId ).append( '\'' );
sb.append( ", version='" ).append( version ).append( '\'' );
sb.append( ", classifier='" ).append( classifier ).append( '\'' );
sb.append( ", type='" ).append( type ).append( '\'' );
sb.append( '}' );
return sb.toString();
}

View File

@ -25,11 +25,16 @@ import org.apache.archiva.admin.model.beans.LegacyArtifactPath;
import org.apache.archiva.admin.model.beans.NetworkConfiguration;
import org.apache.archiva.admin.model.beans.OrganisationInformation;
import org.apache.archiva.admin.model.beans.UiConfiguration;
import org.apache.archiva.model.ArtifactReference;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.rest.api.services.ArchivaAdministrationService;
import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -46,6 +51,10 @@ public class DefaultArchivaAdministrationService
@Inject
private ArchivaAdministration archivaAdministration;
@Inject
@Named( value = "managedRepositoryContent#legacy" )
private ManagedRepositoryContent repositoryContent;
public List<LegacyArtifactPath> getLegacyArtifactPaths()
throws ArchivaRestServiceException
{
@ -62,8 +71,26 @@ public class DefaultArchivaAdministrationService
public void addLegacyArtifactPath( LegacyArtifactPath legacyArtifactPath )
throws ArchivaRestServiceException
{
// Check the proposed Artifact macthes the path
ArtifactReference artifact = new ArtifactReference();
artifact.setGroupId( legacyArtifactPath.getGroupId() );
artifact.setArtifactId( legacyArtifactPath.getArtifactId() );
artifact.setClassifier( legacyArtifactPath.getClassifier() );
artifact.setVersion( legacyArtifactPath.getVersion() );
artifact.setType( legacyArtifactPath.getType() );
String path = repositoryContent.toPath( artifact );
if ( !StringUtils.equals( path, legacyArtifactPath.getPath() ) )
{
throw new ArchivaRestServiceException(
"artifact path reference '" + legacyArtifactPath.getPath() + "' does not match the initial path: '"
+ path + "'", Response.Status.BAD_REQUEST.getStatusCode() );
}
try
{
archivaAdministration.addLegacyArtifactPath( legacyArtifactPath, getAuditInformation() );
}
catch ( RepositoryAdminException e )

View File

@ -45,14 +45,16 @@ public class ArchivaAdministrationServiceTest
public void addAndDeleteLegacyPath()
throws Exception
{
//Path jaxen/jars/jaxen-1.0-FCS-full.jar
//Artifact jaxen:jaxen:1.0-FCS:full:jar
int initialSize = getArchivaAdministrationService().getLegacyArtifactPaths().size();
LegacyArtifactPath legacyArtifactPath = new LegacyArtifactPath();
legacyArtifactPath.setArtifact( "foo" );
legacyArtifactPath.setPath( "bar" );
legacyArtifactPath.setArtifact( "wine:bordeaux:2006:GREAT:jar" );
legacyArtifactPath.setPath( "wine/jars/bordeaux-2006-GREAT.jar" );
getArchivaAdministrationService().addLegacyArtifactPath( legacyArtifactPath );
assertEquals( initialSize + 1, getArchivaAdministrationService().getLegacyArtifactPaths().size() );
getArchivaAdministrationService().deleteLegacyArtifactPath( "bar" );
getArchivaAdministrationService().deleteLegacyArtifactPath( "wine/jars/bordeaux-2006-GREAT.jar" );
assertEquals( initialSize, getArchivaAdministrationService().getLegacyArtifactPaths().size() );
}