Search in sources :

Example 31 with FederationException

use of ddf.catalog.federation.FederationException in project ddf by codice.

the class CatalogFrameworkImplTest method testNullQuery.

/**
     * Tests that the framework properly throws a catalog exception when the query being passed in
     * is null.
     *
     * @throws UnsupportedQueryException
     */
@Test(expected = UnsupportedQueryException.class)
public void testNullQuery() throws UnsupportedQueryException {
    boolean isAvailable = false;
    CatalogProvider provider = new MockMemoryProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<ContentType>(), isAvailable, new Date());
    CatalogFramework framework = this.createDummyCatalogFramework(provider, storageProvider, null, true);
    try {
        framework.query(null);
    } catch (FederationException e) {
        fail();
    } catch (SourceUnavailableException e) {
        fail();
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) ContentType(ddf.catalog.data.ContentType) CatalogProvider(ddf.catalog.source.CatalogProvider) CatalogFramework(ddf.catalog.CatalogFramework) FederationException(ddf.catalog.federation.FederationException) Date(java.util.Date) Test(org.junit.Test)

Example 32 with FederationException

use of ddf.catalog.federation.FederationException in project ddf by codice.

the class RESTEndpoint method getDocument.

/**
     * REST Get. Retrieves the metadata entry specified by the id from the federated source
     * specified by sourceid. Transformer argument is optional, but is used to specify what format
     * the data should be returned.
     *
     * @param encodedSourceId
     * @param encodedId
     * @param transformerParam
     * @param uriInfo
     * @return
     */
@GET
@Path("/sources/{sourceid}/{id}")
public Response getDocument(@Encoded @PathParam("sourceid") String encodedSourceId, @Encoded @PathParam("id") String encodedId, @QueryParam("transform") String transformerParam, @Context UriInfo uriInfo, @Context HttpServletRequest httpRequest) {
    Response response = null;
    Response.ResponseBuilder responseBuilder;
    QueryResponse queryResponse;
    Metacard card = null;
    LOGGER.trace("GET");
    URI absolutePath = uriInfo.getAbsolutePath();
    MultivaluedMap<String, String> map = uriInfo.getQueryParameters();
    if (encodedId != null) {
        LOGGER.debug("Got id: {}", encodedId);
        LOGGER.debug("Got service: {}", transformerParam);
        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 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<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);
            }
            // 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: {}", String.valueOf(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.");
            responseBuilder = Response.ok(content.getInputStream(), content.getMimeTypeValue());
            // 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);
        } catch (DataUsageLimitExceededException e) {
            String exceptionMessage = "Unable to process request. Data usage limit exceeded: ";
            LOGGER.debug(exceptionMessage, e);
            throw new ServerErrorException(exceptionMessage, Status.REQUEST_ENTITY_TOO_LARGE);
        // 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 ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
        }
    } 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) DataUsageLimitExceededException(ddf.catalog.resource.DataUsageLimitExceededException) Resource(ddf.catalog.resource.Resource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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) GET(javax.ws.rs.GET)

Example 33 with FederationException

use of ddf.catalog.federation.FederationException in project ddf by codice.

the class ExceptionsTest method testFederationException.

@Test
public void testFederationException() {
    FederationException fe = new FederationException();
    assertNotNull(fe);
    fe = new FederationException(msg);
    assertEquals(fe.getMessage(), msg);
    fe = new FederationException(testCause);
    assertEquals(fe.getCause(), testCause);
    fe = new FederationException(msg, testCause);
    assertEquals(fe.getMessage(), msg);
    assertEquals(fe.getCause(), testCause);
}
Also used : FederationException(ddf.catalog.federation.FederationException) Test(org.junit.Test)

Example 34 with FederationException

use of ddf.catalog.federation.FederationException in project ddf by codice.

the class ReplicateCommand method query.

@Override
protected SourceResponse query(CatalogFacade framework, Filter filter, int startIndex, long querySize) {
    QueryImpl query = new QueryImpl(filter);
    query.setRequestsTotalResultsCount(true);
    query.setPageSize((int) querySize);
    query.setSortBy(new SortByImpl(Metacard.EFFECTIVE, SortOrder.DESCENDING));
    QueryRequest queryRequest = new QueryRequestImpl(query, Arrays.asList(sourceId));
    query.setStartIndex(startIndex);
    SourceResponse response;
    try {
        LOGGER.debug("Querying with startIndex: {}", startIndex);
        response = framework.query(queryRequest);
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        printErrorMessage(String.format("Received error from %s: %s%n", sourceId, e.getMessage()));
        return null;
    }
    if (response.getProcessingDetails() != null && !response.getProcessingDetails().isEmpty()) {
        for (SourceProcessingDetails details : response.getProcessingDetails()) {
            LOGGER.debug("Got Issues: {}", details.getWarnings());
        }
        return null;
    }
    final long totalHits = response.getHits();
    if (totalHits == 0) {
        console.println("No records were found to replicate.");
        return null;
    }
    return response;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) SortByImpl(ddf.catalog.filter.impl.SortByImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException) SourceProcessingDetails(ddf.catalog.operation.SourceProcessingDetails)

Example 35 with FederationException

use of ddf.catalog.federation.FederationException in project ddf by codice.

the class ResourceOperations method getResourceInfo.

/**
     * Retrieves a resource by URI.
     * <p/>
     * The {@link ResourceRequest} can specify either the product's URI or ID. If the product ID is
     * specified, then the matching {@link Metacard} must first be retrieved and the product URI
     * extracted from this {@link Metacard}.
     *
     * @param resourceRequest
     * @param site
     * @param isEnterprise
     * @param federatedSite
     * @param requestProperties
     * @param fanoutEnabled
     * @return
     * @throws ResourceNotSupportedException
     * @throws ResourceNotFoundException
     */
protected ResourceInfo getResourceInfo(ResourceRequest resourceRequest, String site, boolean isEnterprise, StringBuilder federatedSite, Map<String, Serializable> requestProperties, boolean fanoutEnabled) throws ResourceNotSupportedException, ResourceNotFoundException {
    Metacard metacard;
    URI resourceUri;
    String name = resourceRequest.getAttributeName();
    try {
        if (ResourceRequest.GET_RESOURCE_BY_PRODUCT_URI.equals(name)) {
            // because this is a get resource by product uri, we already
            // have the product uri to return
            LOGGER.debug("get resource by product uri");
            Object value = resourceRequest.getAttributeValue();
            if (value instanceof URI) {
                resourceUri = (URI) value;
                if (StringUtils.isNotBlank(resourceUri.getFragment())) {
                    resourceRequest.getProperties().put(ContentItem.QUALIFIER, resourceUri.getFragment());
                    try {
                        resourceUri = new URI(resourceUri.getScheme(), resourceUri.getSchemeSpecificPart(), null);
                    } catch (URISyntaxException e) {
                        throw new ResourceNotFoundException("Could not resolve URI by doing a URI based query: " + value);
                    }
                }
                Query propertyEqualToUriQuery = createPropertyIsEqualToQuery(Metacard.RESOURCE_URI, resourceUri.toString());
                // if isEnterprise, go out and obtain the actual source
                // where the product's metacard is stored.
                QueryRequest queryRequest = new QueryRequestImpl(anyTag(propertyEqualToUriQuery, site, isEnterprise), isEnterprise, Collections.singletonList(site == null ? this.getId() : site), resourceRequest.getProperties());
                QueryResponse queryResponse = queryOperations.query(queryRequest, null, true, fanoutEnabled);
                if (!queryResponse.getResults().isEmpty()) {
                    metacard = queryResponse.getResults().get(0).getMetacard();
                    federatedSite.append(metacard.getSourceId());
                    LOGGER.debug("Trying to lookup resource URI {} for metacardId: {}", resourceUri, resourceUri);
                    if (!requestProperties.containsKey(Metacard.ID)) {
                        requestProperties.put(Metacard.ID, metacard.getId());
                    }
                    if (!requestProperties.containsKey(Metacard.RESOURCE_URI)) {
                        requestProperties.put(Metacard.RESOURCE_URI, metacard.getResourceURI());
                    }
                } else {
                    throw new ResourceNotFoundException("Could not resolve source id for URI by doing a URI based query: " + resourceUri);
                }
            } else {
                throw new ResourceNotSupportedException("The GetResourceRequest with attribute value of class '" + value.getClass() + "' is not supported by this instance of the CatalogFramework.");
            }
        } else if (ResourceRequest.GET_RESOURCE_BY_ID.equals(name)) {
            // since this is a get resource by id, we need to obtain the
            // product URI
            LOGGER.debug("get resource by id");
            Object value = resourceRequest.getAttributeValue();
            if (value instanceof String) {
                String metacardId = (String) value;
                LOGGER.debug("metacardId = {},   site = {}", metacardId, site);
                QueryRequest queryRequest = new QueryRequestImpl(anyTag(createMetacardIdQuery(metacardId), site, isEnterprise), isEnterprise, Collections.singletonList(site == null ? this.getId() : site), resourceRequest.getProperties());
                QueryResponse queryResponse = queryOperations.query(queryRequest, null, true, fanoutEnabled);
                if (!queryResponse.getResults().isEmpty()) {
                    metacard = queryResponse.getResults().get(0).getMetacard();
                    resourceUri = metacard.getResourceURI();
                    federatedSite.append(metacard.getSourceId());
                    LOGGER.debug("Trying to lookup resource URI {} for metacardId: {}", resourceUri, metacardId);
                } else {
                    throw new ResourceNotFoundException("Could not resolve source id for URI by doing an id based query: " + metacardId);
                }
                if (!requestProperties.containsKey(Metacard.ID)) {
                    requestProperties.put(Metacard.ID, metacardId);
                }
                if (!requestProperties.containsKey(Metacard.RESOURCE_URI)) {
                    requestProperties.put(Metacard.RESOURCE_URI, resourceUri);
                }
            } else {
                throw new ResourceNotSupportedException("The GetResourceRequest with attribute value of class '" + value.getClass() + "' is not supported by this instance of the CatalogFramework.");
            }
        } else {
            throw new ResourceNotSupportedException("The GetResourceRequest with attribute name '" + name + "' is not supported by this instance of the CatalogFramework.");
        }
    } catch (UnsupportedQueryException | FederationException e) {
        throw new ResourceNotFoundException(DEFAULT_RESOURCE_NOT_FOUND_MESSAGE, e);
    }
    LOGGER.debug("Returning resourceURI: {}", resourceUri);
    if (resourceUri == null) {
        throw new ResourceNotFoundException(DEFAULT_RESOURCE_NOT_FOUND_MESSAGE);
    }
    return new ResourceInfo(metacard, resourceUri);
}
Also used : Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) URISyntaxException(java.net.URISyntaxException) FederationException(ddf.catalog.federation.FederationException) URI(java.net.URI) Metacard(ddf.catalog.data.Metacard) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Aggregations

FederationException (ddf.catalog.federation.FederationException)39 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)31 QueryResponse (ddf.catalog.operation.QueryResponse)28 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)27 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)26 QueryRequest (ddf.catalog.operation.QueryRequest)20 Metacard (ddf.catalog.data.Metacard)18 QueryImpl (ddf.catalog.operation.impl.QueryImpl)18 Result (ddf.catalog.data.Result)14 IngestException (ddf.catalog.source.IngestException)11 Test (org.junit.Test)11 Filter (org.opengis.filter.Filter)10 ArrayList (java.util.ArrayList)9 CatalogFramework (ddf.catalog.CatalogFramework)8 CreateResponse (ddf.catalog.operation.CreateResponse)8 Set (java.util.Set)8 CreateRequestImpl (ddf.catalog.operation.impl.CreateRequestImpl)7 Serializable (java.io.Serializable)7 HashMap (java.util.HashMap)7 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)6