From 1130d556c279f35460472d0a7f31217f4a090b36 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Wed, 23 May 2012 16:26:02 +0000 Subject: [PATCH] use bean rather than passing string. return back repositoryId used. git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1341937 13f79535-47bb-0310-9956-ffa450edef68 --- .../rest/api/model/ArtifactContentEntry.java | 31 ++++++++++++++++--- .../rest/api/services/BrowseService.java | 11 ++++--- .../rest/services/DefaultBrowseService.java | 19 ++++++------ .../services/ArtifactContentEntriesTests.java | 29 +++++++++-------- .../rest/services/BrowseServiceTest.java | 14 +++++---- 5 files changed, 67 insertions(+), 37 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ArtifactContentEntry.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ArtifactContentEntry.java index b74a143b9..7c2fac56e 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ArtifactContentEntry.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ArtifactContentEntry.java @@ -35,16 +35,21 @@ public class ArtifactContentEntry private int depth; + private String repositoryId; + public ArtifactContentEntry() { // no op } - public ArtifactContentEntry( String path, boolean file, int depth ) + + public ArtifactContentEntry( String path, boolean file, int depth, String repositoryId ) { + this.path = path; this.file = file; this.depth = depth; + this.repositoryId = repositoryId; } public String getPath() @@ -77,6 +82,17 @@ public class ArtifactContentEntry this.depth = depth; } + public String getRepositoryId() + { + return repositoryId; + } + + public void setRepositoryId( String repositoryId ) + { + this.repositoryId = repositoryId; + } + + @Override public boolean equals( Object o ) { @@ -99,7 +115,11 @@ public class ArtifactContentEntry { return false; } - if ( path != null ? !path.equals( that.path ) : that.path != null ) + if ( !path.equals( that.path ) ) + { + return false; + } + if ( !repositoryId.equals( that.repositoryId ) ) { return false; } @@ -110,20 +130,23 @@ public class ArtifactContentEntry @Override public int hashCode() { - int result = path != null ? path.hashCode() : 0; + int result = path.hashCode(); result = 31 * result + ( file ? 1 : 0 ); result = 31 * result + depth; + result = 31 * result + repositoryId.hashCode(); return result; } + @Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append( "ArtifactContentEntry" ); - sb.append( "{text='" ).append( path ).append( '\'' ); + sb.append( "{path='" ).append( path ).append( '\'' ); sb.append( ", file=" ).append( file ); sb.append( ", depth=" ).append( depth ); + sb.append( ", repositoryId='" ).append( repositoryId ).append( '\'' ); sb.append( '}' ); return sb.toString(); } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java index 6cd280bd5..22028c4ab 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java @@ -22,6 +22,7 @@ import org.apache.archiva.admin.model.beans.ManagedRepository; import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.redback.authorization.RedbackAuthorization; import org.apache.archiva.rest.api.model.Artifact; +import org.apache.archiva.rest.api.model.ArtifactContent; import org.apache.archiva.rest.api.model.ArtifactContentEntry; import org.apache.archiva.rest.api.model.ArtifactDownloadInfo; import org.apache.archiva.rest.api.model.BrowseResult; @@ -172,14 +173,14 @@ public interface BrowseService @Path( "artifactContentText/{g}/{a}/{v}" ) @GET - @Produces( MediaType.TEXT_PLAIN ) + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @RedbackAuthorization( noPermission = true, noRestriction = true ) /** * if path is empty content of the file is returned (for pom view) */ - String getArtifactContentText( @PathParam( "g" ) String groupId, @PathParam( "a" ) String artifactId, - @PathParam( "v" ) String version, @QueryParam( "c" ) String classifier, - @QueryParam( "t" ) String type, @QueryParam( "p" ) String path, - @QueryParam( "repositoryId" ) String repositoryId ) + ArtifactContent getArtifactContentText( @PathParam( "g" ) String groupId, @PathParam( "a" ) String artifactId, + @PathParam( "v" ) String version, @QueryParam( "c" ) String classifier, + @QueryParam( "t" ) String type, @QueryParam( "p" ) String path, + @QueryParam( "repositoryId" ) String repositoryId ) throws ArchivaRestServiceException; } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java index 37bb2f7fb..1bad078d5 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java @@ -39,6 +39,7 @@ import org.apache.archiva.repository.RepositoryContentFactory; import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryNotFoundException; import org.apache.archiva.rest.api.model.Artifact; +import org.apache.archiva.rest.api.model.ArtifactContent; import org.apache.archiva.rest.api.model.ArtifactContentEntry; import org.apache.archiva.rest.api.model.ArtifactDownloadInfo; import org.apache.archiva.rest.api.model.BrowseResult; @@ -635,7 +636,7 @@ public class DefaultBrowseService File file = managedRepositoryContent.toFile( archivaArtifact ); if ( file.exists() ) { - return readFileEntries( file, path ); + return readFileEntries( file, path, repoId ); } } } @@ -714,8 +715,8 @@ public class DefaultBrowseService return artifactDownloadInfos; } - public String getArtifactContentText( String groupId, String artifactId, String version, String classifier, - String type, String path, String repositoryId ) + public ArtifactContent getArtifactContentText( String groupId, String artifactId, String version, String classifier, + String type, String path, String repositoryId ) throws ArchivaRestServiceException { List selectedRepos = getSelectedRepos( repositoryId ); @@ -743,14 +744,14 @@ public class DefaultBrowseService InputStream inputStream = jarFile.getInputStream( zipEntry ); try { - return IOUtils.toString( inputStream ); + return new ArtifactContent( IOUtils.toString( inputStream ), repoId ); } finally { IOUtils.closeQuietly( inputStream ); } } - return FileUtils.readFileToString( file ); + return new ArtifactContent( FileUtils.readFileToString( file ), repoId ); } } catch ( IOException e ) @@ -774,14 +775,14 @@ public class DefaultBrowseService log.debug( "artifact: {}:{}:{}:{}:{} not found", Arrays.asList( groupId, artifactId, version, classifier, type ).toArray( new String[5] ) ); // 404 ? - return ""; + return new ArtifactContent(); } //--------------------------- // internals //--------------------------- - protected List readFileEntries( File file, String filterPath ) + protected List readFileEntries( File file, String filterPath, String repoId ) throws IOException { Map artifactContentEntryMap = new HashMap(); @@ -808,7 +809,7 @@ public class DefaultBrowseService artifactContentEntryMap.put( entryRootPath, new ArtifactContentEntry( entryRootPath, !currentEntry.isDirectory(), - depth ) ); + depth, repoId ) ); } else { @@ -817,7 +818,7 @@ public class DefaultBrowseService { artifactContentEntryMap.put( cleanedEntryName, new ArtifactContentEntry( cleanedEntryName, !currentEntry.isDirectory(), - depth ) ); + depth, repoId ) ); } } } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java index 18c4942d9..10820f8c9 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java @@ -58,12 +58,13 @@ public class ArtifactContentEntriesTests File file = new File( getBasedir(), "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" ); - List artifactContentEntries = browseService.readFileEntries( file, null ); + List artifactContentEntries = browseService.readFileEntries( file, null, "foo" ); log.info( "artifactContentEntries: {}", artifactContentEntries ); assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 2 ).contains( - new ArtifactContentEntry( "org", false, 0 ), new ArtifactContentEntry( "META-INF", false, 0 ) ); + new ArtifactContentEntry( "org", false, 0, "foo" ), + new ArtifactContentEntry( "META-INF", false, 0, "foo" ) ); } @@ -75,12 +76,13 @@ public class ArtifactContentEntriesTests File file = new File( getBasedir(), "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" ); - List artifactContentEntries = browseService.readFileEntries( file, "" ); + List artifactContentEntries = browseService.readFileEntries( file, "", "foo" ); log.info( "artifactContentEntries: {}", artifactContentEntries ); assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 2 ).contains( - new ArtifactContentEntry( "org", false, 0 ), new ArtifactContentEntry( "META-INF", false, 0 ) ); + new ArtifactContentEntry( "org", false, 0, "foo" ), + new ArtifactContentEntry( "META-INF", false, 0, "foo" ) ); } @@ -92,12 +94,13 @@ public class ArtifactContentEntriesTests File file = new File( getBasedir(), "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" ); - List artifactContentEntries = browseService.readFileEntries( file, "/" ); + List artifactContentEntries = browseService.readFileEntries( file, "/", "foo" ); log.info( "artifactContentEntries: {}", artifactContentEntries ); assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 2 ).contains( - new ArtifactContentEntry( "org", false, 0 ), new ArtifactContentEntry( "META-INF", false, 0 ) ); + new ArtifactContentEntry( "org", false, 0, "foo" ), + new ArtifactContentEntry( "META-INF", false, 0, "foo" ) ); } @@ -109,12 +112,12 @@ public class ArtifactContentEntriesTests File file = new File( getBasedir(), "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" ); - List artifactContentEntries = browseService.readFileEntries( file, "org" ); + List artifactContentEntries = browseService.readFileEntries( file, "org", "foo" ); log.info( "artifactContentEntries: {}", artifactContentEntries ); assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 1 ).contains( - new ArtifactContentEntry( "org/apache", false, 1 ) ); + new ArtifactContentEntry( "org/apache", false, 1, "foo" ) ); } @@ -127,12 +130,12 @@ public class ArtifactContentEntriesTests "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" ); List artifactContentEntries = - browseService.readFileEntries( file, "org/apache/commons/logging/impl/" ); + browseService.readFileEntries( file, "org/apache/commons/logging/impl/", "foo" ); log.info( "artifactContentEntries: {}", artifactContentEntries ); assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 16 ).contains( - new ArtifactContentEntry( "org/apache/commons/logging/impl/AvalonLogger.class", true, 5 ) ); + new ArtifactContentEntry( "org/apache/commons/logging/impl/AvalonLogger.class", true, 5, "foo" ) ); } @@ -145,13 +148,13 @@ public class ArtifactContentEntriesTests "src/test/repo-with-osgi/commons-logging/commons-logging/1.1/commons-logging-1.1.jar" ); List artifactContentEntries = - browseService.readFileEntries( file, "org/apache/commons/logging/" ); + browseService.readFileEntries( file, "org/apache/commons/logging/", "foo" ); log.info( "artifactContentEntries: {}", artifactContentEntries ); assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 10 ).contains( - new ArtifactContentEntry( "org/apache/commons/logging/impl", false, 4 ), - new ArtifactContentEntry( "org/apache/commons/logging/LogSource.class", true, 4 ) ); + new ArtifactContentEntry( "org/apache/commons/logging/impl", false, 4, "foo" ), + new ArtifactContentEntry( "org/apache/commons/logging/LogSource.class", true, 4, "foo" ) ); } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java index 8b3aa6e77..86181269f 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java @@ -249,7 +249,8 @@ public class BrowseServiceTest log.info( "artifactContentEntries: {}", artifactContentEntries ); assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 2 ).contains( - new ArtifactContentEntry( "org", false, 0 ), new ArtifactContentEntry( "META-INF", false, 0 ) ); + new ArtifactContentEntry( "org", false, 0, testRepoId ), + new ArtifactContentEntry( "META-INF", false, 0, testRepoId ) ); deleteTestRepo( testRepoId ); } @@ -275,7 +276,7 @@ public class BrowseServiceTest log.info( "artifactContentEntries: {}", artifactContentEntries ); assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 1 ).contains( - new ArtifactContentEntry( "org/apache", false, 1 ) ); + new ArtifactContentEntry( "org/apache", false, 1, testRepoId ) ); deleteTestRepo( testRepoId ); } @@ -301,8 +302,8 @@ public class BrowseServiceTest log.info( "artifactContentEntries: {}", artifactContentEntries ); assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 10 ).contains( - new ArtifactContentEntry( "org/apache/commons/logging/impl", false, 4 ), - new ArtifactContentEntry( "org/apache/commons/logging/LogSource.class", true, 4 ) ); + new ArtifactContentEntry( "org/apache/commons/logging/impl", false, 4, testRepoId ), + new ArtifactContentEntry( "org/apache/commons/logging/LogSource.class", true, 4, testRepoId ) ); deleteTestRepo( testRepoId ); } @@ -351,7 +352,8 @@ public class BrowseServiceTest { String text = browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null, - "org/apache/commons/logging/LogSource.java", testRepoId ); + "org/apache/commons/logging/LogSource.java", + testRepoId ).getContent(); log.debug( "text: {}", text ); @@ -386,7 +388,7 @@ public class BrowseServiceTest { String text = browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null, - testRepoId ); + testRepoId ).getContent(); log.info( "text: {}", text ); -- 2.39.5