Search in sources :

Example 1 with MetacardImpl

use of ddf.catalog.data.impl.MetacardImpl 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 2 with MetacardImpl

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

the class TestOpenSearchSource method testQueryBySearchPhraseContentTypeSetRss.

@Test
public void testQueryBySearchPhraseContentTypeSetRss() throws UnsupportedQueryException, URISyntaxException, IOException {
    WebClient client = mock(WebClient.class);
    Response clientResponse = mock(Response.class);
    when(client.get()).thenReturn(clientResponse);
    when(clientResponse.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
    when(clientResponse.getEntity()).thenReturn(getSampleRssStream());
    SecureCxfClientFactory factory = getMockFactory(client);
    OverriddenOpenSearchSource source = new OverriddenOpenSearchSource(FILTER_ADAPTER, encryptionService);
    InputTransformer inputTransformer = mock(InputTransformer.class);
    MetacardImpl generatedMetacard = new MetacardImpl();
    generatedMetacard.setMetadata(getSample());
    generatedMetacard.setId(SAMPLE_ID);
    generatedMetacard.setContentTypeName("myType");
    try {
        when(inputTransformer.transform(isA(InputStream.class))).thenReturn(generatedMetacard);
        when(inputTransformer.transform(isA(InputStream.class), isA(String.class))).thenReturn(generatedMetacard);
    } catch (IOException e) {
        fail();
    } catch (CatalogTransformerException e) {
        fail();
    }
    source.setInputTransformer(inputTransformer);
    source.setEndpointUrl("http://localhost:8181/services/catalog/query");
    source.init();
    source.setParameters(DEFAULT_PARAMETERS);
    source.factory = factory;
    Filter filter = filterBuilder.attribute(Metacard.METADATA).like().text(SAMPLE_SEARCH_PHRASE);
    SourceResponse response = source.query(new QueryRequestImpl(new QueryImpl(filter)));
    Assert.assertEquals(1, response.getHits());
    List<Result> results = response.getResults();
    Assert.assertTrue(results.size() == 1);
    Result result = results.get(0);
    Metacard metacard = result.getMetacard();
    Assert.assertNotNull(metacard);
    Assert.assertEquals("myType", metacard.getContentTypeName());
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) SecureCxfClientFactory(org.codice.ddf.cxf.SecureCxfClientFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) WebClient(org.apache.cxf.jaxrs.client.WebClient) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Response(javax.ws.rs.core.Response) ResourceResponse(ddf.catalog.operation.ResourceResponse) SourceResponse(ddf.catalog.operation.SourceResponse) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Metacard(ddf.catalog.data.Metacard) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 3 with MetacardImpl

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

the class TestOpenSearchSource method testQueryBySearchPhraseContentTypeSet.

@Test
public void testQueryBySearchPhraseContentTypeSet() throws UnsupportedQueryException, URISyntaxException, IOException {
    WebClient client = mock(WebClient.class);
    Response clientResponse = mock(Response.class);
    when(client.get()).thenReturn(clientResponse);
    when(clientResponse.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
    when(clientResponse.getEntity()).thenReturn(getSampleAtomStream());
    SecureCxfClientFactory factory = getMockFactory(client);
    OverriddenOpenSearchSource source = new OverriddenOpenSearchSource(FILTER_ADAPTER, encryptionService);
    InputTransformer inputTransformer = mock(InputTransformer.class);
    MetacardImpl generatedMetacard = new MetacardImpl();
    generatedMetacard.setMetadata(getSample());
    generatedMetacard.setId(SAMPLE_ID);
    generatedMetacard.setContentTypeName("myType");
    try {
        when(inputTransformer.transform(isA(InputStream.class))).thenReturn(generatedMetacard);
        when(inputTransformer.transform(isA(InputStream.class), isA(String.class))).thenReturn(generatedMetacard);
    } catch (IOException e) {
        fail();
    } catch (CatalogTransformerException e) {
        fail();
    }
    source.setInputTransformer(inputTransformer);
    source.setEndpointUrl("http://localhost:8181/services/catalog/query");
    source.init();
    source.setParameters(DEFAULT_PARAMETERS);
    source.factory = factory;
    Filter filter = filterBuilder.attribute(Metacard.METADATA).like().text(SAMPLE_SEARCH_PHRASE);
    SourceResponse response = source.query(new QueryRequestImpl(new QueryImpl(filter)));
    Assert.assertEquals(1, response.getHits());
    List<Result> results = response.getResults();
    Assert.assertTrue(results.size() == 1);
    Result result = results.get(0);
    Metacard metacard = result.getMetacard();
    Assert.assertNotNull(metacard);
    Assert.assertEquals("myType", metacard.getContentTypeName());
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) SecureCxfClientFactory(org.codice.ddf.cxf.SecureCxfClientFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer) WebClient(org.apache.cxf.jaxrs.client.WebClient) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Response(javax.ws.rs.core.Response) ResourceResponse(ddf.catalog.operation.ResourceResponse) SourceResponse(ddf.catalog.operation.SourceResponse) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Metacard(ddf.catalog.data.Metacard) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 4 with MetacardImpl

use of ddf.catalog.data.impl.MetacardImpl 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 5 with MetacardImpl

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

the class TestGmdConverter method getTestMetacard.

private MetacardImpl getTestMetacard(String wkt) {
    MetacardImpl metacard = getSparseMetacard();
    metacard.setAttribute(Core.CREATED, createdDate.getTime());
    metacard.setAttribute(Core.MODIFIED, modifiedDate.getTime());
    metacard.setAttribute(Core.RESOURCE_DOWNLOAD_URL, RESOURCE_DOWNLOAD_URL);
    metacard.setAttribute(Core.DESCRIPTION, "example description");
    metacard.setAttribute(Core.LOCATION, wkt);
    metacard.setAttribute(Core.METADATA, "</xml>");
    metacard.setAttribute(Core.RESOURCE_SIZE, "123TB");
    metacard.setAttribute(Core.LANGUAGE, (Serializable) Arrays.asList("ger", "eng"));
    metacard.setAttribute(Core.SOURCE_ID, "sourceID");
    metacard.setAttribute(Core.TITLE, "example title");
    metacard.setAttribute(Core.ID, "1234");
    metacard.setAttribute(Core.METACARD_CREATED, modifiedDate.getTime());
    metacard.setAttribute(Core.METACARD_MODIFIED, modifiedDate.getTime());
    metacard.setAttribute(Contact.POINT_OF_CONTACT_NAME, "John Doe");
    metacard.setAttribute(Contact.POINT_OF_CONTACT_EMAIL, "john.doe@example.com");
    metacard.setAttribute(Contact.POINT_OF_CONTACT_PHONE, "12345");
    metacard.setAttribute(Contact.POINT_OF_CONTACT_ADDRESS, "10 Downing Street London Westminster SW1A 2AA United Kingdom");
    metacard.setAttribute(Contact.PUBLISHER_NAME, "Jane Doe");
    metacard.setAttribute(Contact.PUBLISHER_EMAIL, "jane.doe@example.com");
    metacard.setAttribute(Contact.PUBLISHER_PHONE, "6789");
    metacard.setAttribute(Contact.POINT_OF_CONTACT_ADDRESS, "123 Fake Street Springfield MO 12345");
    metacard.setAttribute(Location.COORDINATE_REFERENCE_SYSTEM_CODE, "EPSG:4326");
    metacard.setAttribute(Location.COUNTRY_CODE, "FRA");
    metacard.setAttribute(Location.ALTITUDE, 123.0);
    metacard.setAttribute(DateTime.START, effectiveDate.getTime());
    metacard.setAttribute(DateTime.END, effectiveDate.getTime());
    metacard.setAttribute(Media.FORMAT, "gzip");
    metacard.setAttribute(Media.FORMAT_VERSION, "2.0");
    metacard.setAttribute(Topic.KEYWORD, "keyword");
    metacard.setAttribute(Topic.CATEGORY, "category");
    metacard.setAttribute(Core.RESOURCE_DOWNLOAD_URL, RESOURCE_DOWNLOAD_URL);
    try {
        metacard.setAttribute(Core.RESOURCE_URI, new URI(RESOURCE_URI));
    } catch (URISyntaxException e) {
        LOGGER.debug("URISyntaxException", e);
    }
    return metacard;
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Aggregations

MetacardImpl (ddf.catalog.data.impl.MetacardImpl)644 Test (org.junit.Test)410 Metacard (ddf.catalog.data.Metacard)236 ArrayList (java.util.ArrayList)167 Result (ddf.catalog.data.Result)114 ResultImpl (ddf.catalog.data.impl.ResultImpl)91 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)85 HashMap (java.util.HashMap)78 Date (java.util.Date)76 Serializable (java.io.Serializable)58 QueryResponse (ddf.catalog.operation.QueryResponse)54 URI (java.net.URI)52 SourceResponse (ddf.catalog.operation.SourceResponse)50 BinaryContent (ddf.catalog.data.BinaryContent)49 MetacardTypeImpl (ddf.catalog.data.impl.MetacardTypeImpl)49 QueryRequest (ddf.catalog.operation.QueryRequest)49 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)47 HashSet (java.util.HashSet)45 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)44 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)42