Search in sources :

Example 1 with Feed

use of org.apache.abdera.model.Feed in project camel by apache.

the class UpdatedDateFilterTest method testFilter.

@Test
public void testFilter() throws Exception {
    Document<Feed> doc = AtomUtils.parseDocument("file:src/test/data/feed.atom");
    assertNotNull(doc);
    // timestamp from the feed to use as base
    // 2007-11-13T13:35:25.014Z
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
    cal.set(2007, Calendar.NOVEMBER, 13, 14, 35, 0);
    EntryFilter filter = new UpdatedDateFilter(cal.getTime());
    List<Entry> entries = doc.getRoot().getEntries();
    // must reverse backwards
    for (int i = entries.size() - 1; i > 0; i--) {
        Entry entry = entries.get(i);
        boolean valid = filter.isValidEntry(null, doc, entry);
        // only the 3 last should be true
        if (i > 3) {
            assertEquals("not valid", false, valid);
        } else {
            assertEquals("valid", true, valid);
        }
    }
}
Also used : EntryFilter(org.apache.camel.component.feed.EntryFilter) Entry(org.apache.abdera.model.Entry) Calendar(java.util.Calendar) Feed(org.apache.abdera.model.Feed) Test(org.junit.Test)

Example 2 with Feed

use of org.apache.abdera.model.Feed in project camel by apache.

the class AtomPollingConsumerTest method testNoSplitEntries.

@Test
public void testNoSplitEntries() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.assertIsSatisfied();
    Exchange exchange = mock.getExchanges().get(0);
    Message in = exchange.getIn();
    assertNotNull(in);
    assertTrue(in.getBody() instanceof List);
    assertTrue(in.getHeader(AtomConstants.ATOM_FEED) instanceof Feed);
    Feed feed = in.getHeader(AtomConstants.ATOM_FEED, Feed.class);
    assertEquals("James Strachan", feed.getAuthor().getName());
    List<?> entries = in.getBody(List.class);
    assertEquals(7, entries.size());
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) List(java.util.List) Feed(org.apache.abdera.model.Feed) Test(org.junit.Test)

Example 3 with Feed

use of org.apache.abdera.model.Feed in project camel by apache.

the class AtomEntryPollingConsumer method getDocument.

private Document<Feed> getDocument() throws IOException, ParseException {
    if (document == null) {
        if (ObjectHelper.isEmpty(endpoint.getUsername()) || ObjectHelper.isEmpty(endpoint.getPassword())) {
            document = AtomUtils.parseDocument(endpoint.getFeedUri());
        } else {
            document = AtomUtils.parseDocument(endpoint.getFeedUri(), endpoint.getUsername(), endpoint.getPassword());
        }
        Feed root = document.getRoot();
        if (endpoint.isSortEntries()) {
            sortEntries(root);
        }
        list = root.getEntries();
        entryIndex = list.size() - 1;
    }
    return document;
}
Also used : Feed(org.apache.abdera.model.Feed)

Example 4 with Feed

use of org.apache.abdera.model.Feed in project ddf by codice.

the class AtomTransformer method transform.

@Override
public BinaryContent transform(SourceResponse sourceResponse, Map<String, Serializable> arguments) throws CatalogTransformerException {
    if (sourceResponse == null) {
        throw new CatalogTransformerException("Cannot transform null " + SourceResponse.class.getName());
    }
    Date currentDate = new Date();
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    Feed feed = null;
    try {
        Thread.currentThread().setContextClassLoader(AtomTransformer.class.getClassLoader());
        feed = ABDERA.newFeed();
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }
    /*
         * Atom spec text (rfc4287) Sect 4.2.14: "The "atom:title" element is a Text construct that
         * conveys a human- readable title for an entry or feed."
         */
    feed.setTitle(DEFAULT_FEED_TITLE);
    feed.setUpdated(currentDate);
    // TODO Use the same id for the same query
    // one challenge is a query in one site should not have the same feed id
    // as a query in another site probably could factor in ddf.host and port
    // into the algorithm
    feed.setId(URN_UUID + UUID.randomUUID().toString());
    // TODO SELF LINK For the Feed, possible design --> serialize Query into
    // a URL
    /*
         * Atom spec text (rfc4287): "atom:feed elements SHOULD contain one atom:link element with a
         * rel attribute value of self. This is the preferred URI for retrieving Atom Feed Documents
         * representing this Atom feed. "
         */
    feed.addLink("#", Link.REL_SELF);
    if (!StringUtils.isEmpty(SystemInfo.getOrganization())) {
        feed.addAuthor(SystemInfo.getOrganization());
    } else {
        feed.addAuthor(DEFAULT_AUTHOR);
    }
    /*
         * Atom spec text (rfc4287 sect. 4.2.4): "The "atom:generator" element's content identifies
         * the agent used to generate a feed, for debugging and other purposes." Generator is not
         * required in the atom:feed element.
         */
    if (!StringUtils.isEmpty(SystemInfo.getSiteName())) {
        // text is required.
        feed.setGenerator(null, SystemInfo.getVersion(), SystemInfo.getSiteName());
    }
    /*
         * According to http://www.opensearch.org/Specifications/OpenSearch/1.1 specification,
         * totalResults must be a non-negative integer. Requirements: This attribute is optional.
         */
    if (sourceResponse.getHits() > -1) {
        Element hits = feed.addExtension(OpenSearchConstants.TOTAL_RESULTS);
        hits.setText(Long.toString(sourceResponse.getHits()));
    }
    if (sourceResponse.getRequest() != null && sourceResponse.getRequest().getQuery() != null) {
        Element itemsPerPage = feed.addExtension(OpenSearchConstants.ITEMS_PER_PAGE);
        Element startIndex = feed.addExtension(OpenSearchConstants.START_INDEX);
        /*
             * According to http://www.opensearch.org/Specifications/OpenSearch/1.1 specification,
             * itemsPerPage must be a non-negative integer. It is possible that Catalog pageSize is
             * set to a non-negative integer though. When non-negative we will instead we will
             * change it to the number of search results on current page.
             */
        if (sourceResponse.getRequest().getQuery().getPageSize() > -1) {
            itemsPerPage.setText(Integer.toString(sourceResponse.getRequest().getQuery().getPageSize()));
        } else {
            if (sourceResponse.getResults() != null) {
                itemsPerPage.setText(Integer.toString(sourceResponse.getResults().size()));
            }
        }
        startIndex.setText(Integer.toString(sourceResponse.getRequest().getQuery().getStartIndex()));
    }
    for (Result result : sourceResponse.getResults()) {
        Metacard metacard = result.getMetacard();
        if (metacard == null) {
            continue;
        }
        Entry entry = feed.addEntry();
        String sourceName = DEFAULT_SOURCE_ID;
        if (result.getMetacard().getSourceId() != null) {
            sourceName = result.getMetacard().getSourceId();
        }
        Element source = entry.addExtension(new QName(FEDERATION_EXTENSION_NAMESPACE, "resultSource", "fs"));
        /*
             * According to the os-federation.xsd, the resultSource element text has a max length of
             * 16 and is the shortname of the source id. Previously, we were duplicating the names
             * in both positions, but since we truly do not have a shortname for our source ids, I
             * am purposely omitting the shortname text and leaving it as the empty string. The real
             * source id can still be found in the attribute instead.
             */
        source.setAttributeValue(new QName(FEDERATION_EXTENSION_NAMESPACE, "sourceId"), sourceName);
        if (result.getRelevanceScore() != null) {
            Element relevance = entry.addExtension(new QName("http://a9.com/-/opensearch/extensions/relevance/1.0/", "score", "relevance"));
            relevance.setText(result.getRelevanceScore().toString());
        }
        entry.setId(URN_CATALOG_ID + metacard.getId());
        /*
             * Atom spec text (rfc4287): "The "atom:title" element is a Text construct that conveys
             * a human- readable title for an entry or feed."
             */
        entry.setTitle(metacard.getTitle());
        /*
             * Atom spec text (rfc4287): "The "atom:updated" element is a Date construct indicating
             * the most recent instant in time when an entry or feed was modified in a way the
             * publisher considers significant." Therefore, a new Date is used because we are making
             * the entry for the first time.
             */
        if (metacard.getModifiedDate() != null) {
            entry.setUpdated(metacard.getModifiedDate());
        } else {
            entry.setUpdated(currentDate);
        }
        /*
             * Atom spec text (rfc4287): "Typically, atom:published will be associated with the
             * initial creation or first availability of the resource."
             */
        if (metacard.getCreatedDate() != null) {
            entry.setPublished(metacard.getCreatedDate());
        }
        /*
             * For atom:link elements, Atom spec text (rfc4287): "The value "related" signifies that
             * the IRI in the value of the href attribute identifies a resource related to the
             * resource described by the containing element."
             */
        addLink(resourceActionProvider, metacard, entry, Link.REL_RELATED);
        addLink(viewMetacardActionProvider, metacard, entry, Link.REL_ALTERNATE);
        addLink(thumbnailActionProvider, metacard, entry, REL_PREVIEW);
        /*
             * Atom spec text (rfc4287) Sect. 4.2.2.: "The "atom:category" element conveys
             * information about a category associated with an entry or feed. This specification
             * assigns no meaning to the content (if any) of this element."
             */
        if (metacard.getContentTypeName() != null) {
            entry.addCategory(metacard.getContentTypeName());
        }
        for (Position position : getGeoRssPositions(metacard)) {
            GeoHelper.addPosition(entry, position, Encoding.GML);
        }
        BinaryContent binaryContent = null;
        String contentOutput = metacard.getId();
        Type atomContentType = Type.TEXT;
        if (metacardTransformer != null) {
            try {
                binaryContent = metacardTransformer.transform(metacard, new HashMap<>());
            } catch (CatalogTransformerException | RuntimeException e) {
                LOGGER.debug(COULD_NOT_CREATE_XML_CONTENT_MESSAGE, e);
            }
            if (binaryContent != null) {
                try {
                    byte[] xmlBytes = binaryContent.getByteArray();
                    if (xmlBytes != null && xmlBytes.length > 0) {
                        contentOutput = new String(xmlBytes, StandardCharsets.UTF_8);
                        atomContentType = Type.XML;
                    }
                } catch (IOException e) {
                    LOGGER.debug(COULD_NOT_CREATE_XML_CONTENT_MESSAGE, e);
                }
            }
        }
        tccl = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(AtomTransformer.class.getClassLoader());
            entry.setContent(contentOutput, atomContentType);
        } finally {
            Thread.currentThread().setContextClassLoader(tccl);
        }
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        tccl = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(AtomTransformer.class.getClassLoader());
            feed.writeTo(baos);
        } finally {
            Thread.currentThread().setContextClassLoader(tccl);
        }
    } catch (IOException e) {
        LOGGER.info("Could not write to output stream.", e);
        throw new CatalogTransformerException("Could not transform into Atom.", e);
    }
    return new BinaryContentImpl(new ByteArrayInputStream(baos.toByteArray()), MIME_TYPE);
}
Also used : Position(org.apache.abdera.ext.geo.Position) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Element(org.apache.abdera.model.Element) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) BinaryContent(ddf.catalog.data.BinaryContent) Date(java.util.Date) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) Entry(org.apache.abdera.model.Entry) MimeType(javax.activation.MimeType) Type(org.apache.abdera.model.Content.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) Feed(org.apache.abdera.model.Feed)

Aggregations

Feed (org.apache.abdera.model.Feed)4 Entry (org.apache.abdera.model.Entry)2 Test (org.junit.Test)2 BinaryContent (ddf.catalog.data.BinaryContent)1 Metacard (ddf.catalog.data.Metacard)1 Result (ddf.catalog.data.Result)1 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)1 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 MimeType (javax.activation.MimeType)1 QName (javax.xml.namespace.QName)1 Position (org.apache.abdera.ext.geo.Position)1 Type (org.apache.abdera.model.Content.Type)1 Element (org.apache.abdera.model.Element)1 Exchange (org.apache.camel.Exchange)1