Search in sources :

Example 36 with Reference

use of org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference in project geotoolkit by Geomatys.

the class RenderedImageToReferenceConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Reference convert(final RenderedImage source, final Map<String, Object> params) throws UnconvertibleObjectException {
    if (params.get(TMP_DIR_PATH) == null) {
        throw new UnconvertibleObjectException("The output directory should be defined.");
    }
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    }
    if (!(source instanceof BufferedImage) && !(source instanceof RenderedImage)) {
        throw new UnconvertibleObjectException("The output data is not an instance of RenderedImage.");
    }
    Reference reference = new Reference();
    final String encoding = (String) params.get(ENCODING);
    final String mime = (String) params.get(MIME) != null ? (String) params.get(MIME) : "image/png";
    final String formatName = mime.substring(mime.indexOf("/") + 1).toUpperCase();
    reference.setMimeType(mime);
    reference.setEncoding((String) params.get(ENCODING));
    reference.setSchema((String) params.get(SCHEMA));
    // try to find a suffix file
    String suffix = "";
    ImageReaderSpi spi = XImageIO.getReaderSpiByFormatName(formatName);
    if (spi != null) {
        String[] suffixes = spi.getFileSuffixes();
        if (suffixes != null && suffixes.length != 0) {
            suffix = suffixes[0];
        }
    }
    final String randomFileName = UUID.randomUUID().toString() + suffix;
    try {
        final Path imageFile = buildPath(params, randomFileName);
        if (encoding != null && encoding.equals(WPSEncoding.BASE64.getValue())) {
            try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                ImageIO.write(source, formatName, baos);
                baos.flush();
                byte[] bytesOut = baos.toByteArray();
                IOUtilities.writeString(Base64.getEncoder().encodeToString(bytesOut), imageFile);
            }
        } else {
            try (OutputStream out = Files.newOutputStream(imageFile, StandardOpenOption.CREATE, WRITE, TRUNCATE_EXISTING)) {
                ImageIO.write(source, formatName, out);
            }
        }
        final String relLoc = getRelativeLocation(imageFile, params);
        reference.setHref((String) params.get(TMP_DIR_URL) + "/" + relLoc);
    } catch (IOException ex) {
        throw new UnconvertibleObjectException("Error occured during image writing.", ex);
    }
    return reference;
}
Also used : Path(java.nio.file.Path) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) Reference(org.geotoolkit.wps.xml.v200.Reference) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RenderedImage(java.awt.image.RenderedImage) BufferedImage(java.awt.image.BufferedImage)

Example 37 with Reference

use of org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference in project geotoolkit by Geomatys.

the class StringToReferenceConverter method convert.

@Override
public Reference convert(final String source, final Map<String, Object> params) throws UnconvertibleObjectException {
    if (params.get(TMP_DIR_PATH) == null) {
        throw new UnconvertibleObjectException("The output directory should be defined.");
    }
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    }
    Reference reference = new Reference();
    final String mime = (params.get(MIME) == null) ? "text/plain" : (String) params.get(MIME);
    final String encoding = (params.get(ENCODING) == null) ? "UTF-8" : (String) params.get(ENCODING);
    reference.setMimeType(mime);
    reference.setEncoding(encoding);
    reference.setSchema((String) params.get(SCHEMA));
    final String randomFileName = UUID.randomUUID().toString();
    try {
        // create file
        final Path literalFile = buildPath(params, randomFileName);
        IOUtilities.writeString(source, literalFile);
        final String relLoc = getRelativeLocation(literalFile, params);
        reference.setHref((String) params.get(TMP_DIR_URL) + "/" + relLoc);
    } catch (IOException ex) {
        throw new UnconvertibleObjectException("Error occurs during image writing.", ex);
    }
    return reference;
}
Also used : Path(java.nio.file.Path) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Reference(org.geotoolkit.wps.xml.v200.Reference) IOException(java.io.IOException)

Example 38 with Reference

use of org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference in project geotoolkit by Geomatys.

the class StyledLayerDescriptorToReferenceConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Reference convert(final StyledLayerDescriptor source, final Map<String, Object> params) throws UnconvertibleObjectException {
    if (params.get(TMP_DIR_PATH) == null) {
        throw new UnconvertibleObjectException("The output directory should be defined.");
    }
    if (source == null) {
        throw new UnconvertibleObjectException("The source is not defined.");
    }
    Reference reference = new Reference();
    reference.setMimeType((String) params.get(MIME));
    reference.setEncoding((String) params.get(ENCODING));
    reference.setSchema((String) params.get(SCHEMA));
    if (WPSMimeType.APP_SLD.val().equalsIgnoreCase(reference.getMimeType())) {
        // create file
        final Path dataFile = buildPath(params, UUID.randomUUID().toString() + ".sld");
        try {
            StyleXmlIO xmlio = new StyleXmlIO();
            xmlio.writeSLD(dataFile, source, Specification.StyledLayerDescriptor.V_1_1_0);
        } catch (JAXBException e) {
            throw new UnconvertibleObjectException(e.getMessage(), e);
        }
        final String relLoc = getRelativeLocation(dataFile, params);
        reference.setHref(params.get(TMP_DIR_URL) + "/" + relLoc);
    } else {
        throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + reference.getMimeType());
    }
    return reference;
}
Also used : Path(java.nio.file.Path) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Reference(org.geotoolkit.wps.xml.v200.Reference) JAXBException(javax.xml.bind.JAXBException) StyleXmlIO(org.geotoolkit.sld.xml.StyleXmlIO)

Example 39 with Reference

use of org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference in project geotoolkit by Geomatys.

the class FeatureCollectionToReferenceConverterTest method testJSONConversion.

@Test
public void testJSONConversion() throws DataStoreException, MalformedURLException, IOException, URISyntaxException, FactoryException {
    // Get the test resource
    final Object testResource = ConvertersTestUtils.loadTestResource("/inputs/featurecollection.json");
    final Reference reference = ConvertersTestUtils.initAndRunOutputConversion(FeatureCollection.class, Reference.class, testResource, WPSMimeType.APP_GEOJSON.val(), WPSEncoding.UTF8.getValue());
    // Test reference
    assertEquals(WPSMimeType.APP_GEOJSON.val(), reference.getMimeType());
    assertEquals(WPSEncoding.UTF8.getValue(), reference.getEncoding());
    assertNull(reference.getSchema());
    assertNotNull(reference.getHref());
    final FeatureSet featureCollection = WPSConvertersUtils.readFeatureCollectionFromJson(URI.create(reference.getHref()));
    ConvertersTestUtils.assertFeatureCollectionIsValid((FeatureSet) testResource);
    ConvertersTestUtils.assertFeatureCollectionIsValid(featureCollection);
}
Also used : Reference(org.geotoolkit.wps.xml.v200.Reference) FeatureSet(org.apache.sis.storage.FeatureSet) Test(org.junit.Test)

Example 40 with Reference

use of org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference in project geotoolkit by Geomatys.

the class FeatureToReferenceConverterTest method testJSONConversion.

@Test
public void testJSONConversion() throws DataStoreException, IOException, URISyntaxException, FactoryException {
    // Get the test resource
    final Object testResource = ConvertersTestUtils.loadTestResource("/inputs/feature.json");
    final Reference reference = ConvertersTestUtils.initAndRunOutputConversion(Feature.class, Reference.class, testResource, WPSMimeType.APP_GEOJSON.val(), WPSEncoding.UTF8.getValue());
    // Test reference
    assertEquals(WPSMimeType.APP_GEOJSON.val(), reference.getMimeType());
    assertEquals(WPSEncoding.UTF8.getValue(), reference.getEncoding());
    assertNull(reference.getSchema());
    assertNotNull(reference.getHref());
    Feature feature = WPSConvertersUtils.readFeatureFromJson(URI.create(reference.getHref()));
    ConvertersTestUtils.assertFeatureIsValid((Feature) testResource);
    ConvertersTestUtils.assertFeatureIsValid(feature);
}
Also used : Reference(org.geotoolkit.wps.xml.v200.Reference) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Aggregations

Reference (org.geotoolkit.wps.xml.v200.Reference)34 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)17 Path (java.nio.file.Path)14 Test (org.junit.Test)11 IOException (java.io.IOException)9 URL (java.net.URL)9 HashMap (java.util.HashMap)9 JAXBException (javax.xml.bind.JAXBException)6 AbstractWPSConverterTest (org.geotoolkit.wps.converters.AbstractWPSConverterTest)6 ArrayList (java.util.ArrayList)5 Feature (org.opengis.feature.Feature)5 BufferedReader (java.io.BufferedReader)4 DataStoreException (org.apache.sis.storage.DataStoreException)4 Geometry (org.locationtech.jts.geom.Geometry)4 RenderedImage (java.awt.image.RenderedImage)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 Reference (org.dishevelled.bio.assembly.gfa1.Reference)3 Traversal (org.dishevelled.bio.assembly.gfa1.Traversal)3 Reference (com.google.api.expr.v1alpha1.Reference)2 HashBasedTable (com.google.common.collect.HashBasedTable)2