Guest User

Untitled

a guest
Feb 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. package services;
  7.  
  8. import com.sun.syndication.feed.synd.SyndEntry;
  9. import com.sun.syndication.feed.synd.SyndFeed;
  10. import com.sun.syndication.io.FeedException;
  11. import com.sun.syndication.io.SyndFeedInput;
  12. import com.sun.syndication.io.XmlReader;
  13. import java.io.IOException;
  14. import java.net.MalformedURLException;
  15. import java.net.URL;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import org.apache.log4j.Logger;
  19.  
  20. /**
  21. *
  22. * @author outcastgeek
  23. */
  24. public class FeedsServiceImpl implements FeedsService {
  25.  
  26. private static final Logger LOGGER = Logger.getLogger(FeedsServiceImpl.class);
  27.  
  28. public List<Feed> getFeedsFromSource(String source) {
  29. LOGGER.info("Fetching feed from source: " + source);
  30. List<Feed> feeds = new ArrayList<Feed>();
  31. try {
  32. final URL url = new URL(source);
  33. final SyndFeedInput syndFeedInput = new SyndFeedInput();
  34. final SyndFeed downloadedFeed = syndFeedInput.build(new XmlReader(url));
  35. for (SyndEntry syndEntry : (List<SyndEntry>) downloadedFeed.getEntries()) {
  36. Feed feed = new Feed();
  37. feed.setAuthor(syndEntry.getAuthor());
  38. feed.setTitle(syndEntry.getTitle());
  39. feed.setDescription(syndEntry.getDescription().getValue());
  40. feed.setLink(syndEntry.getLink());
  41. feed.setPubDate(syndEntry.getPublishedDate());
  42. LOGGER.info("Author: " + feed.getAuthor());
  43. LOGGER.info("Title: " + feed.getTitle());
  44. LOGGER.info("Description: " + feed.getDescription());
  45. LOGGER.info("Link: " + feed.getLink());
  46. LOGGER.info("Date: " + feed.getPubDate());
  47. feeds.add(feed);
  48. }
  49. } catch (MalformedURLException e) {
  50. LOGGER.error("Failed to retrieve feeds from this source: " + source + "\nwith error: " + e);
  51. } catch (IOException e) {
  52. LOGGER.error("Failed to retrieve feeds from this source: " + source + "\nwith error: " + e);
  53. } catch (FeedException e) {
  54. LOGGER.error("Failed to retrieve feeds from this source: " + source + "\nwith error: " + e);
  55. }
  56. LOGGER.info("Successfully retrieved feeds: " + feeds);
  57. return feeds;
  58. }
  59.  
  60. public List<Feed> getAllFeedsFromSources(List<String> sources) {
  61. LOGGER.info("Fetching all feeds from sources: " + sources);
  62. List feeds = new ArrayList<Feed>();
  63. for (String source : sources) {
  64. feeds.addAll(getFeedsFromSource(source));
  65. }
  66. LOGGER.info("Done fetching all feeds");
  67. return feeds;
  68. }
  69. }
Add Comment
Please, Sign In to add comment