use of com.google.api.expr.v1alpha1.Reference in project geotoolkit by Geomatys.
the class FileToReferenceConverter method convert.
@Override
public Reference convert(File source, 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));
final Path targetDirectory = buildPath(params, null);
final String tmpDir = getTemproraryDirectoryPath(params);
final String tmpDirUrl = (String) params.get(TMP_DIR_URL);
try {
// if the source is already in the temp folder
if (source.getAbsolutePath().startsWith(tmpDir)) {
reference.setHref(source.getAbsolutePath().replace(tmpDir, tmpDirUrl));
// else we create a link from the source file in temp dir
} else {
final Path target = targetDirectory.resolve(source.getName());
Files.createSymbolicLink(target, source.toPath());
// IOUtilities.copy(source.toPath(),target);
String suffix = getRelativeLocation(target, tmpDir);
reference.setHref(tmpDirUrl + "/" + suffix);
}
} catch (IOException ex) {
throw new UnconvertibleObjectException("Error during moving file to output directory.", ex);
}
return reference;
}
use of com.google.api.expr.v1alpha1.Reference in project geotoolkit by Geomatys.
the class GeometryArrayToReferenceConverter method convert.
@Override
public Reference convert(Geometry[] source, 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));
final String randomFilename = UUID.randomUUID().toString() + ".json";
final Path geometryArrayFile = buildPath(params, randomFilename);
try {
if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(reference.getMimeType())) {
try (OutputStream geometryStream = Files.newOutputStream(geometryArrayFile)) {
GeometryFactory geometryFactory = org.geotoolkit.geometry.jts.JTS.getFactory();
Geometry toWrite = new GeometryCollection(source, geometryFactory);
GeoJSONStreamWriter.writeSingleGeometry(geometryStream, toWrite, JsonEncoding.UTF8, WPSConvertersUtils.FRACTION_DIGITS, true);
}
reference.setHref(geometryArrayFile.toUri().toURL().toString());
return reference;
} else
throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + reference.getMimeType());
} catch (FileNotFoundException ex) {
throw new UnconvertibleObjectException("Unable to find the reference file");
} catch (IOException ex) {
throw new UnconvertibleObjectException(ex);
}
}
use of com.google.api.expr.v1alpha1.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;
}
use of com.google.api.expr.v1alpha1.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;
}
use of com.google.api.expr.v1alpha1.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;
}
Aggregations