use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream 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 org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.
the class AbstractCatalogService method generateMetacard.
private Metacard generateMetacard(MimeType mimeType, String id, InputStream message, String transformerId) throws MetacardCreationException {
Metacard generatedMetacard = null;
List<InputTransformer> listOfCandidates = mimeTypeToTransformerMapper.findMatches(InputTransformer.class, mimeType);
List<String> stackTraceList = new ArrayList<>();
LOGGER.trace("Entering generateMetacard.");
LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
try {
if (null != message) {
IOUtils.copy(message, fileBackedOutputStream);
} else {
throw new MetacardCreationException("Could not copy bytes of content message. Message was NULL.");
}
} catch (IOException e) {
throw new MetacardCreationException("Could not copy bytes of content message.", e);
}
Iterator<InputTransformer> it = listOfCandidates.iterator();
if (StringUtils.isNotEmpty(transformerId)) {
BundleContext bundleContext = getBundleContext();
Collection<ServiceReference<InputTransformer>> serviceReferences = bundleContext.getServiceReferences(InputTransformer.class, "(id=" + transformerId + ")");
it = serviceReferences.stream().map(bundleContext::getService).iterator();
}
while (it.hasNext()) {
InputTransformer transformer = it.next();
try (InputStream inputStreamMessageCopy = fileBackedOutputStream.asByteSource().openStream()) {
generatedMetacard = transformer.transform(inputStreamMessageCopy);
} catch (CatalogTransformerException | IOException e) {
List<String> stackTraces = Arrays.asList(ExceptionUtils.getRootCauseStackTrace(e));
stackTraceList.add(String.format("Transformer [%s] could not create metacard.", transformer));
stackTraceList.addAll(stackTraces);
LOGGER.debug("Transformer [{}] could not create metacard.", transformer, e);
}
if (generatedMetacard != null) {
break;
}
}
if (generatedMetacard == null) {
throw new MetacardCreationException(String.format("Could not create metacard with mimeType %s : %s", mimeType, StringUtils.join(stackTraceList, "\n")));
}
if (id != null) {
generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, id));
}
LOGGER.debug("Metacard id is {}", generatedMetacard.getId());
} catch (IOException e) {
throw new MetacardCreationException("Could not create metacard.", e);
} catch (InvalidSyntaxException e) {
throw new MetacardCreationException("Could not determine transformer", e);
}
return generatedMetacard;
}
use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.
the class OpenSearchSource method doQueryById.
private SourceResponse doQueryById(QueryRequest queryRequest, WebClient restWebClient) throws UnsupportedQueryException {
InputStream responseStream = performRequest(restWebClient);
try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
IOUtils.copyLarge(responseStream, fileBackedOutputStream);
InputTransformer inputTransformer;
try (InputStream inputStream = fileBackedOutputStream.asByteSource().openStream()) {
inputTransformer = getInputTransformer(inputStream);
}
try (InputStream inputStream = fileBackedOutputStream.asByteSource().openStream()) {
final Metacard metacard = inputTransformer.transform(inputStream);
metacard.setSourceId(getId());
ResultImpl result = new ResultImpl(metacard);
List<Result> resultQueue = new ArrayList<>();
resultQueue.add(result);
return new SourceResponseImpl(queryRequest, resultQueue);
}
} catch (IOException | CatalogTransformerException e) {
throw new UnsupportedQueryException("Problem with transformation.", e);
}
}
use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream 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);
}
}
Aggregations