Search in sources :

Example 91 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class AbstractCatalogService method createMetacard.

private BinaryContent createMetacard(InputStream stream, String contentType, String transformerParam) throws CatalogServiceException {
    String transformer = DEFAULT_METACARD_TRANSFORMER;
    if (transformerParam != null) {
        transformer = transformerParam;
    }
    MimeType mimeType = null;
    if (contentType != null) {
        try {
            mimeType = new MimeType(contentType);
        } catch (MimeTypeParseException e) {
            LOGGER.debug("Unable to create MimeType from raw data {}", contentType);
        }
    } else {
        LOGGER.debug("No content type specified in request");
    }
    try {
        Metacard metacard = generateMetacard(mimeType, null, stream, null);
        String metacardId = metacard.getId();
        LOGGER.debug("Metacard {} created", metacardId);
        LOGGER.debug("Transforming metacard {} to {} to be able to return it to client", metacardId, LogSanitizer.sanitize(transformer));
        final BinaryContent content = catalogFramework.transform(metacard, transformer, null);
        LOGGER.debug("Metacard to {} transform complete for {}, preparing response.", LogSanitizer.sanitize(transformer), metacardId);
        LOGGER.trace("EXITING: createMetacard");
        return content;
    } catch (MetacardCreationException | CatalogTransformerException e) {
        throw new CatalogServiceException("Unable to create metacard");
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) {
            LOGGER.debug("Unexpected error closing stream", e);
        }
    }
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) Metacard(ddf.catalog.data.Metacard) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) BinaryContent(ddf.catalog.data.BinaryContent) MimeType(javax.activation.MimeType)

Example 92 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class CswRecordCollectionMessageBodyWriter method writeTo.

@Override
public void writeTo(CswRecordCollection recordCollection, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream outStream) throws IOException, WebApplicationException {
    final String mimeType = recordCollection.getMimeType();
    LOGGER.debug("Attempting to transform RecordCollection with mime-type: {} & outputSchema: {}", mimeType, recordCollection.getOutputSchema());
    QueryResponseTransformer transformer;
    Map<String, Serializable> arguments = new HashMap<String, Serializable>();
    if (StringUtils.isBlank(recordCollection.getOutputSchema()) && StringUtils.isNotBlank(mimeType) && !XML_MIME_TYPES.contains(mimeType)) {
        transformer = transformerManager.getTransformerByMimeType(mimeType);
    } else if (OCTET_STREAM_OUTPUT_SCHEMA.equals(recordCollection.getOutputSchema())) {
        Resource resource = recordCollection.getResource();
        httpHeaders.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(resource.getMimeType()));
        httpHeaders.put(HttpHeaders.CONTENT_DISPOSITION, Arrays.asList(String.format("inline; filename=\"%s\"", resource.getName())));
        // Custom HTTP header to represent that the product data will be returned in the response.
        httpHeaders.put(CswConstants.PRODUCT_RETRIEVAL_HTTP_HEADER, Arrays.asList("true"));
        // Accept-ranges header to represent that ranges in bytes are accepted.
        httpHeaders.put(CswConstants.ACCEPT_RANGES_HEADER, Arrays.asList(CswConstants.BYTES));
        ByteArrayInputStream in = new ByteArrayInputStream(resource.getByteArray());
        IOUtils.copy(in, outStream);
        return;
    } else {
        transformer = transformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
        if (recordCollection.getElementName() != null) {
            arguments.put(CswConstants.ELEMENT_NAMES, recordCollection.getElementName().toArray());
        }
        arguments.put(CswConstants.OUTPUT_SCHEMA_PARAMETER, recordCollection.getOutputSchema());
        arguments.put(CswConstants.ELEMENT_SET_TYPE, recordCollection.getElementSetType());
        arguments.put(CswConstants.IS_BY_ID_QUERY, recordCollection.isById());
        arguments.put(CswConstants.GET_RECORDS, recordCollection.getRequest());
        arguments.put(CswConstants.RESULT_TYPE_PARAMETER, recordCollection.getResultType());
        arguments.put(CswConstants.WRITE_NAMESPACES, false);
    }
    if (transformer == null) {
        throw new WebApplicationException(new CatalogTransformerException("Unable to locate Transformer."));
    }
    BinaryContent content = null;
    try {
        content = transformer.transform(recordCollection.getSourceResponse(), arguments);
    } catch (CatalogTransformerException e) {
        throw new WebApplicationException(e);
    }
    if (content != null) {
        try (InputStream inputStream = content.getInputStream()) {
            IOUtils.copy(inputStream, outStream);
        }
    } else {
        throw new WebApplicationException(new CatalogTransformerException("Transformer returned null."));
    }
}
Also used : Serializable(java.io.Serializable) WebApplicationException(javax.ws.rs.WebApplicationException) QueryResponseTransformer(ddf.catalog.transform.QueryResponseTransformer) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Resource(ddf.catalog.resource.Resource) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent)

Example 93 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class GmdTransformer method handleTransform.

private Metacard handleTransform(InputStream inputStream, String id) throws CatalogTransformerException {
    String xml;
    XstreamPathValueTracker pathValueTracker;
    try (TemporaryFileBackedOutputStream temporaryFileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        IOUtils.copy(inputStream, temporaryFileBackedOutputStream);
        byteArray = temporaryFileBackedOutputStream.asByteSource();
        try (InputStream xmlSourceInputStream = getSourceInputStream()) {
            xml = IOUtils.toString(xmlSourceInputStream);
        }
        argumentHolder.put(XstreamPathConverter.PATH_KEY, buildPaths());
        XMLStreamReader streamReader = xmlFactory.createXMLStreamReader(new StringReader(xml));
        HierarchicalStreamReader reader = new StaxReader(new QNameMap(), streamReader);
        pathValueTracker = (XstreamPathValueTracker) xstream.unmarshal(reader, null, argumentHolder);
        Metacard metacard = toMetacard(pathValueTracker, id);
        metacard.setAttribute(new AttributeImpl(Core.METADATA, xml));
        return metacard;
    } catch (XStreamException | XMLStreamException | IOException e) {
        throw new CatalogTransformerException(TRANSFORM_EXCEPTION_MSG, e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}
Also used : StaxReader(com.thoughtworks.xstream.io.xml.StaxReader) XMLStreamReader(javax.xml.stream.XMLStreamReader) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) XstreamPathValueTracker(org.codice.ddf.spatial.ogc.csw.catalog.converter.XstreamPathValueTracker) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) Metacard(ddf.catalog.data.Metacard) XStreamException(com.thoughtworks.xstream.XStreamException) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap)

Example 94 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class CswTransformProvider method marshal.

/**
 * Marshals Metacards to an xml. This method is not typically be called directly, instead it is
 * called by another XStream Converter using MarshallingContext.convertAnother();
 *
 * @param o - metacard to transform.
 * @param writer - writes the XML.
 * @param context - the marshalling context. Should contain a map entry for {@link
 *     org.codice.ddf.spatial.ogc.csw.catalog.common.CswConstants#TRANSFORMER_LOOKUP_KEY} {@link
 *     org.codice.ddf.spatial.ogc.csw.catalog.common.CswConstants#TRANSFORMER_LOOKUP_VALUE} to
 *     identify which transformer to use. Also contains properties for any arguments to provide
 *     the transformer.
 */
@Override
public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext context) {
    if (o == null) {
        return;
    }
    Metacard metacard = (Metacard) o;
    String keyArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_KEY);
    String valArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_VALUE);
    MetacardTransformer transformer;
    if (StringUtils.isNotBlank(keyArg) && StringUtils.isNotBlank(valArg)) {
        transformer = metacardTransformerManager.getTransformerByProperty(keyArg, valArg);
    } else {
        transformer = metacardTransformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
    }
    if (transformer == null) {
        throw new ConversionException(String.format("Unable to locate a transformer for %s = %s", keyArg, valArg));
    }
    BinaryContent content;
    try {
        content = transformer.transform(metacard, getArguments(context));
    } catch (CatalogTransformerException e) {
        throw new ConversionException("Unable to transform Metacard", e);
    }
    writeXml(content, writer);
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) Metacard(ddf.catalog.data.Metacard) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent)

Example 95 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class CswTransformProvider method unmarshal.

/**
 * Creates a Metacard from the given XML. This method is not typically be called directly, instead
 * it is called by another XStream Converter using UnmarshallingContext.convertAnother();
 *
 * @param reader
 * @param context
 * @return
 */
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    String keyArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_KEY);
    String valArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_VALUE);
    InputTransformer transformer;
    if (StringUtils.isNotBlank(keyArg) && StringUtils.isNotBlank(valArg)) {
        transformer = inputTransformerManager.getTransformerByProperty(keyArg, valArg);
    } else {
        transformer = inputTransformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
    }
    if (transformer == null) {
        throw new ConversionException(String.format("Unable to locate a transformer for %s = %s", keyArg, valArg));
    }
    /* The CswRecordConverter uses unmarshal, which requires the context */
    if (transformer instanceof CswRecordConverter) {
        return ((CswRecordConverter) transformer).unmarshal(reader, context);
    }
    Metacard metacard;
    try (InputStream is = readXml(reader, context)) {
        InputStream inputStream = is;
        if (LOGGER.isDebugEnabled()) {
            String originalInputStream = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
            LOGGER.debug("About to transform\n{}", originalInputStream);
            inputStream = new ByteArrayInputStream(originalInputStream.getBytes(StandardCharsets.UTF_8.name()));
        }
        metacard = transformer.transform(inputStream);
    } catch (IOException | CatalogTransformerException e) {
        throw new ConversionException("Unable to transform Metacard", e);
    }
    return metacard;
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer)

Aggregations

CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)112 IOException (java.io.IOException)53 Metacard (ddf.catalog.data.Metacard)44 InputStream (java.io.InputStream)40 ByteArrayInputStream (java.io.ByteArrayInputStream)29 BinaryContent (ddf.catalog.data.BinaryContent)25 InputTransformer (ddf.catalog.transform.InputTransformer)21 Serializable (java.io.Serializable)21 HashMap (java.util.HashMap)21 Result (ddf.catalog.data.Result)16 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)15 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)14 ArrayList (java.util.ArrayList)13 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)12 Test (org.junit.Test)12 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)10 MimeType (javax.activation.MimeType)10 SourceResponse (ddf.catalog.operation.SourceResponse)9 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)8 List (java.util.List)8