Search in sources :

Example 1 with ResultImpl

use of ddf.catalog.data.impl.ResultImpl in project ddf by codice.

the class OpenSearchSource method query.

@Override
public SourceResponse query(QueryRequest queryRequest) throws UnsupportedQueryException {
    String methodName = "query";
    LOGGER.trace(methodName);
    Serializable metacardId = queryRequest.getPropertyValue(Metacard.ID);
    SourceResponseImpl response = null;
    Subject subject = null;
    WebClient restWebClient = null;
    if (queryRequest.hasProperties()) {
        Object subjectObj = queryRequest.getProperties().get(SecurityConstants.SECURITY_SUBJECT);
        subject = (Subject) subjectObj;
    }
    restWebClient = factory.getWebClientForSubject(subject);
    Query query = queryRequest.getQuery();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Received query: " + query);
    }
    boolean canDoOpenSearch = setOpenSearchParameters(query, subject, restWebClient);
    if (canDoOpenSearch) {
        InputStream responseStream = performRequest(restWebClient);
        response = new SourceResponseImpl(queryRequest, new ArrayList<Result>());
        if (responseStream != null) {
            response = processResponse(responseStream, queryRequest);
        }
    } else {
        if (StringUtils.isEmpty((String) metacardId)) {
            OpenSearchFilterVisitor visitor = new OpenSearchFilterVisitor();
            query.accept(visitor, null);
            metacardId = visitor.getMetacardId();
        }
        restWebClient = newRestClient(query, (String) metacardId, false, subject);
        if (restWebClient != null) {
            InputStream responseStream = performRequest(restWebClient);
            Metacard metacard = null;
            List<Result> resultQueue = new ArrayList<Result>();
            try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
                if (responseStream != null) {
                    IOUtils.copyLarge(responseStream, fileBackedOutputStream);
                    InputTransformer inputTransformer = null;
                    try (InputStream inputStream = fileBackedOutputStream.asByteSource().openStream()) {
                        inputTransformer = getInputTransformer(inputStream);
                    } catch (IOException e) {
                        LOGGER.debug("Problem with transformation.", e);
                    }
                    if (inputTransformer != null) {
                        try (InputStream inputStream = fileBackedOutputStream.asByteSource().openStream()) {
                            metacard = inputTransformer.transform(inputStream);
                        } catch (IOException e) {
                            LOGGER.debug("Problem with transformation.", e);
                        }
                    }
                }
            } catch (IOException | CatalogTransformerException e) {
                LOGGER.debug("Problem with transformation.", e);
            }
            if (metacard != null) {
                metacard.setSourceId(getId());
                ResultImpl result = new ResultImpl(metacard);
                resultQueue.add(result);
                response = new SourceResponseImpl(queryRequest, resultQueue);
                response.setHits(resultQueue.size());
            }
        }
    }
    LOGGER.trace(methodName);
    return response;
}
Also used : Serializable(java.io.Serializable) Query(ddf.catalog.operation.Query) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ResultImpl(ddf.catalog.data.impl.ResultImpl) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) WebClient(org.apache.cxf.jaxrs.client.WebClient) Subject(ddf.security.Subject) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard)

Example 2 with ResultImpl

use of ddf.catalog.data.impl.ResultImpl in project ddf by codice.

the class OpenSearchSource method createResponseFromEntry.

/**
     * Creates a single response from input parameters. Performs XPath operations on the document to
     * retrieve data not passed in.
     *
     * @param entry a single Atom entry
     * @return single response
     * @throws ddf.catalog.source.UnsupportedQueryException
     */
private List<Result> createResponseFromEntry(SyndEntry entry) throws UnsupportedQueryException {
    String id = entry.getUri();
    if (id != null && !id.isEmpty()) {
        id = id.substring(id.lastIndexOf(':') + 1);
    }
    List<SyndContent> contents = entry.getContents();
    List<SyndCategory> categories = entry.getCategories();
    List<Metacard> metacards = new ArrayList<>();
    List<Element> foreignMarkup = entry.getForeignMarkup();
    String relevance = "";
    String source = "";
    for (Element element : foreignMarkup) {
        if (element.getName().equals("score")) {
            relevance = element.getContent(0).getValue();
        }
    }
    //we currently do not support downloading content via an RSS enclosure, this support can be added at a later date if we decide to include it
    for (SyndContent content : contents) {
        MetacardImpl metacard = getMetacardImpl(parseContent(content.getValue(), id));
        metacard.setSourceId(this.shortname);
        String title = metacard.getTitle();
        if (StringUtils.isEmpty(title)) {
            metacard.setTitle(entry.getTitle());
        }
        if (!source.isEmpty()) {
            metacard.setSourceId(source);
        }
        metacards.add(metacard);
    }
    for (int i = 0; i < categories.size() && i < metacards.size(); i++) {
        SyndCategory category = categories.get(i);
        Metacard metacard = metacards.get(i);
        if (StringUtils.isBlank(metacard.getContentTypeName())) {
            ((MetacardImpl) metacard).setContentTypeName(category.getName());
        }
    }
    List<Result> results = new ArrayList<>();
    for (Metacard metacard : metacards) {
        ResultImpl result = new ResultImpl(metacard);
        if (relevance == null || relevance.isEmpty()) {
            LOGGER.debug("couldn't find valid relevance. Setting relevance to 0");
            relevance = "0";
        }
        result.setRelevanceScore(new Double(relevance));
        results.add(result);
    }
    return results;
}
Also used : SyndCategory(com.rometools.rome.feed.synd.SyndCategory) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) SyndContent(com.rometools.rome.feed.synd.SyndContent)

Example 3 with ResultImpl

use of ddf.catalog.data.impl.ResultImpl in project ddf by codice.

the class TestCswQueryResponseTransformer method createResults.

private List<Result> createResults(int start, int finish) {
    List<Result> list = new LinkedList<>();
    for (int i = start; i <= finish; i++) {
        MetacardImpl metacard = new MetacardImpl();
        metacard.setId("id_" + i);
        metacard.setSourceId("source_" + i);
        metacard.setTitle("title " + i);
        list.add(new ResultImpl(metacard));
    }
    return list;
}
Also used : ResultImpl(ddf.catalog.data.impl.ResultImpl) LinkedList(java.util.LinkedList) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result)

Example 4 with ResultImpl

use of ddf.catalog.data.impl.ResultImpl in project ddf by codice.

the class TestAtomTransformer method getSourceResponseStub.

protected SourceResponse getSourceResponseStub(String id, String wkt) {
    SourceResponse response = mock(SourceResponse.class);
    when(response.getHits()).thenReturn(new Long(1));
    when(response.getRequest()).thenReturn(getStubRequest());
    ResultImpl result = new ResultImpl();
    MetacardStub metacard = new MetacardStub("");
    metacard.setId(id);
    metacard.setLocation(wkt);
    result.setMetacard(metacard);
    when(response.getResults()).thenReturn(Arrays.asList((Result) result));
    return response;
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) ResultImpl(ddf.catalog.data.impl.ResultImpl) Result(ddf.catalog.data.Result)

Example 5 with ResultImpl

use of ddf.catalog.data.impl.ResultImpl in project ddf by codice.

the class TestAtomTransformer method testMetacardIsNull.

/**
     * The following rules must be followed in order to be compliant with the Atom specification as
     * defined by http://tools.ietf.org/html/rfc4287#section-4.1.2 <br/>
     * "The following child elements are defined by this specification (note that the presence of
     * some of these elements is required):
     * <p>
     * <li/>atom:entry elements MUST contain one or more atom:author elements, unless the atom:entry
     * contains an atom:source element that contains an atom:author element or, in an Atom Feed
     * Document, the atom:feed element contains an atom:author element itself.
     * <p>
     * <li/>atom:entry elements MAY contain any number of atom:category elements.
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:content element.
     * <p>
     * <li/>atom:entry elements MAY contain any number of atom:contributor elements.
     * <p>
     * <li/>atom:entry elements MUST contain exactly one atom:id element.
     * <p>
     * <li/>atom:entry elements that contain no child atom:content element MUST contain at least one
     * atom:link element with a rel attribute value of "alternate".
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:link element with a rel
     * attribute value of "alternate" that has the same combination of type and hreflang attribute
     * values.
     * <p>
     * <li/>atom:entry elements MAY contain additional atom:link elements beyond those described
     * above.
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:published element.
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:rights element.
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:source element.
     * <p>
     * <li/>atom:entry elements MUST contain an atom:summary element in either of the following
     * cases:
     * <p>
     * <ul>
     * the atom:entry contains an atom:content that has a "src" attribute (and is thus empty).
     * </ul>
     * <p>
     * <ul>
     * the atom:entry contains content that is encoded in Base64; i.e., the "type" attribute of
     * atom:content is a MIME media type [MIMEREG], but is not an XML media type [RFC3023], does not
     * begin with "text/", and does not end with "/xml" or "+xml".
     * </ul>
     * <p>
     * <li/>atom:entry elements MUST NOT contain more than one atom:summary element.
     * <p>
     * <li/>atom:entry elements MUST contain exactly one atom:title element.
     * <p>
     * <li/>atom:entry elements MUST contain exactly one atom:updated element."
     *
     * @throws CatalogTransformerException
     * @throws IOException
     * @throws SAXException
     * @throws XpathException
     */
@Test
public void testMetacardIsNull() throws IOException, CatalogTransformerException, XpathException, SAXException {
    // given
    AtomTransformer transformer = new AtomTransformer();
    MetacardTransformer metacardTransformer = getXmlMetacardTransformerStub();
    transformer.setMetacardTransformer(metacardTransformer);
    setDefaultSystemConfiguration();
    SourceResponse response = mock(SourceResponse.class);
    when(response.getRequest()).thenReturn(getStubRequest());
    ResultImpl result1 = new ResultImpl();
    ResultImpl result2 = new ResultImpl();
    MetacardStub metacard = new MetacardStub("");
    metacard.setId(SAMPLE_ID);
    metacard.setSourceId(SAMPLE_SOURCE_ID);
    result1.setMetacard(metacard);
    result2.setMetacard(null);
    when(response.getResults()).thenReturn(Arrays.asList((Result) result1, result2));
    // when
    BinaryContent binaryContent = transformer.transform(response, null);
    // then
    byte[] bytes = binaryContent.getByteArray();
    /* used to visualize */
    IOUtils.write(bytes, new FileOutputStream(new File(TARGET_FOLDER + getMethodName() + ATOM_EXTENSION)));
    String output = new String(bytes);
    assertFeedCompliant(output);
    assertEntryCompliant(output);
    validateAgainstAtomSchema(bytes);
    assertXpathEvaluatesTo(SAMPLE_SOURCE_ID, "/atom:feed/atom:entry/fs:resultSource/@fs:sourceId", output);
    assertXpathEvaluatesTo("", "/atom:feed/atom:entry/fs:resultSource", output);
    assertXpathEvaluatesTo(AtomTransformer.URN_CATALOG_ID + SAMPLE_ID, "/atom:feed/atom:entry/atom:id", output);
    assertXpathEvaluatesTo(MetacardStub.DEFAULT_TITLE, "/atom:feed/atom:entry/atom:title", output);
    assertXpathExists("/atom:feed/atom:entry/atom:updated", output);
    assertXpathExists("/atom:feed/atom:entry/atom:content", output);
}
Also used : MetacardTransformer(ddf.catalog.transform.MetacardTransformer) SourceResponse(ddf.catalog.operation.SourceResponse) FileOutputStream(java.io.FileOutputStream) ResultImpl(ddf.catalog.data.impl.ResultImpl) BinaryContent(ddf.catalog.data.BinaryContent) File(java.io.File) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Aggregations

ResultImpl (ddf.catalog.data.impl.ResultImpl)159 Result (ddf.catalog.data.Result)114 Test (org.junit.Test)100 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)87 Metacard (ddf.catalog.data.Metacard)61 ArrayList (java.util.ArrayList)55 SourceResponse (ddf.catalog.operation.SourceResponse)50 QueryRequest (ddf.catalog.operation.QueryRequest)39 QueryResponse (ddf.catalog.operation.QueryResponse)31 SourceResponseImpl (ddf.catalog.operation.impl.SourceResponseImpl)31 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)29 BinaryContent (ddf.catalog.data.BinaryContent)26 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)21 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)20 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)18 DAG (org.codice.alliance.nsili.common.UCO.DAG)17 File (java.io.File)14 Serializable (java.io.Serializable)12 QueryImpl (ddf.catalog.operation.impl.QueryImpl)11 FileOutputStream (java.io.FileOutputStream)10