Search in sources :

Example 61 with CatalogTransformerException

use of ddf.catalog.transform.CatalogTransformerException 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 62 with CatalogTransformerException

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

the class MpegTsInputTransformerTest method setup.

@Before
public void setup() throws IOException, CatalogTransformerException {
    metacardTypes = Collections.singletonList(mock(MetacardType.class));
    stanag4609Processor = mock(Stanag4609Processor.class);
    klvHandlerFactory = mock(KlvHandlerFactory.class);
    defaultKlvHandler = mock(KlvHandler.class);
    streamParser = mock(Stanag4609TransportStreamParser.class);
    metacard = new MetacardImpl();
    inputTransformer = mock(InputTransformer.class);
    stanagParserFactory = mock(StanagParserFactory.class);
    klvProcessor = mock(KlvProcessor.class);
    when(inputTransformer.transform(any(), any())).thenReturn(metacard);
    when(stanagParserFactory.createParser(any())).thenReturn(() -> {
        try {
            return streamParser.parse();
        } catch (Exception e) {
            throw new Stanag4609ParseException(e);
        }
    });
}
Also used : Stanag4609TransportStreamParser(org.codice.alliance.libs.stanag4609.Stanag4609TransportStreamParser) KlvHandlerFactory(org.codice.alliance.libs.klv.KlvHandlerFactory) KlvHandler(org.codice.alliance.libs.klv.KlvHandler) StanagParserFactory(org.codice.alliance.libs.klv.StanagParserFactory) Stanag4609Processor(org.codice.alliance.libs.klv.Stanag4609Processor) Stanag4609ParseException(org.codice.alliance.libs.klv.Stanag4609ParseException) InputTransformer(ddf.catalog.transform.InputTransformer) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Stanag4609ParseException(org.codice.alliance.libs.klv.Stanag4609ParseException) IOException(java.io.IOException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) KlvProcessor(org.codice.alliance.libs.klv.KlvProcessor) SecurityClassificationKlvProcessor(org.codice.alliance.libs.klv.SecurityClassificationKlvProcessor) Before(org.junit.Before)

Example 63 with CatalogTransformerException

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

the class ConfluenceInputTransformer method getJsonObject.

private Map<String, Object> getJsonObject(InputStream stream) throws CatalogTransformerException {
    String jsonString = null;
    try {
        jsonString = IOUtils.toString(stream, StandardCharsets.UTF_8);
        Map<String, Object> rootObject = GSON.fromJson(jsonString, MAP_STRING_TO_OBJECT_TYPE);
        LOGGER.debug(jsonString);
        return rootObject;
    } catch (IOException | RuntimeException re) {
        throw new CatalogTransformerException("Invalid json. Could not parse: " + jsonString, re);
    }
}
Also used : CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException)

Example 64 with CatalogTransformerException

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

the class ConfluenceSource method query.

@Override
public SourceResponse query(QueryRequest request) throws UnsupportedQueryException {
    Query query = request.getQuery();
    ConfluenceFilterDelegate confluenceDelegate = new ConfluenceFilterDelegate();
    String cql = filterAdapter.adapt(query, confluenceDelegate);
    if (!confluenceDelegate.isConfluenceQuery() || (StringUtils.isEmpty(cql) && (confluenceSpaces.isEmpty() || !confluenceDelegate.isWildCardQuery()))) {
        return new SourceResponseImpl(request, Collections.emptyList());
    }
    cql = getSortedQuery(query.getSortBy(), getSpaceQuery(cql));
    LOGGER.debug(cql);
    String finalExpandedSections = expandedSections;
    if (includePageContent && StringUtils.isNotBlank(bodyExpansion)) {
        finalExpandedSections += "," + bodyExpansion;
    }
    SearchResource confluence = getClientFactory().getClient();
    String cqlContext = null;
    String excerpt = null;
    Response confluenceResponse = confluence.search(cql, cqlContext, excerpt, finalExpandedSections, query.getStartIndex() - 1, query.getPageSize(), includeArchivedSpaces);
    InputStream stream = null;
    Object entityObj = confluenceResponse.getEntity();
    if (entityObj != null) {
        stream = (InputStream) entityObj;
    }
    if (Response.Status.OK.getStatusCode() != confluenceResponse.getStatus()) {
        String error = "";
        try {
            if (stream != null) {
                error = IOUtils.toString(stream);
            }
        } catch (IOException ioe) {
            LOGGER.debug("Could not convert error message to a string for output.", ioe);
        }
        throw new UnsupportedQueryException(String.format("Received error code from remote source (status %s ): %s", confluenceResponse.getStatus(), error));
    }
    try {
        List<Result> results = transformer.transformConfluenceResponse(stream, bodyExpansion).stream().map(this::getUpdatedResult).collect(Collectors.toList());
        return new SourceResponseImpl(request, results);
    } catch (CatalogTransformerException e) {
        throw new UnsupportedQueryException("Exception processing results from Confluence");
    }
}
Also used : Query(ddf.catalog.operation.Query) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) InputStream(java.io.InputStream) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) SearchResource(org.codice.ddf.confluence.api.SearchResource) Result(ddf.catalog.data.Result) Response(javax.ws.rs.core.Response) ResourceResponse(ddf.catalog.operation.ResourceResponse) SourceResponse(ddf.catalog.operation.SourceResponse)

Example 65 with CatalogTransformerException

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

the class TransformerConsumer method transform.

protected <T> T transform(Class<T> objClass, Object upstreamResponse, Map<String, Serializable> arguments) throws CatalogTransformerException {
    LOGGER.debug("ENTERING: transform");
    T content = null;
    Exchange exchange = endpoint.createExchange();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("exchange pattern = {}", exchange.getPattern());
    }
    // Copy SourceResponse into message body and arguments into headers
    Message in = exchange.getIn();
    in.setBody(upstreamResponse);
    for (Entry<String, Serializable> entry : arguments.entrySet()) {
        in.setHeader(entry.getKey(), entry.getValue());
    }
    try {
        // Send message to next processor in the route. This is configured to be a blocking
        // call and will wait until the entire route completes and returns the result.
        getProcessor().process(exchange);
        LOGGER.debug("AFTER process(exchange)");
        // Entire route has completed - get the output from the last node in the route.
        content = exchange.getMessage().getBody(objClass);
        // and it cannot be converted
        if (null == content) {
            LOGGER.debug("Unable to create {} - throwing exception", objClass.getName());
            throw new CatalogTransformerException("Unable to create t" + objClass.getName());
        }
    } catch (Exception e) {
        Exception e2 = exchange.getException();
        if (e2 instanceof CatalogTransformerException) {
            throw (CatalogTransformerException) e2;
        }
        getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
    } finally {
        // log exception if an exception occurred and was not handled
        if (exchange.getException() != null) {
            getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
        }
    }
    LOGGER.debug("EXITING: transform");
    return content;
}
Also used : Exchange(org.apache.camel.Exchange) Serializable(java.io.Serializable) Message(org.apache.camel.Message) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException)

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