aboutsummaryrefslogtreecommitdiffstats
path: root/archiva-modules
diff options
context:
space:
mode:
authorOlivier Lamy <olamy@apache.org>2014-05-20 16:55:34 +1000
committerOlivier Lamy <olamy@apache.org>2014-05-20 16:55:34 +1000
commit2c09abccbc813a6838bbe6f62c5d7950c6aa85ea (patch)
treeaa190f9a0c3889b4b4e9819f70da4df34659f6e4 /archiva-modules
parent85047d3bb6163b3223ec9a4b90dae2a968d6bcb6 (diff)
downloadarchiva-2c09abccbc813a6838bbe6f62c5d7950c6aa85ea.tar.gz
archiva-2c09abccbc813a6838bbe6f62c5d7950c6aa85ea.zip
[MRM-1843] provide mechanism to obtain the latest version of an artifact
add some query validation. ensure we properly change the repositoryId for queries without any repositoryId so result can come from a remote repository index
Diffstat (limited to 'archiva-modules')
-rw-r--r--archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/AbstractRestService.java4
-rw-r--r--archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultProxyConnectorService.java2
-rw-r--r--archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java161
3 files changed, 163 insertions, 4 deletions
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 3443625e5..3b43fbc09 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
@@ -23,6 +23,7 @@ import org.apache.archiva.admin.model.AuditInformation;
import org.apache.archiva.admin.model.RepositoryAdminException;
import org.apache.archiva.admin.model.admin.ArchivaAdministration;
import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
+import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
import org.apache.archiva.audit.AuditEvent;
import org.apache.archiva.audit.AuditListener;
import org.apache.archiva.common.utils.VersionUtil;
@@ -95,6 +96,9 @@ public abstract class AbstractRestService
protected ArchivaAdministration archivaAdministration;
@Inject
+ protected ProxyConnectorAdmin proxyConnectorAdmin;
+
+ @Inject
protected ManagedRepositoryAdmin managedRepositoryAdmin;
@Inject
diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultProxyConnectorService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultProxyConnectorService.java
index b9557c030..88ebe9616 100644
--- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultProxyConnectorService.java
+++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultProxyConnectorService.java
@@ -41,8 +41,6 @@ public class DefaultProxyConnectorService
extends AbstractRestService
implements ProxyConnectorService
{
- @Inject
- private ProxyConnectorAdmin proxyConnectorAdmin;
private List<Policy> allPolicies;
diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java
index 9dae9491d..c8c46210f 100644
--- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java
+++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java
@@ -19,6 +19,7 @@ package org.apache.archiva.rest.services;
* under the License.
*/
+import org.apache.archiva.admin.model.beans.ProxyConnector;
import org.apache.archiva.indexer.search.RepositorySearch;
import org.apache.archiva.indexer.search.RepositorySearchException;
import org.apache.archiva.indexer.search.SearchFields;
@@ -43,6 +44,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import java.util.Map;
/**
* @author Olivier Lamy
@@ -215,6 +217,79 @@ public class DefaultSearchService
{
try
{
+ // validate query
+
+ if ( StringUtils.isEmpty( groupId ) )
+ {
+ return Response.status( new Response.StatusType()
+ {
+ @Override
+ public int getStatusCode()
+ {
+ return Response.Status.BAD_REQUEST.getStatusCode();
+ }
+
+ @Override
+ public Response.Status.Family getFamily()
+ {
+ return Response.Status.BAD_REQUEST.getFamily();
+ }
+
+ @Override
+ public String getReasonPhrase()
+ {
+ return "groupId mandatory";
+ }
+ } ).build();
+ }
+
+ if ( StringUtils.isEmpty( artifactId ) )
+ {
+ return Response.status( new Response.StatusType()
+ {
+ @Override
+ public int getStatusCode()
+ {
+ return Response.Status.BAD_REQUEST.getStatusCode();
+ }
+
+ @Override
+ public Response.Status.Family getFamily()
+ {
+ return Response.Status.BAD_REQUEST.getFamily();
+ }
+
+ @Override
+ public String getReasonPhrase()
+ {
+ return "artifactId mandatory";
+ }
+ } ).build();
+ }
+
+ if ( StringUtils.isEmpty( version ) )
+ {
+ return Response.status( new Response.StatusType()
+ {
+ @Override
+ public int getStatusCode()
+ {
+ return Response.Status.BAD_REQUEST.getStatusCode();
+ }
+
+ @Override
+ public Response.Status.Family getFamily()
+ {
+ return Response.Status.BAD_REQUEST.getFamily();
+ }
+
+ @Override
+ public String getReasonPhrase()
+ {
+ return "version mandatory";
+ }
+ } ).build();
+ }
SearchFields searchField = new SearchFields();
searchField.setGroupId( groupId );
@@ -222,11 +297,37 @@ public class DefaultSearchService
searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
searchField.setVersion( version );
searchField.setClassifier( classifier );
- searchField.setRepositories( Arrays.asList( repositoryId ) );
+ List<String> userRepos = getObservablesRepoIds().getStrings();
+ searchField.setRepositories(
+ StringUtils.isEmpty( repositoryId ) ? userRepos : Arrays.asList( repositoryId ) );
searchField.setExactSearch( true );
SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
List<Artifact> artifacts = getArtifacts( searchResults );
+ if ( artifacts.isEmpty() )
+ {
+ return Response.status( new Response.StatusType()
+ {
+ @Override
+ public int getStatusCode()
+ {
+ return Response.Status.NO_CONTENT.getStatusCode();
+ }
+
+ @Override
+ public Response.Status.Family getFamily()
+ {
+ return Response.Status.NO_CONTENT.getFamily();
+ }
+
+ @Override
+ public String getReasonPhrase()
+ {
+ return "your query doesn't return any artifact";
+ }
+ } ).build();
+ }
+
// TODO improve that with querying lucene with null value for classifier
// so simple loop and retain only artifact with null classifier
if ( classifier == null )
@@ -243,7 +344,63 @@ public class DefaultSearchService
artifacts = filteredArtifacts;
}
- String artifactUrl = getArtifactUrl( artifacts.get( 0 ), repositoryId );
+ // TODO return json result of the query ?
+ if ( artifacts.size() > 1 )
+ {
+ return Response.status( new Response.StatusType()
+ {
+ @Override
+ public int getStatusCode()
+ {
+ return Response.Status.BAD_REQUEST.getStatusCode();
+ }
+
+ @Override
+ public Response.Status.Family getFamily()
+ {
+ return Response.Status.BAD_REQUEST.getFamily();
+ }
+
+ @Override
+ public String getReasonPhrase()
+ {
+ return "your query return more than one artifact";
+ }
+ } ).build();
+ }
+
+ String artifactUrl = null;
+
+ Artifact artifact = artifacts.get( 0 );
+
+ // we need to configure correctly the repositoryId
+ if ( StringUtils.isEmpty( repositoryId ) )
+ {
+ // is it a good one? if yes nothing to
+ // if not search the repo who is proxy for this remote
+ if ( !userRepos.contains( artifact.getContext() ) )
+ {
+ for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorAdmin.getProxyConnectorAsMap().entrySet() )
+ {
+ for ( ProxyConnector proxyConnector : entry.getValue() )
+ {
+ if ( StringUtils.equals( "remote-" + proxyConnector.getTargetRepoId(),
+ artifact.getContext() ) //
+ && userRepos.contains( entry.getKey() ) )
+ {
+ return Response.temporaryRedirect(
+ new URI( getArtifactUrl( artifact, entry.getKey() ) ) ).build();
+ }
+ }
+ }
+
+ }
+
+ }
+ else
+ {
+ artifactUrl = getArtifactUrl( artifact, repositoryId );
+ }
return Response.temporaryRedirect( new URI( artifactUrl ) ).build();
}