Search in sources :

Example 26 with CatalogTransformerException

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

the class CatalogComponentTest method getMockInputTransformer.

/**
 * Mock an InputTransformer, returning a canned Metacard when the transform() method is invoked on
 * the InputTransformer.
 *
 * @return
 */
protected InputTransformer getMockInputTransformer() {
    InputTransformer inputTransformer = mock(InputTransformer.class);
    Metacard generatedMetacard = getSimpleMetacard();
    try {
        when(inputTransformer.transform(isA(InputStream.class))).thenReturn(generatedMetacard);
        when(inputTransformer.transform(isA(InputStream.class), isA(String.class))).thenReturn(generatedMetacard);
    } catch (IOException e) {
        LOGGER.debug("IOException", e);
    } catch (CatalogTransformerException e) {
        LOGGER.debug("CatalogTransformerException", e);
    }
    return inputTransformer;
}
Also used : Metacard(ddf.catalog.data.Metacard) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer)

Example 27 with CatalogTransformerException

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

the class TransformerConsumer method doStop.

@Override
protected void doStop() throws CatalogTransformerException {
    LOGGER.debug("ENTERING: doStop");
    if (registration != null) {
        LOGGER.debug("Unregistering");
        registration.unregister();
    }
    try {
        super.doStop();
    } catch (Exception e) {
        throw new CatalogTransformerException("Failed to stop Transformer Consumer", e);
    }
    LOGGER.debug("EXITING: doStop");
}
Also used : CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException)

Example 28 with CatalogTransformerException

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

the class AbstractCatalogService method getDocument.

@Override
public BinaryContent getDocument(String encodedSourceId, String encodedId, String transformerParam, URI absolutePath, MultivaluedMap<String, String> queryParameters, HttpServletRequest httpRequest) throws CatalogServiceException, DataUsageLimitExceededException, InternalServerErrorException {
    QueryResponse queryResponse;
    Metacard card = null;
    LOGGER.trace("GET");
    if (encodedId != null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Got id: {}", LogSanitizer.sanitize(encodedId));
            LOGGER.debug("Got service: {}", LogSanitizer.sanitize(transformerParam));
            LOGGER.debug("Map of query parameters: \n{}", LogSanitizer.sanitize(queryParameters));
        }
        Map<String, Serializable> convertedMap = convert(queryParameters);
        convertedMap.put("url", absolutePath.toString());
        LOGGER.debug("Map converted, retrieving product.");
        // default to xml if no transformer specified
        try {
            String id = URLDecoder.decode(encodedId, CharEncoding.UTF_8);
            String transformer = DEFAULT_METACARD_TRANSFORMER;
            if (transformerParam != null) {
                transformer = transformerParam;
            }
            Filter filter = getFilterBuilder().attribute(Metacard.ID).is().equalTo().text(id);
            Collection<String> sources = null;
            if (encodedSourceId != null) {
                String sourceid = URLDecoder.decode(encodedSourceId, CharEncoding.UTF_8);
                sources = new ArrayList<>();
                sources.add(sourceid);
            }
            QueryRequestImpl request = new QueryRequestImpl(new QueryImpl(filter), sources);
            request.setProperties(convertedMap);
            queryResponse = catalogFramework.query(request, null);
            // pull the metacard out of the blocking queue
            List<Result> results = queryResponse.getResults();
            // return null if timeout elapsed)
            if (results != null && !results.isEmpty()) {
                card = results.get(0).getMetacard();
            }
            if (card == null) {
                return null;
            }
            // Check for Range header set the value in the map appropriately so that the
            // catalogFramework
            // can take care of the skipping
            long bytesToSkip = getRangeStart(httpRequest);
            if (bytesToSkip > 0) {
                LOGGER.debug("Bytes to skip: {}", bytesToSkip);
                convertedMap.put(BYTES_TO_SKIP, bytesToSkip);
            }
            LOGGER.debug("Calling transform.");
            final BinaryContent content = catalogFramework.transform(card, transformer, convertedMap);
            LOGGER.debug("Read and transform complete, preparing response.");
            return content;
        } catch (FederationException e) {
            String exceptionMessage = "READ failed due to unexpected exception: ";
            LOGGER.info(exceptionMessage, e);
            throw new InternalServerErrorException(exceptionMessage);
        } catch (CatalogTransformerException e) {
            String exceptionMessage = "Unable to transform Metacard.  Try different transformer: ";
            LOGGER.info(exceptionMessage, e);
            throw new InternalServerErrorException(exceptionMessage);
        } catch (SourceUnavailableException e) {
            String exceptionMessage = "Cannot obtain query results because source is unavailable: ";
            LOGGER.info(exceptionMessage, e);
            throw new InternalServerErrorException(exceptionMessage);
        } catch (UnsupportedQueryException e) {
            String errorMessage = "Specified query is unsupported.  Change query and resubmit: ";
            LOGGER.info(errorMessage, e);
            throw new CatalogServiceException(errorMessage);
        } catch (DataUsageLimitExceededException e) {
            String errorMessage = "Unable to process request. Data usage limit exceeded: ";
            LOGGER.debug(errorMessage, e);
            throw new DataUsageLimitExceededException(errorMessage);
        } catch (OAuthPluginException e) {
            Map<String, String> parameters = e.getParameters();
            String url = constructUrl(httpRequest, e.getBaseUrl(), parameters);
            if (url != null) {
                parameters.put(REDIRECT_URI, url);
                throw new OAuthPluginException(e.getSourceId(), url, e.getBaseUrl(), parameters, e.getErrorType());
            }
            throw e;
        // The catalog framework will throw this if any of the transformers blow up.
        // We need to catch this exception here or else execution will return to CXF and
        // we'll lose this message and end up with a huge stack trace in a GUI or whatever
        // else is connected to this endpoint
        } catch (RuntimeException | UnsupportedEncodingException e) {
            String exceptionMessage = "Unknown error occurred while processing request.";
            LOGGER.info(exceptionMessage, e);
            throw new InternalServerErrorException(exceptionMessage);
        }
    } else {
        throw new CatalogServiceException("No ID specified.");
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Serializable(java.io.Serializable) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent) Result(ddf.catalog.data.Result) QueryImpl(ddf.catalog.operation.impl.QueryImpl) DataUsageLimitExceededException(ddf.catalog.resource.DataUsageLimitExceededException) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FederationException(ddf.catalog.federation.FederationException) Metacard(ddf.catalog.data.Metacard) OAuthPluginException(ddf.catalog.plugin.OAuthPluginException) Filter(org.opengis.filter.Filter) QueryResponse(ddf.catalog.operation.QueryResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 29 with CatalogTransformerException

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

the class AbstractCatalogService method getHeaders.

@Override
public BinaryContent getHeaders(String sourceid, String id, URI absolutePath, MultivaluedMap<String, String> queryParameters) throws CatalogServiceException {
    QueryResponse queryResponse;
    Metacard card = null;
    LOGGER.trace("getHeaders");
    if (id != null) {
        LOGGER.debug("Got id: {}", LogSanitizer.sanitize(id));
        LOGGER.debug("Map of query parameters: \n{}", LogSanitizer.sanitize(queryParameters));
        Map<String, Serializable> convertedMap = convert(queryParameters);
        convertedMap.put("url", absolutePath.toString());
        LOGGER.debug("Map converted, retrieving product.");
        // default to xml if no transformer specified
        try {
            String transformer = DEFAULT_METACARD_TRANSFORMER;
            Filter filter = getFilterBuilder().attribute(Metacard.ID).is().equalTo().text(id);
            Collection<String> sources = null;
            if (sourceid != null) {
                sources = new ArrayList<>();
                sources.add(sourceid);
            }
            QueryRequestImpl request = new QueryRequestImpl(new QueryImpl(filter), sources);
            request.setProperties(convertedMap);
            queryResponse = catalogFramework.query(request, null);
            // pull the metacard out of the blocking queue
            List<Result> results = queryResponse.getResults();
            // return null if timeout elapsed)
            if (results != null && !results.isEmpty()) {
                card = results.get(0).getMetacard();
            }
            if (card == null) {
                return null;
            }
            LOGGER.debug("Calling transform.");
            final BinaryContent content = catalogFramework.transform(card, transformer, convertedMap);
            LOGGER.debug("Read and transform complete, preparing response.");
            return content;
        } catch (FederationException e) {
            String exceptionMessage = "READ failed due to unexpected exception: ";
            LOGGER.info(exceptionMessage, e);
            throw new InternalServerErrorException(exceptionMessage);
        } catch (CatalogTransformerException e) {
            String exceptionMessage = "Unable to transform Metacard.  Try different transformer: ";
            LOGGER.info(exceptionMessage, e);
            throw new InternalServerErrorException(exceptionMessage);
        } catch (SourceUnavailableException e) {
            String exceptionMessage = "Cannot obtain query results because source is unavailable: ";
            LOGGER.info(exceptionMessage, e);
            throw new InternalServerErrorException(exceptionMessage);
        } catch (UnsupportedQueryException e) {
            String errorMessage = "Specified query is unsupported.  Change query and resubmit: ";
            LOGGER.info(errorMessage, e);
            throw new CatalogServiceException(errorMessage);
        // The catalog framework will throw this if any of the transformers blow up. We need to
        // catch this exception
        // here or else execution will return to CXF and we'll lose this message and end up with
        // a huge stack trace
        // in a GUI or whatever else is connected to this endpoint
        } catch (IllegalArgumentException e) {
            throw new CatalogServiceException(e.getMessage());
        }
    } else {
        throw new CatalogServiceException("No ID specified.");
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Serializable(java.io.Serializable) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent) FederationException(ddf.catalog.federation.FederationException) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) QueryResponse(ddf.catalog.operation.QueryResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) InternalServerErrorException(javax.ws.rs.InternalServerErrorException)

Example 30 with CatalogTransformerException

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

the class CswSubscriptionEndpoint method getMetacards.

private List<Metacard> getMetacards(GetRecordsResponseType recordsResponse) throws CswException {
    try {
        InputTransformer transformer = inputTransformerManager.getTransformerBySchema(recordsResponse.getSearchResults().getRecordSchema());
        List<Metacard> metacards = new ArrayList<>();
        for (Object result : recordsResponse.getSearchResults().getAny()) {
            if (result instanceof Node) {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                XML_UTILS.transform((Node) result, new TransformerProperties(), new StreamResult(outputStream));
                InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
                metacards.add(transformer.transform(is));
            }
        }
        return metacards;
    } catch (IOException | CatalogTransformerException e) {
        String msg = "Could not parse SearchResults in getRecordsResponse";
        LOGGER.debug(msg, e);
        throw new CswException(msg, e);
    }
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) TransformerProperties(org.codice.ddf.platform.util.TransformerProperties) ArrayList(java.util.ArrayList) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream)

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