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