]> source.dussan.org Git - archiva.git/commitdiff
add rest service to retrieve file content (from a jar or a pom content)
authorOlivier Lamy <olamy@apache.org>
Mon, 21 May 2012 14:24:21 +0000 (14:24 +0000)
committerOlivier Lamy <olamy@apache.org>
Mon, 21 May 2012 14:24:21 +0000 (14:24 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1341045 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java
archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/search.js

index 7859c35357b0884d952fbd5cbaa910b49b3dca16..6cd280bd57dbcb3c0c7d5bda09318fbe88332bb5 100644 (file)
@@ -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;
 }
index 36f84f1a7588d09d2d3969a95315ab9c35cc8dc6..2983b8dd5e930438669fcea2a95922571c9bfc69 100644 (file)
@@ -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<String> 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
     //---------------------------
index bbce2905663102a2d9f3f908d2b817bb1f7296bc..8b3aa6e77fb3e6b53fed6315025bb5c6098ef715 100644 (file)
@@ -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(
+                "<url>http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/</url>" ).contains(
+                "<subscribe>commons-dev-subscribe@jakarta.apache.org</subscribe>" );
+        }
+        catch ( Exception e )
+        {
+            log.error( e.getMessage(), e );
+            throw e;
+        }
+    }
+
+
 }
index 13fb3ce1f91924a5068a5925b93acc0bb7677251..9a90da8ef135af47f04805933023dab7c7806814 100644 (file)
@@ -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) {
-
-                                 });
+                               });
                   });
 
                 }