Search in sources :

Example 96 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class CswQueryResponseTransformer method multiThreadedMarshal.

/**
 * Multi-threaded marshal of metacard assumes that the query size is unbounded to guard against
 * resource exhaustion with fixed thread-pool and fixed work-queue. CPU-bound for optimum
 * utilization from availableProcessors()+1 thread pool.
 *
 * @param results - the list of results to marshal
 * @param recordSchema - the schema
 * @param arguments - additional args
 * @return - the marshaled results
 * @throws CatalogTransformerException
 */
private String multiThreadedMarshal(List<Result> results, AtomicLong numResults, String recordSchema, final Map<String, Serializable> arguments) throws CatalogTransformerException {
    CompletionService<BinaryContent> completionService = new ExecutorCompletionService<>(queryExecutor);
    final MetacardTransformer transformer = metacardTransformerManager.getTransformerBySchema(recordSchema);
    if (transformer == null) {
        throw new CatalogTransformerException("Cannot find transformer for schema: " + recordSchema);
    }
    Map<Future<BinaryContent>, Result> futures = new HashMap<>(results.size());
    for (Result result : results) {
        final Metacard mc = result.getMetacard();
        // the "current" thread will run submitted task when queueSize exceeded; effectively
        // blocking enqueue of more tasks.
        futures.put(completionService.submit(() -> {
            BinaryContent content = transformer.transform(mc, arguments);
            return content;
        }), result);
    }
    InputStream[] contents = new InputStream[results.size()];
    while (!futures.isEmpty()) {
        try {
            Future<BinaryContent> completedFuture = completionService.take();
            int index = results.indexOf(futures.get(completedFuture));
            try {
                contents[index] = completedFuture.get().getInputStream();
            } catch (ExecutionException | CancellationException e) {
                LOGGER.debug("Error transforming Metacard", e);
                numResults.decrementAndGet();
            } catch (InterruptedException e) {
                numResults.decrementAndGet();
                Thread.currentThread().interrupt();
                throw new CatalogTransformerException("Metacard transform interrupted", e);
            } finally {
                futures.remove(completedFuture);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new CatalogTransformerException("Metacard transform interrupted", e);
        }
    }
    CharArrayWriter accum = new CharArrayWriter(ACCUM_INITIAL_SIZE);
    for (InputStream is : contents) {
        try {
            if (is != null) {
                IOUtils.copy(is, accum);
            }
        } catch (IOException e) {
            LOGGER.debug("Error copying Metacard Binary content", e);
        }
    }
    return accum.toString();
}
Also used : MetacardTransformer(ddf.catalog.transform.MetacardTransformer) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) BinaryContent(ddf.catalog.data.BinaryContent) CharArrayWriter(java.io.CharArrayWriter) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) CancellationException(java.util.concurrent.CancellationException) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 97 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class PptxInputTransformer method transformLogic.

/**
 * This is a three step process. First, create a FileBackedOutputStream because we need to consume
 * the stream twice. Once for the injected inputTransformer and once for Apache POI. Next, extract
 * the metadata with the injected input transformer. And last, use Apache POI to create the
 * thumbnail.
 *
 * @param input
 * @return
 * @throws IOException
 * @throws CatalogTransformerException
 */
private Metacard transformLogic(InputStream input) throws IOException, CatalogTransformerException {
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            int c = IOUtils.copy(input, fileBackedOutputStream);
            LOGGER.debug("copied {} bytes from input stream to file backed output stream", c);
        } catch (IOException e) {
            throw new CatalogTransformerException("Could not copy bytes of content message.", e);
        }
        Metacard metacard = extractInitialMetadata(fileBackedOutputStream.asByteSource().openStream());
        try {
            extractThumbnail(metacard, fileBackedOutputStream.asByteSource().openStream());
        } catch (EncryptedDocumentException e) {
            LOGGER.debug("Unable to generate thumbnail", e);
        }
        return metacard;
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException)

Example 98 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class PropertyJsonMetacardTransformer method convertToJSON.

public static Map<String, Object> convertToJSON(Metacard metacard, List<AttributeType.AttributeFormat> dontInclude) throws CatalogTransformerException {
    if (metacard == null) {
        throw new CatalogTransformerException("Cannot transform null metacard.");
    }
    Map<String, Object> rootObject = new HashMap<>();
    Map<String, Object> properties = new HashMap<>();
    for (AttributeDescriptor ad : metacard.getMetacardType().getAttributeDescriptors()) {
        Attribute attribute = metacard.getAttribute(ad.getName());
        if (attribute != null) {
            Object value = convertAttribute(attribute, ad, dontInclude);
            if (value != null) {
                properties.put(attribute.getName(), value);
            }
        }
    }
    properties.put(METACARD_TYPE_PROPERTY_KEY, metacard.getMetacardType().getName());
    if (StringUtils.isNotBlank(metacard.getSourceId())) {
        properties.put(SOURCE_ID_PROPERTY, metacard.getSourceId());
    }
    rootObject.put("properties", properties);
    return rootObject;
}
Also used : HashMap(java.util.HashMap) Attribute(ddf.catalog.data.Attribute) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException)

Example 99 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class PptxInputTransformerTest method testBadCopy.

@Test(expected = CatalogTransformerException.class)
public void testBadCopy() throws IOException, CatalogTransformerException {
    IOException ioe = new IOException();
    try {
        PptxInputTransformer t = new PptxInputTransformer(inputTransformer);
        InputStream is = mock(InputStream.class);
        when(is.read(any())).thenThrow(ioe);
        t.transform(is);
    } catch (CatalogTransformerException e) {
        assertThat(e.getCause(), is(ioe));
        throw e;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) Test(org.junit.Test)

Example 100 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.

the class ResourceMetacardTransformer method transform.

@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
    LOGGER.trace("Entering resource ResourceMetacardTransformer.transform");
    if (!isValid(metacard)) {
        throw new CatalogTransformerException("Could not transform metacard to a resource because the metacard is not valid.");
    }
    if (StringUtils.isNotEmpty(metacard.getResourceSize())) {
        arguments.put(Metacard.RESOURCE_SIZE, metacard.getResourceSize());
    }
    String id = metacard.getId();
    LOGGER.debug("executing resource request with id '{}'", id);
    final ResourceRequest resourceRequest = new ResourceRequestById(id, arguments);
    ResourceResponse resourceResponse = null;
    String sourceName = metacard.getSourceId();
    if (StringUtils.isBlank(sourceName)) {
        sourceName = catalogFramework.getId();
    }
    String resourceUriAscii = "";
    if (metacard.getResourceURI() != null) {
        resourceUriAscii = metacard.getResourceURI().toASCIIString();
    }
    try {
        resourceResponse = catalogFramework.getResource(resourceRequest, sourceName);
    } catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
        throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
    }
    if (resourceResponse == null) {
        throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii));
    }
    Resource transformedContent = resourceResponse.getResource();
    MimeType mimeType = transformedContent.getMimeType();
    if (mimeType == null) {
        try {
            mimeType = new MimeType(DEFAULT_MIME_TYPE_STR);
            // There is no method to set the MIME type, so in order to set it to our default
            // one, we need to create a new object.
            transformedContent = new ResourceImpl(transformedContent.getInputStream(), mimeType, transformedContent.getName());
        } catch (MimeTypeParseException e) {
            throw new CatalogTransformerException("Could not create default mime type upon null mimeType, for default mime type '" + DEFAULT_MIME_TYPE_STR + "'.", e);
        }
    }
    LOGGER.debug("Found mime type: '{}' for product of metacard with id: '{}'.\nGetting associated resource from input stream. \n", mimeType, id);
    LOGGER.trace("Exiting resource transform for metacard id: '{}'", id);
    return transformedContent;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) Resource(ddf.catalog.resource.Resource) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) MimeType(javax.activation.MimeType) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ResourceRequest(ddf.catalog.operation.ResourceRequest) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Aggregations

CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)112 IOException (java.io.IOException)53 Metacard (ddf.catalog.data.Metacard)44 InputStream (java.io.InputStream)40 ByteArrayInputStream (java.io.ByteArrayInputStream)29 BinaryContent (ddf.catalog.data.BinaryContent)25 InputTransformer (ddf.catalog.transform.InputTransformer)21 Serializable (java.io.Serializable)21 HashMap (java.util.HashMap)21 Result (ddf.catalog.data.Result)16 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)15 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)14 ArrayList (java.util.ArrayList)13 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)12 Test (org.junit.Test)12 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)10 MimeType (javax.activation.MimeType)10 SourceResponse (ddf.catalog.operation.SourceResponse)9 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)8 List (java.util.List)8