Search in sources :

Example 6 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class RESTEndpoint 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));
        } else {
            LOGGER.debug("Metacard had a null id");
        }
    } 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) ByteArrayInputStream(java.io.ByteArrayInputStream) 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 7 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class InMemoryProcessingFramework method storeProcessRequest.

private <T extends ProcessResourceItem> void storeProcessRequest(ProcessRequest<T> processRequest) {
    LOGGER.trace("Storing update request post processing change(s)");
    Map<String, ContentItem> contentItemsToUpdate = new HashMap<>();
    Map<String, Metacard> metacardsToUpdate = new HashMap<>();
    List<TemporaryFileBackedOutputStream> tfbosToCleanUp = new ArrayList<>();
    for (T item : processRequest.getProcessItems()) {
        if (item.getProcessResource() == null && item.isMetacardModified()) {
            metacardsToUpdate.put(item.getMetacard().getId(), item.getMetacard());
        }
        final ProcessResource processResource = item.getProcessResource();
        TemporaryFileBackedOutputStream tfbos = null;
        if (processResource != null && processResource.isModified() && !contentItemsToUpdate.containsKey(getContentItemKey(item.getMetacard(), processResource))) {
            try {
                tfbos = new TemporaryFileBackedOutputStream();
                long numberOfBytes = IOUtils.copyLarge(processResource.getInputStream(), tfbos);
                LOGGER.debug("Copied {} bytes to TemporaryFileBackedOutputStream.", numberOfBytes);
                ByteSource byteSource = tfbos.asByteSource();
                ContentItem contentItem = new ContentItemImpl(item.getMetacard().getId(), processResource.getQualifier(), byteSource, processResource.getMimeType(), processResource.getName(), processResource.getSize(), item.getMetacard());
                contentItemsToUpdate.put(getContentItemKey(item.getMetacard(), processResource), contentItem);
                tfbosToCleanUp.add(tfbos);
            } catch (IOException | RuntimeException e) {
                LOGGER.debug("Unable to store process request", e);
                if (tfbos != null) {
                    close(tfbos);
                }
            }
        }
    }
    storeContentItemUpdates(contentItemsToUpdate, processRequest.getProperties());
    storeMetacardUpdates(metacardsToUpdate, processRequest.getProperties());
    closeTfbos(tfbosToCleanUp);
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessResource(org.codice.ddf.catalog.async.data.api.internal.ProcessResource) IOException(java.io.IOException) Metacard(ddf.catalog.data.Metacard) ByteSource(com.google.common.io.ByteSource) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Example 8 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)

Example 9 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class MimeTypeMapperImpl method guessMimeType.

@Override
public String guessMimeType(InputStream is, String fileExtension) throws MimeTypeResolutionException {
    LOGGER.trace("ENTERING: guessMimeType()");
    String mimeType = null;
    LOGGER.debug("Looping through{} MimeTypeResolvers", mimeTypeResolvers.size());
    // This is to force the TikaMimeTypeResolver to be called
    // after the CustomMimeTypeResolvers to prevent Tika default mapping
    // from being used when a CustomMimeTypeResolver may be more appropriate.
    List<MimeTypeResolver> sortedResolvers = sortResolvers(mimeTypeResolvers);
    if (StringUtils.isEmpty(fileExtension)) {
        try (TemporaryFileBackedOutputStream tfbos = new TemporaryFileBackedOutputStream()) {
            IOUtils.copy(is, tfbos);
            try (InputStream inputStream = tfbos.asByteSource().openStream()) {
                Detector detector = new DefaultDetector();
                MediaType mediaType = detector.detect(inputStream, new Metadata());
                fileExtension = getFileExtensionForMimeType(mediaType.toString()).replace(".", "");
            } finally {
                is = tfbos.asByteSource().openStream();
            }
        } catch (Exception e) {
            LOGGER.debug("Failed to guess mimeType for file without extension.");
        }
    }
    // If file has XML extension, then read root element namespace once so
    // each MimeTypeResolver does not have to open the stream and read the namespace
    String namespace = null;
    if (fileExtension.equals(XML_FILE_EXTENSION)) {
        try {
            namespace = XMLUtils.getRootNamespace(IOUtils.toString(is));
        } catch (IOException ioe) {
            LOGGER.debug("Could not read namespace from input stream.", ioe);
        }
        LOGGER.debug("namespace = {}", namespace);
    }
    // Once a file extension is find for the given mime type, exit the loop.
    for (MimeTypeResolver resolver : sortedResolvers) {
        LOGGER.debug("Calling MimeTypeResolver {}", resolver.getName());
        try {
            // an InputTransformer to create a metacard for that "generic" XML file.
            if (fileExtension.equals(XML_FILE_EXTENSION)) {
                if (namespace != null && resolver.hasSchema()) {
                    if (namespace.equals(resolver.getSchema())) {
                        mimeType = resolver.getMimeTypeForFileExtension(fileExtension);
                    }
                }
            } else {
                mimeType = resolver.getMimeTypeForFileExtension(fileExtension);
            }
        } catch (Exception e) {
            LOGGER.debug("Error resolving mime type for file extension: {}", fileExtension);
            throw new MimeTypeResolutionException(e);
        }
        if (StringUtils.isNotEmpty(mimeType)) {
            LOGGER.debug("mimeType [{}] retrieved from MimeTypeResolver:  ", mimeType, resolver.getName());
            break;
        }
    }
    LOGGER.debug("mimeType = {},   file extension = [{}]", mimeType, fileExtension);
    LOGGER.trace("EXITING: guessMimeType()");
    return mimeType;
}
Also used : MimeTypeResolver(ddf.mime.MimeTypeResolver) DefaultDetector(org.apache.tika.detect.DefaultDetector) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) Detector(org.apache.tika.detect.Detector) DefaultDetector(org.apache.tika.detect.DefaultDetector) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) MediaType(org.apache.tika.mime.MediaType) IOException(java.io.IOException) IOException(java.io.IOException) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException)

Example 10 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class FtpRequestHandlerTest method testMimeTypeMapperNull.

@Test(expected = IllegalArgumentException.class)
public void testMimeTypeMapperNull() {
    String mimeType;
    catalogFramework = mock(CatalogFramework.class);
    FtpRequestHandler ftplett = new FtpRequestHandler(catalogFramework, null, uuidGenerator);
    mimeType = ftplett.getMimeType("xml", new TemporaryFileBackedOutputStream());
    assertEquals("", mimeType);
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) CatalogFramework(ddf.catalog.CatalogFramework) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)18 IOException (java.io.IOException)13 InputStream (java.io.InputStream)10 Metacard (ddf.catalog.data.Metacard)8 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)8 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Test (org.junit.Test)4 ByteSource (com.google.common.io.ByteSource)3 InputTransformer (ddf.catalog.transform.InputTransformer)3 MimeTypeResolutionException (ddf.mime.MimeTypeResolutionException)3 Subject (ddf.security.Subject)3 ArrayList (java.util.ArrayList)3 CreateStorageRequest (ddf.catalog.content.operation.CreateStorageRequest)2 MetacardCreationException (ddf.catalog.data.MetacardCreationException)2 TikaMetadataExtractor (ddf.catalog.transformer.common.tika.TikaMetadataExtractor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Serializable (java.io.Serializable)2 DefaultFtpReply (org.apache.ftpserver.ftplet.DefaultFtpReply)2 FtpFile (org.apache.ftpserver.ftplet.FtpFile)2