From 09a3de2d63310cb66243d73a312bc8754b893033 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Tue, 6 Mar 2012 18:30:59 +0000 Subject: [PATCH] add validation on adding LegacyPath rest method git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1297637 13f79535-47bb-0310-9956-ffa450edef68 --- .../admin/model/beans/LegacyArtifactPath.java | 66 +++++++++++++++---- .../DefaultArchivaAdministrationService.java | 27 ++++++++ .../ArchivaAdministrationServiceTest.java | 8 ++- 3 files changed, 84 insertions(+), 17 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/beans/LegacyArtifactPath.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/beans/LegacyArtifactPath.java index 4b34741fb..d6ca8730e 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/beans/LegacyArtifactPath.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/beans/LegacyArtifactPath.java @@ -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(); } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultArchivaAdministrationService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultArchivaAdministrationService.java index 655d90513..495be22a8 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultArchivaAdministrationService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultArchivaAdministrationService.java @@ -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 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 ) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArchivaAdministrationServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArchivaAdministrationServiceTest.java index bfdda3e46..a7d0c301a 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArchivaAdministrationServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArchivaAdministrationServiceTest.java @@ -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() ); } -- 2.39.5