From fd37dbe4b438edb052a524d0535901ad7f8abfbc Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Mon, 21 May 2012 14:24:21 +0000 Subject: [PATCH] add rest service to retrieve file content (from a jar or a pom content) git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1341045 13f79535-47bb-0310-9956-ffa450edef68 --- .../rest/api/services/BrowseService.java | 13 ++++ .../rest/services/DefaultBrowseService.java | 64 ++++++++++++++++ .../rest/services/BrowseServiceTest.java | 75 +++++++++++++++++++ .../src/main/webapp/js/archiva/search.js | 4 +- 4 files changed, 153 insertions(+), 3 deletions(-) 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 7859c3535..6cd280bd5 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 @@ -169,4 +169,17 @@ public interface BrowseService @PathParam( "v" ) String version, @QueryParam( "repositoryId" ) String repositoryId ) throws ArchivaRestServiceException; + + @Path( "artifactContentText/{g}/{a}/{v}" ) + @GET + @Produces( MediaType.TEXT_PLAIN ) + @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 ) + 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 36f84f1a7..2983b8dd5 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 @@ -53,6 +53,8 @@ import org.apache.archiva.rest.services.utils.ArtifactDownloadInfoBuilder; import org.apache.archiva.rest.services.utils.TreeDependencyNodeVisitor; import org.apache.archiva.security.ArchivaSecurityException; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException; import org.springframework.stereotype.Service; @@ -61,6 +63,7 @@ import javax.inject.Inject; import javax.ws.rs.core.Response; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -72,6 +75,7 @@ import java.util.Map; import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import java.util.zip.ZipEntry; /** * @author Olivier Lamy @@ -709,6 +713,66 @@ public class DefaultBrowseService return artifactDownloadInfos; } + public String getArtifactContentText( String groupId, String artifactId, String version, String classifier, + String type, String path, String repositoryId ) + throws ArchivaRestServiceException + { + List selectedRepos = getSelectedRepos( repositoryId ); + try + { + for ( String repoId : selectedRepos ) + { + + ManagedRepositoryContent managedRepositoryContent = + repositoryContentFactory.getManagedRepositoryContent( repoId ); + ArchivaArtifact archivaArtifact = new ArchivaArtifact( groupId, artifactId, version, classifier, + StringUtils.isEmpty( type ) ? "jar" : type, + repositoryId ); + File file = managedRepositoryContent.toFile( archivaArtifact ); + if ( !file.exists() ) + { + // 404 ? + return ""; + } + if ( StringUtils.isNotBlank( path ) ) + { + // zip entry of the path -> path must a real file entry of the archive + JarFile jarFile = new JarFile( file ); + ZipEntry zipEntry = jarFile.getEntry( path ); + InputStream inputStream = jarFile.getInputStream( zipEntry ); + try + { + return IOUtils.toString( inputStream ); + } + finally + { + IOUtils.closeQuietly( inputStream ); + } + } + return FileUtils.readFileToString( file ); + } + } + catch ( IOException e ) + { + log.error( e.getMessage(), e ); + throw new ArchivaRestServiceException( e.getMessage(), + Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); + } + catch ( RepositoryNotFoundException e ) + { + log.error( e.getMessage(), e ); + throw new ArchivaRestServiceException( e.getMessage(), + Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); + } + catch ( RepositoryException e ) + { + log.error( e.getMessage(), e ); + throw new ArchivaRestServiceException( e.getMessage(), + Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); + } + return ""; + } + //--------------------------- // internals //--------------------------- 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 bbce29056..8b3aa6e77 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 @@ -26,9 +26,11 @@ import org.apache.archiva.rest.api.model.BrowseResultEntry; import org.apache.archiva.rest.api.model.Entry; import org.apache.archiva.rest.api.model.VersionsList; import org.apache.archiva.rest.api.services.BrowseService; +import org.apache.cxf.jaxrs.client.WebClient; import org.fest.assertions.MapAssert; import org.junit.Test; +import javax.ws.rs.core.MediaType; import java.io.File; import java.util.HashMap; import java.util.List; @@ -327,4 +329,77 @@ public class BrowseServiceTest deleteTestRepo( testRepoId ); } + + @Test + public void readArtifactContentText() + throws Exception + { + String testRepoId = "test-repo"; + // force guest user creation if not exists + if ( getUserService( authorizationHeader ).getGuestUser() == null ) + { + assertNotNull( getUserService( authorizationHeader ).createGuestUser() ); + } + + createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false ); + + BrowseService browseService = getBrowseService( authorizationHeader, true ); + + WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN ); + + try + { + String text = + browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null, + "org/apache/commons/logging/LogSource.java", testRepoId ); + + log.debug( "text: {}", text ); + + assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" ); + } + catch ( Exception e ) + { + log.error( e.getMessage(), e ); + throw e; + } + } + + + @Test + public void readArtifactContentTextPom() + throws Exception + { + String testRepoId = "test-repo"; + // force guest user creation if not exists + if ( getUserService( authorizationHeader ).getGuestUser() == null ) + { + assertNotNull( getUserService( authorizationHeader ).createGuestUser() ); + } + + createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false ); + + BrowseService browseService = getBrowseService( authorizationHeader, true ); + + WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN ); + + try + { + String text = + browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null, + testRepoId ); + + log.info( "text: {}", text ); + + assertThat( text ).contains( + "http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/" ).contains( + "commons-dev-subscribe@jakarta.apache.org" ); + } + catch ( Exception e ) + { + log.error( e.getMessage(), e ); + throw e; + } + } + + } diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/search.js b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/search.js index 13fb3ce1f..9a90da8ef 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/search.js +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/search.js @@ -282,14 +282,12 @@ define("search",["jquery","i18n","jquery.tmpl","choosen","order!knockout","knock var entriesUrl = "restServices/archivaServices/browseService/artifactContentEntries/"+encodeURIComponent(self.groupId); entriesUrl+="/"+encodeURIComponent(self.artifactId)+"/"+encodeURIComponent(self.version); entriesUrl+="?repositoryId="+encodeURIComponent(getSelectedBrowsingRepository()); - //entriesUrl+="&p="+encodeURIComponent(artifactContentEntry.name); $("#main-content #artifact_content_tree").fileTree({ script: entriesUrl, root: "" },function(file) { - - }); + }); }); } -- 2.39.5