Search in sources :

Example 91 with Data

use of com.microsoft.applicationinsights.smoketest.schemav2.Data in project geotoolkit by Geomatys.

the class FeatureToComplexConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Data convert(Feature source, Map<String, Object> params) throws UnconvertibleObjectException {
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    } else if (params == null) {
        throw new UnconvertibleObjectException("Not enough information about data format");
    }
    final Object tmpMime = params.get(MIME);
    final String mime;
    if (tmpMime instanceof String) {
        mime = (String) tmpMime;
    } else {
        throw new UnconvertibleObjectException("No valid mime type given. We cannot determine output image format");
    }
    final Data complex = new Data();
    complex.setMimeType(mime);
    final Object tmpEncoding = params.get(ENCODING);
    if (tmpEncoding instanceof String) {
        complex.setEncoding((String) tmpEncoding);
    }
    final FeatureType ft = source.getType();
    final String namespace = NamesExt.getNamespace(ft.getName());
    final Map<String, String> schemaLocation = new HashMap<>();
    if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(complex.getMimeType())) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            try (GeoJSONStreamWriter writer = new GeoJSONStreamWriter(baos, ft, WPSConvertersUtils.FRACTION_DIGITS)) {
                Feature next = writer.next();
                FeatureExt.copy(source, next, true);
                writer.write();
            }
            WPSConvertersUtils.addCDATAToComplex(baos.toString("UTF-8"), complex);
            complex.setSchema(null);
        } catch (DataStoreException e) {
            throw new UnconvertibleObjectException("Can't write Feature into GeoJSON output stream.", e);
        } catch (UnsupportedEncodingException e) {
            throw new UnconvertibleObjectException("Can't convert output stream into String.", e);
        }
    } else if (WPSMimeType.APP_GML.val().equalsIgnoreCase(complex.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(complex.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(complex.getMimeType())) {
        try {
            complex.setSchema(WPSConvertersUtils.writeSchema(ft, params));
            schemaLocation.put(namespace, complex.getSchema());
        } catch (JAXBException ex) {
            throw new UnconvertibleObjectException("Can't write FeatureType into xsd schema.", ex);
        } catch (IOException ex) {
            throw new UnconvertibleObjectException("Can't create xsd schema file.", ex);
        }
        try {
            final ElementFeatureWriter efw = new ElementFeatureWriter(schemaLocation);
            complex.getContent().add(efw.writeFeature(source, null, true));
        } catch (ParserConfigurationException ex) {
            throw new UnconvertibleObjectException("Can't write FeatureCollection into ResponseDocument.", ex);
        }
    } else
        throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + complex.getMimeType());
    return complex;
}
Also used : FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) HashMap(java.util.HashMap) JAXBException(javax.xml.bind.JAXBException) Data(org.geotoolkit.wps.xml.v200.Data) Feature(org.opengis.feature.Feature) ElementFeatureWriter(org.geotoolkit.feature.xml.jaxp.ElementFeatureWriter) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) GeoJSONStreamWriter(org.geotoolkit.storage.geojson.GeoJSONStreamWriter) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 92 with Data

use of com.microsoft.applicationinsights.smoketest.schemav2.Data in project geotoolkit by Geomatys.

the class FileToComplexConverter method convert.

/**
 * Convert a file into Data object, according to the specifications given in params parameter.
 *
 * @param source The file to convert.
 * @param params The parameters used for conversion (Mime-Type/encoding). If null, mime is set to application/octet-stream, and encoding to base64
 * @return
 * @throws UnconvertibleObjectException
 */
@Override
public Data convert(File source, Map<String, Object> params) throws UnconvertibleObjectException {
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    }
    if (params == null) {
        throw new UnconvertibleObjectException("Mandatory parameters are missing.");
    }
    final Object tmpMime = params.get(MIME);
    final String mime;
    final String encoding;
    if (tmpMime instanceof String) {
        mime = (String) tmpMime;
        final Object tmpEncoding = params.get(ENCODING);
        if (tmpEncoding instanceof String)
            encoding = (String) tmpEncoding;
        else
            encoding = null;
    } else {
        mime = WPSMimeType.APP_OCTET.val();
        encoding = WPSEncoding.BASE64.getValue();
    }
    final Data complex = new Data();
    complex.setMimeType(mime);
    complex.setEncoding(encoding);
    // Plain text
    if (mime.startsWith("text")) {
        // XML is special case, we try to find an associate schema.
        if (mime.contains("xml") || mime.contains("gml")) {
            String schemaLocation = source.getAbsolutePath();
            File ogrSchema = new File(schemaLocation);
            // If we find a schema, we ensure it's location is public before giving it.
            if (ogrSchema.exists()) {
                Object tmpDirValue = params.get(TMP_DIR_PATH);
                String tmpDir;
                if (tmpDirValue instanceof URI) {
                    tmpDir = ((URI) params.get(TMP_DIR_PATH)).toString();
                } else if (tmpDirValue instanceof String) {
                    tmpDir = (String) params.get(TMP_DIR_PATH);
                } else {
                    throw new UnconvertibleObjectException("Unexpected type for " + TMP_DIR_PATH + " parameter.");
                }
                String tmpURL = (String) params.get(TMP_DIR_URL);
                if (tmpDir == null || tmpURL == null) {
                    throw new UnconvertibleObjectException("Mandatory parameters are missing.");
                }
                if (!schemaLocation.contains(tmpDir)) {
                    String schemaName = source.getName().replace("\\.[a-z]ml", "").concat(".xsd");
                    Path schemaDest = Paths.get(tmpDir, schemaName);
                    try {
                        IOUtilities.copy(source.toPath(), schemaDest);
                        schemaLocation = schemaDest.toAbsolutePath().toString();
                    } catch (IOException e) {
                        throw new UnconvertibleObjectException("Unexpected error on schema copy.", e);
                    }
                }
                complex.setSchema(schemaLocation.replace(tmpDir, tmpURL));
            }
        }
        // CData needed because files could contain problematic characters.
        complex.getContent().add("<![CDATA[");
        complex.getContent().add(source);
        complex.getContent().add("]]>");
    } else {
        // If no text format, We'll put it as a base64 object.
        if (encoding == null || !encoding.equals(WPSEncoding.BASE64.getValue())) {
            throw new UnconvertibleObjectException("Encoding should be in Base64 for complex request.");
        }
        try (FileInputStream stream = new FileInputStream(source)) {
            byte[] barray = new byte[(int) source.length()];
            stream.read(barray);
            complex.getContent().add(Base64.getEncoder().encodeToString(barray));
        } catch (Exception ex) {
            throw new UnconvertibleObjectException(ex.getMessage(), ex);
        }
    }
    return complex;
}
Also used : Path(java.nio.file.Path) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Data(org.geotoolkit.wps.xml.v200.Data) IOException(java.io.IOException) File(java.io.File) URI(java.net.URI) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException)

Example 93 with Data

use of com.microsoft.applicationinsights.smoketest.schemav2.Data in project geotoolkit by Geomatys.

the class GeometryArrayToComplexConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Data convert(final Geometry[] source, final Map<String, Object> params) throws UnconvertibleObjectException {
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    } else if (params == null) {
        throw new UnconvertibleObjectException("Not enough information about data format");
    }
    final Object tmpMime = params.get(MIME);
    final String mime;
    if (tmpMime instanceof String) {
        mime = (String) tmpMime;
    } else {
        throw new UnconvertibleObjectException("No valid mime type given. We cannot determine output image format");
    }
    final Data complex = new Data();
    complex.setMimeType(mime);
    final Object tmpEncoding = params.get(ENCODING);
    if (tmpEncoding instanceof String) {
        complex.setEncoding((String) tmpEncoding);
    }
    Object tmpGmlVersion = params.get(GMLVERSION);
    final String gmlVersion;
    if (tmpGmlVersion instanceof String) {
        gmlVersion = (String) tmpGmlVersion;
    } else {
        gmlVersion = "3.2.1";
    }
    if (WPSMimeType.APP_GML.val().equalsIgnoreCase(complex.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(complex.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(complex.getMimeType())) {
        try {
            for (final Geometry jtsGeom : source) {
                final AbstractGeometry gmlGeom = JTStoGeometry.toGML(gmlVersion, jtsGeom);
                complex.getContent().add(gmlGeom);
            }
        } catch (NoSuchAuthorityCodeException ex) {
            throw new UnconvertibleObjectException(ex);
        } catch (FactoryException ex) {
            throw new UnconvertibleObjectException(ex);
        }
    } else if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(complex.getMimeType())) {
        GeoJSONGeometry.GeoJSONGeometryCollection geometryCollection = new GeoJSONGeometry.GeoJSONGeometryCollection();
        for (Geometry geometry : source) geometryCollection.getGeometries().add(GeoJSONGeometry.toGeoJSONGeometry(geometry));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            GeoJSONStreamWriter.writeSingleGeometry(baos, WPSConvertersUtils.convertGeoJSONGeometryToGeometry(geometryCollection), JsonEncoding.UTF8, WPSConvertersUtils.FRACTION_DIGITS, true);
            WPSConvertersUtils.addCDATAToComplex(baos.toString("UTF-8"), complex);
        } catch (UnsupportedEncodingException e) {
            throw new UnconvertibleObjectException("Can't convert output stream into String.", e);
        } catch (IOException ex) {
            throw new UnconvertibleObjectException(ex);
        } catch (FactoryException ex) {
            throw new UnconvertibleObjectException("Couldn't decode CRS.", ex);
        }
    } else
        throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + complex.getMimeType());
    return complex;
}
Also used : NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) FactoryException(org.opengis.util.FactoryException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Data(org.geotoolkit.wps.xml.v200.Data) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JTStoGeometry(org.geotoolkit.gml.JTStoGeometry) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Geometry(org.locationtech.jts.geom.Geometry) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry)

Example 94 with Data

use of com.microsoft.applicationinsights.smoketest.schemav2.Data in project geotoolkit by Geomatys.

the class GeometryToComplexConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Data convert(final Geometry source, final Map<String, Object> params) throws UnconvertibleObjectException {
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    } else if (params == null) {
        throw new UnconvertibleObjectException("Not enough information about data format");
    }
    final Object tmpMime = params.get(MIME);
    final String mime;
    if (tmpMime instanceof String) {
        mime = (String) tmpMime;
    } else {
        throw new UnconvertibleObjectException("No valid mime type given. We cannot determine output image format");
    }
    final Data complex = new Data();
    complex.setMimeType(mime);
    final Object tmpEncoding = params.get(ENCODING);
    if (tmpEncoding instanceof String) {
        complex.setEncoding((String) tmpEncoding);
    }
    final Object tmpSchema = params.get(SCHEMA);
    if (tmpSchema instanceof String) {
        complex.setSchema((String) tmpSchema);
    }
    Object tmpGmlVersion = params.get(GMLVERSION);
    final String gmlVersion;
    if (tmpGmlVersion instanceof String) {
        gmlVersion = (String) tmpGmlVersion;
    } else {
        gmlVersion = "3.2.1";
    }
    final String mimeType = complex.getMimeType();
    if (WPSMimeType.APP_GML.val().equalsIgnoreCase(mimeType) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(mimeType) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(mimeType)) {
        try {
            final AbstractGeometry gmlGeom = JTStoGeometry.toGML(gmlVersion, source);
            complex.getContent().add(gmlGeom);
        } catch (NoSuchAuthorityCodeException ex) {
            throw new UnconvertibleObjectException(ex);
        } catch (FactoryException ex) {
            throw new UnconvertibleObjectException(ex);
        }
    } else if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(mimeType)) {
        GeoJSONGeometry jsonGeometry = GeoJSONGeometry.toGeoJSONGeometry(source);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            GeoJSONStreamWriter.writeSingleGeometry(baos, WPSConvertersUtils.convertGeoJSONGeometryToGeometry(jsonGeometry), JsonEncoding.UTF8, WPSConvertersUtils.FRACTION_DIGITS, true);
            WPSConvertersUtils.addCDATAToComplex(baos.toString("UTF-8"), complex);
        } catch (UnsupportedEncodingException e) {
            throw new UnconvertibleObjectException("Can't convert output stream into String.", e);
        } catch (IOException ex) {
            throw new UnconvertibleObjectException(ex);
        } catch (FactoryException ex) {
            throw new UnconvertibleObjectException("Couldn't decode a CRS.", ex);
        }
    } else if (WPSMimeType.APP_EWKT.val().equalsIgnoreCase(mimeType)) {
        Geometry geom = source;
        int srid = 0;
        try {
            CoordinateReferenceSystem crs = Geometries.wrap(geom).get().getCoordinateReferenceSystem();
            if (crs != null) {
                final IdentifiedObjectFinder finder = IdentifiedObjects.newFinder("EPSG");
                finder.setIgnoringAxes(true);
                final CoordinateReferenceSystem epsgcrs = (CoordinateReferenceSystem) finder.findSingleton(crs);
                if (epsgcrs != null) {
                    srid = IdentifiedObjects.lookupEPSG(epsgcrs);
                    // force geometry in longitude first
                    final CoordinateReferenceSystem crs2 = ((AbstractCRS) crs).forConvention(AxesConvention.RIGHT_HANDED);
                    if (crs2 != crs) {
                        geom = org.apache.sis.internal.feature.jts.JTS.transform(geom, crs2);
                    }
                }
            }
        } catch (FactoryException | MismatchedDimensionException | TransformException ex) {
            throw new UnconvertibleObjectException(ex.getMessage(), ex);
        }
        String wkt = geom.toText();
        if (srid > 0) {
            wkt = "SRID=" + srid + ";" + wkt;
        }
        complex.getContent().add(wkt);
    } else {
        throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + complex.getMimeType());
    }
    return complex;
}
Also used : IdentifiedObjectFinder(org.apache.sis.referencing.factory.IdentifiedObjectFinder) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) FactoryException(org.opengis.util.FactoryException) AbstractCRS(org.apache.sis.referencing.crs.AbstractCRS) TransformException(org.opengis.referencing.operation.TransformException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Data(org.geotoolkit.wps.xml.v200.Data) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) JTStoGeometry(org.geotoolkit.gml.JTStoGeometry) Geometry(org.locationtech.jts.geom.Geometry) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 95 with Data

use of com.microsoft.applicationinsights.smoketest.schemav2.Data in project geotoolkit by Geomatys.

the class FeatureCollectionToComplexConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Data convert(final FeatureCollection source, final Map<String, Object> params) throws UnconvertibleObjectException {
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    } else if (params == null) {
        throw new UnconvertibleObjectException("Not enough information about data format");
    }
    final Object tmpMime = params.get(MIME);
    final String mime;
    if (tmpMime instanceof String) {
        mime = (String) tmpMime;
    } else {
        throw new UnconvertibleObjectException("No valid mime type given. We cannot determine output image format");
    }
    final Data complex = new Data();
    complex.setMimeType(mime);
    final Object tmpEncoding = params.get(ENCODING);
    if (tmpEncoding instanceof String) {
        complex.setEncoding((String) tmpEncoding);
    }
    final FeatureType ft = source.getType();
    final String namespace = NamesExt.getNamespace(ft.getName());
    final Map<String, String> schemaLocation = new HashMap<>();
    if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(mime)) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            try (GeoJSONStreamWriter writer = new GeoJSONStreamWriter(baos, ft, WPSConvertersUtils.FRACTION_DIGITS);
                Stream<Feature> st = source.features(false)) {
                Iterator<Feature> iterator = st.iterator();
                while (iterator.hasNext()) {
                    Feature next = iterator.next();
                    Feature neww = writer.next();
                    FeatureExt.copy(next, neww, false);
                    writer.write();
                }
            }
            WPSConvertersUtils.addCDATAToComplex(baos.toString("UTF-8"), complex);
            complex.setSchema(null);
        } catch (DataStoreException e) {
            throw new UnconvertibleObjectException("Can't write Feature into GeoJSON output stream.", e);
        } catch (UnsupportedEncodingException e) {
            throw new UnconvertibleObjectException("Can't convert output stream into String.", e);
        }
    } else if (WPSMimeType.APP_GML.val().equalsIgnoreCase(mime) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(mime) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(mime)) {
        try {
            complex.setSchema(WPSConvertersUtils.writeSchema(ft, params));
            schemaLocation.put(namespace, complex.getSchema());
        } catch (JAXBException ex) {
            throw new UnconvertibleObjectException("Can't write FeatureType into xsd schema.", ex);
        } catch (IOException ex) {
            throw new UnconvertibleObjectException("Can't create xsd schema file.", ex);
        }
        try {
            final ElementFeatureWriter efw = new ElementFeatureWriter(schemaLocation);
            complex.getContent().add(efw.writeFeatureCollection(source, null, true, false, null, true));
        } catch (DataStoreException | ParserConfigurationException ex) {
            throw new UnconvertibleObjectException("Can't write FeatureCollection into ResponseDocument.", ex);
        }
    } else
        throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + complex.getMimeType());
    return complex;
}
Also used : FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) HashMap(java.util.HashMap) JAXBException(javax.xml.bind.JAXBException) Data(org.geotoolkit.wps.xml.v200.Data) Feature(org.opengis.feature.Feature) ElementFeatureWriter(org.geotoolkit.feature.xml.jaxp.ElementFeatureWriter) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) GeoJSONStreamWriter(org.geotoolkit.storage.geojson.GeoJSONStreamWriter)

Aggregations

Test (org.junit.Test)65 Envelope (com.microsoft.applicationinsights.smoketest.schemav2.Envelope)53 RequestData (com.microsoft.applicationinsights.smoketest.schemav2.RequestData)45 Data (org.geotoolkit.wps.xml.v200.Data)42 RemoteDependencyData (com.microsoft.applicationinsights.smoketest.schemav2.RemoteDependencyData)26 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)20 IOException (java.io.IOException)16 DataInput (org.geotoolkit.wps.xml.v200.DataInput)15 Format (org.geotoolkit.wps.xml.v200.Format)12 AiSmokeTest (com.microsoft.applicationinsights.smoketest.AiSmokeTest)10 TargetUri (com.microsoft.applicationinsights.smoketest.TargetUri)10 MessageData (com.microsoft.applicationinsights.smoketest.schemav2.MessageData)10 ComplexData (org.geotoolkit.wps.xml.v200.ComplexData)10 ExceptionData (com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData)9 ReferenceProxy (org.geotoolkit.wps.xml.ReferenceProxy)9 Data (com.microsoft.applicationinsights.smoketest.schemav2.Data)8 DataStoreException (org.apache.sis.storage.DataStoreException)8 LiteralValue (org.geotoolkit.wps.xml.v200.LiteralValue)7 EventData (com.microsoft.applicationinsights.smoketest.schemav2.EventData)6 MetricData (com.microsoft.applicationinsights.smoketest.schemav2.MetricData)6