]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1843] provide mechanism to obtain the latest version of an artifact
authorOlivier Lamy <olamy@apache.org>
Tue, 20 May 2014 06:55:34 +0000 (16:55 +1000)
committerOlivier Lamy <olamy@apache.org>
Tue, 20 May 2014 06:55:34 +0000 (16:55 +1000)
add some query validation. ensure we properly change the repositoryId for queries without any repositoryId so result can come from a remote repository index

archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/AbstractRestService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultProxyConnectorService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java

index 3443625e581701bb9ea00994a129011b562bad0f..3b43fbc096554b19bf4e0a21b70477698ae27db5 100644 (file)
@@ -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;
 
index b9557c030449cd040a9300a90a66afb78db67eac..88ebe961681443655ef3ec05185323ca0f9249a6 100644 (file)
@@ -41,8 +41,6 @@ public class DefaultProxyConnectorService
     extends AbstractRestService
     implements ProxyConnectorService
 {
-    @Inject
-    private ProxyConnectorAdmin proxyConnectorAdmin;
 
     private List<Policy> allPolicies;
 
index 9dae9491d99f5472733cd135d4b7f61d49bb0df6..c8c46210f136cd755b2ab775b2b908cee3fe97aa 100644 (file)
@@ -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();
         }