Search in sources :

Example 16 with QueryResponse

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

the class SearchControllerTest method createFramework.

private CatalogFramework createFramework() {
    final long COUNT = 2;
    CatalogFramework framework = mock(CatalogFramework.class);
    List<Result> results = new ArrayList<Result>();
    for (int i = 0; i < COUNT; i++) {
        Result result = mock(Result.class);
        MetacardImpl metacard = new MetacardImpl();
        metacard.setId("Metacard_" + i);
        metacard.setTitle("Metacard " + i);
        metacard.setLocation("POINT(" + i + " " + i + ")");
        metacard.setType(BasicTypes.BASIC_METACARD);
        metacard.setCreatedDate(TIMESTAMP);
        metacard.setEffectiveDate(TIMESTAMP);
        metacard.setExpirationDate(TIMESTAMP);
        metacard.setModifiedDate(TIMESTAMP);
        metacard.setContentTypeName("TEST");
        metacard.setContentTypeVersion("1.0");
        metacard.setTargetNamespace(URI.create(getClass().getPackage().getName()));
        when(result.getDistanceInMeters()).thenReturn(100.0 * i);
        when(result.getRelevanceScore()).thenReturn(100.0 * (COUNT - i) / COUNT);
        when(result.getMetacard()).thenReturn(metacard);
        results.add(result);
    }
    QueryResponse response = new QueryResponseImpl(mock(QueryRequest.class), new ArrayList<Result>(), COUNT);
    response.getResults().addAll(results);
    try {
        when(framework.query(any(QueryRequest.class))).thenReturn(response);
    } catch (UnsupportedQueryException e) {
        LOGGER.debug("Error querying framework", e);
    } catch (SourceUnavailableException e) {
        LOGGER.debug("Error querying framework", e);
    } catch (FederationException e) {
        LOGGER.debug("Error querying framework", e);
    }
    return framework;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryRequest(ddf.catalog.operation.QueryRequest) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) FederationException(ddf.catalog.federation.FederationException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryResponse(ddf.catalog.operation.QueryResponse) CatalogFramework(ddf.catalog.CatalogFramework)

Example 17 with QueryResponse

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

the class CacheQueryRunnable method run.

@Override
public void run() {
    // check if there are any currently cached results
    QueryResponse response = queryCatalog(null, request, subject, new HashMap<>(CACHE_PROPERTIES));
    search.update(response);
    try {
        searchController.publishResults(request.getId(), search.transform(request.getId()), session);
    } catch (CatalogTransformerException e) {
        LOGGER.debug("Failed to transform cached search results.", e);
    }
    addResults(response.getResults());
    indexResults(response);
}
Also used : QueryResponse(ddf.catalog.operation.QueryResponse) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException)

Example 18 with QueryResponse

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

the class QueryRunnable method queryCatalog.

protected QueryResponse queryCatalog(String sourceId, SearchRequest searchRequest, Subject subject, Map<String, Serializable> properties) {
    Query query = searchRequest.getQuery();
    QueryResponse response = getEmptyResponse(sourceId);
    long startTime = System.currentTimeMillis();
    try {
        if (query != null) {
            List<String> sourceIds;
            if (sourceId == null) {
                sourceIds = new ArrayList<>(searchRequest.getSourceIds());
            } else {
                sourceIds = Collections.singletonList(sourceId);
            }
            QueryRequest request = new QueryRequestImpl(query, false, sourceIds, properties);
            if (subject != null) {
                LOGGER.debug("Adding {} property with value {} to request.", SecurityConstants.SECURITY_SUBJECT, subject);
                request.getProperties().put(SecurityConstants.SECURITY_SUBJECT, subject);
            }
            LOGGER.debug("Sending query: {}", query);
            response = searchController.getFramework().query(request);
        }
    } catch (UnsupportedQueryException | FederationException e) {
        LOGGER.info("Error executing query. {}. Set log level to DEBUG for more information", e.getMessage());
        LOGGER.debug("Error executing query", e);
        response.getProcessingDetails().add(new ProcessingDetailsImpl(sourceId, e));
    } catch (SourceUnavailableException e) {
        LOGGER.info("Error executing query because the underlying source was unavailable. {}. Set log level to DEBUG for more information", e.getMessage());
        LOGGER.debug("Error executing query because the underlying source was unavailable.", e);
        response.getProcessingDetails().add(new ProcessingDetailsImpl(sourceId, e));
    } catch (RuntimeException e) {
        // Account for any runtime exceptions and send back a server error
        // this prevents full stacktraces returning to the client
        // this allows for a graceful server error to be returned
        LOGGER.info("RuntimeException on executing query. {}. Set log level to DEBUG for more information", e.getMessage());
        LOGGER.debug("RuntimeException on executing query", e);
        response.getProcessingDetails().add(new ProcessingDetailsImpl(sourceId, e));
    }
    long estimatedTime = System.currentTimeMillis() - startTime;
    response.getProperties().put("elapsed", estimatedTime);
    return response;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException) ProcessingDetailsImpl(ddf.catalog.operation.impl.ProcessingDetailsImpl) QueryResponse(ddf.catalog.operation.QueryResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl)

Example 19 with QueryResponse

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

the class ResourceOperations method getEnterpriseResourceOptions.

public Map<String, Set<String>> getEnterpriseResourceOptions(String metacardId, boolean fanoutEnabled) throws ResourceNotFoundException {
    LOGGER.trace("ENTERING: getEnterpriseResourceOptions");
    Set<String> supportedOptions = Collections.emptySet();
    try {
        QueryRequest queryRequest = new QueryRequestImpl(createMetacardIdQuery(metacardId), true, null, null);
        QueryResponse queryResponse = queryOperations.query(queryRequest, null, false, fanoutEnabled);
        List<Result> results = queryResponse.getResults();
        if (!results.isEmpty()) {
            Metacard metacard = results.get(0).getMetacard();
            String sourceIdOfResult = metacard.getSourceId();
            if (sourceIdOfResult != null && sourceIdOfResult.equals(getId())) {
                // found entry on local source
                supportedOptions = getOptionsFromLocalProvider(metacard);
            } else if (sourceIdOfResult != null && !sourceIdOfResult.equals(getId())) {
                // found entry on federated source
                supportedOptions = getOptionsFromFederatedSource(metacard, sourceIdOfResult);
            }
        } else {
            String message = "Unable to find metacard " + metacardId + " on enterprise.";
            LOGGER.debug(message);
            LOGGER.trace("EXITING: getEnterpriseResourceOptions");
            throw new ResourceNotFoundException(message);
        }
    } catch (UnsupportedQueryException e) {
        LOGGER.debug("Error finding metacard {}", metacardId, e);
        LOGGER.trace("EXITING: getEnterpriseResourceOptions");
        throw new ResourceNotFoundException("Error finding metacard due to Unsuppported Query", e);
    } catch (FederationException e) {
        LOGGER.debug("Error federating query for metacard {}", metacardId, e);
        LOGGER.trace("EXITING: getEnterpriseResourceOptions");
        throw new ResourceNotFoundException("Error finding metacard due to Federation issue", e);
    } catch (IllegalArgumentException e) {
        LOGGER.debug("Metacard couldn't be found {}", metacardId, e);
        LOGGER.trace("EXITING: getEnterpriseResourceOptions");
        throw new ResourceNotFoundException("Query returned null metacard", e);
    }
    LOGGER.trace("EXITING: getEnterpriseResourceOptions");
    return Collections.singletonMap(ResourceRequest.OPTION_ARGUMENT, supportedOptions);
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryRequest(ddf.catalog.operation.QueryRequest) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) FederationException(ddf.catalog.federation.FederationException) Result(ddf.catalog.data.Result)

Example 20 with QueryResponse

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

the class ResourceOperations method getResourceOptions.

public Map<String, Set<String>> getResourceOptions(String metacardId, String sourceId, boolean fanoutEnabled) throws ResourceNotFoundException {
    LOGGER.trace("ENTERING: getResourceOptions");
    Map<String, Set<String>> optionsMap;
    try {
        LOGGER.debug("source id to get options from: {}", sourceId);
        QueryRequest queryRequest = new QueryRequestImpl(createMetacardIdQuery(metacardId), false, Collections.singletonList(sourceId == null ? this.getId() : sourceId), null);
        QueryResponse queryResponse = queryOperations.query(queryRequest, null, false, fanoutEnabled);
        List<Result> results = queryResponse.getResults();
        if (!results.isEmpty()) {
            Metacard metacard = results.get(0).getMetacard();
            // or the local provider.
            if (StringUtils.isEmpty(sourceId) || sourceId.equals(getId())) {
                optionsMap = Collections.singletonMap(ResourceRequest.OPTION_ARGUMENT, getOptionsFromLocalProvider(metacard));
            } else {
                optionsMap = Collections.singletonMap(ResourceRequest.OPTION_ARGUMENT, getOptionsFromFederatedSource(metacard, sourceId));
            }
        } else {
            String message = "Could not find metacard " + metacardId + " on source " + sourceId;
            throw new ResourceNotFoundException(message);
        }
    } catch (UnsupportedQueryException e) {
        LOGGER.debug("Error finding metacard {}", metacardId, e);
        throw new ResourceNotFoundException("Error finding metacard due to Unsuppported Query", e);
    } catch (FederationException e) {
        LOGGER.debug("Error federating query for metacard {}", metacardId, e);
        throw new ResourceNotFoundException("Error finding metacard due to Federation issue", e);
    } catch (IllegalArgumentException e) {
        LOGGER.debug("Metacard couldn't be found {}", metacardId, e);
        throw new ResourceNotFoundException("Query returned null metacard", e);
    } finally {
        LOGGER.trace("EXITING: getResourceOptions");
    }
    return optionsMap;
}
Also used : Set(java.util.Set) QueryRequest(ddf.catalog.operation.QueryRequest) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Aggregations

QueryResponse (ddf.catalog.operation.QueryResponse)120 QueryRequest (ddf.catalog.operation.QueryRequest)87 Test (org.junit.Test)74 Metacard (ddf.catalog.data.Metacard)67 Result (ddf.catalog.data.Result)54 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)52 ArrayList (java.util.ArrayList)51 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)34 QueryImpl (ddf.catalog.operation.impl.QueryImpl)34 FederationException (ddf.catalog.federation.FederationException)33 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)30 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)24 HashSet (java.util.HashSet)24 Filter (org.opengis.filter.Filter)24 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)23 Source (ddf.catalog.source.Source)22 ResultImpl (ddf.catalog.data.impl.ResultImpl)21 Serializable (java.io.Serializable)21 HashMap (java.util.HashMap)21 InputStream (java.io.InputStream)19