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