From e442d487418f404a2da59c38b17345d4af2c4879 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Wed, 28 Sep 2011 21:37:57 +0000 Subject: [PATCH] fix url calculation for artifacts found in remote repositoires git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1177074 13f79535-47bb-0310-9956-ffa450edef68 --- .../indexer/search/NexusRepositorySearch.java | 89 +++++++++++++++++-- 1 file changed, 81 insertions(+), 8 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java index a2fef9420..09efd05d9 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java @@ -118,7 +118,7 @@ public class NexusRepositorySearch // we retun only artifacts without classifier in quick search, olamy cannot find a way to say with this field empty // FIXME cannot find a way currently to setup this in constructQuery !!! - return search( limits, q, indexingContextIds, NoClassifierArtifactInfoFiler.LIST ); + return search( limits, q, indexingContextIds, NoClassifierArtifactInfoFiler.LIST, principal, selectedRepos ); } @@ -221,11 +221,13 @@ public class NexusRepositorySearch throw new RepositorySearchException( "No search fields set." ); } - return search( limits, q, indexingContextIds, Collections.emptyList() ); + return search( limits, q, indexingContextIds, Collections.emptyList(), principal, + searchFields.getRepositories() ); } private SearchResults search( SearchResultLimits limits, BooleanQuery q, List indexingContextIds, - List filters ) + List filters, String principal, + List selectedRepos ) throws RepositorySearchException { @@ -242,11 +244,15 @@ public class NexusRepositorySearch return results; } - return convertToSearchResults( response, limits, filters ); + return convertToSearchResults( response, limits, filters, principal, selectedRepos ); } catch ( IOException e ) { - throw new RepositorySearchException( e ); + throw new RepositorySearchException( e.getMessage(), e ); + } + catch ( RepositoryAdminException e ) + { + throw new RepositorySearchException( e.getMessage(), e ); } } @@ -400,7 +406,9 @@ public class NexusRepositorySearch private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits, - List artifactInfoFilers ) + List artifactInfoFilers, + String principal, List selectedRepos ) + throws RepositoryAdminException { SearchResults results = new SearchResults(); Set artifactInfos = response.getResults(); @@ -446,7 +454,7 @@ public class NexusRepositorySearch hit.setPrefix( artifactInfo.prefix ); hit.setPackaging( artifactInfo.packaging ); hit.setClassifier( artifactInfo.classifier ); - hit.setUrl( getBaseUrl( artifactInfo ) ); + hit.setUrl( getBaseUrl( artifactInfo, selectedRepos ) ); } results.addHit( id, hit ); @@ -472,9 +480,25 @@ public class NexusRepositorySearch * @param artifactInfo * @return */ - protected String getBaseUrl( ArtifactInfo artifactInfo ) + protected String getBaseUrl( ArtifactInfo artifactInfo, List selectedRepos ) + throws RepositoryAdminException { StringBuilder sb = new StringBuilder(); + if ( StringUtils.startsWith( artifactInfo.context, "remote-" ) ) + { + // it's a remote index result we search a managed which proxying this remote and on which + // current user has read karma + String managedRepoId = + getManagedRepoId( StringUtils.substringAfter( artifactInfo.context, "remote-" ), selectedRepos ); + if ( managedRepoId != null ) + { + sb.append( '/' ).append( managedRepoId ); + } + } + else + { + sb.append( '/' ).append( artifactInfo.context ); + } sb.append( '/' ).append( StringUtils.replaceChars( artifactInfo.groupId, '.', '/' ) ); sb.append( '/' ).append( artifactInfo.artifactId ); @@ -498,6 +522,55 @@ public class NexusRepositorySearch return sb.toString(); } + /** + * return a managed repo for a remote result + * + * @param remoteRepo + * @param selectedRepos + * @return + * @throws RepositoryAdminException + */ + private String getManagedRepoId( String remoteRepo, List selectedRepos ) + throws RepositoryAdminException + { + Map> proxyConnectorMap = proxyConnectorAdmin.getProxyConnectorAsMap(); + if ( proxyConnectorMap == null || proxyConnectorMap.isEmpty() ) + { + return null; + } + if ( selectedRepos != null && !selectedRepos.isEmpty() ) + { + for ( Map.Entry> entry : proxyConnectorMap.entrySet() ) + { + if ( selectedRepos.contains( entry.getKey() ) ) + { + for ( ProxyConnector proxyConnector : entry.getValue() ) + { + if ( StringUtils.equals( remoteRepo, proxyConnector.getTargetRepoId() ) ) + { + return proxyConnector.getSourceRepoId(); + } + } + } + } + } + + // we don't find in search selected repos so return the first one + for ( Map.Entry> entry : proxyConnectorMap.entrySet() ) + { + + for ( ProxyConnector proxyConnector : entry.getValue() ) + { + if ( StringUtils.equals( remoteRepo, proxyConnector.getTargetRepoId() ) ) + { + return proxyConnector.getSourceRepoId(); + } + } + + } + return null; + } + private boolean applyArtifactInfoFilters( ArtifactInfo artifactInfo, List artifactInfoFilers, Map currentResult ) -- 2.39.5