Search in sources :

Example 66 with Resource

use of com.revolsys.spring.resource.Resource in project com.revolsys.open by revolsys.

the class AbstractGeoreferencedImage method loadAuxXmlFile.

protected void loadAuxXmlFile(final long modifiedTime) {
    final Resource resource = getImageResource();
    final String extension = resource.getFileNameExtension();
    final Resource auxFile = resource.newResourceChangeExtension(extension + ".aux.xml");
    if (auxFile.exists() && auxFile.getLastModified() > modifiedTime) {
        loadWorldFileX();
        final int[] dpi = getDpi();
        try {
            int srid = 0;
            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            final DocumentBuilder builder = factory.newDocumentBuilder();
            final InputStream in = auxFile.getInputStream();
            try {
                final Document doc = builder.parse(in);
                final NodeList spatialReferences = doc.getElementsByTagName("SpatialReference");
                for (int i = 0; i < spatialReferences.getLength() && srid == 0; i++) {
                    final Node spatialReference = spatialReferences.item(i);
                    Element sridElement = DomUtil.getFirstChildElement(spatialReference, "LatestWKID");
                    if (sridElement == null) {
                        sridElement = DomUtil.getFirstChildElement(spatialReference, "WKID");
                    }
                    if (sridElement != null) {
                        srid = DomUtil.getInteger(sridElement);
                    }
                }
                GeometryFactory geometryFactory = GeometryFactory.floating2d(srid);
                if (srid == 0) {
                    final NodeList srsList = doc.getElementsByTagName("SRS");
                    for (int i = 0; i < srsList.getLength() && srid == 0; i++) {
                        final Node srsNode = srsList.item(i);
                        final String srsWkt = srsNode.getTextContent();
                        geometryFactory = GeometryFactory.floating2d(srsWkt);
                    }
                }
                setGeometryFactory(geometryFactory);
                final List<Double> sourceControlPoints = DomUtil.getDoubleList(doc, "SourceGCPs");
                final List<Double> targetControlPoints = DomUtil.getDoubleList(doc, "TargetGCPs");
                if (sourceControlPoints.size() > 0 && targetControlPoints.size() > 0) {
                    final List<MappedLocation> tiePoints = new ArrayList<>();
                    for (int i = 0; i < sourceControlPoints.size() && i < targetControlPoints.size(); i += 2) {
                        final double imageX = sourceControlPoints.get(i) * dpi[0];
                        final double imageY = sourceControlPoints.get(i + 1) * dpi[1];
                        final Point sourcePixel = new PointDoubleXY(imageX, imageY);
                        final double x = targetControlPoints.get(i);
                        final double y = targetControlPoints.get(i + 1);
                        final Point targetPoint = geometryFactory.point(x, y);
                        final MappedLocation tiePoint = new MappedLocation(sourcePixel, targetPoint);
                        tiePoints.add(tiePoint);
                    }
                    setTiePoints(tiePoints);
                }
            } finally {
                FileUtil.closeSilent(in);
            }
        } catch (final Throwable e) {
            Logs.error(this, "Unable to read: " + auxFile, e);
        }
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Resource(com.revolsys.spring.resource.Resource) ArrayList(java.util.ArrayList) PropertyChangeArrayList(com.revolsys.collection.PropertyChangeArrayList) Point(com.revolsys.geometry.model.Point) Document(org.w3c.dom.Document) Point(com.revolsys.geometry.model.Point) DocumentBuilder(javax.xml.parsers.DocumentBuilder) PointDoubleXY(com.revolsys.geometry.model.impl.PointDoubleXY)

Example 67 with Resource

use of com.revolsys.spring.resource.Resource in project com.revolsys.open by revolsys.

the class MavenRepositoryCache method copyRepositoryResource.

private boolean copyRepositoryResource(final MavenRepository repository, final Resource resource, final String groupId, final String artifactId, final String version, final String type, final String classifier, final String specificVersion, final String algorithm, final String path) {
    final String sha1Digest = repository.getSha1(groupId, artifactId, version, type, classifier, specificVersion, algorithm);
    final Resource repositoryResource = repository.getRoot().newChildResource(path);
    try {
        if (Property.hasValue(sha1Digest)) {
            final InputStream in = repositoryResource.getInputStream();
            final DigestInputStream digestIn = new DigestInputStream(in, MessageDigest.getInstance("SHA-1"));
            resource.copyFrom(digestIn);
            final MessageDigest messageDigest = digestIn.getMessageDigest();
            final byte[] digest = messageDigest.digest();
            final String fileDigest = Hex.toHex(digest);
            if (!sha1Digest.equals(fileDigest)) {
                Logs.error(this, ".sha1 digest is different for: " + repositoryResource);
                resource.delete();
                return false;
            }
        } else {
            resource.copyFrom(repositoryResource);
        }
        Logs.info(this, "Download maven resource: " + repositoryResource);
        return true;
    } catch (Throwable e) {
        resource.delete();
        while (e instanceof WrappedException) {
            e = e.getCause();
        }
        if (e instanceof FileNotFoundException) {
            return false;
        } else if (e instanceof IOException) {
            final IOException ioException = (IOException) e;
            if (ioException.getMessage().contains(" 404 ")) {
                return false;
            }
        }
        Logs.error(this, "Unable to download MVN resource " + repositoryResource + "\n  " + e.getMessage(), e);
        return false;
    }
}
Also used : WrappedException(com.revolsys.util.WrappedException) DigestInputStream(java.security.DigestInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) PathResource(com.revolsys.spring.resource.PathResource) Resource(com.revolsys.spring.resource.Resource) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Example 68 with Resource

use of com.revolsys.spring.resource.Resource in project com.revolsys.open by revolsys.

the class MavenRepositoryCache method getSnapshotVersion.

@Override
protected Pair<Long, String> getSnapshotVersion(final String groupId, final String artifactId, final String version, final String type, final String classifier, final String algorithm) {
    Pair<Long, String> timestampAndVersion = null;
    MavenRepository snapshotRepository = null;
    for (final MavenRepository repository : this.repositories) {
        final Pair<Long, String> repositorySnapshotVersion = repository.getSnapshotVersion(groupId, artifactId, version, type, classifier, algorithm);
        if (repositorySnapshotVersion != null) {
            boolean matched = false;
            if (timestampAndVersion == null) {
                matched = true;
            } else {
                final Long repositoryLastUpdateTime = repositorySnapshotVersion.getValue1();
                final Long lastUpdateTime = timestampAndVersion.getValue1();
                if (repositoryLastUpdateTime > lastUpdateTime) {
                    matched = true;
                }
            }
            if (matched) {
                timestampAndVersion = repositorySnapshotVersion;
                snapshotRepository = repository;
            }
        }
    }
    if (timestampAndVersion != null) {
        final String specificVersion = timestampAndVersion.getValue2();
        final String path = getPath(groupId, artifactId, version, type, classifier, specificVersion, algorithm);
        final Resource rootResource = getRoot();
        final Resource artifactResource = rootResource.newChildResource(path);
        if (version.equals(specificVersion) || !artifactResource.exists()) {
            copyRepositoryResource(snapshotRepository, artifactResource, groupId, artifactId, version, type, classifier, specificVersion, algorithm, path);
        }
    }
    return timestampAndVersion;
}
Also used : PathResource(com.revolsys.spring.resource.PathResource) Resource(com.revolsys.spring.resource.Resource)

Example 69 with Resource

use of com.revolsys.spring.resource.Resource in project com.revolsys.open by revolsys.

the class MavenRepository method getResource.

private Resource getResource(final String groupId, final String artifactId, final String version, final String type, final String classifier, final String specificVersion, final String algorithm) {
    final String path = getPath(groupId, artifactId, version, type, classifier, specificVersion, algorithm);
    final Resource artifactResource = this.root.newChildResource(path);
    if (!artifactResource.exists()) {
        return handleMissingResource(artifactResource, groupId, artifactId, specificVersion, type, classifier, specificVersion, algorithm);
    }
    return artifactResource;
}
Also used : PathResource(com.revolsys.spring.resource.PathResource) Resource(com.revolsys.spring.resource.Resource)

Example 70 with Resource

use of com.revolsys.spring.resource.Resource in project com.revolsys.open by revolsys.

the class MavenUrlStreamHandler method openConnection.

@Override
protected URLConnection openConnection(final URL url) throws IOException {
    final String protocol = url.getProtocol();
    if (protocol.equals("jar")) {
        final String file = url.getFile();
        int separator = file.indexOf("!/");
        if (separator == -1) {
            throw new MalformedURLException("no !/ found in url spec:" + file);
        } else {
            final String subUrl = file.substring(0, separator++);
            if (subUrl.startsWith("mvn")) {
                final String mavenId = subUrl.substring(4);
                final Resource resource = this.mavenRepository.getResource(mavenId);
                final URL resourceUrl = resource.getURL();
                String entryName = "/";
                if (++separator != file.length()) {
                    entryName = file.substring(separator - 1, file.length());
                }
                final String jarUrl = "jar:" + resourceUrl + "!" + entryName;
                return new URL(jarUrl).openConnection();
            }
        }
    } else if (protocol.equals("mvn")) {
        final String mavenId = url.getFile();
        final Resource resource = this.mavenRepository.getResource(mavenId);
        final URL resourceUrl = resource.getURL();
        return resourceUrl.openConnection();
    }
    return new URL(url.toString()).openConnection();
}
Also used : MalformedURLException(java.net.MalformedURLException) Resource(com.revolsys.spring.resource.Resource) URL(java.net.URL)

Aggregations

Resource (com.revolsys.spring.resource.Resource)78 PathResource (com.revolsys.spring.resource.PathResource)23 MapEx (com.revolsys.collection.map.MapEx)9 File (java.io.File)9 IOException (java.io.IOException)8 InputStream (java.io.InputStream)6 LinkedHashMapEx (com.revolsys.collection.map.LinkedHashMapEx)5 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)5 UrlResource (com.revolsys.spring.resource.UrlResource)5 Writer (java.io.Writer)5 Record (com.revolsys.record.Record)4 RecordDefinition (com.revolsys.record.schema.RecordDefinition)4 DataType (com.revolsys.datatype.DataType)3 BoundingBox (com.revolsys.geometry.model.BoundingBox)3 Geometry (com.revolsys.geometry.model.Geometry)3 AbstractRecordWriter (com.revolsys.io.AbstractRecordWriter)3 ArrayRecord (com.revolsys.record.ArrayRecord)3 RecordReader (com.revolsys.record.io.RecordReader)3 RecordWriter (com.revolsys.record.io.RecordWriter)3 Blob (java.sql.Blob)3