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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations