Search in sources :

Example 41 with QueryResponseImpl

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

the class SortedQueryMonitorTest method setUp.

@Before
public void setUp() throws Exception {
    cachingFederationStrategy = mock(CachingFederationStrategy.class);
    completionService = mock(CompletionService.class);
    queryRequest = mock(QueryRequest.class);
    queryResponse = new QueryResponseImpl(queryRequest);
    query = mock(Query.class);
    // Enforce insertion order for testing purposes
    futures = new LinkedHashMap<>();
    for (int i = 0; i < 4; i++) {
        SourceResponse sourceResponseMock = null;
        Future futureMock = mock(Future.class);
        QueryRequest queryRequest = mock(QueryRequest.class);
        when(queryRequest.getSourceIds()).thenReturn(Collections.singleton("Source-" + i));
        switch(i) {
            case 1:
                sourceResponseMock = mock(SourceResponse.class);
                when(sourceResponseMock.getResults()).thenReturn(Lists.newArrayList(mock(Result.class), mock(Result.class), mock(Result.class)));
                when(sourceResponseMock.getHits()).thenReturn(3L);
                break;
            case 2:
                sourceResponseMock = mock(SourceResponse.class);
                when(sourceResponseMock.getResults()).thenReturn(Lists.newArrayList(mock(Result.class)));
                when(sourceResponseMock.getHits()).thenReturn(1L);
                break;
            case 3:
                sourceResponseMock = mock(SourceResponse.class);
                when(sourceResponseMock.getResults()).thenReturn(Lists.<Result>emptyList());
                when(sourceResponseMock.getHits()).thenReturn(0L);
                break;
        }
        when(futureMock.get()).thenReturn(sourceResponseMock);
        futures.put(futureMock, queryRequest);
    }
}
Also used : QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryRequest(ddf.catalog.operation.QueryRequest) Query(ddf.catalog.operation.Query) SourceResponse(ddf.catalog.operation.SourceResponse) CompletionService(java.util.concurrent.CompletionService) Future(java.util.concurrent.Future) Before(org.junit.Before)

Example 42 with QueryResponseImpl

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

the class Jpeg2000ThumbnailConverterTest method testConversion.

@Test
public void testConversion() throws IOException, StopProcessingException, PluginExecutionException {
    IIORegistry.getDefaultInstance().registerServiceProvider(jpeg2000ThumbnailConverter);
    List<Result> resultList = new ArrayList<>();
    Metacard metacard = new MetacardImpl();
    byte[] j2kbytes = new byte[0];
    resultList.add(new ResultImpl(metacard));
    QueryResponseImpl queryResponse = new QueryResponseImpl(null, resultList, 1);
    // there are two possible byte signatures, so test an example of each one
    for (String image : new String[] { "/Bretagne2.j2k", "/Cevennes2.jp2" }) {
        URL imageResource = Jpeg2000ThumbnailConverterTest.class.getResource(image);
        if (imageResource == null) {
            fail("The Image Resource came back null. Was the resources folder removed?");
        }
        String imageResourcePath = new File(imageResource.getFile()).getAbsolutePath();
        j2kbytes = Files.readAllBytes(Paths.get(imageResourcePath));
        metacard.setAttribute(new AttributeImpl(Metacard.THUMBNAIL, j2kbytes));
        jpeg2000ThumbnailConverter.process(queryResponse);
        // verify the plugin converted the j2k/jp2 image
        assertTrue(!Arrays.equals(j2kbytes, metacard.getThumbnail()));
    }
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ImageIO.write(ImageIO.read(new ByteArrayInputStream(j2kbytes)), "gif", output);
    metacard.setAttribute(new AttributeImpl(Metacard.THUMBNAIL, output.toByteArray()));
    jpeg2000ThumbnailConverter.process(queryResponse);
    // verify the plugin ignored  the non-j2k
    assertTrue(Arrays.equals(output.toByteArray(), metacard.getThumbnail()));
}
Also used : AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) URL(java.net.URL) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File) Test(org.junit.Test)

Example 43 with QueryResponseImpl

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

the class QueryOperations method injectAttributes.

private QueryResponse injectAttributes(QueryResponse response) {
    List<Result> results = response.getResults().stream().map(result -> {
        Metacard original = result.getMetacard();
        Metacard metacard = opsMetacardSupport.applyInjectors(original, frameworkProperties.getAttributeInjectors());
        ResultImpl newResult = new ResultImpl(metacard);
        newResult.setDistanceInMeters(result.getDistanceInMeters());
        newResult.setRelevanceScore(result.getRelevanceScore());
        return newResult;
    }).collect(Collectors.toList());
    QueryResponseImpl queryResponse = new QueryResponseImpl(response.getRequest(), results, true, response.getHits(), response.getProperties());
    queryResponse.setProcessingDetails(response.getProcessingDetails());
    return queryResponse;
}
Also used : ProcessingDetailsImpl(ddf.catalog.operation.impl.ProcessingDetailsImpl) PreQueryPlugin(ddf.catalog.plugin.PreQueryPlugin) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) LoggerFactory(org.slf4j.LoggerFactory) SecurityLogger(ddf.security.common.audit.SecurityLogger) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) MetacardVersion(ddf.catalog.core.versioning.MetacardVersion) TagsFilterDelegate(ddf.catalog.filter.delegate.TagsFilterDelegate) Map(java.util.Map) PolicyPlugin(ddf.catalog.plugin.PolicyPlugin) FederatedSource(ddf.catalog.source.FederatedSource) ResultImpl(ddf.catalog.data.impl.ResultImpl) Set(java.util.Set) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) QueryResponse(ddf.catalog.operation.QueryResponse) List(java.util.List) Operation(ddf.catalog.operation.Operation) PreAuthorizationPlugin(ddf.catalog.plugin.PreAuthorizationPlugin) AccessPlugin(ddf.catalog.plugin.AccessPlugin) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) FederationStrategy(ddf.catalog.federation.FederationStrategy) PostQueryPlugin(ddf.catalog.plugin.PostQueryPlugin) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) HashMap(java.util.HashMap) FilterAdapter(ddf.catalog.filter.FilterAdapter) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Source(ddf.catalog.source.Source) Requests(ddf.catalog.util.impl.Requests) CollectionUtils(org.apache.commons.collections.CollectionUtils) Constants(ddf.catalog.Constants) Metacard(ddf.catalog.data.Metacard) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) ConnectedSource(ddf.catalog.source.ConnectedSource) SecurityConstants(ddf.security.SecurityConstants) QueryRequest(ddf.catalog.operation.QueryRequest) Request(ddf.catalog.operation.Request) Result(ddf.catalog.data.Result) DescribableImpl(ddf.catalog.util.impl.DescribableImpl) QueryImpl(ddf.catalog.operation.impl.QueryImpl) PolicyResponse(ddf.catalog.plugin.PolicyResponse) Logger(org.slf4j.Logger) CollectionPermission(ddf.security.permission.CollectionPermission) StopProcessingException(ddf.catalog.plugin.StopProcessingException) Subject(ddf.security.Subject) FederationException(ddf.catalog.federation.FederationException) FrameworkProperties(ddf.catalog.impl.FrameworkProperties) Query(ddf.catalog.operation.Query) ProcessingDetails(ddf.catalog.operation.ProcessingDetails) NumberUtils(org.apache.commons.lang3.math.NumberUtils) Filter(org.opengis.filter.Filter) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) Collections(java.util.Collections) Metacard(ddf.catalog.data.Metacard) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) ResultImpl(ddf.catalog.data.impl.ResultImpl) Result(ddf.catalog.data.Result)

Example 44 with QueryResponseImpl

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

the class OpenSearchEndpoint method executeQuery.

/**
     * Executes the OpenSearchQuery and formulates the response
     *
     * @param format     - of the results in the response
     * @param query      - the query to execute
     * @param ui         -the ui information to use to format the results
     * @param properties
     * @return the response on the query
     */
private Response executeQuery(String format, OpenSearchQuery query, UriInfo ui, Map<String, Serializable> properties) {
    Response response = null;
    String queryFormat = format;
    MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
    List<String> subscriptionList = queryParams.get(Constants.SUBSCRIPTION_KEY);
    LOGGER.debug("Attempting to execute query: {}", query.toString());
    try {
        Map<String, Serializable> arguments = new HashMap<String, Serializable>();
        String organization = framework.getOrganization();
        String url = ui.getRequestUri().toString();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("organization: {}", organization);
            LOGGER.debug("url: {}", url);
        }
        arguments.put("organization", organization);
        arguments.put("url", url);
        // interval
        if (subscriptionList != null && !subscriptionList.isEmpty()) {
            String subscription = subscriptionList.get(0);
            LOGGER.debug("Subscription: {}", subscription);
            arguments.put(Constants.SUBSCRIPTION_KEY, subscription);
            List<String> intervalList = queryParams.get(UPDATE_QUERY_INTERVAL);
            if (intervalList != null && !intervalList.isEmpty()) {
                arguments.put(UPDATE_QUERY_INTERVAL, intervalList.get(0));
            }
        }
        if (StringUtils.isEmpty(queryFormat)) {
            queryFormat = DEFAULT_FORMAT;
        }
        if (query.getFilter() != null) {
            QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), properties);
            QueryResponse queryResponse;
            LOGGER.debug("Sending query");
            queryResponse = framework.query(queryRequest);
            // pass in the format for the transform
            BinaryContent content = framework.transform(queryResponse, queryFormat, arguments);
            response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
        } else {
            // No query was specified
            QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), null);
            // Create a dummy QueryResponse with zero results
            QueryResponseImpl queryResponseQueue = new QueryResponseImpl(queryRequest, new ArrayList<Result>(), 0);
            // pass in the format for the transform
            BinaryContent content = framework.transform(queryResponseQueue, queryFormat, arguments);
            if (null != content) {
                response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
            }
        }
    } catch (UnsupportedQueryException ce) {
        LOGGER.info("Unsupported query", ce);
        response = Response.status(Response.Status.BAD_REQUEST).entity(wrapStringInPreformattedTags("Unsupported query")).build();
    } catch (CatalogTransformerException e) {
        LOGGER.info("Error transforming response", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Error transforming response")).build();
    } catch (FederationException e) {
        LOGGER.info("Error executing query", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query")).build();
    } catch (SourceUnavailableException e) {
        LOGGER.info("Error executing query because the underlying source was unavailable.", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query because the underlying source was unavailable.")).build();
    } 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", e);
        response = Response.serverError().entity(wrapStringInPreformattedTags("RuntimeException on executing query")).build();
    }
    return response;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Serializable(java.io.Serializable) QueryRequest(ddf.catalog.operation.QueryRequest) HashMap(java.util.HashMap) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent) FederationException(ddf.catalog.federation.FederationException) Result(ddf.catalog.data.Result) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse)

Example 45 with QueryResponseImpl

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

the class OpenSearchEndpointTest method testProcessQueryForProperHandlingOfSiteNameLOCAL.

/**
     * Test method for
     * {@link org.codice.ddf.endpoints.OpenSearchEndpoint#processQuery(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.ws.rs.core.UriInfo, java.lang.String, java.lang.String)}
     * .
     * <p>
     * This test will verify that the string "local" in the sources passed to
     * OpenSearchEndpoint.processQuery is replaced with the local site name (in this case the mocked
     * name "TestSiteName"). The QueryRequest object is checked when the framework.query is called
     * to retrieve the OpenSearchQuery, which contains the Set of sites. An assertion within the
     * Answer object for the call framework.query checks that the sites Set is contains the
     * TEST_SITE_NAME.
     *
     * @throws URISyntaxException
     * @throws FederationException
     * @throws SourceUnavailableException
     * @throws UnsupportedQueryException
     * @throws UnsupportedEncodingException
     * @throws CatalogTransformerException
     */
@SuppressWarnings("unchecked")
@Test
public void testProcessQueryForProperHandlingOfSiteNameLOCAL() throws URISyntaxException, UnsupportedQueryException, SourceUnavailableException, FederationException, UnsupportedEncodingException, CatalogTransformerException {
    // ***Test setup***
    final String testSiteName = "TestSiteName";
    CatalogFramework mockFramework = mock(CatalogFramework.class);
    FilterBuilder mockFilterBuilder = mock(FilterBuilder.class);
    AttributeBuilder mockAB = mock(AttributeBuilder.class);
    ExpressionBuilder mockEB = mock(ExpressionBuilder.class);
    ContextualExpressionBuilder mockCEB = mock(ContextualExpressionBuilder.class);
    Filter mockFilter = mock(Filter.class);
    when(mockFilterBuilder.attribute(anyString())).thenReturn(mockAB);
    when(mockAB.is()).thenReturn(mockEB);
    when(mockEB.like()).thenReturn(mockCEB);
    when(mockCEB.text(anyString())).thenReturn(mockFilter);
    String searchTerms = "searchForThis";
    // "local" MUST be included
    String sources = "test, local";
    String count = "200";
    // dummy UriInfo, not really used functionally
    UriInfo mockUriInfo = mock(UriInfo.class);
    URI uri = new URI("test");
    when(mockUriInfo.getRequestUri()).thenReturn(uri);
    MultivaluedMap<String, String> mockMVMap = mock(MultivaluedMap.class);
    when(mockMVMap.get("subscription")).thenReturn(null);
    when(mockMVMap.get("interval")).thenReturn(null);
    when(mockUriInfo.getQueryParameters()).thenReturn(mockMVMap);
    @SuppressWarnings("unused") BinaryContent mockByteContent = mock(BinaryContent.class);
    // Check on the sites passed in to framework.query
    when(mockFramework.query(any(QueryRequest.class))).thenAnswer(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) {
            QueryRequest queryRequest = (QueryRequest) invocation.getArguments()[0];
            // ***Test verification***
            // This assert is the whole point of this unit test
            Assert.assertTrue(((OpenSearchQuery) queryRequest.getQuery()).getSiteIds().contains(testSiteName));
            return new QueryResponseImpl(queryRequest);
        }
    });
    // setup the BinaryContent for the call to Response.ok(...)
    // This is just needed to get the method to complete, the
    BinaryContent mockBinaryContent = mock(BinaryContent.class);
    InputStream is = new ByteArrayInputStream("Test String From InputStream".getBytes("UTF-8"));
    when(mockBinaryContent.getInputStream()).thenReturn(is);
    when(mockBinaryContent.getMimeTypeValue()).thenReturn("text/plain");
    when(mockFramework.transform(any(QueryResponse.class), anyString(), anyMap())).thenReturn(mockBinaryContent);
    OpenSearchEndpoint osEndPoint = new OpenSearchEndpoint(mockFramework, mockFilterBuilder);
    System.setProperty(SystemInfo.SITE_NAME, testSiteName);
    // ***Test Execution***
    osEndPoint.processQuery(searchTerms, null, sources, null, null, count, null, null, null, null, null, null, null, null, null, null, null, null, mockUriInfo, null, null, null);
}
Also used : AttributeBuilder(ddf.catalog.filter.AttributeBuilder) QueryRequest(ddf.catalog.operation.QueryRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Matchers.anyString(org.mockito.Matchers.anyString) ContextualExpressionBuilder(ddf.catalog.filter.ContextualExpressionBuilder) ContextualExpressionBuilder(ddf.catalog.filter.ContextualExpressionBuilder) ExpressionBuilder(ddf.catalog.filter.ExpressionBuilder) BinaryContent(ddf.catalog.data.BinaryContent) URI(java.net.URI) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) Filter(org.opengis.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) FilterBuilder(ddf.catalog.filter.FilterBuilder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) QueryResponse(ddf.catalog.operation.QueryResponse) CatalogFramework(ddf.catalog.CatalogFramework) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Aggregations

QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)49 Result (ddf.catalog.data.Result)32 QueryRequest (ddf.catalog.operation.QueryRequest)31 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)27 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)25 QueryResponse (ddf.catalog.operation.QueryResponse)24 ResultImpl (ddf.catalog.data.impl.ResultImpl)23 Metacard (ddf.catalog.data.Metacard)22 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)20 Query (ddf.catalog.operation.Query)15 SourceResponse (ddf.catalog.operation.SourceResponse)15 Source (ddf.catalog.source.Source)15 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)14 HashMap (java.util.HashMap)14 FederationStrategy (ddf.catalog.federation.FederationStrategy)12 StopProcessingException (ddf.catalog.plugin.StopProcessingException)12 FederationException (ddf.catalog.federation.FederationException)10 ByteSource (com.google.common.io.ByteSource)9 ValidationQueryFactory (ddf.catalog.cache.solr.impl.ValidationQueryFactory)9