From 2c09abccbc813a6838bbe6f62c5d7950c6aa85ea Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Tue, 20 May 2014 16:55:34 +1000 Subject: [PATCH] [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 --- .../rest/services/AbstractRestService.java | 4 + .../DefaultProxyConnectorService.java | 2 - .../rest/services/DefaultSearchService.java | 161 +++++++++++++++++- 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; @@ -94,6 +95,9 @@ public abstract class AbstractRestService @Inject protected ArchivaAdministration archivaAdministration; + @Inject + protected ProxyConnectorAdmin proxyConnectorAdmin; + @Inject protected ManagedRepositoryAdmin managedRepositoryAdmin; 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 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 userRepos = getObservablesRepoIds().getStrings(); + searchField.setRepositories( + StringUtils.isEmpty( repositoryId ) ? userRepos : Arrays.asList( repositoryId ) ); searchField.setExactSearch( true ); SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null ); List 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> 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(); } -- 2.39.5