Search in sources :

Example 66 with CatalogTransformerException

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

the class InputTransformerProducer method generateMetacard.

private Optional<Metacard> generateMetacard(MimeType mimeType, MimeTypeToTransformerMapper mapper, TemporaryFileBackedOutputStream tfbos, String metacardId) {
    LOGGER.trace("ENTERING: generateMetacard");
    List<InputTransformer> listOfCandidates = mapper.findMatches(InputTransformer.class, mimeType);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
    }
    Metacard generatedMetacard = null;
    // can create the metacard, then do not need to try any remaining InputTransformers.
    for (InputTransformer transformer : listOfCandidates) {
        try (InputStream inputStreamMessageCopy = tfbos.asByteSource().openStream()) {
            if (StringUtils.isEmpty(metacardId)) {
                generatedMetacard = transformer.transform(inputStreamMessageCopy);
            } else {
                generatedMetacard = transformer.transform(inputStreamMessageCopy, metacardId);
            }
        } catch (CatalogTransformerException e) {
            LOGGER.debug("Transformer [{}] could not create metacard.", transformer, e);
        } catch (IOException e) {
            LOGGER.debug("Could not open input stream", e);
        }
        if (generatedMetacard != null) {
            break;
        }
    }
    LOGGER.trace("EXITING: generateMetacard");
    return Optional.ofNullable(generatedMetacard);
}
Also used : Metacard(ddf.catalog.data.Metacard) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer)

Example 67 with CatalogTransformerException

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

the class InputTransformerProducer method transform.

@Override
protected Object transform(Message in, String mimeType, String transformerId, MimeTypeToTransformerMapper mapper) throws MimeTypeParseException, CatalogTransformerException {
    MimeType derivedMimeType = null;
    try (InputStream message = in.getBody(InputStream.class);
        TemporaryFileBackedOutputStream tfbos = new TemporaryFileBackedOutputStream()) {
        if (message == null) {
            throw new CatalogTransformerException("Message body was null; unable to generate Metacard!");
        }
        // First try to get mimeType from file extension passed in the Camel Message headers
        IOUtils.copy(message, tfbos);
        String fileExtensionHeader = getHeaderAsStringAndRemove(in, FILE_EXTENSION_HEADER);
        if (StringUtils.isNotEmpty(fileExtensionHeader)) {
            Optional<String> fileMimeType = getMimeTypeFor(tfbos.asByteSource().openBufferedStream(), fileExtensionHeader);
            if (fileMimeType.isPresent()) {
                LOGGER.trace("Setting mimetype to [{}] from Message header [{}]", fileMimeType.get(), FILE_EXTENSION_HEADER);
                derivedMimeType = new MimeType(fileMimeType.get());
            }
        }
        if (derivedMimeType == null) {
            if (StringUtils.isNotEmpty(mimeType)) {
                // mimeType and tranformerId
                if (StringUtils.isNotEmpty(transformerId)) {
                    derivedMimeType = new MimeType(mimeType + ";" + MimeTypeToTransformerMapper.ID_KEY + "=" + transformerId);
                    LOGGER.trace("Using mimeType to [{}]", derivedMimeType);
                } else {
                    LOGGER.trace("Using CatalogEndpoint's configured mimeType [{}]", mimeType);
                    derivedMimeType = new MimeType(mimeType);
                }
            } else {
                LOGGER.debug("Unable to determine mimeType. Defaulting to [{}]", DEFAULT_MIME_TYPE);
                derivedMimeType = new MimeType(DEFAULT_MIME_TYPE);
            }
        }
        String metacardUpdateID = getHeaderAsStringAndRemove(in, METACARD_ID_HEADER);
        return generateMetacard(derivedMimeType, mapper, tfbos, metacardUpdateID).orElseThrow(() -> new CatalogTransformerException(String.format("Did not find an InputTransformer for MIME Type [%s] and %s [%s]", mimeType, MimeTypeToTransformerMapper.ID_KEY, transformerId)));
    } catch (IOException e) {
        throw new CatalogTransformerException("Unable to transform incoming product", e);
    }
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) MimeType(javax.activation.MimeType)

Example 68 with CatalogTransformerException

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

the class TransformerConsumer method doStart.

@Override
protected void doStart() throws CatalogTransformerException {
    LOGGER.debug("ENTERING: doStart");
    try {
        super.doStart();
    } catch (Exception e) {
        throw new CatalogTransformerException("Failed to start Transformer Consumer", e);
    }
    DictionaryMap<String, String> props = new DictionaryMap<>();
    if (endpoint.getTransformerId() != null) {
        props.put(MimeTypeToTransformerMapper.ID_KEY, endpoint.getTransformerId());
    }
    if (endpoint.getMimeType() != null) {
        props.put(MimeTypeToTransformerMapper.MIME_TYPE_KEY, endpoint.getMimeType());
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Registering as QueryResponseTransformer with id={}", endpoint.getTransformerId());
    }
    // Register this Catalog Consumer as an QueryResponseTransformer in the OSGi registry.
    // The transformer ID (e.g., id=text/xml;id=xml) specified in the Camel route node
    // is used as the "id" key for this QueryResponseTransformer in the OSGi registry.
    // (This is how the CatalogContentPlugin will be able to look up this transformer by
    // mimetype)
    registration = endpoint.getComponent().getBundleContext().registerService(transformerClass.getName(), this, props);
    LOGGER.debug("EXITING: doStart");
}
Also used : CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) DictionaryMap(org.codice.ddf.configuration.DictionaryMap)

Example 69 with CatalogTransformerException

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

the class QueryResponseTransformerProducer method transform.

@Override
protected Object transform(Message in, String mimeType, String transformerId, MimeTypeToTransformerMapper mapper) throws MimeTypeParseException, CatalogTransformerException {
    // Look up the QueryResponseTransformer for the request's mime type.
    // If a transformer is found, then transform the request's payload into a BinaryContent
    // Otherwise, throw an exception.
    MimeType derivedMimeType = new MimeType(mimeType);
    if (transformerId != null) {
        derivedMimeType = new MimeType(mimeType + ";" + MimeTypeToTransformerMapper.ID_KEY + "=" + transformerId);
    }
    List<QueryResponseTransformer> matches = mapper.findMatches(QueryResponseTransformer.class, derivedMimeType);
    Object binaryContent = null;
    if (matches != null && matches.size() == 1) {
        Map<String, Serializable> arguments = new HashMap<>();
        for (Entry<String, Object> entry : in.getHeaders().entrySet()) {
            if (entry.getValue() instanceof Serializable) {
                arguments.put(entry.getKey(), (Serializable) entry.getValue());
            }
        }
        LOGGER.debug("Found a matching QueryResponseTransformer for [{}]", transformerId);
        QueryResponseTransformer transformer = matches.get(0);
        SourceResponse srcResp = in.getBody(SourceResponse.class);
        if (null != srcResp) {
            binaryContent = transformer.transform(srcResp, arguments);
        }
    } else {
        LOGGER.debug("Did not find an QueryResponseTransformer for [{}]", transformerId);
        throw new CatalogTransformerException("Did not find an QueryResponseTransformer for [" + transformerId + "]");
    }
    return binaryContent;
}
Also used : Serializable(java.io.Serializable) SourceResponse(ddf.catalog.operation.SourceResponse) QueryResponseTransformer(ddf.catalog.transform.QueryResponseTransformer) HashMap(java.util.HashMap) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) MimeType(javax.activation.MimeType)

Example 70 with CatalogTransformerException

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

the class TransformerProducer method process.

/*
   * (non-Javadoc)
   *
   * @see org.apache.camel.Processor#process(org.apache.camel.Exchange)
   */
@Override
public void process(Exchange exchange) throws CatalogTransformerException, MimeTypeParseException, IOException {
    LOGGER.trace("ENTERING: process");
    LOGGER.debug("exchange pattern = {}", exchange.getPattern());
    Message in = exchange.getIn();
    // Get the MIME Type and ID of the transformer to use to transform the
    // request's payload from the endpoint that manages this producer
    String transformerId = in.getHeader(CatalogComponent.ID_PARAMETER, String.class);
    String mimeType = in.getHeader(CatalogComponent.MIME_TYPE_PARAMETER, String.class);
    if (transformerId != null) {
        LOGGER.debug("transformerId retrieved from message header");
        in.removeHeader(CatalogComponent.ID_PARAMETER);
    } else {
        LOGGER.debug("transformerId retrieved from CamelCatalogEndpoint");
        transformerId = endpoint.getTransformerId();
    }
    LOGGER.debug("transformerId = {}", transformerId);
    if (mimeType != null) {
        in.removeHeader(CatalogComponent.MIME_TYPE_PARAMETER);
    } else {
        LOGGER.debug("No mimeType provided, defaulting to Camel CatalogEndpoint mimeType of [{}]", endpoint.getMimeType());
        mimeType = endpoint.getMimeType();
    }
    LOGGER.debug("MIME Type = {}", mimeType);
    MimeTypeToTransformerMapper mapper = endpoint.getComponent().getMimeTypeToTransformerMapper();
    Object metacard;
    if (mapper != null) {
        LOGGER.debug("Got a MimeTypeToTransformerMapper service");
        metacard = transform(in, mimeType, transformerId, mapper);
    } else {
        LOGGER.debug("Did not find a MimeTypeToTransformerMapper service");
        throw new CatalogTransformerException("Did not find a MimeTypeToTransformerMapper service");
    }
    // Set the body to the Metacard from the transformation
    in.setBody(metacard);
    LOGGER.trace("EXITING: process");
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) Message(org.apache.camel.Message) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException)

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