mirror of https://github.com/apache/archiva.git
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
This commit is contained in:
parent
c78203c5bb
commit
fd37dbe4b4
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
//---------------------------
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue