Search in sources :

Example 81 with QueryRequestImpl

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

the class CachingFederationStrategyTest method testFederateQueryNoUpdateToCache.

@Test
public void testFederateQueryNoUpdateToCache() throws Exception {
    properties.put(QUERY_MODE, NATIVE_QUERY_MODE);
    QueryRequest fedQueryRequest = new QueryRequestImpl(mockQuery, properties);
    Source mockSource = mock(Source.class);
    when(mockSource.query(any(QueryRequest.class))).thenReturn(mockResponse);
    QueryResponse federateResponse = strategy.federate(Arrays.asList(mockSource), fedQueryRequest);
    assertThat(requestArgumentCaptor.getValue().getPropertyValue(QUERY_MODE), is(NATIVE_QUERY_MODE));
    verify(mockSource).query(any(QueryRequest.class));
    verify(cache, times(0)).query(any(QueryRequest.class));
    verifyCacheNotUpdated();
    assertThat(federateResponse.getRequest().getQuery(), is(requestArgumentCaptor.getValue().getQuery()));
}
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 82 with QueryRequestImpl

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

the class CachingFederationStrategyTest method testFederateQueryUpdateCacheBlocking.

@Test
public void testFederateQueryUpdateCacheBlocking() throws Exception {
    properties.put(QUERY_MODE, INDEX_QUERY_MODE);
    QueryRequest fedQueryRequest = new QueryRequestImpl(mockQuery, properties);
    Source mockSource = mock(Source.class);
    when(mockSource.query(any(QueryRequest.class))).thenReturn(mockResponse);
    doNothing().when(cacheCommitPhaser).add(cacheArgs.capture());
    QueryResponse federateResponse = strategy.federate(Arrays.asList(mockSource), fedQueryRequest);
    assertThat(requestArgumentCaptor.getValue().getPropertyValue(QUERY_MODE), is(INDEX_QUERY_MODE));
    verify(mockSource).query(any(QueryRequest.class));
    verify(cache, times(0)).query(any(QueryRequest.class));
    // CacheCommitPhaser.add() is called
    verify(cacheCommitPhaser).add(cacheArgs.getValue());
    verifyCacheUpdated();
    assertThat(federateResponse.getRequest().getQuery(), is(requestArgumentCaptor.getValue().getQuery()));
}
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 83 with QueryRequestImpl

use of ddf.catalog.operation.impl.QueryRequestImpl 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 84 with QueryRequestImpl

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

the class CachingFederationStrategyTest method testCatalogProviderSource.

@Test
public void testCatalogProviderSource() throws Exception {
    CatalogProvider catalogProviderSource = mock(CatalogProvider.class);
    when(catalogProviderSource.getId()).thenReturn(SystemInfo.getSiteName());
    QueryRequest fedQueryRequest = new QueryRequestImpl(mockQuery, properties);
    strategy.federate(Arrays.asList(catalogProviderSource), fedQueryRequest);
    verify(validationQueryFactory).getQueryRequestWithValidationFilter(any(QueryRequest.class), any(Boolean.class), any(Boolean.class));
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) CatalogProvider(ddf.catalog.source.CatalogProvider) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 85 with QueryRequestImpl

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

QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)216 QueryImpl (ddf.catalog.operation.impl.QueryImpl)187 Test (org.junit.Test)142 Filter (org.opengis.filter.Filter)103 SourceResponse (ddf.catalog.operation.SourceResponse)100 QueryRequest (ddf.catalog.operation.QueryRequest)86 Metacard (ddf.catalog.data.Metacard)72 Result (ddf.catalog.data.Result)60 QueryResponse (ddf.catalog.operation.QueryResponse)46 ArrayList (java.util.ArrayList)46 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)32 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)29 Query (ddf.catalog.operation.Query)28 HashMap (java.util.HashMap)28 FederationException (ddf.catalog.federation.FederationException)27 Serializable (java.io.Serializable)27 Matchers.containsString (org.hamcrest.Matchers.containsString)22 Matchers.anyString (org.mockito.Matchers.anyString)20 SortByImpl (ddf.catalog.filter.impl.SortByImpl)19 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)18