diff --git a/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultRepositoryProxyConnectors.java b/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultRepositoryProxyConnectors.java index 0fdf4814f..8a09013c1 100644 --- a/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultRepositoryProxyConnectors.java +++ b/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultRepositoryProxyConnectors.java @@ -74,9 +74,10 @@ import org.slf4j.LoggerFactory; /** * DefaultRepositoryProxyConnectors - * + * * @version $Id$ - * @todo exception handling needs work - "not modified" is not really an exceptional case, and it has more layers than your average brown onion + * @todo exception handling needs work - "not modified" is not really an exceptional case, and it has more layers than + * your average brown onion * @plexus.component role-hint="default" */ public class DefaultRepositoryProxyConnectors @@ -132,228 +133,204 @@ public class DefaultRepositoryProxyConnectors * @plexus.requirement */ private WagonFactory wagonFactory; - + public File fetchFromProxies( ManagedRepositoryContent repository, ArtifactReference artifact ) throws ProxyDownloadException { - File workingDirectory = createWorkingDirectory(repository); - try + File localFile = toLocalFile( repository, artifact ); + + Properties requestProperties = new Properties(); + requestProperties.setProperty( "filetype", "artifact" ); + requestProperties.setProperty( "version", artifact.getVersion() ); + requestProperties.setProperty( "managedRepositoryId", repository.getId() ); + + List connectors = getProxyConnectors( repository ); + Map previousExceptions = new LinkedHashMap(); + for ( ProxyConnector connector : connectors ) { - File localFile = toLocalFile( repository, artifact ); - - Properties requestProperties = new Properties(); - requestProperties.setProperty( "filetype", "artifact" ); - requestProperties.setProperty( "version", artifact.getVersion() ); - requestProperties.setProperty( "managedRepositoryId", repository.getId() ); - - List connectors = getProxyConnectors( repository ); - Map previousExceptions = new LinkedHashMap(); - for ( ProxyConnector connector : connectors ) + if ( connector.isDisabled() ) { - if (connector.isDisabled()) - { - continue; - } - - RemoteRepositoryContent targetRepository = connector.getTargetRepository(); - requestProperties.setProperty( "remoteRepositoryId", targetRepository.getId() ); - - String targetPath = targetRepository.toPath( artifact ); - - try - { - File downloadedFile = - transferFile( connector, targetRepository, targetPath, repository, workingDirectory, localFile, requestProperties, - true ); - - if ( fileExists( downloadedFile ) ) - { - log.debug( "Successfully transferred: " + downloadedFile.getAbsolutePath() ); - return downloadedFile; - } - } - catch ( NotFoundException e ) - { - log.debug( "Artifact " + Keys.toKey( artifact ) + " not found on repository \"" - + targetRepository.getRepository().getId() + "\"." ); - } - catch ( NotModifiedException e ) - { - log.debug( "Artifact " + Keys.toKey( artifact ) + " not updated on repository \"" - + targetRepository.getRepository().getId() + "\"." ); - } - catch ( ProxyException e ) - { - validatePolicies( this.downloadErrorPolicies, connector.getPolicies(), requestProperties, artifact, - targetRepository, localFile, e, previousExceptions ); - } + continue; } - if ( !previousExceptions.isEmpty() ) - { - throw new ProxyDownloadException( "Failures occurred downloading from some remote repositories", - previousExceptions ); - } + RemoteRepositoryContent targetRepository = connector.getTargetRepository(); + requestProperties.setProperty( "remoteRepositoryId", targetRepository.getId() ); - log.debug( "Exhausted all target repositories, artifact " + Keys.toKey( artifact ) + " not found." ); + String targetPath = targetRepository.toPath( artifact ); + + try + { + File downloadedFile = + transferFile( connector, targetRepository, targetPath, repository, localFile, requestProperties, + true ); + + if ( fileExists( downloadedFile ) ) + { + log.debug( "Successfully transferred: " + downloadedFile.getAbsolutePath() ); + return downloadedFile; + } + } + catch ( NotFoundException e ) + { + log.debug( "Artifact " + Keys.toKey( artifact ) + " not found on repository \"" + + targetRepository.getRepository().getId() + "\"." ); + } + catch ( NotModifiedException e ) + { + log.debug( "Artifact " + Keys.toKey( artifact ) + " not updated on repository \"" + + targetRepository.getRepository().getId() + "\"." ); + } + catch ( ProxyException e ) + { + validatePolicies( this.downloadErrorPolicies, connector.getPolicies(), requestProperties, artifact, + targetRepository, localFile, e, previousExceptions ); + } } - finally + + if ( !previousExceptions.isEmpty() ) { - FileUtils.deleteQuietly(workingDirectory); + throw new ProxyDownloadException( "Failures occurred downloading from some remote repositories", + previousExceptions ); } + log.debug( "Exhausted all target repositories, artifact " + Keys.toKey( artifact ) + " not found." ); + return null; } public File fetchFromProxies( ManagedRepositoryContent repository, String path ) { - File workingDir = createWorkingDirectory(repository); - try - { - File localFile = new File( repository.getRepoRoot(), path ); + File localFile = new File( repository.getRepoRoot(), path ); - // no update policies for these paths - if ( localFile.exists() ) + // no update policies for these paths + if ( localFile.exists() ) + { + return null; + } + + Properties requestProperties = new Properties(); + requestProperties.setProperty( "filetype", "resource" ); + requestProperties.setProperty( "managedRepositoryId", repository.getId() ); + + List connectors = getProxyConnectors( repository ); + for ( ProxyConnector connector : connectors ) + { + if ( connector.isDisabled() ) { - return null; + continue; } - Properties requestProperties = new Properties(); - requestProperties.setProperty( "filetype", "resource" ); - requestProperties.setProperty( "managedRepositoryId", repository.getId() ); + RemoteRepositoryContent targetRepository = connector.getTargetRepository(); + requestProperties.setProperty( "remoteRepositoryId", targetRepository.getId() ); - List connectors = getProxyConnectors( repository ); - for ( ProxyConnector connector : connectors ) + String targetPath = path; + + try { - if (connector.isDisabled()) - { - continue; - } - - RemoteRepositoryContent targetRepository = connector.getTargetRepository(); - requestProperties.setProperty( "remoteRepositoryId", targetRepository.getId() ); + File downloadedFile = + transferFile( connector, targetRepository, targetPath, repository, localFile, requestProperties, + false ); - String targetPath = path; - - try + if ( fileExists( downloadedFile ) ) { - File downloadedFile = - transferFile( connector, targetRepository, targetPath, repository, workingDir, localFile, requestProperties, false ); - - if ( fileExists( downloadedFile ) ) - { - log.debug( "Successfully transferred: " + downloadedFile.getAbsolutePath() ); - return downloadedFile; - } - } - catch ( NotFoundException e ) - { - log.debug( "Resource " + path + " not found on repository \"" - + targetRepository.getRepository().getId() + "\"." ); - } - catch ( NotModifiedException e ) - { - log.debug( "Resource " + path + " not updated on repository \"" - + targetRepository.getRepository().getId() + "\"." ); - } - catch ( ProxyException e ) - { - log.warn( "Transfer error from repository \"" + targetRepository.getRepository().getId() - + "\" for resource " + path + ", continuing to next repository. Error message: " + e.getMessage() ); - log.debug( "Full stack trace", e ); + log.debug( "Successfully transferred: " + downloadedFile.getAbsolutePath() ); + return downloadedFile; } } + catch ( NotFoundException e ) + { + log.debug( "Resource " + path + " not found on repository \"" + + targetRepository.getRepository().getId() + "\"." ); + } + catch ( NotModifiedException e ) + { + log.debug( "Resource " + path + " not updated on repository \"" + + targetRepository.getRepository().getId() + "\"." ); + } + catch ( ProxyException e ) + { + log.warn( "Transfer error from repository \"" + targetRepository.getRepository().getId() + + "\" for resource " + path + ", continuing to next repository. Error message: " + e.getMessage() ); + log.debug( "Full stack trace", e ); + } + } - log.debug( "Exhausted all target repositories, resource " + path + " not found." ); - } - finally - { - FileUtils.deleteQuietly(workingDir); - } + log.debug( "Exhausted all target repositories, resource " + path + " not found." ); return null; } - - public File fetchMetatadaFromProxies(ManagedRepositoryContent repository, String logicalPath) + + public File fetchMetatadaFromProxies( ManagedRepositoryContent repository, String logicalPath ) { - File workingDir = createWorkingDirectory(repository); - try + File localFile = new File( repository.getRepoRoot(), logicalPath ); + + Properties requestProperties = new Properties(); + requestProperties.setProperty( "filetype", "metadata" ); + boolean metadataNeedsUpdating = false; + long originalTimestamp = getLastModified( localFile ); + + List connectors = getProxyConnectors( repository ); + for ( ProxyConnector connector : connectors ) { - File localFile = new File(repository.getRepoRoot(), logicalPath); - - Properties requestProperties = new Properties(); - requestProperties.setProperty( "filetype", "metadata" ); - boolean metadataNeedsUpdating = false; - long originalTimestamp = getLastModified( localFile ); - - List connectors = getProxyConnectors( repository ); - for ( ProxyConnector connector : connectors ) + if ( connector.isDisabled() ) { - if (connector.isDisabled()) - { - continue; - } - - RemoteRepositoryContent targetRepository = connector.getTargetRepository(); - - File localRepoFile = toLocalRepoFile( repository, targetRepository, logicalPath ); - long originalMetadataTimestamp = getLastModified( localRepoFile ); - - try - { - transferFile( connector, targetRepository, logicalPath, repository, workingDir, localRepoFile, requestProperties, true ); - - if ( hasBeenUpdated( localRepoFile, originalMetadataTimestamp ) ) - { - metadataNeedsUpdating = true; - } - } - catch ( NotFoundException e ) - { - log.debug( "Metadata " + logicalPath - + " not found on remote repository \"" - + targetRepository.getRepository().getId() + "\".", e ); - } - catch ( NotModifiedException e ) - { - log.debug( "Metadata " + logicalPath - + " not updated on remote repository \"" - + targetRepository.getRepository().getId() + "\".", e ); - } - catch ( ProxyException e ) - { - log.warn( "Transfer error from repository \"" + targetRepository.getRepository().getId() + - "\" for versioned Metadata " + logicalPath + - ", continuing to next repository. Error message: " + e.getMessage() ); - log.debug( "Full stack trace", e ); - } + continue; } - if ( hasBeenUpdated( localFile, originalTimestamp ) ) - { - metadataNeedsUpdating = true; - } + RemoteRepositoryContent targetRepository = connector.getTargetRepository(); - if ( metadataNeedsUpdating || !localFile.exists()) + File localRepoFile = toLocalRepoFile( repository, targetRepository, logicalPath ); + long originalMetadataTimestamp = getLastModified( localRepoFile ); + + try { - try + transferFile( connector, targetRepository, logicalPath, repository, localRepoFile, requestProperties, + true ); + + if ( hasBeenUpdated( localRepoFile, originalMetadataTimestamp ) ) { - metadataTools.updateMetadata( repository, logicalPath ); - } - catch ( RepositoryMetadataException e ) - { - log.warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e ); + metadataNeedsUpdating = true; } } - - if ( fileExists( localFile ) ) + catch ( NotFoundException e ) { - return localFile; + log.debug( "Metadata " + logicalPath + " not found on remote repository \"" + + targetRepository.getRepository().getId() + "\".", e ); + } + catch ( NotModifiedException e ) + { + log.debug( "Metadata " + logicalPath + " not updated on remote repository \"" + + targetRepository.getRepository().getId() + "\".", e ); + } + catch ( ProxyException e ) + { + log.warn( "Transfer error from repository \"" + targetRepository.getRepository().getId() + + "\" for versioned Metadata " + logicalPath + ", continuing to next repository. Error message: " + + e.getMessage() ); + log.debug( "Full stack trace", e ); } } - finally + + if ( hasBeenUpdated( localFile, originalTimestamp ) ) { - FileUtils.deleteQuietly(workingDir); + metadataNeedsUpdating = true; + } + + if ( metadataNeedsUpdating || !localFile.exists() ) + { + try + { + metadataTools.updateMetadata( repository, logicalPath ); + } + catch ( RepositoryMetadataException e ) + { + log.warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e ); + } + } + + if ( fileExists( localFile ) ) + { + return localFile; } return null; @@ -379,7 +356,7 @@ public class DefaultRepositoryProxyConnectors long currentLastModified = getLastModified( file ); return ( currentLastModified > originalLastModified ); } - + private File toLocalRepoFile( ManagedRepositoryContent repository, RemoteRepositoryContent targetRepository, String targetPath ) { @@ -405,7 +382,7 @@ public class DefaultRepositoryProxyConnectors /** * Simple method to test if the file exists on the local disk. - * + * * @param file the file to test. (may be null) * @return true if file exists. false if the file param is null, doesn't exist, or is not of type File. */ @@ -431,25 +408,25 @@ public class DefaultRepositoryProxyConnectors /** * Perform the transfer of the file. - * - * @param connector the connector configuration to use. - * @param remoteRepository the remote repository get the resource from. - * @param remotePath the path in the remote repository to the resource to get. - * @param repository the managed repository that will hold the file - * @param resource the local file to place the downloaded resource into + * + * @param connector the connector configuration to use. + * @param remoteRepository the remote repository get the resource from. + * @param remotePath the path in the remote repository to the resource to get. + * @param repository the managed repository that will hold the file + * @param resource the local file to place the downloaded resource into * @param requestProperties the request properties to utilize for policy handling. - * @param executeConsumers whether to execute the consumers after proxying + * @param executeConsumers whether to execute the consumers after proxying * @return the local file that was downloaded, or null if not downloaded. - * @throws NotFoundException if the file was not found on the remote repository. - * @throws NotModifiedException if the localFile was present, and the resource was present on remote repository, - * but the remote resource is not newer than the local File. - * @throws ProxyException if transfer was unsuccessful. + * @throws NotFoundException if the file was not found on the remote repository. + * @throws NotModifiedException if the localFile was present, and the resource was present on remote repository, but + * the remote resource is not newer than the local File. + * @throws ProxyException if transfer was unsuccessful. */ private File transferFile( ProxyConnector connector, RemoteRepositoryContent remoteRepository, String remotePath, - ManagedRepositoryContent repository, File workingDirectory, File resource, Properties requestProperties, + ManagedRepositoryContent repository, File resource, Properties requestProperties, boolean executeConsumers ) throws ProxyException, NotModifiedException - { + { String url = remoteRepository.getURL().getUrl(); if ( !url.endsWith( "/" ) ) { @@ -464,9 +441,9 @@ public class DefaultRepositoryProxyConnectors // Path must belong to whitelist. if ( !matchesPattern( remotePath, connector.getWhitelist() ) ) { - log.debug( "Path [" + remotePath + - "] is not part of defined whitelist (skipping transfer from repository [" + - remoteRepository.getRepository().getName() + "])." ); + log.debug( "Path [" + remotePath + + "] is not part of defined whitelist (skipping transfer from repository [" + + remoteRepository.getRepository().getName() + "])." ); return null; } } @@ -474,8 +451,8 @@ public class DefaultRepositoryProxyConnectors // Is target path part of blacklist? if ( matchesPattern( remotePath, connector.getBlacklist() ) ) { - log.debug( "Path [" + remotePath + "] is part of blacklist (skipping transfer from repository [" + - remoteRepository.getRepository().getName() + "])." ); + log.debug( "Path [" + remotePath + "] is part of blacklist (skipping transfer from repository [" + + remoteRepository.getRepository().getName() + "])." ); return null; } @@ -500,83 +477,97 @@ public class DefaultRepositoryProxyConnectors File tmpMd5 = null; File tmpSha1 = null; File tmpResource = null; - - Wagon wagon = null; + + File workingDirectory = createWorkingDirectory( repository ); try { - RepositoryURL repoUrl = remoteRepository.getURL(); - String protocol = repoUrl.getProtocol(); - wagon = (Wagon) wagonFactory.getWagon( "wagon#" + protocol ); - if ( wagon == null ) + Wagon wagon = null; + try { - throw new ProxyException( "Unsupported target repository protocol: " + protocol ); + RepositoryURL repoUrl = remoteRepository.getURL(); + String protocol = repoUrl.getProtocol(); + wagon = (Wagon) wagonFactory.getWagon( "wagon#" + protocol ); + if ( wagon == null ) + { + throw new ProxyException( "Unsupported target repository protocol: " + protocol ); + } + + boolean connected = connectToRepository( connector, wagon, remoteRepository ); + if ( connected ) + { + tmpResource = new File( workingDirectory, resource.getName() ); + transferSimpleFile( wagon, remoteRepository, remotePath, repository, resource, tmpResource ); + + // TODO: these should be used to validate the download based on the policies, not always downloaded + // to + // save on connections since md5 is rarely used + tmpSha1 = + transferChecksum( wagon, remoteRepository, remotePath, repository, resource, workingDirectory, + ".sha1" ); + tmpMd5 = + transferChecksum( wagon, remoteRepository, remotePath, repository, resource, workingDirectory, + ".md5" ); + } + } + catch ( NotFoundException e ) + { + urlFailureCache.cacheFailure( url ); + throw e; + } + catch ( NotModifiedException e ) + { + // Do not cache url here. + throw e; + } + catch ( ProxyException e ) + { + urlFailureCache.cacheFailure( url ); + throw e; + } + finally + { + if ( wagon != null ) + { + try + { + wagon.disconnect(); + } + catch ( ConnectionException e ) + { + log.warn( "Unable to disconnect wagon.", e ); + } + } } - boolean connected = connectToRepository( connector, wagon, remoteRepository ); - if ( connected ) + // Handle post-download policies. + try { - tmpResource = transferSimpleFile( wagon, remoteRepository, remotePath, repository, workingDirectory, resource ); - - // TODO: these should be used to validate the download based on the policies, not always downloaded to - // save on connections since md5 is rarely used - tmpSha1 = transferChecksum( wagon, remoteRepository, remotePath, repository, workingDirectory, resource, ".sha1" ); - tmpMd5 = transferChecksum( wagon, remoteRepository, remotePath, repository, workingDirectory, resource, ".md5" ); + validatePolicies( this.postDownloadPolicies, connector.getPolicies(), requestProperties, tmpResource ); + } + catch ( PolicyViolationException e ) + { + log.info( "Transfer invalidated from " + url + " : " + e.getMessage() ); + executeConsumers = false; + if ( !fileExists( tmpResource ) ) + { + resource = null; + } + } + + if ( resource != null ) + { + synchronized ( resource.getAbsolutePath().intern() ) + { + File directory = resource.getParentFile(); + moveFileIfExists( tmpMd5, directory ); + moveFileIfExists( tmpSha1, directory ); + moveFileIfExists( tmpResource, directory ); + } } - } - catch ( NotFoundException e ) - { - urlFailureCache.cacheFailure( url ); - throw e; - } - catch ( NotModifiedException e ) - { - // Do not cache url here. - throw e; - } - catch ( ProxyException e ) - { - urlFailureCache.cacheFailure( url ); - throw e; } finally { - if ( wagon != null ) - { - try - { - wagon.disconnect(); - } - catch ( ConnectionException e ) - { - log.warn( "Unable to disconnect wagon.", e ); - } - } - } - - // Handle post-download policies. - try - { - validatePolicies( this.postDownloadPolicies, connector.getPolicies(), requestProperties, tmpResource ); - } - catch ( PolicyViolationException e ) - { - log.info( "Transfer invalidated from " + url + " : " + e.getMessage() ); - executeConsumers = false; - if ( !fileExists( tmpResource ) ) - { - resource = null; - } - } - - if (resource != null) - { - synchronized (resource.getAbsolutePath().intern()) - { - File directory = resource.getParentFile(); - moveFileIfExists(tmpMd5, directory); - moveFileIfExists(tmpSha1, directory); - moveFileIfExists(tmpResource, directory); - } + FileUtils.deleteQuietly( workingDirectory ); } if ( executeConsumers ) @@ -584,24 +575,23 @@ public class DefaultRepositoryProxyConnectors // Just-in-time update of the index and database by executing the consumers for this artifact consumers.executeConsumers( connector.getSourceRepository().getRepository(), resource ); } - + return resource; } - - - + /** * Moves the file into repository location if it exists * * @param fileToMove this could be either the main artifact, sha1 or md5 checksum file. * @param directory directory to write files to */ - private void moveFileIfExists(File fileToMove, File directory) throws ProxyException + private void moveFileIfExists( File fileToMove, File directory ) + throws ProxyException { - if (fileToMove != null && fileToMove.exists()) + if ( fileToMove != null && fileToMove.exists() ) { - File newLocation = new File(directory, fileToMove.getName()); - moveTempToTarget(fileToMove, newLocation); + File newLocation = new File( directory, fileToMove.getName() ); + moveTempToTarget( fileToMove, newLocation ); } } @@ -609,37 +599,37 @@ public class DefaultRepositoryProxyConnectors *

* Quietly transfer the checksum file from the remote repository to the local file. *

- * - * @param wagon the wagon instance (should already be connected) to use. + * + * @param wagon the wagon instance (should already be connected) to use. * @param remoteRepository the remote repository to transfer from. - * @param remotePath the remote path to the resource to get. - * @param repository the managed repository that will hold the file - * @param localFile the local file that should contain the downloaded contents - * @param type the type of checksum to transfer (example: ".md5" or ".sha1") + * @param remotePath the remote path to the resource to get. + * @param repository the managed repository that will hold the file + * @param localFile the local file that should contain the downloaded contents + * @param type the type of checksum to transfer (example: ".md5" or ".sha1") * @throws ProxyException if copying the downloaded file into place did not succeed. */ private File transferChecksum( Wagon wagon, RemoteRepositoryContent remoteRepository, String remotePath, - ManagedRepositoryContent repository, File workingDirectory, File localFile, String type ) + ManagedRepositoryContent repository, File resource, File tmpDirectory, String ext ) throws ProxyException { - File hashFile = new File( localFile.getAbsolutePath() + type ); - File tmpChecksum = new File(workingDirectory, hashFile.getName()); - String url = remoteRepository.getURL().getUrl() + remotePath; + String url = remoteRepository.getURL().getUrl() + remotePath + ext; // Transfer checksum does not use the policy. - if ( urlFailureCache.hasFailedBefore( url + type ) ) + if ( urlFailureCache.hasFailedBefore( url ) ) { return null; } + File destFile = new File( tmpDirectory, resource.getName() + ext ); + try { - transferSimpleFile( wagon, remoteRepository, remotePath + type, repository, workingDirectory, hashFile ); - log.debug( "Checksum" + type + " Downloaded: " + hashFile ); + transferSimpleFile( wagon, remoteRepository, remotePath + ext, repository, resource, destFile ); + log.debug( "Checksum " + url + " Downloaded: " + destFile + " to move to " + resource ); } catch ( NotFoundException e ) { - urlFailureCache.cacheFailure( url + type ); + urlFailureCache.cacheFailure( url ); log.debug( "Transfer failed, checksum not found: " + url ); // Consume it, do not pass this on. } @@ -650,45 +640,41 @@ public class DefaultRepositoryProxyConnectors } catch ( ProxyException e ) { - urlFailureCache.cacheFailure( url + type ); + urlFailureCache.cacheFailure( url ); log.warn( "Transfer failed on checksum: " + url + " : " + e.getMessage(), e ); // Critical issue, pass it on. throw e; } - return tmpChecksum; + return destFile; } /** * Perform the transfer of the remote file to the local file specified. - * - * @param wagon the wagon instance to use. + * + * @param wagon the wagon instance to use. * @param remoteRepository the remote repository to use - * @param remotePath the remote path to attempt to get - * @param repository the managed repository that will hold the file - * @param localFile the local file to save to + * @param remotePath the remote path to attempt to get + * @param repository the managed repository that will hold the file + * @param origFile the local file to save to * @return The local file that was transfered. * @throws ProxyException if there was a problem moving the downloaded file into place. * @throws WagonException if there was a problem tranfering the file. */ - private File transferSimpleFile( Wagon wagon, RemoteRepositoryContent remoteRepository, String remotePath, - ManagedRepositoryContent repository, File workingDirectory, File localFile ) + private void transferSimpleFile( Wagon wagon, RemoteRepositoryContent remoteRepository, String remotePath, + ManagedRepositoryContent repository, File origFile, File destFile ) throws ProxyException { assert ( remotePath != null ); // Transfer the file. - File temp = null; - try { - temp = new File(workingDirectory, localFile.getName()); - boolean success = false; - if ( !localFile.exists() ) + if ( !origFile.exists() ) { log.debug( "Retrieving " + remotePath + " from " + remoteRepository.getRepository().getName() ); - wagon.get( remotePath, temp ); + wagon.get( remotePath, destFile ); success = true; // You wouldn't get here on failure, a WagonException would have been thrown. @@ -697,32 +683,29 @@ public class DefaultRepositoryProxyConnectors else { log.debug( "Retrieving " + remotePath + " from " + remoteRepository.getRepository().getName() - + " if updated" ); - success = wagon.getIfNewer( remotePath, temp, localFile.lastModified() ); + + " if updated" ); + success = wagon.getIfNewer( remotePath, destFile, origFile.lastModified() ); if ( !success ) { - throw new NotModifiedException( - "Not downloaded, as local file is newer than remote side: " + localFile.getAbsolutePath() ); + throw new NotModifiedException( "Not downloaded, as local file is newer than remote side: " + + origFile.getAbsolutePath() ); } - if ( temp.exists() ) + if ( destFile.exists() ) { log.debug( "Downloaded successfully." ); } } - - return temp; } catch ( ResourceDoesNotExistException e ) { - throw new NotFoundException( - "Resource [" + remoteRepository.getURL() + "/" + remotePath + "] does not exist: " + e.getMessage(), - e ); + throw new NotFoundException( "Resource [" + remoteRepository.getURL() + "/" + remotePath + + "] does not exist: " + e.getMessage(), e ); } catch ( WagonException e ) { // TODO: shouldn't have to drill into the cause, but TransferFailedException is often not descriptive enough - + String msg = "Download failure on resource [" + remoteRepository.getURL() + "/" + remotePath + "]:" + e.getMessage(); if ( e.getCause() != null ) @@ -735,10 +718,12 @@ public class DefaultRepositoryProxyConnectors /** * Apply the policies. - * - * @param policies the map of policies to execute. (Map of String policy keys, to {@link DownloadPolicy} objects) - * @param settings the map of settings for the policies to execute. (Map of String policy keys, to String policy setting) - * @param request the request properties (utilized by the {@link DownloadPolicy#applyPolicy(String,Properties,File)}) + * + * @param policies the map of policies to execute. (Map of String policy keys, to {@link DownloadPolicy} objects) + * @param settings the map of settings for the policies to execute. (Map of String policy keys, to String policy + * setting) + * @param request the request properties (utilized by the {@link DownloadPolicy#applyPolicy(String,Properties,File)} + * ) * @param localFile the local file (utilized by the {@link DownloadPolicy#applyPolicy(String,Properties,File)}) */ private void validatePolicies( Map policies, Map settings, @@ -799,8 +784,8 @@ public class DefaultRepositoryProxyConnectors if ( !previousExceptions.containsKey( content.getId() ) ) { throw new ProxyDownloadException( - "An error occurred in downloading from the remote repository, and the policy is to fail immediately", - content.getId(), exception ); + "An error occurred in downloading from the remote repository, and the policy is to fail immediately", + content.getId(), exception ); } } else @@ -809,37 +794,39 @@ public class DefaultRepositoryProxyConnectors previousExceptions.remove( content.getId() ); } - log.warn( "Transfer error from repository \"" + content.getRepository().getId() + "\" for artifact " + - Keys.toKey( artifact ) + ", continuing to next repository. Error message: " + exception.getMessage() ); + log.warn( "Transfer error from repository \"" + content.getRepository().getId() + "\" for artifact " + + Keys.toKey( artifact ) + ", continuing to next repository. Error message: " + exception.getMessage() ); log.debug( "Full stack trace", exception ); } - + /** * Creates a working directory in the repository root for this request + * * @param repository * @return file location of working directory + * @throws IOException */ - private File createWorkingDirectory(ManagedRepositoryContent repository) + private File createWorkingDirectory( ManagedRepositoryContent repository ) { - //TODO: This is ugly - lets actually clean this up when we get the new repository api + // TODO: This is ugly - lets actually clean this up when we get the new repository api try { - File tmpDir = File.createTempFile(".workingdirectory", null, new File(repository.getRepoRoot())); + File tmpDir = File.createTempFile( ".workingdirectory", null, new File( repository.getRepoRoot() ) ); tmpDir.delete(); tmpDir.mkdirs(); return tmpDir; } - catch (IOException e) + catch ( IOException e ) { - throw new RuntimeException("Could not create working directory for this request", e); + throw new RuntimeException( "Could not create working directory for this request", e ); } } /** - * Used to move the temporary file to its real destination. This is patterned from the way WagonManager handles - * its downloaded files. - * - * @param temp The completed download file + * Used to move the temporary file to its real destination. This is patterned from the way WagonManager handles its + * downloaded files. + * + * @param temp The completed download file * @param target The final location of the downloaded file * @throws ProxyException when the temp file cannot replace the target file */ @@ -862,32 +849,33 @@ public class DefaultRepositoryProxyConnectors } catch ( IOException e ) { - if (target.exists()) + if ( target.exists() ) { - log.debug("Tried to copy file " + temp.getName() + " to " + target.getAbsolutePath() + " but file with this name already exists."); + log.debug( "Tried to copy file " + temp.getName() + " to " + target.getAbsolutePath() + + " but file with this name already exists." ); } else { - throw new ProxyException( "Cannot copy tmp file " + temp.getAbsolutePath() + " to its final location", e ); + throw new ProxyException( "Cannot copy tmp file " + temp.getAbsolutePath() + + " to its final location", e ); } } finally { - FileUtils.deleteQuietly(temp); + FileUtils.deleteQuietly( temp ); } } } /** * Using wagon, connect to the remote repository. - * - * @param connector the connector configuration to utilize (for obtaining network proxy configuration from) - * @param wagon the wagon instance to establish the connection on. + * + * @param connector the connector configuration to utilize (for obtaining network proxy configuration from) + * @param wagon the wagon instance to establish the connection on. * @param remoteRepository the remote repository to connect to. * @return true if the connection was successful. false if not connected. */ - private boolean connectToRepository( ProxyConnector connector, Wagon wagon, - RemoteRepositoryContent remoteRepository ) + private boolean connectToRepository( ProxyConnector connector, Wagon wagon, RemoteRepositoryContent remoteRepository ) { boolean connected = false; @@ -896,9 +884,9 @@ public class DefaultRepositoryProxyConnectors { networkProxy = (ProxyInfo) this.networkProxyMap.get( connector.getProxyId() ); } - + if ( log.isDebugEnabled() ) - { + { if ( networkProxy != null ) { // TODO: move to proxyInfo.toString() @@ -923,35 +911,33 @@ public class DefaultRepositoryProxyConnectors if ( StringUtils.isNotBlank( username ) && StringUtils.isNotBlank( password ) ) { - log.debug( "Using username " + username + " to connect to remote repository " - + remoteRepository.getURL() ); + log.debug( "Using username " + username + " to connect to remote repository " + remoteRepository.getURL() ); authInfo = new AuthenticationInfo(); authInfo.setUserName( username ); authInfo.setPassword( password ); } - //Convert seconds to milliseconds + // Convert seconds to milliseconds int timeoutInMilliseconds = remoteRepository.getRepository().getTimeout() * 1000; - //Set timeout - wagon.setTimeout(timeoutInMilliseconds); + // Set timeout + wagon.setTimeout( timeoutInMilliseconds ); try { - Repository wagonRepository = new Repository( remoteRepository.getId(), remoteRepository.getURL().toString() ); + Repository wagonRepository = + new Repository( remoteRepository.getId(), remoteRepository.getURL().toString() ); wagon.connect( wagonRepository, authInfo, networkProxy ); connected = true; } catch ( ConnectionException e ) { - log.warn( - "Could not connect to " + remoteRepository.getRepository().getName() + ": " + e.getMessage() ); + log.warn( "Could not connect to " + remoteRepository.getRepository().getName() + ": " + e.getMessage() ); connected = false; } catch ( AuthenticationException e ) { - log.warn( - "Could not connect to " + remoteRepository.getRepository().getName() + ": " + e.getMessage() ); + log.warn( "Could not connect to " + remoteRepository.getRepository().getName() + ": " + e.getMessage() ); connected = false; } @@ -960,8 +946,8 @@ public class DefaultRepositoryProxyConnectors /** * Tests whitelist and blacklist patterns against path. - * - * @param path the path to test. + * + * @param path the path to test. * @param patterns the list of patterns to check. * @return true if the path matches at least 1 pattern in the provided patterns list. */ @@ -1003,10 +989,10 @@ public class DefaultRepositoryProxyConnectors public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) { - if ( ConfigurationNames.isNetworkProxy( propertyName ) || - ConfigurationNames.isManagedRepositories( propertyName ) || - ConfigurationNames.isRemoteRepositories( propertyName ) || - ConfigurationNames.isProxyConnector( propertyName ) ) + if ( ConfigurationNames.isNetworkProxy( propertyName ) + || ConfigurationNames.isManagedRepositories( propertyName ) + || ConfigurationNames.isRemoteRepositories( propertyName ) + || ConfigurationNames.isProxyConnector( propertyName ) ) { initConnectorsAndNetworkProxies(); } @@ -1017,7 +1003,7 @@ public class DefaultRepositoryProxyConnectors /* do nothing */ } - @SuppressWarnings("unchecked") + @SuppressWarnings( "unchecked" ) private void initConnectorsAndNetworkProxies() { synchronized ( this.proxyConnectorMap ) @@ -1025,8 +1011,8 @@ public class DefaultRepositoryProxyConnectors ProxyConnectorOrderComparator proxyOrderSorter = new ProxyConnectorOrderComparator(); this.proxyConnectorMap.clear(); - List proxyConfigs = archivaConfiguration.getConfiguration() - .getProxyConnectors(); + List proxyConfigs = + archivaConfiguration.getConfiguration().getProxyConnectors(); for ( ProxyConnectorConfiguration proxyConfig : proxyConfigs ) { String key = proxyConfig.getSourceRepoId(); @@ -1036,10 +1022,8 @@ public class DefaultRepositoryProxyConnectors // Create connector object. ProxyConnector connector = new ProxyConnector(); - connector.setSourceRepository( repositoryFactory.getManagedRepositoryContent( proxyConfig - .getSourceRepoId() ) ); - connector.setTargetRepository( repositoryFactory.getRemoteRepositoryContent( proxyConfig - .getTargetRepoId() ) ); + connector.setSourceRepository( repositoryFactory.getManagedRepositoryContent( proxyConfig.getSourceRepoId() ) ); + connector.setTargetRepository( repositoryFactory.getRemoteRepositoryContent( proxyConfig.getTargetRepoId() ) ); connector.setProxyId( proxyConfig.getProxyId() ); connector.setPolicies( proxyConfig.getPolicies() ); @@ -1095,7 +1079,8 @@ public class DefaultRepositoryProxyConnectors { this.networkProxyMap.clear(); - List networkProxies = archivaConfiguration.getConfiguration().getNetworkProxies(); + List networkProxies = + archivaConfiguration.getConfiguration().getNetworkProxies(); for ( NetworkProxyConfiguration networkProxyConfig : networkProxies ) { String key = networkProxyConfig.getId();