Search in sources :

Example 1 with SearchResource

use of org.codice.ddf.confluence.api.SearchResource 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)

Aggregations

Result (ddf.catalog.data.Result)1 Query (ddf.catalog.operation.Query)1 ResourceResponse (ddf.catalog.operation.ResourceResponse)1 SourceResponse (ddf.catalog.operation.SourceResponse)1 SourceResponseImpl (ddf.catalog.operation.impl.SourceResponseImpl)1 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)1 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Response (javax.ws.rs.core.Response)1 SearchResource (org.codice.ddf.confluence.api.SearchResource)1