From: Martin Stockhammer Date: Sat, 18 Dec 2021 22:58:58 +0000 (+0100) Subject: Fixing redirect url for certain cases X-Git-Tag: archiva-2.2.7~2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=90e36807406f0082dbf9049ade21c94f3ffedd34;p=archiva.git Fixing redirect url for certain cases --- 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 90e3cdad9..b6398c3eb 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 @@ -232,6 +232,8 @@ public abstract class AbstractRestService protected String getArtifactUrl( Artifact artifact, String repositoryId ) throws ArchivaRestServiceException { + log.debug( "Getting artifact url {}", artifact ); + log.debug( "Getting artifact context {}", artifact.getContext() ); try { @@ -244,13 +246,16 @@ public abstract class AbstractRestService sb.append( "/repository" ); + // when artifact come from a remote repository when have here the remote repo id // we must replace it with a valid managed one available for the user. if ( StringUtils.isEmpty( repositoryId ) ) { List userRepos = userRepositories.getObservableRepositoryIds( getPrincipal() ); + log.debug( "Available repositories: {}", StringUtils.join( userRepos, "," ) ); // is it a good one? if yes nothing to // if not search the repo who is proxy for this remote + boolean found = false; if ( !userRepos.contains( artifact.getContext() ) ) { for ( Map.Entry> entry : proxyConnectorAdmin.getProxyConnectorAsMap().entrySet() ) @@ -262,8 +267,16 @@ public abstract class AbstractRestService && userRepos.contains( entry.getKey() ) ) { sb.append( '/' ).append( entry.getKey() ); + found = true; + break; } } + if (found) { + break; + } + } + if (!found) { + sb.append( '/' ).append( artifact.getRepositoryId( ) ); } } 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 358fe9911..f221166ce 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 @@ -77,22 +77,22 @@ public class DefaultSearchService { if ( StringUtils.isBlank( queryString ) ) { - return Collections.emptyList(); + return Collections.emptyList( ); } SearchResultLimits limits = new SearchResultLimits( 0 ); try { SearchResults searchResults = - repositorySearch.search( getPrincipal(), getObservableRepos(), queryString, limits, - Collections.emptyList() ); + repositorySearch.search( getPrincipal( ), getObservableRepos( ), queryString, limits, + Collections.emptyList( ) ); return getArtifacts( searchResults ); } catch ( RepositorySearchException e ) { - log.error( e.getMessage(), e ); - throw new ArchivaRestServiceException( e.getMessage(), e ); + log.error( e.getMessage( ), e ); + throw new ArchivaRestServiceException( e.getMessage( ), e ); } } @@ -100,29 +100,29 @@ public class DefaultSearchService public List quickSearchWithRepositories( SearchRequest searchRequest ) throws ArchivaRestServiceException { - String queryString = searchRequest.getQueryTerms(); + String queryString = searchRequest.getQueryTerms( ); if ( StringUtils.isBlank( queryString ) ) { - return Collections.emptyList(); + return Collections.emptyList( ); } - List repositories = searchRequest.getRepositories(); - if ( repositories == null || repositories.isEmpty() ) + List repositories = searchRequest.getRepositories( ); + if ( repositories == null || repositories.isEmpty( ) ) { - repositories = getObservableRepos(); + repositories = getObservableRepos( ); } SearchResultLimits limits = - new SearchResultLimits( searchRequest.getPageSize(), searchRequest.getSelectedPage() ); + new SearchResultLimits( searchRequest.getPageSize( ), searchRequest.getSelectedPage( ) ); try { - SearchResults searchResults = repositorySearch.search( getPrincipal(), repositories, queryString, limits, - Collections.emptyList() ); + SearchResults searchResults = repositorySearch.search( getPrincipal( ), repositories, queryString, limits, + Collections.emptyList( ) ); return getArtifacts( searchResults ); } catch ( RepositorySearchException e ) { - log.error( e.getMessage(), e ); - throw new ArchivaRestServiceException( e.getMessage(), e ); + log.error( e.getMessage( ), e ); + throw new ArchivaRestServiceException( e.getMessage( ), e ); } } @@ -132,23 +132,23 @@ public class DefaultSearchService { if ( StringUtils.isBlank( groupId ) || StringUtils.isBlank( artifactId ) ) { - return Collections.emptyList(); + return Collections.emptyList( ); } - SearchFields searchField = new SearchFields(); + SearchFields searchField = new SearchFields( ); searchField.setGroupId( groupId ); searchField.setArtifactId( artifactId ); searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging ); - searchField.setRepositories( getObservableRepos() ); + searchField.setRepositories( getObservableRepos( ) ); try { - SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null ); + SearchResults searchResults = repositorySearch.search( getPrincipal( ), searchField, null ); return getArtifacts( searchResults ); } catch ( RepositorySearchException e ) { - log.error( e.getMessage(), e ); - throw new ArchivaRestServiceException( e.getMessage(), e ); + log.error( e.getMessage( ), e ); + throw new ArchivaRestServiceException( e.getMessage( ), e ); } } @@ -158,27 +158,27 @@ public class DefaultSearchService { if ( searchRequest == null ) { - return Collections.emptyList(); + return Collections.emptyList( ); } - SearchFields searchField = getModelMapper().map( searchRequest, SearchFields.class ); + SearchFields searchField = getModelMapper( ).map( searchRequest, SearchFields.class ); SearchResultLimits limits = new SearchResultLimits( 0 ); - limits.setPageSize( searchRequest.getPageSize() ); + limits.setPageSize( searchRequest.getPageSize( ) ); // if no repos set we use ones available for the user - if ( searchField.getRepositories() == null || searchField.getRepositories().isEmpty() ) + if ( searchField.getRepositories( ) == null || searchField.getRepositories( ).isEmpty( ) ) { - searchField.setRepositories( getObservableRepos() ); + searchField.setRepositories( getObservableRepos( ) ); } try { - SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits ); + SearchResults searchResults = repositorySearch.search( getPrincipal( ), searchField, limits ); return getArtifacts( searchResults ); } catch ( RepositorySearchException e ) { - log.error( e.getMessage(), e ); - throw new ArchivaRestServiceException( e.getMessage(), e ); + log.error( e.getMessage( ), e ); + throw new ArchivaRestServiceException( e.getMessage( ), e ); } } @@ -186,20 +186,20 @@ public class DefaultSearchService public GroupIdList getAllGroupIds( List selectedRepos ) throws ArchivaRestServiceException { - List observableRepos = getObservableRepos(); + List observableRepos = getObservableRepos( ); List repos = ListUtils.intersection( observableRepos, selectedRepos ); - if ( repos == null || repos.isEmpty() ) + if ( repos == null || repos.isEmpty( ) ) { - return new GroupIdList( Collections.emptyList() ); + return new GroupIdList( Collections.emptyList( ) ); } try { - return new GroupIdList( new ArrayList<>( repositorySearch.getAllGroupIds( getPrincipal(), repos ) ) ); + return new GroupIdList( new ArrayList<>( repositorySearch.getAllGroupIds( getPrincipal( ), repos ) ) ); } catch ( RepositorySearchException e ) { - log.error( e.getMessage(), e ); - throw new ArchivaRestServiceException( e.getMessage(), e ); + log.error( e.getMessage( ), e ); + throw new ArchivaRestServiceException( e.getMessage( ), e ); } } @@ -210,23 +210,23 @@ public class DefaultSearchService { // if no repos set we use ones available for the user - if ( checksumSearch.getRepositories() == null || checksumSearch.getRepositories().isEmpty() ) + if ( checksumSearch.getRepositories( ) == null || checksumSearch.getRepositories( ).isEmpty( ) ) { - checksumSearch.setRepositories( getObservableRepos() ); + checksumSearch.setRepositories( getObservableRepos( ) ); } - RepositorySession repositorySession = repositorySessionFactory.createSession(); + RepositorySession repositorySession = repositorySessionFactory.createSession( ); - MetadataRepository metadataRepository = repositorySession.getRepository(); + MetadataRepository metadataRepository = repositorySession.getRepository( ); - Set artifactSet = new HashSet<>(); + Set artifactSet = new HashSet<>( ); try { - for ( String repoId : checksumSearch.getRepositories() ) + for ( String repoId : checksumSearch.getRepositories( ) ) { Collection artifactMetadatas = - metadataRepository.getArtifactsByChecksum( repoId, checksumSearch.getChecksum() ); + metadataRepository.getArtifactsByChecksum( repoId, checksumSearch.getChecksum( ) ); artifactSet.addAll( buildArtifacts( artifactMetadatas, repoId ) ); } @@ -235,22 +235,22 @@ public class DefaultSearchService } catch ( MetadataRepositoryException e ) { - log.error( e.getMessage(), e ); - throw new ArchivaRestServiceException( e.getMessage(), e ); + log.error( e.getMessage( ), e ); + throw new ArchivaRestServiceException( e.getMessage( ), e ); } finally { - repositorySession.closeQuietly(); + repositorySession.closeQuietly( ); } } @Override - public StringList getObservablesRepoIds() + public StringList getObservablesRepoIds( ) throws ArchivaRestServiceException { - return new StringList( getObservableRepos() ); + return new StringList( getObservableRepos( ) ); } @Override @@ -264,124 +264,125 @@ public class DefaultSearchService if ( StringUtils.isEmpty( groupId ) ) { - return Response.status( new Response.StatusType() + return Response.status( new Response.StatusType( ) { @Override - public int getStatusCode() + public int getStatusCode( ) { - return Response.Status.BAD_REQUEST.getStatusCode(); + return Response.Status.BAD_REQUEST.getStatusCode( ); } @Override - public Response.Status.Family getFamily() + public Response.Status.Family getFamily( ) { - return Response.Status.BAD_REQUEST.getFamily(); + return Response.Status.BAD_REQUEST.getFamily( ); } @Override - public String getReasonPhrase() + public String getReasonPhrase( ) { return "groupId mandatory"; } - } ).build(); + } ).build( ); } if ( StringUtils.isEmpty( version ) ) { - return Response.status( new Response.StatusType() + return Response.status( new Response.StatusType( ) { @Override - public int getStatusCode() + public int getStatusCode( ) { - return Response.Status.BAD_REQUEST.getStatusCode(); + return Response.Status.BAD_REQUEST.getStatusCode( ); } @Override - public Response.Status.Family getFamily() + public Response.Status.Family getFamily( ) { - return Response.Status.BAD_REQUEST.getFamily(); + return Response.Status.BAD_REQUEST.getFamily( ); } @Override - public String getReasonPhrase() + public String getReasonPhrase( ) { return "version mandatory"; } - } ).build(); + } ).build( ); } if ( StringUtils.isEmpty( artifactId ) ) { - return Response.status( new Response.StatusType() + return Response.status( new Response.StatusType( ) { @Override - public int getStatusCode() + public int getStatusCode( ) { - return Response.Status.BAD_REQUEST.getStatusCode(); + return Response.Status.BAD_REQUEST.getStatusCode( ); } @Override - public Response.Status.Family getFamily() + public Response.Status.Family getFamily( ) { - return Response.Status.BAD_REQUEST.getFamily(); + return Response.Status.BAD_REQUEST.getFamily( ); } @Override - public String getReasonPhrase() + public String getReasonPhrase( ) { return "artifactId mandatory"; } - } ).build(); + } ).build( ); } - SearchFields searchField = new SearchFields(); + SearchFields searchField = new SearchFields( ); searchField.setGroupId( groupId ); searchField.setArtifactId( artifactId ); searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging ); - if ( literalVersion.booleanValue() || !StringUtils.equals( version, LATEST_KEYWORD ) ) + if ( literalVersion.booleanValue( ) || !StringUtils.equals( version, LATEST_KEYWORD ) ) { searchField.setVersion( version ); } searchField.setClassifier( classifier ); - List userRepos = getObservablesRepoIds().getStrings(); + List userRepos = getObservablesRepoIds( ).getStrings( ); searchField.setRepositories( StringUtils.isEmpty( repositoryId ) ? userRepos : Arrays.asList( repositoryId ) ); + log.debug( "Searching repository {}", repositoryId ); searchField.setExactSearch( true ); - SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null ); - List artifacts = getArtifacts( searchResults ); + SearchResults searchResults = repositorySearch.search( getPrincipal( ), searchField, null ); + List artifacts = getArtifacts( searchResults, repositoryId ); - if ( artifacts.isEmpty() ) + if ( artifacts.isEmpty( ) ) { - return Response.status( new Response.StatusType() + return Response.status( new Response.StatusType( ) { @Override - public int getStatusCode() + public int getStatusCode( ) { - return Response.Status.NO_CONTENT.getStatusCode(); + return Response.Status.NO_CONTENT.getStatusCode( ); } @Override - public Response.Status.Family getFamily() + public Response.Status.Family getFamily( ) { - return Response.Status.NO_CONTENT.getFamily(); + return Response.Status.NO_CONTENT.getFamily( ); } @Override - public String getReasonPhrase() + public String getReasonPhrase( ) { return "your query doesn't return any artifact"; } - } ).build(); + } ).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 ) { - List filteredArtifacts = new ArrayList<>( artifacts.size() ); + List filteredArtifacts = new ArrayList<>( artifacts.size( ) ); for ( Artifact artifact : artifacts ) { - if ( artifact.getClassifier() == null ) + if ( artifact.getClassifier( ) == null ) { filteredArtifacts.add( artifact ); } @@ -391,60 +392,65 @@ public class DefaultSearchService } // TODO return json result of the query ? - if ( artifacts.size() > 1 && !StringUtils.equals( version, LATEST_KEYWORD ) ) + if ( artifacts.size( ) > 1 && ( literalVersion || !StringUtils.equals( version, LATEST_KEYWORD ) ) ) { - return Response.status( new Response.StatusType() + return Response.status( new Response.StatusType( ) { @Override - public int getStatusCode() + public int getStatusCode( ) { - return Response.Status.BAD_REQUEST.getStatusCode(); + return Response.Status.BAD_REQUEST.getStatusCode( ); } @Override - public Response.Status.Family getFamily() + public Response.Status.Family getFamily( ) { - return Response.Status.BAD_REQUEST.getFamily(); + return Response.Status.BAD_REQUEST.getFamily( ); } @Override - public String getReasonPhrase() + public String getReasonPhrase( ) { return "your query return more than one artifact"; } - } ).build(); + } ).build( ); } // version is LATEST so we have to find the latest one from the result - if ( artifacts.size() > 1 && StringUtils.equals( version, LATEST_KEYWORD ) ) + if ( !literalVersion && ( artifacts.size( ) > 1 && StringUtils.equals( version, LATEST_KEYWORD ) ) ) { - TreeMap artifactPerVersion = new TreeMap<>( VersionComparator.getInstance() ); + TreeMap artifactPerVersion = new TreeMap<>( VersionComparator.getInstance( ) ); for ( Artifact artifact : artifacts ) { - artifactPerVersion.put( artifact.getVersion(), artifact ); + artifactPerVersion.put( artifact.getVersion( ), artifact ); } return Response.temporaryRedirect( - new URI( artifactPerVersion.lastEntry().getValue().getUrl() ) ).build(); + new URI( artifactPerVersion.lastEntry( ).getValue( ).getUrl( ) ) ).build( ); } Artifact artifact = artifacts.get( 0 ); - - return Response.temporaryRedirect( new URI( artifact.getUrl() ) ).build(); + log.debug( "Returning artifact {}, {}", artifact.getUrl( ), artifact.getRepositoryId( ) ); + return Response.temporaryRedirect( new URI( artifact.getUrl( ) ) ).build( ); } catch ( Exception e ) { - throw new ArchivaRestServiceException( e.getMessage(), e ); + throw new ArchivaRestServiceException( e.getMessage( ), e ); } } + protected List getArtifacts( SearchResults searchResults ) + throws ArchivaRestServiceException + { + return getArtifacts( searchResults, null ); + } //------------------------------------- // internal //------------------------------------- - protected List getArtifacts( SearchResults searchResults ) + protected List getArtifacts( SearchResults searchResults, String repositoryId ) throws ArchivaRestServiceException { @@ -466,7 +472,7 @@ public class DefaultSearchService if ( StringUtils.isNotBlank( version ) ) { versionned.setVersion( version ); - versionned.setUrl( getArtifactUrl( versionned ) ); + versionned.setUrl( getArtifactUrl( versionned, repositoryId ) ); artifacts.add( versionned );