Search in sources :

Example 16 with SourceResponseImpl

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

the class TestXmlResponseQueueTransformer method givenMetacardTypeName.

private SourceResponse givenMetacardTypeName(String metacardTypeName) {
    MetacardType type = getMetacardTypeStub(metacardTypeName, new HashSet<AttributeDescriptor>());
    Metacard metacard = new MetacardImpl(type);
    SourceResponse response = new SourceResponseImpl(null, Arrays.asList((Result) new ResultImpl(metacard)));
    return response;
}
Also used : Metacard(ddf.catalog.data.Metacard) SourceResponse(ddf.catalog.operation.SourceResponse) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) ResultImpl(ddf.catalog.data.impl.ResultImpl) MetacardType(ddf.catalog.data.MetacardType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result)

Example 17 with SourceResponseImpl

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

the class TestZipCompression method createSourceResponseWithURISchemes.

private SourceResponse createSourceResponseWithURISchemes(String scheme, String derivedResourceScheme) throws Exception {
    List<Result> resultList = new ArrayList<>();
    for (String string : METACARD_ID_LIST) {
        MetacardImpl metacard = new MetacardImpl();
        metacard.setId(string);
        if (scheme != null && string.equals(METACARD_ID_LIST.get(METACARD_ID_LIST.size() - 1))) {
            URI uri = new URI(scheme + metacard.getId());
            metacard.setResourceURI(uri);
            if (StringUtils.isNotBlank(derivedResourceScheme)) {
                metacard.setAttribute(Metacard.DERIVED_RESOURCE_URI, derivedResourceScheme);
            }
        }
        Result result = new ResultImpl(metacard);
        resultList.add(result);
    }
    return new SourceResponseImpl(null, resultList);
}
Also used : SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) Matchers.anyString(org.mockito.Matchers.anyString) URI(java.net.URI) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result)

Example 18 with SourceResponseImpl

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

the class SolrMetacardClientImpl method query.

@Override
public SourceResponse query(QueryRequest request) throws UnsupportedQueryException {
    if (request == null || request.getQuery() == null) {
        return new QueryResponseImpl(request, new ArrayList<Result>(), true, 0L);
    }
    SolrQuery query = getSolrQuery(request, filterDelegateFactory.newInstance(resolver));
    long totalHits;
    List<Result> results = new ArrayList<>();
    try {
        QueryResponse solrResponse = client.query(query, SolrRequest.METHOD.POST);
        totalHits = solrResponse.getResults().getNumFound();
        SolrDocumentList docs = solrResponse.getResults();
        for (SolrDocument doc : docs) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("SOLR DOC: {}", doc.getFieldValue(Metacard.ID + SchemaFields.TEXT_SUFFIX));
            }
            ResultImpl tmpResult;
            try {
                tmpResult = createResult(doc);
            } catch (MetacardCreationException e) {
                throw new UnsupportedQueryException("Could not create metacard(s).", e);
            }
            results.add(tmpResult);
        }
    } catch (SolrServerException | IOException | SolrException e) {
        throw new UnsupportedQueryException("Could not complete solr query.", e);
    }
    SourceResponse sourceResponse = new SourceResponseImpl(request, results, totalHits);
    return sourceResponse;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) SourceResponse(ddf.catalog.operation.SourceResponse) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Result(ddf.catalog.data.Result) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrException(org.apache.solr.common.SolrException)

Example 19 with SourceResponseImpl

use of ddf.catalog.operation.impl.SourceResponseImpl 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();
    LOGGER.debug("WFS Source {}: Received query: \n{}", getId(), query);
    if (query.getStartIndex() < 1) {
        throw new UnsupportedQueryException("Start Index is one-based and must be an integer greater than 0; should not be [" + query.getStartIndex() + "]");
    }
    SourceResponseImpl simpleResponse = null;
    // WFS v1.0 specification does not support response indicating total
    // number
    // of features satisfying query constraints.
    // Hence, we save off the original
    // page size from the query request and create a copy of the query,
    // changing
    // the page size by a multiplier and the current page number of results
    // so that
    // more features are returned as the user pages through the results,
    // getting
    // a better sense of how many total features exist that satisfy the
    // query.
    int origPageSize = query.getPageSize();
    if (origPageSize <= 0 || origPageSize > WFS_MAX_FEATURES_RETURNED) {
        origPageSize = WFS_MAX_FEATURES_RETURNED;
    }
    QueryImpl modifiedQuery = new QueryImpl(query);
    // Determine current page number of results being requested.
    // Example: startIndex = 21 and origPageSize=10, then requesting to go
    // to page number 3.
    // Note: Integer division will truncate remainders so 4 / 2 will return 0 and not .5.  Also,
    // pages are numbered 1 - N so we add 1 to the result
    int pageNumber = query.getStartIndex() / origPageSize + 1;
    // Modified page size is based on current page number and a constant
    // multiplier,
    // but limited to a max value to prevent time consuming queries just to
    // get an
    // approximation of total number of features.
    // So as page number increases the pageSize increases.
    // Example:
    // pageNumber=2, modifiedPageSize=60
    // pageNumber=3, modifiedPageSize=90
    int modifiedPageSize = Math.min(pageNumber * origPageSize * WFS_QUERY_PAGE_SIZE_MULTIPLIER, WFS_MAX_FEATURES_RETURNED);
    LOGGER.debug("WFS Source {}: modified page size = {}", getId(), modifiedPageSize);
    modifiedQuery.setPageSize(modifiedPageSize);
    GetFeatureType getFeature = buildGetFeatureRequest(modifiedQuery);
    try {
        LOGGER.debug("WFS Source {}: Sending query ...", getId());
        WfsFeatureCollection featureCollection = wfs.getFeature(getFeature);
        if (featureCollection == null) {
            throw new UnsupportedQueryException("Invalid results returned from server");
        }
        availabilityTask.updateLastAvailableTimestamp(System.currentTimeMillis());
        LOGGER.debug("WFS Source {}: Received featureCollection with {} metacards.", getId(), featureCollection.getFeatureMembers().size());
        // Only return the number of results originally asked for in the
        // query, or the entire list of results if it is smaller than the
        // original page size.
        int numberOfResultsToReturn = Math.min(origPageSize, featureCollection.getFeatureMembers().size());
        List<Result> results = new ArrayList<Result>(numberOfResultsToReturn);
        int stopIndex = Math.min((origPageSize * pageNumber) + query.getStartIndex(), featureCollection.getFeatureMembers().size() + 1);
        LOGGER.debug("WFS Source {}: startIndex = {}, stopIndex = {}, origPageSize = {}, pageNumber = {}", getId(), query.getStartIndex(), stopIndex, origPageSize, pageNumber);
        for (int i = query.getStartIndex(); i < stopIndex; i++) {
            Metacard mc = featureCollection.getFeatureMembers().get(i - 1);
            mc = transform(mc, DEFAULT_WFS_TRANSFORMER_ID);
            Result result = new ResultImpl(mc);
            results.add(result);
            debugResult(result);
        }
        Long totalHits = (long) featureCollection.getFeatureMembers().size();
        simpleResponse = new SourceResponseImpl(request, results, totalHits);
    } 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.v1_0_0.catalog.common.Wfs) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) WfsException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) JAXBException(javax.xml.bind.JAXBException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) WebApplicationException(javax.ws.rs.WebApplicationException) SecurityServiceException(ddf.security.service.SecurityServiceException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Result(ddf.catalog.data.Result) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Metacard(ddf.catalog.data.Metacard) WfsException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException) WfsFeatureCollection(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsFeatureCollection) GetFeatureType(ogc.schema.opengis.wfs.v_1_0_0.GetFeatureType)

Example 20 with SourceResponseImpl

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

the class TwitterSource method query.

@Override
public SourceResponse query(QueryRequest request) throws UnsupportedQueryException {
    Twitter instance = twitterFactory.getInstance();
    try {
        instance.getOAuth2Token();
    } catch (TwitterException e) {
        throw new UnsupportedQueryException("Unable to get OAuth2 token.", e);
    }
    TwitterFilterVisitor visitor = new TwitterFilterVisitor();
    request.getQuery().accept(visitor, null);
    Query query = new Query();
    query.setCount(request.getQuery().getPageSize());
    if (visitor.hasSpatial()) {
        GeoLocation geoLocation = new GeoLocation(visitor.getLatitude(), visitor.getLongitude());
        query.setGeoCode(geoLocation, visitor.getRadius(), Query.Unit.km);
    }
    if (visitor.getContextualSearch() != null) {
        query.setQuery(visitor.getContextualSearch().getSearchPhrase());
    }
    if (visitor.getTemporalSearch() != null) {
        Calendar.Builder builder = new Calendar.Builder();
        builder.setInstant(visitor.getTemporalSearch().getStartDate());
        Calendar calendar = builder.build();
        query.setSince(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DAY_OF_MONTH));
        builder = new Calendar.Builder();
        builder.setInstant(visitor.getTemporalSearch().getEndDate());
        calendar = builder.build();
        query.setUntil(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DAY_OF_MONTH));
    }
    QueryResult queryResult;
    try {
        queryResult = instance.search().search(query);
    } catch (TwitterException e) {
        throw new UnsupportedQueryException(e);
    }
    List<Result> resultList = new ArrayList<>(queryResult.getCount());
    resultList.addAll(queryResult.getTweets().stream().map(status -> new ResultImpl(getMetacard(status))).collect(Collectors.toList()));
    return new SourceResponseImpl(request, resultList);
}
Also used : Query(twitter4j.Query) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ConfigurationBuilder(twitter4j.conf.ConfigurationBuilder) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Twitter(twitter4j.Twitter) ResultImpl(ddf.catalog.data.impl.ResultImpl) QueryResult(twitter4j.QueryResult) Result(ddf.catalog.data.Result) QueryResult(twitter4j.QueryResult) GeoLocation(twitter4j.GeoLocation) TwitterException(twitter4j.TwitterException)

Aggregations

SourceResponseImpl (ddf.catalog.operation.impl.SourceResponseImpl)36 SourceResponse (ddf.catalog.operation.SourceResponse)24 ResultImpl (ddf.catalog.data.impl.ResultImpl)20 Result (ddf.catalog.data.Result)19 Test (org.junit.Test)19 Metacard (ddf.catalog.data.Metacard)17 QueryRequest (ddf.catalog.operation.QueryRequest)13 ArrayList (java.util.ArrayList)12 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)9 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)6 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)6 IOException (java.io.IOException)6 Query (ddf.catalog.operation.Query)5 BundleContext (org.osgi.framework.BundleContext)5 Matchers.anyString (org.mockito.Matchers.anyString)4 BinaryContent (ddf.catalog.data.BinaryContent)3 QueryImpl (ddf.catalog.operation.impl.QueryImpl)3 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)3 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)3 Serializable (java.io.Serializable)3