Search in sources :

Example 26 with SourceUnavailableException

use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.

the class CatalogFrameworkImplTest method testProviderRuntimeExceptionOnDeleteByID.

@Test(expected = IngestException.class)
public void testProviderRuntimeExceptionOnDeleteByID() throws IngestException {
    MockEventProcessor eventAdmin = new MockEventProcessor();
    // use exception provider instead of memory
    MockExceptionProvider provider = new MockExceptionProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<ContentType>(), true, null);
    MockMemoryStorageProvider storageProvider = new MockMemoryStorageProvider();
    CatalogFramework framework = this.createDummyCatalogFramework(provider, storageProvider, eventAdmin, true);
    List<String> ids = new ArrayList<String>();
    ids.add("1234");
    DeleteRequest request = new DeleteRequestImpl((String[]) ids.toArray(new String[ids.size()]));
    // expected to throw exception due to catalog provider
    try {
        framework.delete(request);
    } catch (SourceUnavailableException e) {
        fail();
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) ContentType(ddf.catalog.data.ContentType) MockMemoryStorageProvider(ddf.catalog.content.impl.MockMemoryStorageProvider) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) CatalogFramework(ddf.catalog.CatalogFramework) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) DeleteRequest(ddf.catalog.operation.DeleteRequest) Test(org.junit.Test)

Example 27 with SourceUnavailableException

use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.

the class CatalogFrameworkImplTest method testProviderRuntimeExceptionOnUpdateByID.

@Test(expected = IngestException.class)
public void testProviderRuntimeExceptionOnUpdateByID() throws IngestException {
    MockEventProcessor eventAdmin = new MockEventProcessor();
    // use exception provider instead of memory
    MockExceptionProvider provider = new MockExceptionProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<ContentType>(), true, null);
    CatalogFramework framework = this.createDummyCatalogFramework(provider, storageProvider, eventAdmin, true);
    List<Entry<Object, Metacard>> metacards = new ArrayList<Entry<Object, Metacard>>();
    HashMap<Object, Metacard> map = new HashMap<Object, Metacard>();
    // expected to throw exception due to catalog provider being unavailable
    try {
        MetacardImpl newCard = new MetacardImpl();
        newCard.setId(null);
        newCard.setResourceURI(new URI("uri:///1234"));
        map.put(Metacard.ID, newCard);
        metacards.addAll(map.entrySet());
        UpdateRequest update = new UpdateRequestImpl(null, Metacard.ID, null);
        framework.update(update);
    } catch (URISyntaxException e) {
        fail();
    } catch (SourceUnavailableException e) {
        fail();
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) ContentType(ddf.catalog.data.ContentType) HashMap(java.util.HashMap) UpdateRequest(ddf.catalog.operation.UpdateRequest) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Metacard(ddf.catalog.data.Metacard) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) CatalogFramework(ddf.catalog.CatalogFramework) Matchers.anyObject(org.mockito.Matchers.anyObject) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Test(org.junit.Test)

Example 28 with SourceUnavailableException

use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.

the class RESTEndpoint method addDocument.

/**
     * REST Post. Creates a new metadata entry in the catalog.
     *
     * @param message
     * @return
     */
@POST
@Consumes("multipart/*")
public Response addDocument(@Context HttpHeaders headers, @Context UriInfo requestUriInfo, @Context HttpServletRequest httpRequest, MultipartBody multipartBody, @QueryParam("transform") String transformerParam, InputStream message) {
    LOGGER.debug("POST");
    Response response;
    MimeType mimeType = getMimeType(headers);
    try {
        if (message != null) {
            CreateInfo createInfo = null;
            if (multipartBody != null) {
                List<Attachment> contentParts = multipartBody.getAllAttachments();
                if (contentParts != null && contentParts.size() > 0) {
                    createInfo = parseAttachments(contentParts, transformerParam);
                } else {
                    LOGGER.debug("No file contents attachment found");
                }
            }
            CreateResponse createResponse;
            if (createInfo == null) {
                CreateRequest createRequest = new CreateRequestImpl(generateMetacard(mimeType, null, message, transformerParam));
                createResponse = catalogFramework.create(createRequest);
            } else {
                CreateStorageRequest streamCreateRequest = new CreateStorageRequestImpl(Collections.singletonList(new IncomingContentItem(uuidGenerator, createInfo.getStream(), createInfo.getContentType(), createInfo.getFilename(), createInfo.getMetacard())), null);
                createResponse = catalogFramework.create(streamCreateRequest);
            }
            String id = createResponse.getCreatedMetacards().get(0).getId();
            LOGGER.debug("Create Response id [{}]", id);
            UriBuilder uriBuilder = requestUriInfo.getAbsolutePathBuilder().path("/" + id);
            ResponseBuilder responseBuilder = Response.created(uriBuilder.build());
            responseBuilder.header(Metacard.ID, id);
            response = responseBuilder.build();
            LOGGER.debug("Entry successfully saved, id: {}", id);
            if (INGEST_LOGGER.isInfoEnabled()) {
                INGEST_LOGGER.info("Entry successfully saved, id: {}", id);
            }
        } else {
            String errorMessage = "No content found, cannot do CREATE.";
            LOGGER.info(errorMessage);
            throw new ServerErrorException(errorMessage, Status.BAD_REQUEST);
        }
    } 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 ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
    } 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 ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
    } catch (MetacardCreationException | IngestException 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 ServerErrorException(exceptionMessage, Status.BAD_REQUEST);
    } finally {
        IOUtils.closeQuietly(message);
    }
    return response;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) InternalIngestException(ddf.catalog.source.InternalIngestException) CreateResponse(ddf.catalog.operation.CreateResponse) CreateRequest(ddf.catalog.operation.CreateRequest) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) MimeType(javax.activation.MimeType) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) CreateResponse(ddf.catalog.operation.CreateResponse) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException) InternalIngestException(ddf.catalog.source.InternalIngestException) UriBuilder(javax.ws.rs.core.UriBuilder) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 29 with SourceUnavailableException

use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.

the class RESTEndpoint method deleteDocument.

/**
     * REST Delete. Deletes a record from the catalog.
     *
     * @param id
     * @return
     */
@DELETE
@Path("/{id}")
public Response deleteDocument(@PathParam("id") String id, @Context HttpServletRequest httpRequest) {
    LOGGER.debug("DELETE");
    Response response;
    try {
        if (id != null) {
            DeleteRequestImpl deleteReq = new DeleteRequestImpl(id);
            catalogFramework.delete(deleteReq);
            response = Response.ok(id).build();
            LOGGER.debug("Attempting to delete Metacard with id: {}", id);
        } else {
            String errorMessage = "ID of entry not specified, cannot do DELETE.";
            LOGGER.info(errorMessage);
            throw new ServerErrorException(errorMessage, Status.BAD_REQUEST);
        }
    } catch (SourceUnavailableException ce) {
        String exceptionMessage = "Could not delete entry from catalog since the source is unavailable: ";
        LOGGER.info(exceptionMessage, ce);
        throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
    } catch (InternalIngestException e) {
        String exceptionMessage = "Error deleting entry from catalog: ";
        LOGGER.info(exceptionMessage, e);
        throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
    } catch (IngestException e) {
        String exceptionMessage = "Error deleting entry from catalog: ";
        LOGGER.info(exceptionMessage, e);
        throw new ServerErrorException(exceptionMessage, Status.BAD_REQUEST);
    }
    return response;
}
Also used : SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) CreateResponse(ddf.catalog.operation.CreateResponse) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) InternalIngestException(ddf.catalog.source.InternalIngestException) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) IngestException(ddf.catalog.source.IngestException) InternalIngestException(ddf.catalog.source.InternalIngestException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 30 with SourceUnavailableException

use of ddf.catalog.source.SourceUnavailableException in project ddf by codice.

the class RESTEndpoint method getHeaders.

/**
     * REST Head. Returns headers only.  Primarily used to let the client know that range requests (though limited)
     * are accepted.
     *
     * @param sourceid
     * @param id
     * @param uriInfo
     * @param httpRequest
     * @return
     */
@HEAD
@Path("/sources/{sourceid}/{id}")
public Response getHeaders(@PathParam("sourceid") String sourceid, @PathParam("id") String id, @Context UriInfo uriInfo, @Context HttpServletRequest httpRequest) {
    Response response;
    Response.ResponseBuilder responseBuilder;
    QueryResponse queryResponse;
    Metacard card = null;
    LOGGER.trace("getHeaders");
    URI absolutePath = uriInfo.getAbsolutePath();
    MultivaluedMap<String, String> map = uriInfo.getQueryParameters();
    if (id != null) {
        LOGGER.debug("Got id: {}", id);
        LOGGER.debug("Map of query parameters: \n{}", map.toString());
        Map<String, Serializable> convertedMap = convert(map);
        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<String>();
                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) {
                throw new ServerErrorException("Unable to retrieve requested metacard.", Status.NOT_FOUND);
            }
            LOGGER.debug("Calling transform.");
            final BinaryContent content = catalogFramework.transform(card, transformer, convertedMap);
            LOGGER.debug("Read and transform complete, preparing response.");
            responseBuilder = Response.noContent();
            // Add the Accept-ranges header to let the client know that we accept ranges in bytes
            responseBuilder.header(HEADER_ACCEPT_RANGES, BYTES);
            String filename = null;
            if (content instanceof Resource) {
                // If we got a resource, we can extract the filename.
                filename = ((Resource) content).getName();
            } else {
                String fileExtension = getFileExtensionForMimeType(content.getMimeTypeValue());
                if (StringUtils.isNotBlank(fileExtension)) {
                    filename = id + fileExtension;
                }
            }
            if (StringUtils.isNotBlank(filename)) {
                LOGGER.debug("filename: {}", filename);
                responseBuilder.header(HEADER_CONTENT_DISPOSITION, "inline; filename=\"" + filename + "\"");
            }
            long size = content.getSize();
            if (size > 0) {
                responseBuilder.header(HEADER_CONTENT_LENGTH, size);
            }
            response = responseBuilder.build();
        } catch (FederationException e) {
            String exceptionMessage = "READ failed due to unexpected exception: ";
            LOGGER.info(exceptionMessage, e);
            throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
        } catch (CatalogTransformerException e) {
            String exceptionMessage = "Unable to transform Metacard.  Try different transformer: ";
            LOGGER.info(exceptionMessage, e);
            throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
        } catch (SourceUnavailableException e) {
            String exceptionMessage = "Cannot obtain query results because source is unavailable: ";
            LOGGER.info(exceptionMessage, e);
            throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
        } catch (UnsupportedQueryException e) {
            String exceptionMessage = "Specified query is unsupported.  Change query and resubmit: ";
            LOGGER.info(exceptionMessage, e);
            throw new ServerErrorException(exceptionMessage, Status.BAD_REQUEST);
        // 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 ServerErrorException(e, Status.BAD_REQUEST);
        }
    } else {
        throw new ServerErrorException("No ID specified.", Status.BAD_REQUEST);
    }
    return response;
}
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) URI(java.net.URI) Result(ddf.catalog.data.Result) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Resource(ddf.catalog.resource.Resource) FederationException(ddf.catalog.federation.FederationException) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) CreateResponse(ddf.catalog.operation.CreateResponse) Metacard(ddf.catalog.data.Metacard) Filter(org.opengis.filter.Filter) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) QueryResponse(ddf.catalog.operation.QueryResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Path(javax.ws.rs.Path) HEAD(javax.ws.rs.HEAD)

Aggregations

SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)63 FederationException (ddf.catalog.federation.FederationException)27 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)26 QueryResponse (ddf.catalog.operation.QueryResponse)22 IngestException (ddf.catalog.source.IngestException)22 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)21 Metacard (ddf.catalog.data.Metacard)20 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)20 CatalogFramework (ddf.catalog.CatalogFramework)18 QueryImpl (ddf.catalog.operation.impl.QueryImpl)18 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)17 CreateResponse (ddf.catalog.operation.CreateResponse)16 ContentType (ddf.catalog.data.ContentType)14 QueryRequest (ddf.catalog.operation.QueryRequest)14 HashMap (java.util.HashMap)14 Filter (org.opengis.filter.Filter)12 Result (ddf.catalog.data.Result)11 CreateRequestImpl (ddf.catalog.operation.impl.CreateRequestImpl)11 SourceDescriptor (ddf.catalog.source.SourceDescriptor)10