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;
}
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);
}
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;
}
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;
}
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);
}
Aggregations