diff options
author | Olivier Lamy <olamy@apache.org> | 2014-05-21 10:12:43 +1000 |
---|---|---|
committer | Olivier Lamy <olamy@apache.org> | 2014-05-21 10:12:43 +1000 |
commit | b272a1bc2ff3009bc47895253a50439572fa9d2e (patch) | |
tree | 3503cbf62d10a8cc8257e4ca718be03c13e4734b /archiva-modules | |
parent | 511858d4ab738b77aaefec9b752f513e3148c1ad (diff) | |
download | archiva-b272a1bc2ff3009bc47895253a50439572fa9d2e.tar.gz archiva-b272a1bc2ff3009bc47895253a50439572fa9d2e.zip |
[MRM-1843] provide mechanism to obtain the latest version of an artifact
Implements LATEST keyword
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/DefaultSearchService.java | 52 |
1 files changed, 25 insertions, 27 deletions
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 f030a3b86..b923e37c7 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.common.utils.VersionComparator; import org.apache.archiva.indexer.search.RepositorySearch; import org.apache.archiva.indexer.search.RepositorySearchException; import org.apache.archiva.indexer.search.SearchFields; @@ -43,16 +44,19 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.TreeMap; /** * @author Olivier Lamy */ -@Service("searchService#rest") +@Service( "searchService#rest" ) public class DefaultSearchService extends AbstractRestService implements SearchService { + private static final String LATEST_KEYWORD = "LATEST"; + @Inject private RepositorySearch repositorySearch; @@ -265,35 +269,14 @@ public class DefaultSearchService } ).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 ); searchField.setArtifactId( artifactId ); searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging ); - searchField.setVersion( version ); + if ( !StringUtils.equals( version, LATEST_KEYWORD ) ) + { + searchField.setVersion( version ); + } searchField.setClassifier( classifier ); List<String> userRepos = getObservablesRepoIds().getStrings(); searchField.setRepositories( @@ -343,7 +326,7 @@ public class DefaultSearchService } // TODO return json result of the query ? - if ( artifacts.size() > 1 ) + if ( artifacts.size() > 1 && !StringUtils.equals( version, LATEST_KEYWORD ) ) { return Response.status( new Response.StatusType() { @@ -367,6 +350,21 @@ public class DefaultSearchService } ).build(); } + // version is LATEST so we have to find the latest one from the result + if ( artifacts.size() > 1 && StringUtils.equals( version, LATEST_KEYWORD ) ) + { + TreeMap<String, Artifact> artifactPerVersion = new TreeMap<>( VersionComparator.getInstance() ); + + for ( Artifact artifact : artifacts ) + { + artifactPerVersion.put( artifact.getVersion(), artifact ); + } + + return Response.temporaryRedirect( + new URI( artifactPerVersion.lastEntry().getValue().getUrl() ) ).build(); + + } + Artifact artifact = artifacts.get( 0 ); return Response.temporaryRedirect( new URI( artifact.getUrl() ) ).build(); |