use of org.codice.ddf.registry.converter.RegistryConversionException in project ddf by codice.
the class RegistryTransformer method transform.
@Override
public Metacard transform(InputStream inputStream, String id) throws IOException, CatalogTransformerException {
MetacardImpl metacard;
try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
try {
IOUtils.copy(inputStream, fileBackedOutputStream);
} catch (IOException e) {
throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard. Error reading input stream.", e);
} finally {
IOUtils.closeQuietly(inputStream);
}
try (InputStream inputStreamCopy = fileBackedOutputStream.asByteSource().openStream()) {
metacard = (MetacardImpl) unmarshal(inputStreamCopy);
} catch (ParserException e) {
throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard. Parser exception caught", e);
} catch (RegistryConversionException e) {
throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard. Conversion exception caught", e);
}
if (metacard == null) {
throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard.");
} else if (StringUtils.isNotEmpty(id)) {
metacard.setAttribute(Metacard.ID, id);
}
String xml;
try (Reader reader = fileBackedOutputStream.asByteSource().asCharSource(Charsets.UTF_8).openStream()) {
xml = CharStreams.toString(reader);
}
metacard.setAttribute(Metacard.METADATA, xml);
metacard.setTags(Collections.singleton(RegistryConstants.REGISTRY_TAG));
} catch (IOException e) {
throw new CatalogTransformerException("Unable to transform from CSW RIM Service Record to Metacard. Error using file-backed stream.", e);
}
return metacard;
}
Aggregations