Java Code Examples for com.sun.syndication.feed.synd.SyndFeed

The following are top voted examples for showing how to use com.sun.syndication.feed.synd.SyndFeed. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: rufus   File: FeedProcessorImpl.java   View source code 6 votes vote down vote up
private Map<Channel, List<Document>> buildChannelMap(long userId) {
    List<Source> sources;
    if (userId == PUB_USER_KEY) {
        sources = articleDao.getPublicSources();
    } else if (articleDao.hasSubscriptions(userId)) {
        sources = articleDao.getSources(userId);
    } else {
        return Collections.emptyMap();
    }

    List<RufusFeed> requests = sources.parallelStream().map(RufusFeed::generate).collect(Collectors.toList());

    Map<Channel, List<Document>> ret = new HashMap<>();
    requests.stream().filter(r -> r.getFeed() != null).forEach(r -> {
        Pair<SyndFeed, List<SyndEntry>> synd = feedPair(r);
        ret.put(Channel.of(
            synd.getKey().getTitle(),
            synd.getKey().getLanguage(),
            synd.getKey().getLink(),
            r.getSource()),
            extractDocuments(synd));
    });

    return ret;
}
 
Example 2
Project: rufus   File: FeedProcessorImpl.java   View source code 6 votes vote down vote up
private static List<Document> extractDocuments(Pair<SyndFeed, List<SyndEntry>> feedEntry) {
    List<Document> ret = new ArrayList<>();
    feedEntry.getRight().forEach(e -> {
        FeedUtils.mergeAuthors(e);
        String description = e.getDescription() != null ? FeedUtils.clean(e.getDescription().getValue()) : StringUtils.EMPTY;
        if (description.length() > FeedConstants.MAX_DESCRIP_LEN) {
            description = FeedUtils.truncate(description, FeedConstants.MAX_DESCRIP_LEN);
        }
        ret.add(Document.of(
            StringEscapeUtils.unescapeHtml4(e.getTitle()),
            e.getPublishedDate(),
            e.getAuthors(),
            description,
            e.getLink(),
            feedEntry.getLeft().getTitle()
        ));
    });
    return ret;
}
 
Example 3
Project: parabuild-ci   File: ConverterForRSS090.java   View source code 6 votes vote down vote up
public void copyInto(WireFeed feed,SyndFeed syndFeed) {
    syndFeed.setModules(ModuleUtils.cloneModules(feed.getModules()));

    syndFeed.setEncoding(feed.getEncoding());
    Channel channel = (Channel) feed;
    syndFeed.setTitle(channel.getTitle());
    syndFeed.setLink(channel.getLink());
    syndFeed.setDescription(channel.getDescription());

    Image image = channel.getImage();
    if (image!=null) {
        syndFeed.setImage(createSyndImage(image));
    }

    List items = channel.getItems();
    if (items!=null) {
        syndFeed.setEntries(createSyndEntries(items));
    }
}
 
Example 4
Project: parabuild-ci   File: ConverterForRSS090.java   View source code 6 votes vote down vote up
protected WireFeed createRealFeed(String type,SyndFeed syndFeed) {
    Channel channel = new Channel(type);
    channel.setModules(ModuleUtils.cloneModules(syndFeed.getModules()));

    channel.setEncoding(syndFeed.getEncoding());

    channel.setTitle(syndFeed.getTitle());
    channel.setLink(syndFeed.getLink());
    channel.setDescription(syndFeed.getDescription());
    SyndImage sImage = syndFeed.getImage();
    if (sImage!=null) {
        channel.setImage(createRSSImage(sImage));
    }

    List sEntries = syndFeed.getEntries();
    if (sEntries!=null) {
        channel.setItems(createRSSItems(sEntries));
    }
    return channel;
}
 
Example 5
Project: parabuild-ci   File: ConverterForRSS091Userland.java   View source code 6 votes vote down vote up
public void copyInto(WireFeed feed,SyndFeed syndFeed) {
    Channel channel = (Channel) feed;
    super.copyInto(channel,syndFeed);
    syndFeed.setLanguage(channel.getLanguage());        //c
    syndFeed.setCopyright(channel.getCopyright());      //c
    Date pubDate = channel.getPubDate();
    if (pubDate!=null) {
        syndFeed.setPublishedDate(pubDate);     //c
    }

    String author = channel.getManagingEditor();
    if (author!=null) {    
        List creators = ((DCModule) syndFeed.getModule(DCModule.URI)).getCreators();
        if (!creators.contains(author)) {
            Set s = new HashSet(); // using a set to remove duplicates
            s.addAll(creators);    // DC creators
            s.add(author);         // feed native author
            creators.clear();
            creators.addAll(s);
        }
    }

}
 
Example 6
Project: parabuild-ci   File: FeedReader.java   View source code 6 votes vote down vote up
public static void main(String[] args) {
    boolean ok = false;
    if (args.length==1) {
        try {
            URL feedUrl = new URL(args[0]);
            SyndFeedInput input = new SyndFeedInput();

            SyndFeed feed = input.build(new XmlReader(feedUrl));

            System.out.println(feed);

            ok = true;
        }
        catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("ERROR: "+ex.getMessage());
        }
    }

    if (!ok) {
        System.out.println();
        System.out.println("FeedReader reads and prints any RSS/Atom feed type.");
        System.out.println("The first parameter must be the URL of the feed to read.");
        System.out.println();
    }
}
 
Example 7
Project: parabuild-ci   File: PublishedResultFeedGenerator.java   View source code 6 votes vote down vote up
/**
 * Returns a feed for all builds that are allowed for a given user.
 *
 * @param userID for that to return feeds.
 *
 * @return a feed for all builds that are allowed for the given user.
 */
public SyndFeed getAllBuildsFeed(final int userID) {

  final List buildStatusList = org.parabuild.ci.security.SecurityManager.getInstance().getFeedBuildStatuses(userID);

  // traverse build statuses
  final List entries = new ArrayList(11);
  final BuildStatusURLGenerator urlGenerator = new BuildStatusURLGenerator();
  final ConfigurationManager cm = ConfigurationManager.getInstance();
  for (final Iterator i = buildStatusList.iterator(); i.hasNext();) {
    final int activeBuildID = ((BuildState)i.next()).getActiveBuildID();
    entries.addAll(getBuildEntries(cm, activeBuildID, urlGenerator));
  }

  // create feed and set collected entries
  final SyndFeed feed = new SyndFeedImpl();
  feed.setTitle("Parabuild Published Result Feed");
  feed.setLink(urlGenerator.makeBuildListURL());
  feed.setDescription("This feed provides information about published results at Parabuild server");
  feed.setEntries(entries);

  // return result
  return feed;
}
 
Example 8
Project: parabuild-ci   File: FeedGenerator.java   View source code 6 votes vote down vote up
/**
 * Returns a feed for all builds that are allowed for a given user.
 *
 * @param userID for that to return feeds.
 * @return a feed for all builds that are allowed for the given user.
 */
public SyndFeed getAllBuildsFeed(final int userID) {

  final List buildStatusList = SecurityManager.getInstance().getFeedBuildStatuses(userID);

  // traverse build statuses
  final List entries = new ArrayList(1);
  final BuildStatusURLGenerator urlGenerator = new BuildStatusURLGenerator();
  final ConfigurationManager cm = ConfigurationManager.getInstance();
  for (final Iterator i = buildStatusList.iterator(); i.hasNext();) {
    final int activeBuildID = ((BuildState) i.next()).getActiveBuildID();
    entries.addAll(getBuildEntries(cm, activeBuildID, urlGenerator));
  }

  // create feed and set collected entries
  final SyndFeed feed = new SyndFeedImpl();
  feed.setTitle("Parabuild Feed");
  feed.setLink(urlGenerator.makeBuildListURL());
  feed.setDescription("This feed provides information about build statuses at Parabuild server");
  feed.setEntries(entries);

  // return result
  return feed;
}
 
Example 9
Project: dwnewsbot   File: FetchAtomWorker.java   View source code 6 votes vote down vote up
public void execute(JobExecutionContext context) throws JobExecutionException {
    log.info("Start FetchAtomWorker job");

    EntityManagerFactory emf = (EntityManagerFactory) context.getMergedJobDataMap().get("emf");
    DataManager dm = new DataManager (emf);
    ServletContext scontext = (ServletContext) context.getMergedJobDataMap().get("scontext");

    Iterator it = dm.feeds.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry <String, String[]> pair = (Map.Entry)it.next();
        System.out.println(pair.getKey() + " = " + pair.getValue());

        try {
            URL feedUrl = new URL(pair.getValue()[1]);

            SyndFeedInput input = new SyndFeedInput();
            SyndFeed feed = input.build(new XmlReader(feedUrl));
            for (SyndEntry entry : (List<SyndEntry>) feed.getEntries()) {
                dm.saveNewsItem(entry, pair.getKey());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
 
Example 10
Project: apache-archiva   File: RssFeedGenerator.java   View source code 6 votes vote down vote up
public SyndFeed generateFeed( String title, String description, List<RssFeedEntry> dataEntries )
{
    if( dataEntries.size() ==  0 )
    {
        log.debug( "No updates found, feed not generated." );
        return null;
    }
    
    SyndFeed feed = new SyndFeedImpl();
    feed.setTitle( title );        
    feed.setDescription( description );
    feed.setLanguage( DEFAULT_LANGUAGE );
    feed.setPublishedDate( dataEntries.get( dataEntries.size() - 1 ).getPublishedDate() );
    feed.setFeedType( DEFAULT_FEEDTYPE );
    feed.setEntries( getEntries( dataEntries ) );

    log.debug( "Finished generating the feed \'{}\'.", title );
    
    return feed;
}
 
Example 11
Project: expresso   File: SyndEntryAdapter.java   View source code 6 votes vote down vote up
public SyndEntryAdapter(SyndFeed feed, SyndEntry entry) {
    if (entry.getTitleEx() != null) {
        this.title = entry.getTitle();
        this.titleType = entry.getTitleEx().getType();
    }
    if (entry.getDescription() != null) {
        this.description = entry.getDescription().getValue();
        this.descriptionType = entry.getDescription().getType();
    }
    this.uri = entry.getUri();
    this.author = entry.getAuthor();
    this.link = entry.getLink();

    if (entry.getPublishedDate() != null)
        this.publishedDate = ISO_8601_DATE_FORMAT.format(entry.getPublishedDate());
    if (entry.getUpdatedDate() != null)
        this.updatedDate = ISO_8601_DATE_FORMAT.format(entry.getUpdatedDate());
    this.fetchDate = ISO_8601_DATE_FORMAT.format(new Date());

    this.sourceLink = feed.getLink();
    this.sourceUri = feed.getUri();
    this.sourceTitle = feed.getTitle();
    this.language = feed.getLanguage();
    this.sourceType = feed.getFeedType();
}
 
Example 12
Project: Camel   File: AggregateRssFeedStrategy.java   View source code 6 votes vote down vote up
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
    if (oldExchange == null) {
        return newExchange;
    }
    SyndFeed oldFeed = oldExchange.getIn().getBody(SyndFeed.class);
    SyndFeed newFeed = newExchange.getIn().getBody(SyndFeed.class);
    if (oldFeed != null && newFeed != null) {                
        List<SyndEntryImpl> oldEntries = CastUtils.cast(oldFeed.getEntries());                  
        List<SyndEntryImpl> newEntries = CastUtils.cast(newFeed.getEntries());
        List<SyndEntryImpl> mergedList = new ArrayList<SyndEntryImpl>(oldEntries.size() + newEntries.size());
        mergedList.addAll(oldEntries);
        mergedList.addAll(newEntries);
        oldFeed.setEntries(mergedList);    
    } else {
        log.debug("Could not merge exchanges. One body was null.");
    }
    return oldExchange;
}
 
Example 13
Project: Camel   File: RssPollingConsumerTest.java   View source code 6 votes vote down vote up
@Test
public void testGrabbingListOfEntries() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(1);
    mock.assertIsSatisfied();

    Exchange exchange = mock.getExchanges().get(0);
    Message in = exchange.getIn();
    assertNotNull(in);
    assertTrue(in.getBody() instanceof SyndFeed);
    assertTrue(in.getHeader(RssConstants.RSS_FEED) instanceof SyndFeed);

    SyndFeed feed = in.getHeader(RssConstants.RSS_FEED, SyndFeed.class);
    assertTrue(feed.getAuthor().contains("Jonathan Anstey"));

    SyndFeed body = in.getBody(SyndFeed.class);
    assertEquals(10, body.getEntries().size());
}
 
Example 14
Project: FeedExpander   File: FeedCreatorTest.java   View source code 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testCreateNullHeadlineAndLink() throws Exception {
	String createdFeed = new String(createFeedCreator("feeds/valid_page/index.html")
			.createFeed(".headline,.article,.footer", null, null, null)
			.setTitle("Title")
			.build());
	SyndFeedInput input = new SyndFeedInput();
	SyndFeed feed = input.build(new XmlReader(new ByteArrayInputStream(createdFeed.getBytes())));
	assertEquals("Title", feed.getTitle());
	
	List<SyndEntry> entries = feed.getEntries();

	assertEquals(null, entries.get(0).getTitle());
	assertEquals(null, entries.get(0).getLink());
	assertEquals(EMPTY, entries.get(0).getAuthor());
	assertEquals(null, entries.get(1).getTitle());
	assertEquals(null, entries.get(1).getLink());
	assertEquals(EMPTY, entries.get(1).getAuthor());
}
 
Example 15
Project: sakai   File: SimpleRSSPortlet.java   View source code 6 votes vote down vote up
/**
 * Get the feed content
 * @param request
 * @param response
 * @return Map of params or null if any required data is missing
 */
private SyndFeed getFeedContent(RenderRequest request, RenderResponse response) {
	
	SyndFeed feed;
	
	//check cache, otherwise get fresh
	//we use the feedUrl as the cacheKey
	String feedUrl = getConfiguredFeedUrl(request);
	if(StringUtils.isBlank(feedUrl)) {
		log.debug("No feed URL configured");
		doError("error.no.config", "error.heading.config", getPortletModeUrl(response, PortletMode.EDIT), request, response);
		return null;
	}
	
	String cacheKey = feedUrl;
	
	feed = feedCache.get(cacheKey);
	if(feed != null) {
		log.debug("Fetching data from feed cache for: " + cacheKey);
	} else {
		//get from remote
		feed = getRemoteFeed(feedUrl, request, response);
	}
	
	return feed;
}
 
Example 16
Project: sakai   File: SimpleRSSPortlet.java   View source code 6 votes vote down vote up
/**
 * Helper to get the remote feed data and cache it
 * @param feedUrl
 * @param request
 * @param response
 * @return
 */
private SyndFeed getRemoteFeed(String feedUrl, RenderRequest request, RenderResponse response) {
	
	//get feed data
	SyndFeed feed = new FeedParser().parseFeed(feedUrl);
	if(feed == null) {
		log.error("No data was returned from remote server.");
		doError("error.no.remote.data", "error.heading.general", request, response);
		return null;
	}
	
	//cache the data,
	log.debug("Adding data to feed cache for: " + feedUrl);
	feedCache.put(feedUrl, feed);
	
	return feed;
}
 
Example 17
Project: sakai   File: SimpleRSSPortlet.java   View source code 6 votes vote down vote up
/**
 * Helper for extracting the enclosures (media) associated with entries in the feed.
 * They are returned as a map of String to Attachment where String is the entry Uri
 * 
 * @param feed		The raw SyndFeed to process
 * @param feedUrl	The url of this feed
 * @return
 */
private Map<String,Attachment> getFeedMedia(SyndFeed feed, String feedUrl) {
	
	Map<String,Attachment> media;
	
	//check cache
	media = mediaCache.get(feedUrl);
	if(media != null) {
		log.debug("Fetching data from media cache for: " + feedUrl);
		return media;
	} else {
	
		//parse the enclosures for this feed
		media = FeedParser.parseFeedEnclosures(feed);
		
		//cache the data
		log.debug("Adding data to media cache for: " + feedUrl);
		mediaCache.put(feedUrl, media);
	}
	
	return media;
}
 
Example 18
Project: feeds   File: ConverterForRSS091Userland.java   View source code 6 votes vote down vote up
@Override
protected WireFeed createRealFeed(String type, SyndFeed syndFeed) {
    Channel channel = (Channel) super.createRealFeed(type, syndFeed);
    channel.setLanguage(syndFeed.getLanguage()); //c
    channel.setCopyright(syndFeed.getCopyright()); //c
    channel.setPubDate(syndFeed.getPublishedDate()); //c        

    if ((syndFeed.getAuthors() != null) && (syndFeed.getAuthors()
                                                        .size() > 0)) {
        SyndPerson author = (SyndPerson) syndFeed.getAuthors()
                                                 .get(0);
        channel.setManagingEditor(author.getName());
    }

    return channel;
}
 
Example 19
Project: crawly   File: Crawly.java   View source code 6 votes vote down vote up
private TreeMap gatherLinks(String section) {
    TreeMap linkMap = new TreeMap(Collections.reverseOrder());
    //for sites that support rss we do rss
    SyndFeed feed = getSectionLinksFromRssFeed(section);
    if (feed != null) {
        for (SyndEntry anEntry : (List<SyndEntry>) feed.getEntries()) {
            String aLink = anEntry.getLink();
            if (aLink.contains(ampLiteral)) {
                aLink = aLink.replace(ampLiteral, "");
            }
            if (!shoudRemove(anEntry)) {
                linkMap.put(aLink, aLink);
            }
        }
    }
    //for sites that do not have or do not support
    return linkMap;
}
 
Example 20
Project: bingesurfing   File: Feeder.java   View source code 6 votes vote down vote up
public void read() {
	try {
		URL source = new URL(Defaults.FEEDURL);
		SyndFeedInput input = new SyndFeedInput();
		SyndFeed feed = input.build(new XmlReader(source));
		@SuppressWarnings("unchecked")
		List<SyndEntry> entries = feed.getEntries();
		urls = new ArrayList<String>();
		for(SyndEntry entry : entries) {
			String sURL = entry.getLink();
			urls.add(sURL);
		} // for
	} catch (IllegalArgumentException | FeedException | IOException e) {
		e.printStackTrace();
	}

	long seed = System.nanoTime();
	Collections.shuffle(urls, new Random(seed));
}
 
Example 21
Project: switchyard   File: CamelRSSPollTest.java   View source code 6 votes vote down vote up
@Test
public void shouldRetrieveGreetings() throws Exception {

    _testKit.removeService("PrintService");
    final MockHandler printService = _testKit.registerInOnlyService("PrintService");
    Thread.sleep(10001);
    
    final LinkedBlockingQueue<Exchange> receivedMessages = printService.getMessages();
    for (Exchange e : receivedMessages) {
        SyndFeed feed = (SyndFeed) e.getMessage().getContent();
        @SuppressWarnings("unchecked")
        List<SyndEntry> entries = feed.getEntries();
        Assert.assertEquals(feed.getEntries().size(), 1);
        Iterator<SyndEntry> itEntries = entries.iterator();
 
        while (itEntries.hasNext()) {
            SyndEntry entry = (SyndEntry) itEntries.next();
            Assert.assertTrue(entry.getTitle().equals(SONG_TITLE));
            Assert.assertTrue(entry.getLink().equals(SONG_URL));
            Assert.assertTrue(entry.getDescription().getValue().equals(SONG_DESCRIPTION));
        }
    }
}
 
Example 22
Project: rssify   File: RssServlet.java   View source code 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String sourceConfigurationId = req.getPathInfo();
    log.info("Received " + sourceConfigurationId);
    SourceConfiguration sourceConfiguration = Configuration.sourceConfigurations.get(sourceConfigurationId);
    if (sourceConfiguration == null) {
        log.severe("Wrong resource " + req.getPathInfo());
        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    List<SourceItem> items = DatabaseService.getItems(sourceConfigurationId, 25);
    SyndFeed feed = FeedUtils.createFeed(sourceConfigurationId, sourceConfiguration, items);

    SyndFeedOutput output = new SyndFeedOutput();
    resp.setCharacterEncoding("UTF-8");
    PrintWriter writer = resp.getWriter();

    try {
        output.output(feed, writer);
    } catch (FeedException e) {
        e.printStackTrace();
    }

    writer.close();
}
 
Example 23
Project: distributed-rss   File: RSSThreadWorkerReadFeedTest.java   View source code 6 votes vote down vote up
/**
 * Test method for read feed. Checks if we get back the feed object.
 * {@link com.jernejerin.RSSThreadWorker#readFeed(java.lang.String, com.sun.syndication.io.SyndFeedInput)}
 * 
 * @throws Exception
 */
@Test
public void testReadFeed() throws Exception {
	try {
		SyndFeed feed = RSSThreadWorker.readFeed(this.url, this.input);
		if (feed == null)
			logger.info("The value should not be null! URL: " + this.url);
		assertNotNull("The value should not be null! URL: " + this.url,
				feed);
	} catch (Exception ex) {
		logger.error("Exception happened, which means problem with this URL: "
				+ this.url);
		fail("Exception happened, which means problem with this URL: "
				+ this.url);
	}
}
 
Example 24
Project: InSpider   File: AtomCodeListFactory.java   View source code 6 votes vote down vote up
public AtomCodeList(final String codeSpace, final SyndFeed feed) {
	this.codeSpace = codeSpace;

	@SuppressWarnings("unchecked")
	final List<SyndEntry> entries = (List<SyndEntry>) feed.getEntries();

	for (final SyndEntry entry : entries) {
		if (entry.getUri().startsWith(feed.getUri())) {
			String id = entry.getUri().substring(feed.getUri().length());
			if (id.startsWith("/")) {
				id = id.substring(1);
			}
			values.add(id);
		} else {
			values.add(entry.getTitle());
		}
	}
}
 
Example 25
Project: chitanka4kindle   File: OpdsPage.java   View source code 6 votes vote down vote up
public int getItemsCount() throws IOException {
	if (itemsCount == Integer.MIN_VALUE) {
		if (url == null) {
			itemsCount = 1;
		} else {
			parsePagesCount();
			
			int firstPageEntries = firstPage.getEntries().size();
			if (pagesCount == 1) {
				itemsCount = firstPageEntries;
			} else {
				SyndFeed lastPage = parsePage(pagesCount);
				itemsCount = firstPageEntries * (pagesCount - 1)
						+ lastPage.getEntries().size();
			}
		}
	}
	return itemsCount;
}
 
Example 26
Project: sakai   File: SimpleRSSPortlet.java   View source code 6 votes vote down vote up
/**
 * Get the feed content
 * @param request
 * @param response
 * @return Map of params or null if any required data is missing
 */
private SyndFeed getFeedContent(RenderRequest request, RenderResponse response) {
	
	SyndFeed feed;
	
	//check cache, otherwise get fresh
	//we use the feedUrl as the cacheKey
	String feedUrl = getConfiguredFeedUrl(request);
	if(StringUtils.isBlank(feedUrl)) {
		log.debug("No feed URL configured");
		doError("error.no.config", "error.heading.config", getPortletModeUrl(response, PortletMode.EDIT), request, response);
		return null;
	}
	
	String cacheKey = feedUrl;
	
	feed = feedCache.get(cacheKey);
	if(feed != null) {
		log.debug("Fetching data from feed cache for: " + cacheKey);
	} else {
		//get from remote
		feed = getRemoteFeed(feedUrl, request, response);
	}
	
	return feed;
}
 
Example 27
Project: sakai   File: SimpleRSSPortlet.java   View source code 6 votes vote down vote up
/**
 * Helper to get the remote feed data and cache it
 * @param feedUrl
 * @param request
 * @param response
 * @return
 */
private SyndFeed getRemoteFeed(String feedUrl, RenderRequest request, RenderResponse response) {
	
	//get feed data
	SyndFeed feed = new FeedParser().parseFeed(feedUrl);
	if(feed == null) {
		log.error("No data was returned from remote server.");
		doError("error.no.remote.data", "error.heading.general", request, response);
		return null;
	}
	
	//cache the data,
	log.debug("Adding data to feed cache for: " + feedUrl);
	feedCache.put(feedUrl, feed);
	
	return feed;
}
 
Example 28
Project: sakai   File: SimpleRSSPortlet.java   View source code 6 votes vote down vote up
/**
 * Helper for extracting the enclosures (media) associated with entries in the feed.
 * They are returned as a map of String to Attachment where String is the entry Uri
 * 
 * @param feed		The raw SyndFeed to process
 * @param feedUrl	The url of this feed
 * @return
 */
private Map<String,Attachment> getFeedMedia(SyndFeed feed, String feedUrl) {
	
	Map<String,Attachment> media;
	
	//check cache
	media = mediaCache.get(feedUrl);
	if(media != null) {
		log.debug("Fetching data from media cache for: " + feedUrl);
		return media;
	} else {
	
		//parse the enclosures for this feed
		media = FeedParser.parseFeedEnclosures(feed);
		
		//cache the data
		log.debug("Adding data to media cache for: " + feedUrl);
		mediaCache.put(feedUrl, media);
	}
	
	return media;
}
 
Example 29
Project: marmotta   File: ConverterForRSS091Userland.java   View source code 6 votes vote down vote up
@Override
protected WireFeed createRealFeed(String type, SyndFeed syndFeed) {
    Channel channel = (Channel) super.createRealFeed(type, syndFeed);
    channel.setLanguage(syndFeed.getLanguage()); //c
    channel.setCopyright(syndFeed.getCopyright()); //c
    channel.setPubDate(syndFeed.getPublishedDate()); //c        

    if ((syndFeed.getAuthors() != null) && (syndFeed.getAuthors()
                                                        .size() > 0)) {
        SyndPerson author = (SyndPerson) syndFeed.getAuthors()
                                                 .get(0);
        channel.setManagingEditor(author.getName());
    }

    return channel;
}
 
Example 30
Project: PrimefacesShowcase   File: TwitterRSSService.java   View source code 6 votes vote down vote up
public List<String> getTweets(String username) {
	List<String> tweets = new ArrayList<String>();
	
	try {
		URL feedSource = new URL("http://api.twitter.com/1/statuses/user_timeline.rss?count=100&screen_name=" + username);
           
		SyndFeedInput input = new SyndFeedInput();
		SyndFeed feed = input.build(new XmlReader(feedSource));
		for(Object f : feed.getEntries()) {
			SyndEntry entry = (SyndEntry) f;
			tweets.add(entry.getTitle().substring(username.length() + 2));
		}
	}catch(Exception e) {
		logger.severe(e.getMessage());
	}
	
	return tweets;
}
 
Example 31
Project: PrimefacesShowcase   File: YAHOOWeatherService.java   View source code 6 votes vote down vote up
public String getConditions(String city, String unit) {

		try {
			URL feedSource = new URL("http://weather.yahooapis.com/forecastrss?p=" + city + "&u=" + unit);

			SyndFeedInput input = new SyndFeedInput();
			SyndFeed feed = input.build(new XmlReader(feedSource));
			String value = ((SyndEntry) feed.getEntries().get(0)).getDescription().getValue();
			
			return value.split("<a href")[0];		//Remove links
		} catch (Exception e) {
			logger.severe(e.getMessage());
		}
		
		return "Unable to retrieve conditions";
	}
 
Example 32
Project: wildfly-camel   File: RSSFeedServlet.java   View source code 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    SyndFeed feed = new SyndFeedImpl();
    feed.setFeedType("rss_2.0");
    feed.setTitle("WildFly-Camel Test RSS Feed");
    feed.setLink("http://localhost:8080/rss-tests");
    feed.setDescription("Test RSS feed for the camel-rss component");

    List<SyndEntry> entries = new ArrayList<>();
    for (int i = 1; i <= 5; i++) {
        entries.add(createFeedEntry("Test entry: ", "Test content: ", i));
    }
    feed.setEntries(entries);

    SyndFeedOutput output = new SyndFeedOutput();
    try {
        output.output(feed, response.getWriter());
    } catch (FeedException e) {
        throw new IllegalStateException("Error generating RSS feed", e);
    }
}
 
Example 33
Project: XPagesToolkit   File: FeedReaderService.java   View source code 6 votes vote down vote up
public List<RSSEntry> getAllEntriesFromURL(String strURL) {
	List<RSSEntry> lstRC = new ArrayList<RSSEntry>();
	if (!StringUtil.isEmpty(strURL)) {
		Thread currentThread = Thread.currentThread();
		ClassLoader clCurrent = currentThread.getContextClassLoader();

		try {
			currentThread.setContextClassLoader(XPTRSSActivator.class.getClassLoader());

			URL feedUrl = new URL(strURL);
			SyndFeedInput input = new SyndFeedInput();
			SyndFeed feed = input.build(new XmlReader(feedUrl));
			lstRC = processFeed2List(feed);

		} catch (Exception e) {
			LoggerFactory.logError(getClass(), "getAllEntriesFromURL", e);
		} finally {
			currentThread.setContextClassLoader(clCurrent);
		}
	}
	return lstRC;
}
 
Example 34
Project: XPagesToolkit   File: FeedReaderService.java   View source code 6 votes vote down vote up
public RSSFeed getFeedFromURL(String strURL) {
	RSSFeed rssFeed = new RSSFeed();
	rssFeed.setURL(strURL);
	Thread currentThread = Thread.currentThread();
	ClassLoader clCurrent = currentThread.getContextClassLoader();
	try {
		currentThread.setContextClassLoader(XPTRSSActivator.class.getClassLoader());
		URL feedUrl = new URL(strURL);
		SyndFeedInput input = new SyndFeedInput();
		SyndFeed feed = input.build(new XmlReader(feedUrl));
		rssFeed.setAuthor(feed.getAuthor());
		rssFeed.setDescription(feed.getDescription());
		rssFeed.setTitle(feed.getTitle());
		if (feed.getImage() != null) {
			rssFeed.setImageURL(feed.getImage().getUrl());
		}
		rssFeed.setEntries(processFeed2List(feed));
	} catch (Exception e) {
		LoggerFactory.logError(getClass(), "getFeedFromURL", e);
	} finally {
		currentThread.setContextClassLoader(clCurrent);
	}
	return rssFeed;
}
 
Example 35
Project: XPagesToolkit   File: FeedReaderService.java   View source code 6 votes vote down vote up
private List<RSSEntry> processFeed2List(SyndFeed feed) {
	List<RSSEntry> lstRC = new ArrayList<RSSEntry>();
	for (Object synEntryObject : feed.getEntries()) {
		SyndEntry sEntry = (SyndEntry) synEntryObject;
		RSSEntry rssEntry = new RSSEntry();
		rssEntry.setTitle(sEntry.getTitle());
		rssEntry.setLink(sEntry.getLink());
		if (sEntry.getDescription() != null) {
			rssEntry.setDescription(sEntry.getDescription().getValue());
		} else {
			rssEntry.setDescription("");
		}
		rssEntry.setAuthors(buildStringList(sEntry.getAuthors()));
		rssEntry.setCategories(buildStringList(sEntry.getCategories()));
		rssEntry.setContents(buildStringList(sEntry.getContents()));
		rssEntry.setLinks(buildStringList(sEntry.getLinks()));
		rssEntry.setCreated(sEntry.getPublishedDate());
		rssEntry.setUpdated(sEntry.getUpdatedDate());
		lstRC.add(rssEntry);
	}
	return lstRC;
}
 
Example 36
Project: NK-VirtualGlobe   File: GeoRSSUtils.java   View source code 6 votes vote down vote up
/**
 * This convenience method checks whether there is any geoRss Element and
 * will return it (georss simple or W3GGeo).
 * 
 * @param entry
 *            the element in the feed which might have a georss element
 * @return a georssmodule or null if none is present
 */
public static GeoRSSModule getGeoRSS(SyndFeed feed) {
	GeoRSSModule geoRSSModule = (GeoRSSModule) feed
			.getModule(GeoRSSModule.GEORSS_GEORSS_URI);
	GeoRSSModule w3cGeo = (GeoRSSModule) feed
			.getModule(GeoRSSModule.GEORSS_W3CGEO_URI);
	GeoRSSModule gml = (GeoRSSModule) feed
			.getModule(GeoRSSModule.GEORSS_GML_URI);

	if (geoRSSModule == null && w3cGeo != null) {
		geoRSSModule = w3cGeo;
	} else if (geoRSSModule == null && gml != null) {
		geoRSSModule = gml;
	} else if (geoRSSModule != null && w3cGeo != null) {
		// sanity check
		if (!geoRSSModule.getGeometry().equals(w3cGeo.getGeometry())) {
			throw new Error("geometry of simple and w3c do not match");
		}
	}

	return geoRSSModule;
}
 
Example 37
Project: NK-VirtualGlobe   File: ConverterForRSS090.java   View source code 6 votes vote down vote up
public void copyInto(WireFeed feed,SyndFeed syndFeed) {
    syndFeed.setModules(ModuleUtils.cloneModules(feed.getModules()));
    if (((List)feed.getForeignMarkup()).size() > 0) {
        syndFeed.setForeignMarkup(feed.getForeignMarkup());
    }
    syndFeed.setEncoding(feed.getEncoding());
    Channel channel = (Channel) feed;
    syndFeed.setTitle(channel.getTitle());
    syndFeed.setLink(channel.getLink());
    syndFeed.setDescription(channel.getDescription());

    Image image = channel.getImage();
    if (image!=null) {
        syndFeed.setImage(createSyndImage(image));
    }

    List items = channel.getItems();
    if (items!=null) {
        syndFeed.setEntries(createSyndEntries(items));
    }
}
 
Example 38
Project: NK-VirtualGlobe   File: ConverterForRSS090.java   View source code 6 votes vote down vote up
protected WireFeed createRealFeed(String type,SyndFeed syndFeed) {
    Channel channel = new Channel(type);
    channel.setModules(ModuleUtils.cloneModules(syndFeed.getModules()));

    channel.setEncoding(syndFeed.getEncoding());

    channel.setTitle(syndFeed.getTitle());
    channel.setLink(syndFeed.getLink());
    channel.setDescription(syndFeed.getDescription());
    SyndImage sImage = syndFeed.getImage();
    if (sImage!=null) {
        channel.setImage(createRSSImage(sImage));
    }

    List sEntries = syndFeed.getEntries();
    if (sEntries!=null) {
        channel.setItems(createRSSItems(sEntries));
    }
    return channel;
}
 
Example 39
Project: NK-VirtualGlobe   File: ConverterForRSS091Userland.java   View source code 6 votes vote down vote up
public void copyInto(WireFeed feed,SyndFeed syndFeed) {
    Channel channel = (Channel) feed;
    super.copyInto(channel,syndFeed);
    syndFeed.setLanguage(channel.getLanguage());        //c
    syndFeed.setCopyright(channel.getCopyright());      //c
    Date pubDate = channel.getPubDate();
    if (pubDate!=null) {
        syndFeed.setPublishedDate(pubDate);  //c
    } else if (channel.getLastBuildDate() != null) {
        syndFeed.setPublishedDate(channel.getLastBuildDate());  //c
    }
    

    String author = channel.getManagingEditor();
    if (author!=null) {    
        List creators = ((DCModule) syndFeed.getModule(DCModule.URI)).getCreators();
        if (!creators.contains(author)) {
            Set s = new HashSet(); // using a set to remove duplicates
            s.addAll(creators);    // DC creators
            s.add(author);         // feed native author
            creators.clear();
            creators.addAll(s);
        }
    }

}
 
Example 40
Project: RSS-Mailer   File: RssReader.java   View source code 6 votes vote down vote up
/**
 * Retrieve the last available feed item, null if it's already in memory.
 * @return the last available feed item
 */
public FeedItem getFeed() {
	FeedItem feedItem = null;
	
	try {
		reader = new XmlReader(url);
		SyndFeed feed = new SyndFeedInput().build(reader);
		SyndEntry newEntry = ((SyndEntry)feed.getEntries().get(0));
		
		if(lastEntry == null || newEntry.getPublishedDate().after(lastEntry.getPublishedDate())) {
			log.info("Retrieving new feed.");
			lastEntry = newEntry;
			feedItem = populateFeedItem(lastEntry);
		}
		
	} catch (IllegalArgumentException | FeedException | IOException e) {
		log.error("There's a problem trying to read feed", e.getMessage());
	}
	
	return feedItem;
}