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);
}
use of ddf.catalog.operation.impl.SourceResponseImpl in project ddf by codice.
the class ConfluenceSource method query.
@Override
public SourceResponse query(QueryRequest request) throws UnsupportedQueryException {
Query query = request.getQuery();
ConfluenceFilterDelegate confluenceDelegate = new ConfluenceFilterDelegate();
String cql = filterAdapter.adapt(query, confluenceDelegate);
if (!confluenceDelegate.isConfluenceQuery() || (StringUtils.isEmpty(cql) && (confluenceSpaces.isEmpty() || !confluenceDelegate.isWildCardQuery()))) {
return new SourceResponseImpl(request, Collections.emptyList());
}
cql = getSortedQuery(query.getSortBy(), getSpaceQuery(cql));
LOGGER.debug(cql);
String finalExpandedSections = expandedSections;
if (includePageContent) {
finalExpandedSections += ",body.view";
}
SearchResource confluence = getClientFactory().getClient();
String cqlContext = null;
String excerpt = null;
Response confluenceResponse = confluence.search(cql, cqlContext, excerpt, finalExpandedSections, query.getStartIndex() - 1, query.getPageSize(), includeArchivedSpaces);
InputStream stream = null;
Object entityObj = confluenceResponse.getEntity();
if (entityObj != null) {
stream = (InputStream) entityObj;
}
if (Response.Status.OK.getStatusCode() != confluenceResponse.getStatus()) {
String error = "";
try {
if (stream != null) {
error = IOUtils.toString(stream);
}
} catch (IOException ioe) {
LOGGER.debug("Could not convert error message to a string for output.", ioe);
}
throw new UnsupportedQueryException(String.format("Received error code from remote source (status %s ): %s", confluenceResponse.getStatus(), error));
}
try {
List<Result> results = transformer.transformConfluenceResponse(stream).stream().map(this::getUpdatedResult).collect(Collectors.toList());
return new SourceResponseImpl(request, results);
} catch (IOException | CatalogTransformerException e) {
throw new UnsupportedQueryException("Exception processing results from Confluence");
}
}
use of ddf.catalog.operation.impl.SourceResponseImpl in project ddf by codice.
the class OpenSearchSource method processResponse.
/**
* @param is
* @param queryRequest
* @return
* @throws ddf.catalog.source.UnsupportedQueryException
*/
private SourceResponseImpl processResponse(InputStream is, QueryRequest queryRequest) throws UnsupportedQueryException {
List<Result> resultQueue = new ArrayList<>();
SyndFeedInput syndFeedInput = new SyndFeedInput();
SyndFeed syndFeed = null;
try {
syndFeed = syndFeedInput.build(new InputStreamReader(is, StandardCharsets.UTF_8));
} catch (FeedException e) {
LOGGER.debug("Unable to read RSS/Atom feed.", e);
}
List<SyndEntry> entries = null;
long totalResults = 0;
if (syndFeed != null) {
entries = syndFeed.getEntries();
for (SyndEntry entry : entries) {
resultQueue.addAll(createResponseFromEntry(entry));
}
totalResults = entries.size();
List<Element> foreignMarkup = syndFeed.getForeignMarkup();
for (Element element : foreignMarkup) {
if (element.getName().equals("totalResults")) {
try {
totalResults = Long.parseLong(element.getContent(0).getValue());
} catch (NumberFormatException | IndexOutOfBoundsException e) {
// totalResults is already initialized to the correct value, so don't change it here.
LOGGER.debug("Received invalid number of results.", e);
}
}
}
}
SourceResponseImpl response = new SourceResponseImpl(queryRequest, resultQueue);
response.setHits(totalResults);
return response;
}
use of ddf.catalog.operation.impl.SourceResponseImpl in project ddf by codice.
the class TestGeoJsonQueryResponseTransformer method testNullResults.
@Test
public void testNullResults() throws CatalogTransformerException, IOException, ParseException {
SourceResponse sourceResponse = new SourceResponseImpl(null, null, 0L);
JSONObject obj = transform(sourceResponse, 0, 0);
verifyResponse(obj, 0, 0);
}
use of ddf.catalog.operation.impl.SourceResponseImpl in project ddf by codice.
the class TestGeoJsonQueryResponseTransformer method setupResponse.
private SourceResponse setupResponse(int count, long hitsTotal) {
List<Result> results = new LinkedList<Result>();
for (int i = 0; i < count; i++) {
results.add(setupResult());
}
SourceResponse sourceResponse = new SourceResponseImpl(null, results, hitsTotal);
return sourceResponse;
}
Aggregations