Search in sources :

Example 16 with Resource

use of org.apache.ivy.plugins.repository.Resource in project ant-ivy by apache.

the class BasicResolver method check.

/**
 * Checks the given resource checksum if a checksum resource exists.
 *
 * @param resource
 *            the resource to check
 * @param dest
 *            the file where the resource has been downloaded
 * @param algorithm
 *            the checksum algorithm to use
 * @return true if the checksum has been successfully checked, false if the checksum wasn't
 *         available
 * @throws IOException
 *             if a checksum exist but do not match the downloaded file checksum
 */
private boolean check(Resource resource, File dest, String algorithm) throws IOException {
    if (!ChecksumHelper.isKnownAlgorithm(algorithm)) {
        throw new IllegalArgumentException("Unknown checksum algorithm: " + algorithm);
    }
    Resource csRes = resource.clone(resource.getName() + "." + algorithm);
    if (csRes.exists()) {
        Message.debug(algorithm + " file found for " + resource + ": checking...");
        File csFile = File.createTempFile("ivytmp", algorithm);
        try {
            get(csRes, csFile);
            try {
                ChecksumHelper.check(dest, csFile, algorithm);
                Message.verbose(algorithm + " OK for " + resource);
                return true;
            } catch (IOException ex) {
                dest.delete();
                throw ex;
            }
        } finally {
            csFile.delete();
        }
    } else {
        return false;
    }
}
Also used : ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) MDResolvedResource(org.apache.ivy.plugins.resolver.util.MDResolvedResource) Resource(org.apache.ivy.plugins.repository.Resource) FileResource(org.apache.ivy.plugins.repository.file.FileResource) URLResource(org.apache.ivy.plugins.repository.url.URLResource) IOException(java.io.IOException) File(java.io.File)

Example 17 with Resource

use of org.apache.ivy.plugins.repository.Resource in project ant-ivy by apache.

the class IBiblioResolver method findTimestampedSnapshotVersion.

private String findTimestampedSnapshotVersion(final ModuleRevisionId mrid) {
    if (!isM2compatible()) {
        return null;
    }
    if (!shouldUseMavenMetadata(getWholePattern())) {
        return null;
    }
    try {
        final String metadataLocation = IvyPatternHelper.substitute(root + "[organisation]/[module]/[revision]/maven-metadata.xml", mrid);
        final Resource metadata = getRepository().getResource(metadataLocation);
        if (!metadata.exists()) {
            Message.verbose("\tmaven-metadata not available for: " + mrid);
            return null;
        }
        try (final InputStream metadataStream = metadata.openStream()) {
            final StringBuilder timestamp = new StringBuilder();
            final StringBuilder buildNumber = new StringBuilder();
            XMLHelper.parse(metadataStream, null, new ContextualSAXHandler() {

                @Override
                public void endElement(String uri, String localName, String qName) throws SAXException {
                    if ("metadata/versioning/snapshot/timestamp".equals(getContext())) {
                        timestamp.append(getText());
                    }
                    if ("metadata/versioning/snapshot/buildNumber".equals(getContext())) {
                        buildNumber.append(getText());
                    }
                    super.endElement(uri, localName, qName);
                }
            }, null);
            if (timestamp.length() > 0) {
                // we have found a timestamp, so this is a snapshot unique version
                String rev = mrid.getRevision();
                rev = rev.substring(0, rev.length() - "SNAPSHOT".length());
                rev += timestamp.toString() + "-" + buildNumber.toString();
                return rev;
            }
        }
    } catch (IOException | SAXException | ParserConfigurationException e) {
        Message.debug("impossible to access maven metadata file, ignored", e);
    }
    return null;
}
Also used : ContextualSAXHandler(org.apache.ivy.util.ContextualSAXHandler) InputStream(java.io.InputStream) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) Resource(org.apache.ivy.plugins.repository.Resource) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 18 with Resource

use of org.apache.ivy.plugins.repository.Resource in project ant-ivy by apache.

the class IBiblioResolver method listRevisionsWithMavenMetadata.

private List<String> listRevisionsWithMavenMetadata(Repository repository, String metadataLocation) {
    List<String> revs = null;
    InputStream metadataStream = null;
    try {
        Resource metadata = repository.getResource(metadataLocation);
        if (metadata.exists()) {
            Message.verbose("\tlisting revisions from maven-metadata: " + metadata);
            final List<String> metadataRevs = new ArrayList<>();
            metadataStream = metadata.openStream();
            XMLHelper.parse(metadataStream, null, new ContextualSAXHandler() {

                @Override
                public void endElement(String uri, String localName, String qName) throws SAXException {
                    if ("metadata/versioning/versions/version".equals(getContext())) {
                        metadataRevs.add(getText().trim());
                    }
                    super.endElement(uri, localName, qName);
                }
            }, null);
            revs = metadataRevs;
        } else {
            Message.verbose("\tmaven-metadata not available: " + metadata);
        }
    } catch (IOException e) {
        Message.verbose("impossible to access maven metadata file, ignored", e);
    } catch (SAXException | ParserConfigurationException e) {
        Message.verbose("impossible to parse maven metadata file, ignored", e);
    } finally {
        if (metadataStream != null) {
            try {
                metadataStream.close();
            } catch (IOException e) {
            // ignored
            }
        }
    }
    return revs;
}
Also used : ContextualSAXHandler(org.apache.ivy.util.ContextualSAXHandler) InputStream(java.io.InputStream) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) Resource(org.apache.ivy.plugins.repository.Resource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 19 with Resource

use of org.apache.ivy.plugins.repository.Resource in project ant-ivy by apache.

the class URLRepository method get.

public void get(String source, File destination) throws IOException {
    fireTransferInitiated(getResource(source), TransferEvent.REQUEST_GET);
    try {
        Resource res = getResource(source);
        long totalLength = res.getContentLength();
        if (totalLength > 0) {
            progress.setTotalLength(totalLength);
        }
        FileUtil.copy(new URL(source), destination, progress, getTimeoutConstraint());
    } catch (IOException | RuntimeException ex) {
        fireTransferError(ex);
        throw ex;
    } finally {
        progress.setTotalLength(null);
    }
}
Also used : Resource(org.apache.ivy.plugins.repository.Resource) IOException(java.io.IOException) URL(java.net.URL)

Example 20 with Resource

use of org.apache.ivy.plugins.repository.Resource in project ant-ivy by apache.

the class SFTPResource method init.

private void init() {
    if (!init) {
        Resource r = repository.resolveResource(path);
        contentLength = r.getContentLength();
        lastModified = r.getLastModified();
        exists = r.exists();
        init = true;
    }
}
Also used : Resource(org.apache.ivy.plugins.repository.Resource)

Aggregations

Resource (org.apache.ivy.plugins.repository.Resource)20 ResolvedResource (org.apache.ivy.plugins.resolver.util.ResolvedResource)10 File (java.io.File)9 IOException (java.io.IOException)8 URL (java.net.URL)7 URLResource (org.apache.ivy.plugins.repository.url.URLResource)7 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)5 ArrayList (java.util.ArrayList)4 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)4 FileResource (org.apache.ivy.plugins.repository.file.FileResource)4 MDResolvedResource (org.apache.ivy.plugins.resolver.util.MDResolvedResource)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 ParseException (java.text.ParseException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 CacheResourceOptions (org.apache.ivy.core.cache.CacheResourceOptions)2