Search in sources :

Example 1 with PolicyViolationException

use of org.apache.archiva.policies.PolicyViolationException in project archiva by apache.

the class DefaultRepositoryProxyConnectors method transferFile.

/**
 * 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 requestProperties the request properties to utilize for policy handling.
 * @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.
 */
private Path transferFile(ProxyConnector connector, RemoteRepositoryContent remoteRepository, String remotePath, ManagedRepositoryContent repository, Path resource, Properties requestProperties, boolean executeConsumers) throws ProxyException, NotModifiedException, RepositoryAdminException {
    String url = remoteRepository.getURL().getUrl();
    if (!url.endsWith("/")) {
        url = url + "/";
    }
    url = url + remotePath;
    requestProperties.setProperty("url", url);
    // Is a whitelist defined?
    if (CollectionUtils.isNotEmpty(connector.getWhitelist())) {
        // Path must belong to whitelist.
        if (!matchesPattern(remotePath, connector.getWhitelist())) {
            log.debug("Path [{}] is not part of defined whitelist (skipping transfer from repository [{}]).", remotePath, remoteRepository.getRepository().getName());
            return null;
        }
    }
    // Is target path part of blacklist?
    if (matchesPattern(remotePath, connector.getBlacklist())) {
        log.debug("Path [{}] is part of blacklist (skipping transfer from repository [{}]).", remotePath, remoteRepository.getRepository().getName());
        return null;
    }
    // Handle pre-download policy
    try {
        validatePolicies(this.preDownloadPolicies, connector.getPolicies(), requestProperties, resource);
    } catch (PolicyViolationException e) {
        String emsg = "Transfer not attempted on " + url + " : " + e.getMessage();
        if (fileExists(resource)) {
            log.debug("{} : using already present local file.", emsg);
            return resource;
        }
        log.debug(emsg);
        return null;
    }
    Path workingDirectory = createWorkingDirectory(repository);
    Path tmpResource = workingDirectory.resolve(resource.getFileName());
    Path tmpMd5 = workingDirectory.resolve(resource.getFileName().toString() + ".md5");
    Path tmpSha1 = workingDirectory.resolve(resource.getFileName().toString() + ".sha1");
    try {
        transferResources(connector, remoteRepository, tmpMd5, tmpSha1, tmpResource, url, remotePath, resource, workingDirectory, repository);
        // Handle post-download policies.
        try {
            validatePolicies(this.postDownloadPolicies, connector.getPolicies(), requestProperties, tmpResource);
        } catch (PolicyViolationException e) {
            log.warn("Transfer invalidated from {} : {}", url, e.getMessage());
            executeConsumers = false;
            if (!fileExists(tmpResource)) {
                resource = null;
            }
        }
        if (resource != null) {
            synchronized (resource.toAbsolutePath().toString().intern()) {
                Path directory = resource.getParent();
                moveFileIfExists(tmpMd5, directory);
                moveFileIfExists(tmpSha1, directory);
                moveFileIfExists(tmpResource, directory);
            }
        }
    } finally {
        org.apache.archiva.common.utils.FileUtils.deleteQuietly(workingDirectory);
    }
    if (executeConsumers) {
        // Just-in-time update of the index and database by executing the consumers for this artifact
        // consumers.executeConsumers( connector.getSourceRepository().getRepository(), resource );
        queueRepositoryTask(connector.getSourceRepository().getRepository().getId(), resource);
    }
    return resource;
}
Also used : Path(java.nio.file.Path) PolicyViolationException(org.apache.archiva.policies.PolicyViolationException)

Aggregations

Path (java.nio.file.Path)1 PolicyViolationException (org.apache.archiva.policies.PolicyViolationException)1