Search in sources :

Example 1 with RegistryConversionException

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;
}
Also used : ParserException(org.codice.ddf.parser.ParserException) RegistryConversionException(org.codice.ddf.registry.converter.RegistryConversionException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Aggregations

MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 ParserException (org.codice.ddf.parser.ParserException)1 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)1 RegistryConversionException (org.codice.ddf.registry.converter.RegistryConversionException)1