use of org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference in project geotoolkit by Geomatys.
the class FeatureToReferenceConverter method convert.
/**
* {@inheritDoc}
*/
@Override
public Reference convert(final Feature 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 directory should be defined.");
}
FeatureType ft = null;
if (source instanceof Feature) {
ft = source.getType();
} else {
throw new UnconvertibleObjectException("The requested output reference data is not an instance of Feature.");
}
Reference reference = new Reference();
reference.setMimeType((String) params.get(MIME));
reference.setEncoding((String) params.get(ENCODING));
final String namespace = NamesExt.getNamespace(ft.getName());
final Map<String, String> schemaLocation = new HashMap<>();
final String randomFileName = UUID.randomUUID().toString();
final String tmpDirUrl = (String) params.get(TMP_DIR_URL);
if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(reference.getMimeType())) {
// create file
final Path dataFile = buildPath(params, randomFileName + ".json");
try {
try (OutputStream fos = Files.newOutputStream(dataFile);
GeoJSONStreamWriter writer = new GeoJSONStreamWriter(fos, ft, WPSConvertersUtils.FRACTION_DIGITS)) {
Feature next = writer.next();
FeatureExt.copy(source, next, true);
writer.write();
}
} catch (DataStoreException e) {
throw new UnconvertibleObjectException("Can't write Feature into GeoJSON output stream.", e);
} catch (IOException e) {
throw new UnconvertibleObjectException(e);
}
final String relLoc = getRelativeLocation(dataFile, params);
reference.setHref(tmpDirUrl + "/" + relLoc);
reference.setSchema(null);
} else if (WPSMimeType.APP_GML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(reference.getMimeType())) {
// Write FeatureType
try {
reference.setSchema(WPSConvertersUtils.writeSchema(ft, params));
schemaLocation.put(namespace, reference.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);
}
// Write Feature
final XmlFeatureWriter featureWriter = new JAXPStreamFeatureWriter(schemaLocation);
// create file
final Path dataFile = buildPath(params, randomFileName + ".xml");
try (final OutputStream dataStream = Files.newOutputStream(dataFile);
final AutoCloseable xmlCloser = () -> featureWriter.dispose()) {
// Write feature in file
featureWriter.write(source, dataStream);
final String relLoc = getRelativeLocation(dataFile, params);
reference.setHref(tmpDirUrl + "/" + relLoc);
} catch (XMLStreamException ex) {
throw new UnconvertibleObjectException("Stax exception while writing the feature collection", ex);
} catch (DataStoreException ex) {
throw new UnconvertibleObjectException("FeatureStore exception while writing the feature collection", ex);
} catch (FeatureStoreRuntimeException ex) {
throw new UnconvertibleObjectException("FeatureStoreRuntimeException exception while writing the feature collection", ex);
} catch (Exception ex) {
throw new UnconvertibleObjectException(ex);
}
} else
throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + reference.getMimeType());
return reference;
}
use of org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference in project geotoolkit by Geomatys.
the class FeatureTypeToReferenceConverter method convert.
/**
* {@inheritDoc}
*/
@Override
public Reference convert(final FeatureType 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 directory should be defined.");
}
Reference reference = new Reference();
reference.setMimeType((String) params.get(MIME));
reference.setEncoding((String) params.get(ENCODING));
reference.setSchema((String) params.get(SCHEMA));
reference.setMimeType((String) params.get(MIME));
reference.setEncoding((String) params.get(ENCODING));
final String randomFileName = UUID.randomUUID().toString();
// Write FeatureType
try {
final String schemaFileName = randomFileName + "_schema" + ".xsd";
// create file
final Path schemaFile = buildPath(params, schemaFileName);
final OutputStream schemaStream = Files.newOutputStream(schemaFile);
// write featureType xsd on file
final JAXBFeatureTypeWriter xmlFTWriter = new JAXBFeatureTypeWriter();
xmlFTWriter.write(source, schemaStream);
final String relLoc = getRelativeLocation(schemaFile, params);
reference.setHref((String) params.get(TMP_DIR_URL) + "/" + relLoc);
} 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);
}
return reference;
}
use of org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference in project geotoolkit by Geomatys.
the class GeometryToReferenceConverter method convert.
/**
* {@inheritDoc}
*/
@Override
public Reference convert(final Geometry 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 Geometry)) {
throw new UnconvertibleObjectException("The geometry is not an JTS geometry.");
}
Reference reference = new Reference();
reference.setMimeType((String) params.get(MIME));
reference.setEncoding((String) params.get(ENCODING));
reference.setSchema((String) params.get(SCHEMA));
String gmlVersion = (String) params.get(GMLVERSION);
if (gmlVersion == null) {
gmlVersion = "3.1.1";
}
final String randomPathName = UUID.randomUUID().toString() + getFileExtension(reference.getMimeType());
final Path geometryFile = buildPath(params, randomPathName);
final String tmpDirUrl = (String) params.get(TMP_DIR_URL);
OutputStream geometryStream = null;
try {
geometryStream = Files.newOutputStream(geometryFile);
if (WPSMimeType.APP_GML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(reference.getMimeType()) || reference.getMimeType() == null) {
// default to XML
final Marshaller m = WPSMarshallerPool.getInstance().acquireMarshaller();
m.marshal(JTStoGeometry.toGML(gmlVersion, source), geometryStream);
final String relLoc = getRelativeLocation(geometryFile, params);
reference.setHref(tmpDirUrl + '/' + relLoc.replace('\\', '/'));
WPSMarshallerPool.getInstance().recycle(m);
} else if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(reference.getMimeType())) {
GeoJSONStreamWriter.writeSingleGeometry(geometryStream, source, JsonEncoding.UTF8, WPSConvertersUtils.FRACTION_DIGITS, true);
final String relLoc = getRelativeLocation(geometryFile, params);
reference.setHref(tmpDirUrl + '/' + relLoc.replace('\\', '/'));
} else {
throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + reference.getMimeType());
}
} catch (FactoryException ex) {
throw new UnconvertibleObjectException("Can't convert the JTS geometry to OpenGIS.", ex);
} catch (FileNotFoundException ex) {
throw new UnconvertibleObjectException("Can't create output reference file.", ex);
} catch (JAXBException ex) {
throw new UnconvertibleObjectException("JAXB exception while writing the geometry", ex);
} catch (IOException ex) {
throw new UnconvertibleObjectException(ex);
} finally {
try {
if (geometryStream != null)
geometryStream.close();
} catch (IOException ex) {
throw new UnconvertibleObjectException("Can't close the output reference file stream.", ex);
}
}
return reference;
}
use of org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference in project geotoolkit by Geomatys.
the class NumberToReferenceConverter method convert.
@Override
public Reference convert(final Number 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();
reference.setMimeType((String) params.get(MIME));
reference.setEncoding((String) params.get(ENCODING));
reference.setSchema((String) params.get(SCHEMA));
reference.setMimeType("text/plain");
reference.setEncoding("UTF-8");
reference.setSchema(null);
final String randomFileName = UUID.randomUUID().toString();
try {
// create file
final Path literalFile = buildPath(params, randomFileName);
IOUtilities.writeString(String.valueOf(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 string writing.", ex);
}
return reference;
}
use of org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference in project geotoolkit by Geomatys.
the class UriToReferenceConverter method convert.
@Override
public Reference convert(URI source, Map<String, Object> params) throws UnconvertibleObjectException {
if (source == null)
return null;
final Reference reference = new Reference();
reference.setHref(source.toString());
mapParameters(reference, params);
return reference;
}
Aggregations