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