Search in sources :

Example 1 with NotModifiedException

use of org.apache.archiva.proxy.base.NotModifiedException in project archiva by apache.

the class MavenRepositoryProxyHandler method transferResources.

/**
 * @param connector
 * @param remoteRepository
 * @param tmpResource
 * @param checksumFiles
 * @param url
 * @param remotePath
 * @param resource
 * @param workingDirectory
 * @param repository
 * @throws ProxyException
 * @throws NotModifiedException
 */
@Override
protected void transferResources(ProxyConnector connector, RemoteRepository remoteRepository, StorageAsset tmpResource, StorageAsset[] checksumFiles, String url, String remotePath, StorageAsset resource, Path workingDirectory, ManagedRepository repository) throws ProxyException, NotModifiedException {
    Wagon wagon = null;
    try {
        URI repoUrl = remoteRepository.getLocation();
        String protocol = repoUrl.getScheme();
        NetworkProxy networkProxy = null;
        String proxyId = connector.getProxyId();
        if (StringUtils.isNotBlank(proxyId)) {
            networkProxy = getNetworkProxy(proxyId);
        }
        WagonFactoryRequest wagonFactoryRequest = new WagonFactoryRequest("wagon#" + protocol, remoteRepository.getExtraHeaders());
        if (networkProxy == null) {
            log.warn("No network proxy with id {} found for connector {}->{}", proxyId, connector.getSourceRepository().getId(), connector.getTargetRepository().getId());
        } else {
            wagonFactoryRequest = wagonFactoryRequest.networkProxy(networkProxy);
        }
        wagon = wagonFactory.getWagon(wagonFactoryRequest);
        if (wagon == null) {
            throw new ProxyException("Unsupported target repository protocol: " + protocol);
        }
        boolean connected = connectToRepository(connector, wagon, remoteRepository);
        if (connected) {
            transferArtifact(wagon, remoteRepository, remotePath, resource.getFilePath(), tmpResource);
            // save on connections since md5 is rarely used
            for (StorageAsset checksumFile : checksumFiles) {
                String ext = "." + StringUtils.substringAfterLast(checksumFile.getName(), ".");
                transferChecksum(wagon, remoteRepository, remotePath, resource.getFilePath(), ext, checksumFile.getFilePath());
            }
        }
    } catch (NotModifiedException e) {
        // Do not cache url here.
        throw e;
    } catch (ProxyException e) {
        urlFailureCache.cacheFailure(url);
        throw e;
    } catch (WagonFactoryException e) {
        throw new ProxyException(e.getMessage(), e);
    } finally {
        if (wagon != null) {
            try {
                wagon.disconnect();
            } catch (ConnectionException e) {
                log.warn("Unable to disconnect wagon.", e);
            }
        }
    }
}
Also used : WagonFactoryRequest(org.apache.archiva.maven.common.proxy.WagonFactoryRequest) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) WagonFactoryException(org.apache.archiva.maven.common.proxy.WagonFactoryException) Wagon(org.apache.maven.wagon.Wagon) ProxyException(org.apache.archiva.proxy.base.ProxyException) URI(java.net.URI) NetworkProxy(org.apache.archiva.proxy.model.NetworkProxy) ConnectionException(org.apache.maven.wagon.ConnectionException) NotModifiedException(org.apache.archiva.proxy.base.NotModifiedException)

Example 2 with NotModifiedException

use of org.apache.archiva.proxy.base.NotModifiedException in project archiva by apache.

the class MavenRepositoryProxyHandler method transferSimpleFile.

/**
 * Perform the transfer of the remote file to the local file specified.
 *
 * @param wagon            the wagon instance to use.
 * @param remoteRepository the remote repository to use
 * @param remotePath       the remote path to attempt to get
 * @param origFile         the local file to save to
 * @throws ProxyException if there was a problem moving the downloaded file into place.
 */
protected void transferSimpleFile(Wagon wagon, RemoteRepository remoteRepository, String remotePath, Path origFile, Path destFile) throws ProxyException {
    assert (remotePath != null);
    // Transfer the file.
    try {
        if (!Files.exists(origFile)) {
            log.debug("Retrieving {} from {}", remotePath, remoteRepository.getId());
            wagon.get(addParameters(remotePath, remoteRepository), destFile.toFile());
            // You wouldn't get here on failure, a WagonException would have been thrown.
            log.debug("Downloaded successfully.");
        } else {
            boolean success;
            log.debug("Retrieving {} from {} if updated", remotePath, remoteRepository.getId());
            try {
                success = wagon.getIfNewer(addParameters(remotePath, remoteRepository), destFile.toFile(), Files.getLastModifiedTime(origFile).toMillis());
            } catch (IOException e) {
                throw new ProxyException("Failed to the modification time of " + origFile.toAbsolutePath());
            }
            if (!success) {
                throw new NotModifiedException("Not downloaded, as local file is newer than remote side: " + origFile.toAbsolutePath());
            }
            if (Files.exists(destFile)) {
                log.debug("Downloaded successfully.");
            }
        }
    } catch (ResourceDoesNotExistException e) {
        throw new NotFoundException("Resource [" + remoteRepository.getLocation() + "/" + 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.getLocation() + "/" + remotePath + "]:" + e.getMessage();
        if (e.getCause() != null) {
            msg += " (cause: " + e.getCause() + ")";
        }
        throw new ProxyException(msg, e);
    }
}
Also used : NotFoundException(org.apache.archiva.proxy.base.NotFoundException) IOException(java.io.IOException) ProxyException(org.apache.archiva.proxy.base.ProxyException) WagonException(org.apache.maven.wagon.WagonException) ResourceDoesNotExistException(org.apache.maven.wagon.ResourceDoesNotExistException) NotModifiedException(org.apache.archiva.proxy.base.NotModifiedException)

Aggregations

NotModifiedException (org.apache.archiva.proxy.base.NotModifiedException)2 ProxyException (org.apache.archiva.proxy.base.ProxyException)2 IOException (java.io.IOException)1 URI (java.net.URI)1 WagonFactoryException (org.apache.archiva.maven.common.proxy.WagonFactoryException)1 WagonFactoryRequest (org.apache.archiva.maven.common.proxy.WagonFactoryRequest)1 NotFoundException (org.apache.archiva.proxy.base.NotFoundException)1 NetworkProxy (org.apache.archiva.proxy.model.NetworkProxy)1 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)1 ConnectionException (org.apache.maven.wagon.ConnectionException)1 ResourceDoesNotExistException (org.apache.maven.wagon.ResourceDoesNotExistException)1 Wagon (org.apache.maven.wagon.Wagon)1 WagonException (org.apache.maven.wagon.WagonException)1