Search in sources :

Example 31 with TemporaryFileBackedOutputStream

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);
    }
}
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 32 with TemporaryFileBackedOutputStream

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;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) BoundedInputStream(org.apache.commons.io.input.BoundedInputStream) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) ServiceReference(org.osgi.framework.ServiceReference) Metacard(ddf.catalog.data.Metacard) ArrayList(java.util.ArrayList) List(java.util.List) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext)

Example 33 with TemporaryFileBackedOutputStream

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);
    }
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard)

Example 34 with TemporaryFileBackedOutputStream

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

Aggregations

TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)34 IOException (java.io.IOException)23 InputStream (java.io.InputStream)21 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)14 Metacard (ddf.catalog.data.Metacard)13 ByteSource (com.google.common.io.ByteSource)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Test (org.junit.Test)7 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)5 InputTransformer (ddf.catalog.transform.InputTransformer)5 ArrayList (java.util.ArrayList)5 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)4 ZipOutputStream (java.util.zip.ZipOutputStream)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 MetacardCreationException (ddf.catalog.data.MetacardCreationException)3 MimeTypeResolutionException (ddf.mime.MimeTypeResolutionException)3 Subject (ddf.security.Subject)3 CreateStorageRequest (ddf.catalog.content.operation.CreateStorageRequest)2 Result (ddf.catalog.data.Result)2 ResultImpl (ddf.catalog.data.impl.ResultImpl)2