Search in sources :

Example 1 with CatalogServiceException

use of org.codice.ddf.rest.api.CatalogServiceException in project ddf by codice.

the class AbstractCatalogService method deleteDocument.

@Override
public void deleteDocument(String id) throws CatalogServiceException {
    LOGGER.debug("DELETE");
    try {
        if (id != null) {
            DeleteRequestImpl deleteReq = new DeleteRequestImpl(new HtmlPolicyBuilder().toFactory().sanitize(id));
            catalogFramework.delete(deleteReq);
            LOGGER.debug("Attempting to delete Metacard with id: {}", LogSanitizer.sanitize(id));
        } else {
            String errorMessage = "ID of entry not specified, cannot do DELETE.";
            LOGGER.info(errorMessage);
            throw new CatalogServiceException(errorMessage);
        }
    } catch (SourceUnavailableException ce) {
        String exceptionMessage = "Could not delete entry from catalog since the source is unavailable: ";
        LOGGER.info(exceptionMessage, ce);
        throw new InternalServerErrorException(exceptionMessage);
    } catch (InternalIngestException e) {
        String exceptionMessage = "Error deleting entry from catalog: ";
        LOGGER.info(exceptionMessage, e);
        throw new InternalServerErrorException(exceptionMessage);
    } catch (IngestException e) {
        String errorMessage = "Error deleting entry from catalog: ";
        LOGGER.info(errorMessage, e);
        throw new CatalogServiceException(errorMessage);
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) InternalIngestException(ddf.catalog.source.InternalIngestException) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) IngestException(ddf.catalog.source.IngestException) InternalIngestException(ddf.catalog.source.InternalIngestException) HtmlPolicyBuilder(org.owasp.html.HtmlPolicyBuilder)

Example 2 with CatalogServiceException

use of org.codice.ddf.rest.api.CatalogServiceException 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 3 with CatalogServiceException

use of org.codice.ddf.rest.api.CatalogServiceException in project ddf by codice.

the class AbstractCatalogService method updateDocument.

private void updateDocument(Map.Entry<AttachmentInfo, Metacard> attachmentInfoAndMetacard, String id, List<String> contentTypeList, String transformerParam, InputStream message) throws CatalogServiceException {
    try {
        MimeType mimeType = getMimeType(contentTypeList);
        if (attachmentInfoAndMetacard == null) {
            UpdateRequest updateRequest = new UpdateRequestImpl(id, generateMetacard(mimeType, id, message, transformerParam));
            catalogFramework.update(updateRequest);
        } else {
            UpdateStorageRequest streamUpdateRequest = new UpdateStorageRequestImpl(Collections.singletonList(new IncomingContentItem(id, attachmentInfoAndMetacard.getKey().getStream(), attachmentInfoAndMetacard.getKey().getContentType(), attachmentInfoAndMetacard.getKey().getFilename(), 0, attachmentInfoAndMetacard.getValue())), null);
            catalogFramework.update(streamUpdateRequest);
        }
        LOGGER.debug("Metacard {} updated.", LogSanitizer.sanitize(id));
    } catch (SourceUnavailableException e) {
        String exceptionMessage = "Cannot update catalog entry: Source is unavailable: ";
        LOGGER.info(exceptionMessage, e);
        throw new InternalServerErrorException(exceptionMessage);
    } catch (InternalIngestException e) {
        String exceptionMessage = "Error cataloging updated metadata: ";
        LOGGER.info(exceptionMessage, e);
        throw new InternalServerErrorException(exceptionMessage);
    } catch (MetacardCreationException | IngestException e) {
        String errorMessage = "Error cataloging updated metadata: ";
        LOGGER.info(errorMessage, e);
        throw new CatalogServiceException(errorMessage);
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) UpdateStorageRequestImpl(ddf.catalog.content.operation.impl.UpdateStorageRequestImpl) InternalIngestException(ddf.catalog.source.InternalIngestException) UpdateRequest(ddf.catalog.operation.UpdateRequest) MimeType(javax.activation.MimeType) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) IngestException(ddf.catalog.source.IngestException) InternalIngestException(ddf.catalog.source.InternalIngestException) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl)

Example 4 with CatalogServiceException

use of org.codice.ddf.rest.api.CatalogServiceException 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 5 with CatalogServiceException

use of org.codice.ddf.rest.api.CatalogServiceException in project ddf by codice.

the class AbstractCatalogService method addDocument.

private String addDocument(Map.Entry<AttachmentInfo, Metacard> attachmentInfoAndMetacard, List<String> contentTypeList, String transformerParam, InputStream message) throws CatalogServiceException {
    try {
        LOGGER.debug("POST");
        MimeType mimeType = getMimeType(contentTypeList);
        CreateResponse createResponse;
        if (attachmentInfoAndMetacard == null) {
            CreateRequest createRequest = new CreateRequestImpl(generateMetacard(mimeType, null, message, transformerParam));
            createResponse = catalogFramework.create(createRequest);
        } else {
            String id = attachmentInfoAndMetacard.getValue() == null ? null : attachmentInfoAndMetacard.getValue().getId();
            if (id == null) {
                id = uuidGenerator.generateUuid();
            }
            CreateStorageRequest streamCreateRequest = new CreateStorageRequestImpl(Collections.singletonList(new IncomingContentItem(id, attachmentInfoAndMetacard.getKey().getStream(), attachmentInfoAndMetacard.getKey().getContentType(), attachmentInfoAndMetacard.getKey().getFilename(), 0L, attachmentInfoAndMetacard.getValue())), null);
            createResponse = catalogFramework.create(streamCreateRequest);
        }
        String id = createResponse.getCreatedMetacards().get(0).getId();
        LOGGER.debug("Create Response id [{}]", id);
        LOGGER.debug("Entry successfully saved, id: {}", id);
        if (INGEST_LOGGER.isInfoEnabled()) {
            INGEST_LOGGER.info("Entry successfully saved, id: {}", id);
        }
        return id;
    } catch (SourceUnavailableException e) {
        String exceptionMessage = "Cannot create catalog entry because source is unavailable: ";
        LOGGER.info(exceptionMessage, e);
        // Catalog framework logs these exceptions to the ingest logger so we don't have to.
        throw new InternalServerErrorException(exceptionMessage);
    } catch (InternalIngestException e) {
        String exceptionMessage = "Error while storing entry in catalog: ";
        LOGGER.info(exceptionMessage, e);
        // Catalog framework logs these exceptions to the ingest logger so we don't have to.
        throw new InternalServerErrorException(exceptionMessage);
    } catch (MetacardCreationException | IngestException e) {
        String errorMessage = "Error while storing entry in catalog: ";
        LOGGER.info(errorMessage, e);
        // Catalog framework logs these exceptions to the ingest logger so we don't have to.
        throw new CatalogServiceException(errorMessage);
    } finally {
        IOUtils.closeQuietly(message);
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) InternalIngestException(ddf.catalog.source.InternalIngestException) CreateResponse(ddf.catalog.operation.CreateResponse) CreateRequest(ddf.catalog.operation.CreateRequest) MimeType(javax.activation.MimeType) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) IngestException(ddf.catalog.source.IngestException) InternalIngestException(ddf.catalog.source.InternalIngestException) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest)

Aggregations

CatalogServiceException (org.codice.ddf.rest.api.CatalogServiceException)12 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)8 BinaryContent (ddf.catalog.data.BinaryContent)6 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)5 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)4 Metacard (ddf.catalog.data.Metacard)3 MetacardCreationException (ddf.catalog.data.MetacardCreationException)3 IngestException (ddf.catalog.source.IngestException)3 InternalIngestException (ddf.catalog.source.InternalIngestException)3 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)3 MimeType (javax.activation.MimeType)3 Path (javax.ws.rs.Path)3 Response (javax.ws.rs.core.Response)3 CatalogFramework (ddf.catalog.CatalogFramework)2 CreateStorageRequest (ddf.catalog.content.operation.CreateStorageRequest)2 Result (ddf.catalog.data.Result)2 FederationException (ddf.catalog.federation.FederationException)2 CreateRequest (ddf.catalog.operation.CreateRequest)2 QueryResponse (ddf.catalog.operation.QueryResponse)2 QueryImpl (ddf.catalog.operation.impl.QueryImpl)2