Search in sources :

Example 81 with UnsupportedQueryException

use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.

the class CatalogFrameworkQueryTest method testDuringQuery.

@Test
public void testDuringQuery() {
    List<Metacard> metacards = new ArrayList<Metacard>();
    MetacardImpl newCard1 = new MetacardImpl();
    newCard1.setId(null);
    Calendar duringStart = Calendar.getInstance();
    Calendar card1Exp = Calendar.getInstance();
    card1Exp.add(Calendar.YEAR, 1);
    Calendar duringEnd1 = Calendar.getInstance();
    duringEnd1.add(Calendar.YEAR, 2);
    Calendar card2Exp = Calendar.getInstance();
    card2Exp.add(Calendar.YEAR, 3);
    Calendar duringEnd2 = Calendar.getInstance();
    duringEnd2.add(Calendar.YEAR, 4);
    newCard1.setExpirationDate(card1Exp.getTime());
    metacards.add(newCard1);
    MetacardImpl newCard2 = new MetacardImpl();
    newCard2.setId(null);
    newCard2.setExpirationDate(card2Exp.getTime());
    metacards.add(newCard2);
    String mcId1 = null;
    String mcId2 = null;
    CreateResponse createResponse = null;
    try {
        createResponse = framework.create(new CreateRequestImpl(metacards, null));
    } catch (IngestException e1) {
        LOGGER.error("Failure", e1);
        fail();
    } catch (SourceUnavailableException e1) {
        LOGGER.error("Failure", e1);
        fail();
    }
    assertEquals(createResponse.getCreatedMetacards().size(), metacards.size());
    for (Metacard curCard : createResponse.getCreatedMetacards()) {
        if (curCard.getExpirationDate().equals(card1Exp.getTime())) {
            mcId1 = curCard.getId();
        } else {
            mcId2 = curCard.getId();
        }
        assertNotNull(curCard.getId());
    }
    FilterFactory filterFactory = new FilterFactoryImpl();
    Period duringPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(duringStart.getTime())), new DefaultInstant(new DefaultPosition(duringEnd1.getTime())));
    QueryImpl query = new QueryImpl(filterFactory.during(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(duringPeriod)));
    QueryRequest queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("Expecting return 1 result.", 1, response.getHits());
        assertEquals("During filter should return metacard[" + mcId1 + "]", mcId1, response.getResults().get(0).getMetacard().getId());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
    duringPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(card1Exp.getTime())), new DefaultInstant(new DefaultPosition(duringEnd2.getTime())));
    query = new QueryImpl(filterFactory.during(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(duringPeriod)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("During filter should return 1 result", 1, response.getHits());
        assertEquals("During filter should return metacard[" + mcId2 + "]", mcId2, response.getResults().get(0).getMetacard().getId());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
    duringPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(duringStart.getTime())), new DefaultInstant(new DefaultPosition(duringEnd2.getTime())));
    query = new QueryImpl(filterFactory.during(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(duringPeriod)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("During filter should return 2 result", 2, response.getHits());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryRequest(ddf.catalog.operation.QueryRequest) CreateResponse(ddf.catalog.operation.CreateResponse) Calendar(java.util.Calendar) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) Period(org.opengis.temporal.Period) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) DefaultInstant(org.geotools.temporal.object.DefaultInstant) FederationException(ddf.catalog.federation.FederationException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) FilterFactory(org.opengis.filter.FilterFactory) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) DefaultPosition(org.geotools.temporal.object.DefaultPosition) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) Test(org.junit.Test)

Example 82 with UnsupportedQueryException

use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.

the class TestRegistryStore method setup.

@Before
public void setup() throws Exception {
    parser = new XmlParser();
    marshaller = new MetacardMarshaller(new XmlParser());
    context = mock(BundleContext.class);
    provider = mock(Converter.class);
    cswSourceConfiguration = new CswSourceConfiguration();
    factory = mock(SecureCxfClientFactory.class);
    transformer = mock(TransformerManager.class);
    encryptionService = mock(EncryptionService.class);
    configAdmin = mock(ConfigurationAdmin.class);
    configuration = mock(Configuration.class);
    subject = mock(Subject.class);
    queryResults = new ArrayList<>();
    registryStore = spy(new RegistryStoreImpl(context, cswSourceConfiguration, provider, factory, encryptionService) {

        @Override
        protected void validateOperation() {
        }

        @Override
        public boolean isAvailable() {
            return availability;
        }

        @Override
        protected SourceResponse query(QueryRequest queryRequest, ElementSetType elementSetName, List<QName> elementNames, Csw csw) throws UnsupportedQueryException {
            if (queryResults == null) {
                throw new UnsupportedQueryException("Test - Bad Query");
            }
            return new SourceResponseImpl(queryRequest, queryResults);
        }

        @Override
        protected CapabilitiesType getCapabilities() {
            return mock(CapabilitiesType.class);
        }

        @Override
        public void configureCswSource() {
        }

        ;

        @Override
        protected Subject getSystemSubject() {
            return subject;
        }

        @Override
        BundleContext getBundleContext() {
            return context;
        }
    });
    registryStore.setFilterBuilder(filterBuilder);
    registryStore.setFilterAdapter(filterAdapter);
    registryStore.setConfigAdmin(configAdmin);
    registryStore.setMetacardMarshaller(new MetacardMarshaller(parser));
    registryStore.setSchemaTransformerManager(transformer);
    registryStore.setAutoPush(true);
    registryStore.setRegistryUrl("http://test.url:0101/example");
    properties = new Hashtable<>();
    properties.put(RegistryStoreImpl.ID, "registryId");
    registryStore.setMetacardMarshaller(marshaller);
    when(configAdmin.getConfiguration(any())).thenReturn(configuration);
    when(configuration.getProperties()).thenReturn(properties);
}
Also used : CswSourceConfiguration(org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration) XmlParser(org.codice.ddf.parser.xml.XmlParser) TransformerManager(org.codice.ddf.spatial.ogc.csw.catalog.common.transformer.TransformerManager) Configuration(org.osgi.service.cm.Configuration) CswSourceConfiguration(org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration) QueryRequest(ddf.catalog.operation.QueryRequest) SecureCxfClientFactory(org.codice.ddf.cxf.SecureCxfClientFactory) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) MetacardMarshaller(org.codice.ddf.registry.schemabindings.helper.MetacardMarshaller) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) Subject(ddf.security.Subject) EncryptionService(ddf.security.encryption.EncryptionService) ElementSetType(net.opengis.cat.csw.v_2_0_2.ElementSetType) Converter(com.thoughtworks.xstream.converters.Converter) List(java.util.List) ArrayList(java.util.ArrayList) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Example 83 with UnsupportedQueryException

use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.

the class WfsSource method query.

@Override
public SourceResponse query(QueryRequest request) throws UnsupportedQueryException {
    Wfs wfs = factory.getClient();
    Query query = request.getQuery();
    if (query == null) {
        LOGGER.debug("WFS Source {}: Incoming query is null.", getId());
        return null;
    }
    LOGGER.debug("WFS Source {}: Received query: \n{}", getId(), query);
    SourceResponseImpl simpleResponse = null;
    GetFeatureType getFeature = buildGetFeatureRequest(query);
    try {
        LOGGER.debug("WFS Source {}: Sending query ...", getId());
        Wfs20FeatureCollection featureCollection = wfs.getFeature(getFeature);
        int numResults = -1;
        if (featureCollection == null) {
            throw new UnsupportedQueryException("Invalid results returned from server");
        }
        numResults = featureCollection.getMembers().size();
        if (featureCollection.getNumberReturned() == null) {
            LOGGER.debug("Number Returned Attribute was not added to the response");
        } else if (!featureCollection.getNumberReturned().equals(BigInteger.valueOf(numResults))) {
            LOGGER.debug("Number Returned Attribute ({}) did not match actual number returned ({})", featureCollection.getNumberReturned(), numResults);
        }
        availabilityTask.updateLastAvailableTimestamp(System.currentTimeMillis());
        LOGGER.debug("WFS Source {}: Received featureCollection with {} metacards.", getId(), numResults);
        List<Result> results = new ArrayList<Result>(numResults);
        for (int i = 0; i < numResults; i++) {
            Metacard mc = featureCollection.getMembers().get(i);
            mc = transform(mc, DEFAULT_WFS_TRANSFORMER_ID);
            Result result = new ResultImpl(mc);
            results.add(result);
            debugResult(result);
        }
        //Fetch total results available
        long totalResults = 0;
        if (featureCollection.getNumberMatched() == null) {
            totalResults = Long.valueOf(numResults);
        } else if (featureCollection.getNumberMatched().equals(UNKNOWN)) {
            totalResults = Long.valueOf(numResults);
        } else if (StringUtils.isNumeric(featureCollection.getNumberMatched())) {
            totalResults = Long.parseLong(featureCollection.getNumberMatched());
        }
        simpleResponse = new SourceResponseImpl(request, results, totalResults);
    } catch (WfsException wfse) {
        LOGGER.debug(WFS_ERROR_MESSAGE, wfse);
        throw new UnsupportedQueryException("Error received from WFS Server", wfse);
    } catch (Exception ce) {
        String msg = handleClientException(ce);
        throw new UnsupportedQueryException(msg, ce);
    }
    return simpleResponse;
}
Also used : Query(ddf.catalog.operation.Query) Wfs(org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.common.Wfs) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) Wfs20FeatureCollection(org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.common.Wfs20FeatureCollection) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) SecurityServiceException(ddf.security.service.SecurityServiceException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) WfsException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) JAXBException(javax.xml.bind.JAXBException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) WfsException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException) GetFeatureType(net.opengis.wfs.v_2_0_0.GetFeatureType)

Example 84 with UnsupportedQueryException

use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.

the class MetacardApplication method attemptDeleteDeletedMetacard.

private void attemptDeleteDeletedMetacard(String id) throws UnsupportedQueryException, SourceUnavailableException, FederationException {
    LOGGER.trace("Attemping to delete metacard [{}]", id);
    Filter tags = filterBuilder.attribute(Metacard.TAGS).is().like().text(DeletedMetacard.DELETED_TAG);
    Filter deletion = filterBuilder.attribute(DeletedMetacard.DELETION_OF_ID).is().like().text(id);
    Filter filter = filterBuilder.allOf(tags, deletion);
    QueryResponse response = null;
    try {
        response = catalogFramework.query(new QueryRequestImpl(new QueryImpl(filter), false));
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.debug("Could not find the deleted metacard marker to delete", e);
    }
    if (response == null || response.getResults() == null || response.getResults().size() != 1) {
        LOGGER.debug("There should have been one deleted metacard marker");
        return;
    }
    final QueryResponse _response = response;
    try {
        executeAsSystem(() -> catalogFramework.delete(new DeleteRequestImpl(_response.getResults().get(0).getMetacard().getId())));
    } catch (ExecutionException e) {
        LOGGER.debug("Could not delete the deleted metacard marker", e);
    }
    LOGGER.trace("Deleted delete marker metacard successfully");
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) QueryResponse(ddf.catalog.operation.QueryResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) FederationException(ddf.catalog.federation.FederationException) ExecutionException(org.apache.shiro.subject.ExecutionException)

Example 85 with UnsupportedQueryException

use of ddf.catalog.source.UnsupportedQueryException in project ddf by codice.

the class Associated method putAssociations.

public void putAssociations(String id, Collection<Edge> edges) throws UnsupportedQueryException, SourceUnavailableException, FederationException, IngestException {
    Collection<Edge> oldEdges = getAssociations(id);
    List<String> ids = Stream.concat(oldEdges.stream(), edges.stream()).flatMap(e -> Stream.of(e.child, e.parent)).filter(Objects::nonNull).map(m -> m.get(Metacard.ID)).filter(Objects::nonNull).map(Object::toString).distinct().collect(Collectors.toList());
    Map<String, Metacard> metacards = util.getMetacards(ids, getNonrestrictedTagsFilter()).entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getMetacard()));
    Map<String, Metacard> changedMetacards = new HashMap<>();
    Set<Edge> oldEdgeSet = new HashSet<>(oldEdges);
    Set<Edge> newEdgeSet = new HashSet<>(edges);
    Set<Edge> oldDiff = Sets.difference(oldEdgeSet, newEdgeSet);
    Set<Edge> newDiff = Sets.difference(newEdgeSet, oldEdgeSet);
    for (Edge edge : oldDiff) {
        removeEdge(edge, metacards, changedMetacards);
    }
    for (Edge edge : newDiff) {
        addEdge(edge, metacards, changedMetacards);
    }
    if (changedMetacards.isEmpty()) {
        return;
    }
    catalogFramework.update(new UpdateRequestImpl(changedMetacards.keySet().toArray(new String[0]), new ArrayList<>(changedMetacards.values())));
}
Also used : QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) CatalogFramework(ddf.catalog.CatalogFramework) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MetacardVersion(ddf.catalog.core.versioning.MetacardVersion) SortBy(org.opengis.filter.sort.SortBy) EndpointUtil(org.codice.ddf.catalog.ui.util.EndpointUtil) Metacard(ddf.catalog.data.Metacard) Map(java.util.Map) HashCodeBuilder(org.apache.commons.lang3.builder.HashCodeBuilder) Result(ddf.catalog.data.Result) EqualsBuilder(org.apache.commons.lang3.builder.EqualsBuilder) QueryImpl(ddf.catalog.operation.impl.QueryImpl) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) IngestException(ddf.catalog.source.IngestException) DeletedMetacard(ddf.catalog.core.versioning.DeletedMetacard) Set(java.util.Set) FederationException(ddf.catalog.federation.FederationException) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) QueryResponse(ddf.catalog.operation.QueryResponse) List(java.util.List) Stream(java.util.stream.Stream) Attribute(ddf.catalog.data.Attribute) Optional(java.util.Optional) Filter(org.opengis.filter.Filter) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Metacard(ddf.catalog.data.Metacard) DeletedMetacard(ddf.catalog.core.versioning.DeletedMetacard) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)85 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)34 FederationException (ddf.catalog.federation.FederationException)31 QueryRequest (ddf.catalog.operation.QueryRequest)31 Metacard (ddf.catalog.data.Metacard)28 QueryImpl (ddf.catalog.operation.impl.QueryImpl)27 Filter (org.opengis.filter.Filter)27 ArrayList (java.util.ArrayList)26 Result (ddf.catalog.data.Result)25 QueryResponse (ddf.catalog.operation.QueryResponse)25 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)25 Test (org.junit.Test)21 SourceResponse (ddf.catalog.operation.SourceResponse)16 IngestException (ddf.catalog.source.IngestException)15 IOException (java.io.IOException)15 Query (ddf.catalog.operation.Query)12 CreateResponse (ddf.catalog.operation.CreateResponse)10 GeotoolsFilterAdapterImpl (ddf.catalog.filter.proxy.adapter.GeotoolsFilterAdapterImpl)9 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)9 Subject (ddf.security.Subject)9