Search in sources :

Example 86 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class QueryOperations method doQuery.

/**
     * Executes a query using the specified {@link QueryRequest} and {@link FederationStrategy}.
     * Based on the isEnterprise and sourceIds list in the query request, the federated query may
     * include the local provider and {@link ConnectedSource}s.
     *
     * @param queryRequest the {@link QueryRequest}
     * @param strategy     the {@link FederationStrategy}
     * @return the {@link QueryResponse}
     * @throws FederationException
     */
QueryResponse doQuery(QueryRequest queryRequest, FederationStrategy strategy) throws FederationException {
    Set<String> sourceIds = getCombinedIdSet(queryRequest);
    LOGGER.debug("source ids: {}", sourceIds);
    QuerySources querySources = new QuerySources(frameworkProperties).initializeSources(this, queryRequest, sourceIds).addConnectedSources(this, frameworkProperties).addCatalogProvider(this);
    if (querySources.isEmpty()) {
        // TODO change to SourceUnavailableException
        throw new FederationException("SiteNames could not be resolved due to invalid site names, none of the sites " + "were available, or the current subject doesn't have permission to access the sites.");
    }
    LOGGER.debug("Calling strategy.federate()");
    Query originalQuery = queryRequest.getQuery();
    if (originalQuery != null && originalQuery.getTimeoutMillis() <= 0) {
        Query modifiedQuery = new QueryImpl(originalQuery, originalQuery.getStartIndex(), originalQuery.getPageSize(), originalQuery.getSortBy(), originalQuery.requestsTotalResultsCount(), queryTimeoutMillis);
        queryRequest = new QueryRequestImpl(modifiedQuery, queryRequest.isEnterprise(), queryRequest.getSourceIds(), queryRequest.getProperties());
    }
    QueryResponse response = strategy.federate(querySources.sourcesToQuery, queryRequest);
    frameworkProperties.getQueryResponsePostProcessor().processResponse(response);
    return addProcessingDetails(querySources.exceptions, response);
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) Query(ddf.catalog.operation.Query) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) FederationException(ddf.catalog.federation.FederationException)

Example 87 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class CachingFederationStrategyTest method testFederateDuplicateSources.

@Test
public void testFederateDuplicateSources() throws Exception {
    Query mockQ = new QueryImpl(mock(NullFilterImpl.class), 2, 2, mock(SortBy.class), true, LONG_TIMEOUT);
    QueryRequest fedQueryRequest = new QueryRequestImpl(mockQ, properties);
    List<Source> sources = new ArrayList<>();
    // Multiple sources needed for OffsetResultHandler to be used
    for (int i = 0; i < 2; i++) {
        Source mockSource = mock(Source.class);
        when(mockSource.getId()).thenReturn("mock source");
        sources.add(mockSource);
    }
    strategy.federate(sources, fedQueryRequest);
    verify(sources.get(0), atLeastOnce()).query(any(QueryRequest.class));
    verify(sources.get(1), times(0)).query(any(QueryRequest.class));
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) NullFilterImpl(org.geotools.filter.NullFilterImpl) Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) SortBy(org.opengis.filter.sort.SortBy) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ArrayList(java.util.ArrayList) Source(ddf.catalog.source.Source) Test(org.junit.Test)

Example 88 with QueryImpl

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

Example 89 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class TestCswSource method testAddingContentTypesOnQueries.

@Test
public void testAddingContentTypesOnQueries() throws CswException, UnsupportedQueryException, SecurityServiceException {
    Csw mockCsw = createMockCsw();
    List<String> expectedNames = new LinkedList<>(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"));
    ServiceRegistration<?> mockRegisteredMetacardType = (ServiceRegistration<?>) mock(ServiceRegistration.class);
    LOGGER.info("mockRegisteredMetacardType: {}", mockRegisteredMetacardType);
    doReturn(mockRegisteredMetacardType).when(mockContext).registerService(eq(MetacardType.class.getName()), any(MetacardType.class), Matchers.any());
    ServiceReference<?> mockServiceReference = (ServiceReference<?>) mock(ServiceReference.class);
    doReturn(mockServiceReference).when(mockRegisteredMetacardType).getReference();
    when(mockServiceReference.getProperty(eq(Metacard.CONTENT_TYPE))).thenReturn(expectedNames);
    AbstractCswSource source = getCswSource(mockCsw, mockContext);
    assertThat(source.getContentTypes(), hasSize(10));
    Set<ContentType> expected = generateContentType(expectedNames);
    assertThat(source.getContentTypes(), is(expected));
    CswRecordCollection collection = generateCswCollection("/getRecordsResponse.xml");
    when(mockCsw.getRecords(any(GetRecordsType.class))).thenReturn(collection);
    QueryImpl propertyIsLikeQuery = new QueryImpl(builder.attribute(Metacard.ANY_TEXT).is().like().text("*"));
    expectedNames.add("dataset");
    expectedNames.add("dataset 2");
    expectedNames.add("dataset 3");
    expected = generateContentType(expectedNames);
    source.query(new QueryRequestImpl(propertyIsLikeQuery));
    assertThat(source.getContentTypes(), hasSize(13));
    assertThat(source.getContentTypes(), is(expected));
}
Also used : ContentType(ddf.catalog.data.ContentType) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) Matchers.anyString(org.mockito.Matchers.anyString) LinkedList(java.util.LinkedList) MetacardType(ddf.catalog.data.MetacardType) ServiceReference(org.osgi.framework.ServiceReference) QueryImpl(ddf.catalog.operation.impl.QueryImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 90 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class TestCswSource method testQueryWitNullSorting.

@Test
public void testQueryWitNullSorting() throws JAXBException, UnsupportedQueryException, DatatypeConfigurationException, SAXException, IOException, SecurityServiceException {
    // Setup
    final String searchPhrase = "*";
    final int pageSize = 1;
    final int numRecordsReturned = 1;
    final long numRecordsMatched = 1;
    setupMockContextForMetacardTypeRegistrationAndUnregistration(getDefaultContentTypes());
    try {
        configureMockCsw(numRecordsReturned, numRecordsMatched, CswConstants.VERSION_2_0_2);
    } catch (CswException e) {
        fail("Could not configure Mock Remote CSW: " + e.getMessage());
    }
    QueryImpl query = new QueryImpl(builder.attribute(Metacard.ANY_TEXT).is().like().text(searchPhrase));
    query.setPageSize(pageSize);
    query.setSortBy(null);
    AbstractCswSource cswSource = getCswSource(mockCsw, mockContext);
    cswSource.setCswUrl(URL);
    cswSource.setId(ID);
    // Perform test
    SourceResponse response = cswSource.query(new QueryRequestImpl(query));
    // Verify
    Assert.assertNotNull(response);
    assertThat(response.getResults().size(), is(numRecordsReturned));
    assertThat(response.getHits(), is(numRecordsMatched));
    ArgumentCaptor<GetRecordsType> captor = ArgumentCaptor.forClass(GetRecordsType.class);
    try {
        verify(mockCsw, atLeastOnce()).getRecords(captor.capture());
    } catch (CswException e) {
        fail("Could not verify mock CSW record count: " + e.getMessage());
    }
    GetRecordsType getRecordsType = captor.getValue();
    QueryType cswQuery = (QueryType) getRecordsType.getAbstractQuery().getValue();
    assertThat(cswQuery.getSortBy(), nullValue());
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) SourceResponse(ddf.catalog.operation.SourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) Matchers.anyString(org.mockito.Matchers.anyString) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) Test(org.junit.Test)

Aggregations

QueryImpl (ddf.catalog.operation.impl.QueryImpl)232 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)186 Test (org.junit.Test)149 Filter (org.opengis.filter.Filter)117 SourceResponse (ddf.catalog.operation.SourceResponse)95 QueryRequest (ddf.catalog.operation.QueryRequest)66 Metacard (ddf.catalog.data.Metacard)61 ArrayList (java.util.ArrayList)50 Result (ddf.catalog.data.Result)49 Matchers.containsString (org.hamcrest.Matchers.containsString)30 Query (ddf.catalog.operation.Query)29 QueryResponse (ddf.catalog.operation.QueryResponse)28 SortByImpl (ddf.catalog.filter.impl.SortByImpl)27 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)25 SortBy (org.opengis.filter.sort.SortBy)25 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)24 Serializable (java.io.Serializable)23 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)22 HashMap (java.util.HashMap)20 Matchers.anyString (org.mockito.Matchers.anyString)20