Search in sources :

Example 46 with QueryResponseImpl

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

the class FilterPluginTest method setup.

@Before
public void setup() {
    AuthorizingRealm realm = mock(AuthorizingRealm.class);
    when(realm.getName()).thenReturn("mockRealm");
    when(realm.isPermitted(any(PrincipalCollection.class), any(Permission.class))).then(makeDecision());
    Collection<org.apache.shiro.realm.Realm> realms = new ArrayList<>();
    realms.add(realm);
    DefaultSecurityManager manager = new DefaultSecurityManager();
    manager.setRealms(realms);
    SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(new Principal() {

        @Override
        public String getName() {
            return "testuser";
        }
    }, realm.getName());
    Subject systemSubject = new MockSubject(manager, principalCollection);
    plugin = new FilterPlugin() {

        @Override
        protected Subject getSystemSubject() {
            return systemSubject;
        }
    };
    QueryRequestImpl request = getSampleRequest();
    Map<String, Serializable> properties = new HashMap<>();
    Subject subject = new MockSubject(manager, principalCollection);
    properties.put(SecurityConstants.SECURITY_SUBJECT, subject);
    request.setProperties(properties);
    incomingResponse = new QueryResponseImpl(request);
    ResourceRequest resourceRequest = mock(ResourceRequest.class);
    when(resourceRequest.getProperties()).thenReturn(properties);
    resourceResponse = new ResourceResponseImpl(resourceRequest, mock(Resource.class));
    resourceResponse.setProperties(properties);
    DeleteRequest deleteRequest = mock(DeleteRequest.class);
    when(deleteRequest.getProperties()).thenReturn(properties);
    List<Metacard> deletedMetacards = new ArrayList<>();
    deletedMetacards.add(getExactRolesMetacard());
    deleteResponse = new DeleteResponseImpl(deleteRequest, properties, deletedMetacards);
    List<Metacard> badDeletedMetacards = new ArrayList<>();
    badDeletedMetacards.add(getMoreRolesMetacard());
    badDeleteResponse = new DeleteResponseImpl(deleteRequest, properties, badDeletedMetacards);
    createRequest = new CreateRequestImpl(getExactRolesMetacard());
    createRequest.setProperties(properties);
    badCreateRequest = new CreateRequestImpl(getMoreRolesMetacard());
    badCreateRequest.setProperties(properties);
    updateRequest = new UpdateRequestImpl(getExactRolesMetacard().getId(), getExactRolesMetacard());
    updateRequest.setProperties(properties);
    ResultImpl result1 = new ResultImpl(getMoreRolesMetacard());
    ResultImpl result2 = new ResultImpl(getMissingRolesMetacard());
    ResultImpl result3 = new ResultImpl(getExactRolesMetacard());
    ResultImpl result4 = new ResultImpl(getNoRolesMetacard());
    ResultImpl result5 = new ResultImpl(getNoSecurityAttributeMetacard());
    incomingResponse.addResult(result1, false);
    incomingResponse.addResult(result2, false);
    incomingResponse.addResult(result3, false);
    incomingResponse.addResult(result4, false);
    incomingResponse.addResult(result5, true);
}
Also used : Serializable(java.io.Serializable) FilterPlugin(ddf.catalog.security.filter.plugin.FilterPlugin) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) ResultImpl(ddf.catalog.data.impl.ResultImpl) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) CollectionPermission(ddf.security.permission.CollectionPermission) Permission(org.apache.shiro.authz.Permission) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) DelegatingSubject(org.apache.shiro.subject.support.DelegatingSubject) Subject(ddf.security.Subject) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) Metacard(ddf.catalog.data.Metacard) DeleteResponseImpl(ddf.catalog.operation.impl.DeleteResponseImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) ResourceRequest(ddf.catalog.operation.ResourceRequest) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) DeleteRequest(ddf.catalog.operation.DeleteRequest) Principal(java.security.Principal) Before(org.junit.Before)

Example 47 with QueryResponseImpl

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

the class FilterPluginTest method testNoSubject.

@Test(expected = StopProcessingException.class)
public void testNoSubject() throws Exception {
    QueryResponseImpl response = new QueryResponseImpl(getSampleRequest());
    plugin.processPostQuery(response);
    fail("Plugin should have thrown exception when no subject was sent in.");
}
Also used : QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) Test(org.junit.Test)

Example 48 with QueryResponseImpl

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

the class TestCswEndpoint method testPostGetRecordById.

@Test
public void testPostGetRecordById() throws CswException, FederationException, SourceUnavailableException, UnsupportedQueryException {
    final GetRecordByIdType getRecordByIdType = new GetRecordByIdType();
    getRecordByIdType.setId(Collections.singletonList("123,456"));
    getRecordByIdType.setOutputFormat(MediaType.APPLICATION_XML);
    getRecordByIdType.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
    final Metacard metacard1 = new MetacardImpl();
    final Metacard metacard2 = new MetacardImpl();
    final List<Result> mockResults = Arrays.asList(new ResultImpl(metacard1), new ResultImpl(metacard2));
    final QueryResponse queryResponse = new QueryResponseImpl(null, mockResults, mockResults.size());
    doReturn(queryResponse).when(catalogFramework).query(any(QueryRequest.class));
    final CswRecordCollection cswRecordCollection = csw.getRecordById(getRecordByIdType, null);
    verifyCswRecordCollection(cswRecordCollection, metacard1, metacard2);
    // "summary" is the default if none is specified in the request.
    assertThat(cswRecordCollection.getElementSetType(), is(ElementSetType.SUMMARY));
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryRequest(ddf.catalog.operation.QueryRequest) QueryResponse(ddf.catalog.operation.QueryResponse) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ResultImpl(ddf.catalog.data.impl.ResultImpl) GetRecordByIdType(net.opengis.cat.csw.v_2_0_2.GetRecordByIdType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 49 with QueryResponseImpl

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

the class CachingFederationStrategy method sourceFederate.

private QueryResponse sourceFederate(List<Source> sources, final QueryRequest queryRequest) {
    if (LOGGER.isDebugEnabled()) {
        for (Source source : sources) {
            if (source != null) {
                LOGGER.debug("source to query: {}", source.getId());
            }
        }
    }
    Query originalQuery = queryRequest.getQuery();
    int offset = originalQuery.getStartIndex();
    final int pageSize = originalQuery.getPageSize();
    // limit offset to max value
    if (offset > this.maxStartIndex) {
        offset = this.maxStartIndex;
    }
    final QueryResponseImpl queryResponseQueue = new QueryResponseImpl(queryRequest, null);
    Map<Future<SourceResponse>, QueryRequest> futures = new HashMap<>();
    Query modifiedQuery = getModifiedQuery(originalQuery, sources.size(), offset, pageSize);
    QueryRequest modifiedQueryRequest = new QueryRequestImpl(modifiedQuery, queryRequest.isEnterprise(), queryRequest.getSourceIds(), queryRequest.getProperties());
    CompletionService<SourceResponse> queryCompletion = new ExecutorCompletionService<>(queryExecutorService);
    // Do NOT call source.isAvailable() when checking sources
    for (final Source source : sources) {
        if (source != null) {
            if (!futuresContainsSource(source, futures)) {
                LOGGER.debug("running query on source: {}", source.getId());
                QueryRequest sourceQueryRequest = new QueryRequestImpl(modifiedQuery, queryRequest.isEnterprise(), Collections.singleton(source.getId()), new HashMap<>(queryRequest.getProperties()));
                try {
                    for (PreFederatedQueryPlugin service : preQuery) {
                        try {
                            sourceQueryRequest = service.process(source, sourceQueryRequest);
                        } catch (PluginExecutionException e) {
                            LOGGER.info("Error executing PreFederatedQueryPlugin", e);
                        }
                    }
                } catch (StopProcessingException e) {
                    LOGGER.info("Plugin stopped processing", e);
                }
                if (source instanceof CatalogProvider && SystemInfo.getSiteName().equals(source.getId())) {
                    // TODO RAP 12 Jul 16: DDF-2294 - Extract into a new PreFederatedQueryPlugin
                    sourceQueryRequest = validationQueryFactory.getQueryRequestWithValidationFilter(sourceQueryRequest, showErrors, showWarnings);
                }
                futures.put(queryCompletion.submit(new CallableSourceResponse(source, sourceQueryRequest)), sourceQueryRequest);
            } else {
                LOGGER.info("Duplicate source found with name {}. Ignoring second one.", source.getId());
            }
        }
    }
    QueryResponseImpl offsetResults = null;
    // OffsetResultHandler does.
    if (offset > 1 && sources.size() > 1) {
        offsetResults = new QueryResponseImpl(queryRequest, null);
        queryExecutorService.submit(new OffsetResultHandler(queryResponseQueue, offsetResults, pageSize, offset));
    }
    queryExecutorService.submit(sortedQueryMonitorFactory.createMonitor(queryCompletion, futures, queryResponseQueue, modifiedQueryRequest, postQuery));
    QueryResponse queryResponse;
    if (offset > 1 && sources.size() > 1) {
        queryResponse = offsetResults;
        LOGGER.debug("returning offsetResults");
    } else {
        queryResponse = queryResponseQueue;
        LOGGER.debug("returning returnResults: {}", queryResponse);
    }
    LOGGER.debug("returning Query Results: {}", queryResponse);
    return queryResponse;
}
Also used : Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) PreFederatedQueryPlugin(ddf.catalog.plugin.PreFederatedQueryPlugin) HashMap(java.util.HashMap) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) StopProcessingException(ddf.catalog.plugin.StopProcessingException) Source(ddf.catalog.source.Source) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) CatalogProvider(ddf.catalog.source.CatalogProvider) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) Future(java.util.concurrent.Future) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException)

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