aboutsummaryrefslogtreecommitdiffstats
path: root/archiva-modules/archiva-web/archiva-rest
diff options
context:
space:
mode:
authorCarlos Sanchez <csanchez@maestrodev.com>2014-11-05 20:11:08 +0100
committerCarlos Sanchez <carlos@apache.org>2014-12-01 16:48:13 +0100
commite4da1fa6f5a56bbf63bae1de7d1b7dd0ae0375aa (patch)
tree6477dd5905f983cc3f4cd83580795ab8a7be9ef8 /archiva-modules/archiva-web/archiva-rest
parentd9473241c956359325aa10bdbff4e39be284c559 (diff)
downloadarchiva-e4da1fa6f5a56bbf63bae1de7d1b7dd0ae0375aa.tar.gz
archiva-e4da1fa6f5a56bbf63bae1de7d1b7dd0ae0375aa.zip
[MRM-1390] Add REST methods to search JCR store for generic metadata and properties
Added REST methods: artifactsByProjectVersionMetadata/{key}/{value} artifactsByMetadata/{key}/{value} artifactsByProperty/{key}/{value} searchArtifacts/{text} searchArtifacts/{key}/{text} In JCR implementation When searching into any property (key = nil) we can't do exact searchs
Diffstat (limited to 'archiva-modules/archiva-web/archiva-rest')
-rw-r--r--archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java98
-rw-r--r--archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/AbstractRestService.java8
-rw-r--r--archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java102
-rw-r--r--archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java2
-rw-r--r--archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java86
5 files changed, 295 insertions, 1 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 5cd0f50b9..777d15d90 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
@@ -231,4 +231,102 @@ public interface BrowseService
@RedbackAuthorization(noPermission = true, noRestriction = true)
List<Artifact> getArtifacts( @PathParam("r") String repositoryId )
throws ArchivaRestServiceException;
+
+ /**
+ * Return List of artifacts from this repository with project version level metadata key matching value. If
+ * repository is not provided the search runs in all repositories.
+ *
+ * @param key
+ * @param value
+ * @param repositoryId
+ * @return
+ * @throws ArchivaRestServiceException
+ * @since 2.2
+ */
+ @Path( "artifactsByProjectVersionMetadata/{key}/{value}" )
+ @GET
+ @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
+ @RedbackAuthorization( noPermission = true, noRestriction = true )
+ List<Artifact> getArtifactsByProjectVersionMetadata( @PathParam( "key" ) String key, @PathParam( "value" ) String value,
+ @QueryParam("repositoryId") String repositoryId )
+ throws ArchivaRestServiceException;
+
+ /**
+ * Return List of artifacts from this repository with artifact metadata key matching value.
+ * If repository is not provided the search runs in all repositories.
+ *
+ * @param key
+ * @param value
+ * @param repositoryId
+ * @return
+ * @throws ArchivaRestServiceException
+ * @since 2.2
+ */
+ @Path( "artifactsByMetadata/{key}/{value}" )
+ @GET
+ @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
+ @RedbackAuthorization( noPermission = true, noRestriction = true )
+ List<Artifact> getArtifactsByMetadata( @PathParam( "key" ) String key, @PathParam( "value" ) String value,
+ @QueryParam("repositoryId") String repositoryId )
+ throws ArchivaRestServiceException;
+
+ /**
+ * Return List of artifacts from this repository with property key matching value.
+ * If repository is not provided the search runs in all repositories.
+ *
+ * @param key
+ * @param value
+ * @param repositoryId
+ * @return
+ * @throws ArchivaRestServiceException
+ * @since 2.2
+ */
+ @Path( "artifactsByProperty/{key}/{value}" )
+ @GET
+ @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
+ @RedbackAuthorization( noPermission = true, noRestriction = true )
+ List<Artifact> getArtifactsByProperty( @PathParam( "key" ) String key, @PathParam( "value" ) String value,
+ @QueryParam("repositoryId") String repositoryId )
+ throws ArchivaRestServiceException;
+
+ /**
+ * Search artifacts with any property matching text. If repository is not provided the search runs in all
+ * repositories. If exact is true only the artifacts whose property match exactly are returned.
+ *
+ * @param text
+ * @param repositoryId
+ * @param exact
+ * @return
+ * @throws ArchivaRestServiceException
+ * @since 2.2
+ */
+ @Path( "searchArtifacts/{text}" )
+ @GET
+ @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
+ @RedbackAuthorization( noPermission = true, noRestriction = true )
+ List<Artifact> searchArtifacts( @PathParam( "text" ) String text,
+ @QueryParam( "repositoryId" ) String repositoryId,
+ @QueryParam( "exact" ) Boolean exact )
+ throws ArchivaRestServiceException;
+
+ /**
+ * Search artifacts with the property specified by key matching text. If repository is not provided the search runs
+ * in all repositories. If exact is true only the artifacts whose property match exactly are returned.
+ *
+ * @param key
+ * @param text
+ * @param repositoryId
+ * @param exact
+ * @return
+ * @throws ArchivaRestServiceException
+ * @since 2.2
+ */
+ @Path( "searchArtifacts/{key}/{text}" )
+ @GET
+ @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
+ @RedbackAuthorization( noPermission = true, noRestriction = true )
+ List<Artifact> searchArtifacts( @PathParam( "key" ) String key, @PathParam( "text" ) String text,
+ @QueryParam( "repositoryId" ) String repositoryId,
+ @QueryParam( "exact" ) Boolean exact )
+ throws ArchivaRestServiceException;
}
diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/AbstractRestService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/AbstractRestService.java
index b0107cc38..2282959b3 100644
--- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/AbstractRestService.java
+++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/AbstractRestService.java
@@ -62,6 +62,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
+
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -322,9 +323,14 @@ public abstract class AbstractRestService
for ( ArtifactMetadata artifact : artifactMetadatas )
{
+ String repoId = repositoryId != null ? repositoryId : artifact.getRepositoryId();
+ if ( repoId == null ) {
+ throw new IllegalStateException( "Repository Id is null" );
+ }
+
ArtifactBuilder builder =
new ArtifactBuilder().forArtifactMetadata( artifact ).withManagedRepositoryContent(
- repositoryContentFactory.getManagedRepositoryContent( repositoryId ) );
+ repositoryContentFactory.getManagedRepositoryContent( repoId ) );
Artifact art = builder.build();
art.setUrl( getArtifactUrl( art, repositoryId ) );
artifacts.add( art );
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 c7af0fece..5e5a4654b 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
@@ -942,6 +942,66 @@ public class DefaultBrowseService
}
@Override
+ public List<Artifact> getArtifactsByProjectVersionMetadata( String key, String value, String repositoryId )
+ throws ArchivaRestServiceException
+ {
+ RepositorySession repositorySession = repositorySessionFactory.createSession();
+ try
+ {
+ List<ArtifactMetadata> artifactMetadatas = repositorySession.getRepository().getArtifactsByProjectVersionMetadata( key, value, repositoryId );
+ return buildArtifacts( artifactMetadatas, repositoryId );
+ }
+ catch ( MetadataRepositoryException e )
+ {
+ throw new ArchivaRestServiceException( e.getMessage(), e );
+ }
+ finally
+ {
+ repositorySession.close();
+ }
+ }
+
+ @Override
+ public List<Artifact> getArtifactsByMetadata( String key, String value, String repositoryId )
+ throws ArchivaRestServiceException
+ {
+ RepositorySession repositorySession = repositorySessionFactory.createSession();
+ try
+ {
+ List<ArtifactMetadata> artifactMetadatas = repositorySession.getRepository().getArtifactsByMetadata( key, value, repositoryId );
+ return buildArtifacts( artifactMetadatas, repositoryId );
+ }
+ catch ( MetadataRepositoryException e )
+ {
+ throw new ArchivaRestServiceException( e.getMessage(), e );
+ }
+ finally
+ {
+ repositorySession.close();
+ }
+ }
+
+ @Override
+ public List<Artifact> getArtifactsByProperty( String key, String value, String repositoryId )
+ throws ArchivaRestServiceException
+ {
+ RepositorySession repositorySession = repositorySessionFactory.createSession();
+ try
+ {
+ List<ArtifactMetadata> artifactMetadatas = repositorySession.getRepository().getArtifactsByProperty( key, value, repositoryId );
+ return buildArtifacts( artifactMetadatas, repositoryId );
+ }
+ catch ( MetadataRepositoryException e )
+ {
+ throw new ArchivaRestServiceException( e.getMessage(), e );
+ }
+ finally
+ {
+ repositorySession.close();
+ }
+ }
+
+ @Override
public Boolean importMetadata( MetadataAddRequest metadataAddRequest, String repositoryId )
throws ArchivaRestServiceException
{
@@ -959,6 +1019,48 @@ public class DefaultBrowseService
return result;
}
+ @Override
+ public List<Artifact> searchArtifacts( String text, String repositoryId, Boolean exact )
+ throws ArchivaRestServiceException
+ {
+ RepositorySession repositorySession = repositorySessionFactory.createSession();
+ try
+ {
+ List<ArtifactMetadata> artifactMetadatas =
+ repositorySession.getRepository().searchArtifacts( text, repositoryId, exact == null ? false : exact );
+ return buildArtifacts( artifactMetadatas, repositoryId );
+ }
+ catch ( MetadataRepositoryException e )
+ {
+ throw new ArchivaRestServiceException( e.getMessage(), e );
+ }
+ finally
+ {
+ repositorySession.close();
+ }
+ }
+
+ @Override
+ public List<Artifact> searchArtifacts( String key, String text, String repositoryId, Boolean exact )
+ throws ArchivaRestServiceException
+ {
+ RepositorySession repositorySession = repositorySessionFactory.createSession();
+ try
+ {
+ List<ArtifactMetadata> artifactMetadatas =
+ repositorySession.getRepository().searchArtifacts( key, text, repositoryId, exact == null ? false : exact );
+ return buildArtifacts( artifactMetadatas, repositoryId );
+ }
+ catch ( MetadataRepositoryException e )
+ {
+ throw new ArchivaRestServiceException( e.getMessage(), e );
+ }
+ finally
+ {
+ repositorySession.close();
+ }
+ }
+
//---------------------------
// internals
//---------------------------
diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java
index 310448015..2453e6af8 100644
--- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java
+++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java
@@ -252,6 +252,7 @@ public abstract class AbstractArchivaRestTest
protected BrowseService getBrowseService( String authzHeader, boolean useXml )
{
+ // START SNIPPET: cxf-browseservice-creation
BrowseService service =
JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
BrowseService.class,
@@ -274,6 +275,7 @@ public abstract class AbstractArchivaRestTest
WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
}
return service;
+ // END SNIPPET: cxf-browseservice-creation
}
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 99cb83b5d..9490cf6a6 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
@@ -237,6 +237,92 @@ public class BrowseServiceTest
@Test
+ public void getArtifactsByMetadata()
+ throws Exception
+ {
+ // START SNIPPET: get-artifacts-by-metadata
+ BrowseService browseService = getBrowseService( authorizationHeader, true );
+
+ List<Artifact> artifactDownloadInfos = browseService.getArtifactsByMetadata( "type", "pom", TEST_REPO_ID );
+
+ assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 11 );
+ // END SNIPPET: get-artifacts-by-metadata
+ }
+
+
+ @Test
+ public void getArtifactsByProjectVersionMetadata()
+ throws Exception
+ {
+ // START SNIPPET: get-artifacts-by-project-version-metadata
+ BrowseService browseService = getBrowseService( authorizationHeader, true );
+
+ browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
+
+ List<Artifact> artifactDownloadInfos = browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", TEST_REPO_ID );
+
+ assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
+ // END SNIPPET: get-artifacts-by-project-version-metadata
+ }
+
+
+ @Test
+ public void getArtifactsByProjectVersionMetadataWithNoRepository()
+ throws Exception
+ {
+ BrowseService browseService = getBrowseService( authorizationHeader, true );
+
+ browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
+
+ List<Artifact> artifactDownloadInfos = browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", null );
+
+ assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
+ }
+
+
+ @Test
+ public void getArtifactsByProperty()
+ throws Exception
+ {
+ // START SNIPPET: get-artifacts-by-property
+ BrowseService browseService = getBrowseService( authorizationHeader, true );
+
+ List<Artifact> artifactDownloadInfos = browseService.getArtifactsByProperty( "org.name", "The Apache Software Foundation", TEST_REPO_ID );
+
+ assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
+ // END SNIPPET: get-artifacts-by-property
+ }
+
+
+ @Test
+ public void searchArtifacts()
+ throws Exception
+ {
+ // START SNIPPET: search-artifacts
+ BrowseService browseService = getBrowseService( authorizationHeader, true );
+
+ List<Artifact> artifactDownloadInfos = browseService.searchArtifacts( "The Apache Software Foundation", TEST_REPO_ID, true );
+
+ assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
+ // END SNIPPET: search-artifacts
+ }
+
+
+ @Test
+ public void searchArtifactsByField()
+ throws Exception
+ {
+ // START SNIPPET: search-artifacts-by-field
+ BrowseService browseService = getBrowseService( authorizationHeader, true );
+
+ List<Artifact> artifactDownloadInfos = browseService.searchArtifacts( "org.name", "The Apache Software Foundation", TEST_REPO_ID, true );
+
+ assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
+ // END SNIPPET: search-artifacts-by-field
+ }
+
+
+ @Test
public void readArtifactContentText()
throws Exception
{