Search in sources :

Example 31 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)

Example 32 with SourceResponseImpl

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");
    }
}
Also used : Query(ddf.catalog.operation.Query) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) InputStream(java.io.InputStream) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) SearchResource(org.codice.ddf.confluence.api.SearchResource) Result(ddf.catalog.data.Result) ResourceResponse(ddf.catalog.operation.ResourceResponse) SourceResponse(ddf.catalog.operation.SourceResponse) Response(javax.ws.rs.core.Response)

Example 33 with SourceResponseImpl

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) FeedException(com.rometools.rome.io.FeedException) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) Result(ddf.catalog.data.Result) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SyndFeedInput(com.rometools.rome.io.SyndFeedInput)

Example 34 with SourceResponseImpl

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);
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) JSONObject(net.minidev.json.JSONObject) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) Test(org.junit.Test)

Example 35 with SourceResponseImpl

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;
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) LinkedList(java.util.LinkedList) Result(ddf.catalog.data.Result)

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