Search in sources :

Example 76 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class SortedFederationStrategyTest method testFederateGetEmptyHits.

@Test
public void testFederateGetEmptyHits() throws Exception {
    QueryRequest fedQueryRequest = new QueryRequestImpl(mockQuery, false, null, properties);
    List<Source> sourceList = ImmutableList.of();
    QueryResponse federateResponse = strategy.federate(sourceList, fedQueryRequest);
    assertThat(federateResponse.getHits(), is((long) 0));
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) Source(ddf.catalog.source.Source) Test(org.junit.Test)

Example 77 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class SortedFederationStrategyTest method testFederateGetHits.

@Test
public void testFederateGetHits() throws Exception {
    QueryRequest fedQueryRequest = new QueryRequestImpl(mockQuery, true, null, null);
    Source mockSource1 = getMockSource();
    Source mockSource2 = getMockSource();
    Source mockSource3 = getMockSource();
    List<Source> sourceList = ImmutableList.of(mockSource1, mockSource2, mockSource3);
    long numHits = 2;
    when(mockResponse.getHits()).thenReturn(numHits);
    QueryResponse federateResponse = strategy.federate(sourceList, fedQueryRequest);
    assertThat(federateResponse.getHits(), is(numHits * sourceList.size()));
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) Source(ddf.catalog.source.Source) Test(org.junit.Test)

Example 78 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class UpdateOperations method populateMetacards.

private UpdateRequest populateMetacards(UpdateRequest updateRequest) throws IngestException {
    QueryRequestImpl queryRequest = createQueryRequest(updateRequest);
    QueryResponse queryResponse;
    try {
        queryResponse = queryOperations.doQuery(queryRequest, frameworkProperties.getFederationStrategy());
    } catch (FederationException e) {
        LOGGER.debug("Unable to complete query for updated metacards.", e);
        throw new IngestException("Exception during runtime while performing update");
    }
    if (!foundAllUpdateRequestMetacards(updateRequest, queryResponse)) {
        logFailedQueryInfo(updateRequest, queryResponse);
        throw new IngestException("Could not find all metacards specified in request");
    }
    updateRequest = rewriteRequestToAvoidHistoryConflicts(updateRequest, queryResponse);
    // Construct the metacardMap using the metacard's ID in order to match the UpdateRequest
    HashMap<String, Metacard> metacardMap = new HashMap<>(queryResponse.getResults().stream().map(Result::getMetacard).collect(Collectors.toMap(metacard -> getAttributeStringValue(metacard, Core.ID), Function.identity())));
    updateRequest.getProperties().put(Constants.ATTRIBUTE_UPDATE_MAP_KEY, metacardMap);
    updateRequest.getProperties().put(Constants.OPERATION_TRANSACTION_KEY, new OperationTransactionImpl(OperationTransaction.OperationType.UPDATE, metacardMap.values()));
    return updateRequest;
}
Also used : Metacard(ddf.catalog.data.Metacard) OperationTransactionImpl(ddf.catalog.operation.impl.OperationTransactionImpl) HashMap(java.util.HashMap) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) InternalIngestException(ddf.catalog.source.InternalIngestException) IngestException(ddf.catalog.source.IngestException) FederationException(ddf.catalog.federation.FederationException) Result(ddf.catalog.data.Result)

Example 79 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class ResourceCacheService method queryForMetacard.

private Optional<Metacard> queryForMetacard(String metacardId) {
    Filter filter = frameworkProperties.getFilterBuilder().attribute(Metacard.ID).is().equalTo().text(metacardId);
    QueryRequest queryRequest = new QueryRequestImpl(new QueryImpl(filter), true);
    QueryResponse queryResponse = null;
    try {
        queryResponse = catalogFramework.query(queryRequest);
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Unable to lookup metacard for metacard id [{}].", metacardId);
        return Optional.empty();
    }
    return queryResponse != null && queryResponse.getResults().size() == 1 ? Optional.of(queryResponse.getResults().get(0).getMetacard()) : Optional.empty();
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) QueryRequest(ddf.catalog.operation.QueryRequest) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException)

Example 80 with QueryResponse

use of ddf.catalog.operation.QueryResponse 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)

Aggregations

QueryResponse (ddf.catalog.operation.QueryResponse)187 QueryRequest (ddf.catalog.operation.QueryRequest)130 Test (org.junit.Test)113 Metacard (ddf.catalog.data.Metacard)91 Result (ddf.catalog.data.Result)85 ArrayList (java.util.ArrayList)73 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)70 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)55 QueryImpl (ddf.catalog.operation.impl.QueryImpl)48 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)43 FederationException (ddf.catalog.federation.FederationException)40 ResultImpl (ddf.catalog.data.impl.ResultImpl)39 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)39 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)33 Filter (org.opengis.filter.Filter)33 HashMap (java.util.HashMap)31 Serializable (java.io.Serializable)30 Source (ddf.catalog.source.Source)28 HashSet (java.util.HashSet)28 CatalogFramework (ddf.catalog.CatalogFramework)26