Search in sources :

Example 1 with FeedEntry

use of org.glassfish.jersey.examples.feedcombiner.model.FeedEntry in project jersey by jersey.

the class FeedDownloadTask method run.

@Override
public void run() {
    CombinedFeed fetchedCombinedFeed = datastore.get(combinedFeedID, CombinedFeed.class);
    if (fetchedCombinedFeed == null) {
        LOG.warn("There is no CombinedFeed for the ID: " + combinedFeedID);
        return;
    }
    List<FeedEntry> entries = fetchedCombinedFeed.getUrls().stream().map(feedDownloader).flatMap(Collection::stream).map(entryMapper).filter(Objects::nonNull).sorted((e1, e2) -> e2.getPublishDate().compareTo(e1.getPublishDate())).collect(Collectors.toList());
    CombinedFeed combinedFeed = of(fetchedCombinedFeed).feedEntries(entries).build();
    datastore.put(combinedFeedID, combinedFeed);
    LOG.debug("New entries for the CombinedFeed were downloaded [combined-feed={}, entries-count={}]", combinedFeed.getId(), entries.size());
}
Also used : SyndEntry(com.sun.syndication.feed.synd.SyndEntry) FeedEntry(org.glassfish.jersey.examples.feedcombiner.model.FeedEntry) URL(java.net.URL) Date(java.util.Date) Collection(java.util.Collection) CombinedFeed(org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed) LoggerFactory(org.slf4j.LoggerFactory) InMemoryDataStore(org.glassfish.jersey.examples.feedcombiner.store.InMemoryDataStore) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) CombinedFeedBuilder.of(org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed.CombinedFeedBuilder.of) FeedEntry(org.glassfish.jersey.examples.feedcombiner.model.FeedEntry) CombinedFeed(org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed) Objects(java.util.Objects) Collection(java.util.Collection)

Example 2 with FeedEntry

use of org.glassfish.jersey.examples.feedcombiner.model.FeedEntry in project jersey by jersey.

the class FeedEntriesAtomBodyWriter method writeTo.

@Override
public void writeTo(List<FeedEntry> entries, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    List<SyndEntry> syndEntries = entries.parallelStream().map(entry -> {
        SyndContent description = new SyndContentImpl();
        description.setType(MediaType.TEXT_PLAIN);
        description.setValue(entry.getDescription());
        SyndEntry syndEntry = new SyndEntryImpl();
        syndEntry.setTitle(entry.getTitle());
        syndEntry.setLink(entry.getLink());
        syndEntry.setPublishedDate(entry.getPublishDate());
        syndEntry.setDescription(description);
        return syndEntry;
    }).collect(Collectors.toList());
    SyndFeed feed = new SyndFeedImpl();
    feed.setFeedType("atom_1.0");
    feed.setTitle("Combined Feed");
    feed.setDescription("Combined Feed created by a feed-combiner application");
    feed.setPublishedDate(new Date());
    feed.setEntries(syndEntries);
    writeSyndFeed(entityStream, feed);
}
Also used : SyndEntry(com.sun.syndication.feed.synd.SyndEntry) Produces(javax.ws.rs.Produces) Provider(javax.ws.rs.ext.Provider) FeedEntry(org.glassfish.jersey.examples.feedcombiner.model.FeedEntry) Date(java.util.Date) SyndFeedImpl(com.sun.syndication.feed.synd.SyndFeedImpl) LoggerFactory(org.slf4j.LoggerFactory) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) FeedException(com.sun.syndication.io.FeedException) MediaType(javax.ws.rs.core.MediaType) SyndContentImpl(com.sun.syndication.feed.synd.SyndContentImpl) OutputStreamWriter(java.io.OutputStreamWriter) OutputStream(java.io.OutputStream) Logger(org.slf4j.Logger) Collection(java.util.Collection) IOException(java.io.IOException) SyndFeedOutput(com.sun.syndication.io.SyndFeedOutput) Collectors(java.util.stream.Collectors) SyndContent(com.sun.syndication.feed.synd.SyndContent) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) SyndEntryImpl(com.sun.syndication.feed.synd.SyndEntryImpl) Annotation(java.lang.annotation.Annotation) WebApplicationException(javax.ws.rs.WebApplicationException) SyndFeed(com.sun.syndication.feed.synd.SyndFeed) SyndFeed(com.sun.syndication.feed.synd.SyndFeed) SyndContent(com.sun.syndication.feed.synd.SyndContent) SyndContentImpl(com.sun.syndication.feed.synd.SyndContentImpl) SyndEntry(com.sun.syndication.feed.synd.SyndEntry) SyndEntryImpl(com.sun.syndication.feed.synd.SyndEntryImpl) SyndFeedImpl(com.sun.syndication.feed.synd.SyndFeedImpl) Date(java.util.Date)

Example 3 with FeedEntry

use of org.glassfish.jersey.examples.feedcombiner.model.FeedEntry in project jersey by jersey.

the class FeedDownloadTaskTest method testSuccess.

@Test
public void testSuccess() throws Exception {
    URL url = new URL("http://localhost");
    String id = "1";
    CombinedFeed feed = new CombinedFeed.CombinedFeedBuilder(id, singletonList(url)).refreshPeriod(5L).build();
    expect(datastore.get(feedId, CombinedFeed.class)).andReturn(feed);
    expect(downloader.apply(url)).andReturn(syndEntries());
    Capture<CombinedFeed> feedCapture = EasyMock.newCapture();
    expect(datastore.put(eq(id), capture(feedCapture))).andReturn(null);
    replayAll();
    testedClass.run();
    verifyAll();
    CombinedFeed capturedFeed = feedCapture.getValue();
    assertEquals(2, capturedFeed.getFeedEntries().size());
    for (FeedEntry entry : capturedFeed.getFeedEntries()) {
        if (TITLES[0].equals(entry.getTitle())) {
            assertEquals(feedEntries().get(0), entry);
        } else {
            assertEquals(feedEntries().get(1), entry);
        }
    }
}
Also used : FeedEntry(org.glassfish.jersey.examples.feedcombiner.model.FeedEntry) CombinedFeed(org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed) URL(java.net.URL) Test(org.junit.Test)

Example 4 with FeedEntry

use of org.glassfish.jersey.examples.feedcombiner.model.FeedEntry in project jersey by jersey.

the class CombinedFeedResourceTest method testGetEntriesJSON.

@Test
public void testGetEntriesJSON() {
    Response response = callGetEntries(APPLICATION_JSON_TYPE);
    assertEquals(Status.OK.getStatusCode(), response.getStatus());
    List<FeedEntry> restoredEntities = response.readEntity(new GenericType<List<FeedEntry>>() {
    });
    assertEquals(feedEntries(), restoredEntities);
}
Also used : Response(javax.ws.rs.core.Response) FeedEntry(org.glassfish.jersey.examples.feedcombiner.model.FeedEntry) List(java.util.List) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

FeedEntry (org.glassfish.jersey.examples.feedcombiner.model.FeedEntry)4 List (java.util.List)3 SyndEntry (com.sun.syndication.feed.synd.SyndEntry)2 URL (java.net.URL)2 Collection (java.util.Collection)2 Date (java.util.Date)2 Collectors (java.util.stream.Collectors)2 CombinedFeed (org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed)2 Test (org.junit.Test)2 LoggerFactory (org.slf4j.LoggerFactory)2 SyndContent (com.sun.syndication.feed.synd.SyndContent)1 SyndContentImpl (com.sun.syndication.feed.synd.SyndContentImpl)1 SyndEntryImpl (com.sun.syndication.feed.synd.SyndEntryImpl)1 SyndFeed (com.sun.syndication.feed.synd.SyndFeed)1 SyndFeedImpl (com.sun.syndication.feed.synd.SyndFeedImpl)1 FeedException (com.sun.syndication.io.FeedException)1 SyndFeedOutput (com.sun.syndication.io.SyndFeedOutput)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1