Search in sources :

Example 21 with CatalogTransformerException

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

the class CatalogComponentTest method testTransformMetacardNoProducerInputTransformerRegistered.

@Test
public void testTransformMetacardNoProducerInputTransformerRegistered() throws Exception {
    LOGGER.debug("Running testTransformMetacard()");
    // Mock the MimeTypeToTransformerMapper and register it in the OSGi
    // Registry (PojoSR)
    MimeTypeToTransformerMapper matchingService = mock(MimeTypeToTransformerMapper.class);
    // bundleContext.registerService(
    // MimeTypeToTransformerMapper.class.getName(), matchingService, null );
    catalogComponent.setMimeTypeToTransformerMapper(matchingService);
    // Mock the MimeTypeToTransformerMapper returning empty list of
    // InputTransformers
    List list = new ArrayList<InputTransformer>();
    when(matchingService.findMatches(eq(InputTransformer.class), isA(MimeType.class))).thenReturn(list);
    // Send in sample XML as InputStream to InputTransformer
    InputStream input = IOUtils.toInputStream(xmlInput);
    // Get the InputTransformer registered with the ID associated with the
    // <from> node in the Camel route
    InputTransformer transformer = getTransformer("text/xml", "identity");
    assertNotNull("InputTransformer for text/xml;id=identity not found", transformer);
    // Attempt to transform the XML input into a Metacard
    try {
        transformer.transform(input);
        fail("Should have thrown a CatalogTransformerException");
    } catch (CatalogTransformerException e) {
        assertEquals("Did not find an InputTransformer for MIME Type [text/xml] and id [xml]", e.getMessage());
    }
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) InputTransformer(ddf.catalog.transform.InputTransformer) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 22 with CatalogTransformerException

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

the class OpenSearchEndpoint method executeQuery.

/**
 * Executes the OpenSearchQuery and formulates the response
 *
 * @param format - of the results in the response
 * @param query - the query to execute
 * @param ui -the ui information to use to format the results
 * @return the response on the query
 */
private Response executeQuery(String format, OpenSearchQuery query, UriInfo ui, Map<String, Serializable> properties) {
    Response response = null;
    String queryFormat = format;
    MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
    List<String> subscriptionList = queryParams.get(Constants.SUBSCRIPTION_KEY);
    LOGGER.trace("Attempting to execute query: {}", query);
    try {
        Map<String, Serializable> arguments = new HashMap<>();
        String organization = framework.getOrganization();
        String url = ui.getRequestUri().toString();
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("organization: {}", organization);
            LOGGER.trace("url: {}", url);
        }
        arguments.put("organization", organization);
        arguments.put("url", url);
        // interval
        if (CollectionUtils.isNotEmpty(subscriptionList)) {
            String subscription = subscriptionList.get(0);
            LOGGER.trace("Subscription: {}", subscription);
            arguments.put(Constants.SUBSCRIPTION_KEY, subscription);
            List<String> intervalList = queryParams.get(UPDATE_QUERY_INTERVAL);
            if (CollectionUtils.isNotEmpty(intervalList)) {
                arguments.put(UPDATE_QUERY_INTERVAL, intervalList.get(0));
            }
        }
        if (StringUtils.isEmpty(queryFormat)) {
            queryFormat = DEFAULT_FORMAT;
        }
        if (query.getFilter() != null) {
            QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), properties);
            QueryResponse queryResponse;
            LOGGER.trace("Sending query");
            queryResponse = framework.query(queryRequest);
            // pass in the format for the transform
            BinaryContent content = framework.transform(queryResponse, queryFormat, arguments);
            response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
        } else {
            // No query was specified
            QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), null);
            // Create a dummy QueryResponse with zero results
            QueryResponseImpl queryResponseQueue = new QueryResponseImpl(queryRequest, new ArrayList<>(), 0);
            // pass in the format for the transform
            BinaryContent content = framework.transform(queryResponseQueue, queryFormat, arguments);
            if (null != content) {
                response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
            }
        }
    } catch (UnsupportedQueryException ce) {
        LOGGER.debug("Unsupported query", ce);
        response = Response.status(Response.Status.BAD_REQUEST).entity(wrapStringInPreformattedTags("Unsupported query")).build();
    } catch (CatalogTransformerException e) {
        LOGGER.debug("Error transforming response", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Error transforming response")).build();
    } catch (FederationException e) {
        LOGGER.debug("Error executing query", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query")).build();
    } catch (SourceUnavailableException e) {
        LOGGER.debug("Error executing query because the underlying source was unavailable.", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query because the underlying source was unavailable.")).build();
    } catch (RuntimeException e) {
        // Account for any runtime exceptions and send back a server error
        // this prevents full stacktraces returning to the client
        // this allows for a graceful server error to be returned
        LOGGER.debug("RuntimeException on executing query", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("RuntimeException on executing query")).build();
    }
    return response;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Serializable(java.io.Serializable) QueryRequest(ddf.catalog.operation.QueryRequest) HashMap(java.util.HashMap) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent) FederationException(ddf.catalog.federation.FederationException) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse)

Example 23 with CatalogTransformerException

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

the class MetacardFactory method generateMetacard.

Metacard generateMetacard(String mimeTypeRaw, String id, String fileName, Path tmpContentPath) throws MetacardCreationException, MimeTypeParseException {
    Metacard generatedMetacard = null;
    MimeType mimeType = new MimeType(mimeTypeRaw);
    List<InputTransformer> listOfCandidates = mimeTypeToTransformerMapper.findMatches(InputTransformer.class, mimeType);
    List<String> stackTraceList = new ArrayList<>();
    LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
    for (InputTransformer candidate : listOfCandidates) {
        try (InputStream transformerStream = com.google.common.io.Files.asByteSource(tmpContentPath.toFile()).openStream()) {
            generatedMetacard = candidate.transform(transformerStream);
        } catch (RuntimeException | CatalogTransformerException | IOException e) {
            List<String> stackTraces = Arrays.asList(ExceptionUtils.getRootCauseStackTrace(e));
            stackTraceList.add(String.format("Transformer [%s] could not create metacard.", candidate));
            stackTraceList.addAll(stackTraces);
            LOGGER.debug("Transformer [{}] could not create metacard.", candidate, e);
        }
        if (generatedMetacard != null) {
            break;
        }
    }
    if (generatedMetacard == null) {
        throw new MetacardCreationException(String.format("Could not create metacard with mimeType %s : %s", mimeTypeRaw, StringUtils.join(stackTraceList, "\n")));
    }
    if (id != null) {
        generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, id));
    } else {
        generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, uuidGenerator.generateUuid()));
    }
    if (StringUtils.isBlank(generatedMetacard.getTitle())) {
        generatedMetacard.setAttribute(new AttributeImpl(Metacard.TITLE, fileName));
    }
    return generatedMetacard;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) 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) MimeType(javax.activation.MimeType) Metacard(ddf.catalog.data.Metacard) ArrayList(java.util.ArrayList) List(java.util.List)

Example 24 with CatalogTransformerException

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

the class CatalogComponent method createEndpoint.

/*
   * (non-Javadoc)
   *
   * @see org.apache.camel.impl.DefaultComponent#createEndpoint(java.lang.String,
   * java.lang.String, java.util.Map)
   */
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws CatalogTransformerException {
    LOGGER.trace("ENTERING: createEndpoint");
    LOGGER.debug("CatalogEndpoint: uri = {}, remaining = {}, parameters = {}", uri, remaining, parameters);
    String transformerId = getAndRemoveParameter(parameters, ID_PARAMETER, String.class);
    String mimeType = getAndRemoveParameter(parameters, MIME_TYPE_PARAMETER, String.class);
    LOGGER.debug("transformerId = {}", transformerId);
    Endpoint endpoint = new CatalogEndpoint(uri, this, transformerId, mimeType, remaining, catalogFramework, mimeTypeMapper);
    try {
        setProperties(endpoint, parameters);
    } catch (Exception e) {
        throw new CatalogTransformerException("Failed to create transformer endpoint", e);
    }
    LOGGER.trace("EXITING: createEndpoint");
    return endpoint;
}
Also used : Endpoint(org.apache.camel.Endpoint) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException)

Example 25 with CatalogTransformerException

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

the class CatalogComponentTest method testTransformMetacardNoMimeTypeToTransformerMapperRegistered.

@Test
public void testTransformMetacardNoMimeTypeToTransformerMapperRegistered() throws Exception {
    LOGGER.debug("Running testTransformMetacard_NoMimeTypeToTransformerMapperRegistered()");
    catalogComponent.setMimeTypeToTransformerMapper(null);
    // Mock a XML InputTransformer and register it in the OSGi Registry
    // (PojoSR)
    InputTransformer mockTransformer = getMockInputTransformer();
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(MimeTypeToTransformerMapper.ID_KEY, "xml");
    props.put(MimeTypeToTransformerMapper.MIME_TYPE_KEY, "text/xml");
    bundleContext.registerService(InputTransformer.class.getName(), mockTransformer, props);
    // Send in sample XML as InputStream to InputTransformer
    InputStream input = IOUtils.toInputStream(xmlInput);
    // Get the InputTransformer registered with the ID associated with the
    // <from> node in the Camel route
    InputTransformer transformer = getTransformer("text/xml", "identity");
    assertNotNull("InputTransformer for text/xml;id=identity not found", transformer);
    // Attempt to transform the XML input into a Metacard
    try {
        transformer.transform(input);
        fail("Should have thrown a CatalogTransformerException");
    } catch (CatalogTransformerException e) {
        assertEquals("Did not find a MimeTypeToTransformerMapper service", e.getMessage());
    }
}
Also used : Hashtable(java.util.Hashtable) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) InputTransformer(ddf.catalog.transform.InputTransformer) Test(org.junit.Test)

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