@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;
}
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;
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;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
+import java.util.zip.ZipEntry;
/**
* @author Olivier Lamy
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
//---------------------------
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;
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;
+ }
+ }
+
+
}
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) {
-
- });
+ });
});
}