Search in sources :

Example 16 with MetacardCreationException

use of ddf.catalog.data.MetacardCreationException in project ddf by codice.

the class InputTransformerProducer method generateMetacard.

private Metacard generateMetacard(MimeType mimeType, MimeTypeToTransformerMapper mapper, InputStream message) throws MetacardCreationException {
    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;
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            IOUtils.copy(message, fileBackedOutputStream);
        } catch (IOException e) {
            throw new MetacardCreationException("Could not copy bytes of content message.", e);
        }
        // can create the metacard, then do not need to try any remaining InputTransformers.
        for (InputTransformer transformer : listOfCandidates) {
            try (InputStream inputStreamMessageCopy = fileBackedOutputStream.asByteSource().openStream()) {
                generatedMetacard = transformer.transform(inputStreamMessageCopy);
            } catch (IOException | CatalogTransformerException e) {
                LOGGER.debug("Transformer [" + transformer + "] could not create metacard.", e);
            }
            if (generatedMetacard != null) {
                break;
            }
        }
        if (generatedMetacard == null) {
            throw new MetacardCreationException("Could not create metacard with mimeType " + mimeType + ". No valid transformers found.");
        }
        LOGGER.trace("EXITING: generateMetacard");
    } catch (IOException e) {
        throw new MetacardCreationException("Could not create metacard.", e);
    }
    return generatedMetacard;
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardCreationException(ddf.catalog.data.MetacardCreationException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer)

Example 17 with MetacardCreationException

use of ddf.catalog.data.MetacardCreationException in project ddf by codice.

the class InputTransformerProducer method transform.

protected Object transform(Message in, Object obj, String mimeType, String transformerId, MimeTypeToTransformerMapper mapper) throws MimeTypeParseException, IOException, CatalogTransformerException {
    // Look up the InputTransformer for the request's mime type.
    // If a transformer is found, then transform the request's payload into
    // a Metacard.
    // Otherwise, throw an exception.
    MimeType derivedMimeType = new MimeType(mimeType);
    if (transformerId != null) {
        derivedMimeType = new MimeType(mimeType + ";" + MimeTypeToTransformerMapper.ID_KEY + "=" + transformerId);
    }
    InputStream message = null;
    Metacard metacard = null;
    try {
        message = in.getBody(InputStream.class);
        if (null != message) {
            metacard = generateMetacard(derivedMimeType, mapper, message);
        } else {
            throw new CatalogTransformerException("Message body was null; unable to generate Metacard!");
        }
    } catch (MetacardCreationException e) {
        throw new CatalogTransformerException("Did not find an InputTransformer for MIME Type [" + mimeType + "] and " + MimeTypeToTransformerMapper.ID_KEY + " [" + transformerId + "]", e);
    } finally {
        if (null != message) {
            IOUtils.closeQuietly(message);
        }
    }
    return metacard;
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardCreationException(ddf.catalog.data.MetacardCreationException) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) MimeType(javax.activation.MimeType)

Example 18 with MetacardCreationException

use of ddf.catalog.data.MetacardCreationException in project alliance by codice.

the class CreateMetacardRolloverAction method doAction.

@Override
public MetacardImpl doAction(MetacardImpl metacard, File tempFile) throws RolloverActionException {
    MetacardImpl newMetacard;
    try {
        newMetacard = new MetacardImpl(findMetacardType());
    } catch (MetacardCreationException e) {
        throw new RolloverActionException(String.format("unable to create metacard: tempFile=%s", tempFile), e);
    }
    newMetacard.setContentTypeName(Constants.MPEGTS_MIME_TYPE);
    return newMetacard;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 19 with MetacardCreationException

use of ddf.catalog.data.MetacardCreationException 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);
        }
    }
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) Metacard(ddf.catalog.data.Metacard) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) BinaryContent(ddf.catalog.data.BinaryContent) MimeType(javax.activation.MimeType)

Example 20 with MetacardCreationException

use of ddf.catalog.data.MetacardCreationException 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)

Aggregations

MetacardCreationException (ddf.catalog.data.MetacardCreationException)25 Metacard (ddf.catalog.data.Metacard)13 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)10 MimeType (javax.activation.MimeType)9 IngestException (ddf.catalog.source.IngestException)8 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)6 SolrServerException (org.apache.solr.client.solrj.SolrServerException)6 SolrException (org.apache.solr.common.SolrException)6 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)5 InputStream (java.io.InputStream)5 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)4 InputTransformer (ddf.catalog.transform.InputTransformer)4 List (java.util.List)4 MimeTypeParseException (javax.activation.MimeTypeParseException)4 SolrQuery (org.apache.solr.client.solrj.SolrQuery)4 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)4 SolrDocument (org.apache.solr.common.SolrDocument)4 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)3 InternalIngestException (ddf.catalog.source.InternalIngestException)3