Monday, September 1, 2014

Developing a simple rss feed aggregator web application

Developing a simple RSS Feed Aggregator web application:

Let us see how we can develop a very simple RSS Feed aggregator application in easy steps>>

Prerequisites: 

Following jar files are required in order to use feed api:

  1. jdom-1.1.1.jar
  2. purl-org-content-0.3.jar
  3. rome-1.0.0.jar
Following jquery library is required:
  • jquery-1.9.1.js [Note: you can use any version of jquery library but should be 1.9+]
Steps required:
  • Create a dynamic web project, for example: "RSSFeedAggregator"
  • Create a jsp file for example: 'index.jsp'
  • Add the entry of this jsp file to web.xml as given below:
<web-app>
  <display-name>RSSFeedAggregator</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

  • Add the following code to "index.jsp" page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>RSSFeedAggregator</title>
<script src="js/jquery-1.9.1.js" type="text/javascript"></script>
<script src="js/common.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function() {
setInterval(synchRSSFeeds, 15000); //Refresh the page after 15000  
});
/*
 * Function is used to call ajaxRequest 
 */
function ajaxRequest(actionName, dataUrl, callBack,options) {
var optionsLocal = {
"showSpinner" : true
};
optionsLocal = $.extend(optionsLocal, options);
$.ajax({
url : actionName,
type : "POST",
data : dataUrl,
dataType : "html",
cache : false,
context : document.body,
success : function(data) {
if (callBack) {
callBack(data, options);
}
},
error : function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
    alert(thrownError);
}
});
}

function synchRSSFeeds(){
ajaxRequest("RSSReaderServlet", "", syncRSSFeedsManp, {"showSpinner":false});
}

function syncRSSFeedsManp(data){
$("#feeds").html(data);
}

</script>
<style type="text/css">
.footernew {
background-repeat: no-repeat;
height: 65px;
font-size: 12px !important;
color: black !important;
width: 927px;
}

#pagewrap {
min-width: 80%;
margin-left: auto;
margin-right: auto;
width: 980px;
margin: 20px auto 0px;
}

.gradientBoxesWithOuterShadows {
height: 420px;
width: auto;
padding: 5px;
background-color: lightgray;
/* outer shadows  (note the rgba is red, green, blue, alpha) */
-webkit-box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.4);
-moz-box-shadow: 0px 1px 6px rgba(23, 69, 88, .5);
/* rounded corners */
-webkit-border-radius: 12px;
-moz-border-radius: 7px;
border-radius: 7px;
/* gradients */
/*old color code- #D7E9F5*/
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, white),
color-stop(15%, white), color-stop(100%, #BDBDC9));
background: -moz-linear-gradient(top, white 0%, white 55%, #BDBDC9 130%);
width: auto;
padding: 5px;
}
.imglogo {
height: 30px;
width: 170px;
float: left;
border: 0;
}

</style>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
</head>
<body bgcolor="#EDEDED">
<DIV id="pagewrap">
   <img id="logo" src="your-comp-logo.jpg" class="imglogo" />
<%-- DISPLAY THE FEEDS HERE --%>
<MARQUEE behavior="scroll" direction="left">
<h2>RSS Feeds ...</h2>
</MARQUEE>
<hr/>
<div class="gradientBoxesWithOuterShadows">
<div id="feedsdiv">
<marquee behavior="scroll" direction="down" scrollamount="2" height="400px"
 onmouseover="this.stop();" onmouseout="this.start();">
<div id="feeds"></div>
</marquee>
</div>
</div>
<table cellpadding="0" cellspacing="0" align="center"
style="padding-top: 5px">
<tr>
<td class="footernew">
<table cellpadding="0" cellspacing="0" width="100%" class="normal">
<tr>
<td align="center">Copyright Info</td>
</tr>
</table>
</td>
</tr>
</table>
</DIV>
</body>
</html>

  • Create a FeedAggregator servlet for e.g. 'RSSReaderServlet'
package com.rss.feedservices;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rss.model.FEEDMessage;
import com.rss.utils.PubDateComparator;

@WebServlet("/RSSReaderServlet")
public class RSSReaderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
StringBuffer feedsContent = new StringBuffer("<table border='1'>");
feedsContent
.append("<tr><th>Feeds</th><th>Published Date</th><th>Author</th></tr>");
RSSReaderHelper feedHlpr = new RSSReaderHelper();
List<FEEDMessage> feeds_gen = feedHlpr.getRSSFeeds("http://example.com/rss/feeds?id=544554544");
        List<FEEDMessage> feeds=new ArrayList<FEEDMessage>();
        feeds.addAll(feeds_gen);
        Collections.sort(feeds,new PubDateComparator());
for (Iterator<FEEDMessage> iterator = feeds.iterator(); iterator
.hasNext();) {
FEEDMessage feedMessage = iterator.next();
feedsContent
.append("<tr><td width='70%'><a target='_blank' href='")
.append(feedMessage.getFeedLink())
.append("'>")
.append(feedMessage.getFeedTitle())
.append("</a></td><td width='20%' align='center'>")
.append(feedMessage.getFeedPubDate())
.append("</td><td width='10%' align='center'>")
.append(feedMessage.getFeedAuthor() == "" ? "Abhinav"
: feedMessage.getFeedAuthor()).append("</td></tr>");
}
feedsContent.append("</table>");
response.getWriter().write(feedsContent.toString());
}
}

  • Create a helper class which we gonna use in the servlet to fetch the feeds e.g. 'RSSReaderHelper'.
package com.rss.utils;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.rss.model.FEEDMessage;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

public class RSSReaderHelper {
public List<FEEDMessage> getRSSFeeds(String feedUrl)
throws MalformedURLException {
URL url = new URL(feedUrl);
FEEDMessage feedMsg = null;
List<FEEDMessage> feedList = new ArrayList<FEEDMessage>();
try (XmlReader reader = new XmlReader(url);) {
SyndFeed syndFeed = new SyndFeedInput().build(reader);
for (Iterator<SyndEntry> itr = syndFeed.getEntries().iterator(); itr.hasNext();) {
SyndEntry entry = itr.next();
feedMsg = new FEEDMessage();
feedMsg.setFeedAuthor(entry.getAuthor());
feedMsg.setFeedLink(entry.getLink());
feedMsg.setFeedTitle(entry.getTitle());
feedMsg.setFeedContents(entry.getContents());
feedMsg.setFeedUri(entry.getUri());
feedMsg.setFeedUpdateDate(entry.getUpdatedDate());
feedMsg.setFeedPubDate(entry.getPublishedDate());
feedMsg.setFeedDESC(entry.getDescription());
feedList.add(feedMsg);
}
} catch (MalformedURLException mfe) {
mfe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return feedList;
}
}

  • Create a model class that will hold feeds e.g. "FEEDMessage"
package com.rss.model;
import java.util.Date;
import java.util.List;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEntry;
public class FEEDMessage {
private List<SyndEntry> feedContents;
private SyndContent feedDESC;
private String feedAuthor;
private String feedTitle;
private String feedLink;
private String feedUri;
private Date feedUpdateDate;
private Date feedPubDate;

................
......... //setter & getter 

}

  • Create a date comparator class to sort the feeds on latest date e.g PubDateComparator.
package com.rss.utils;
import java.util.Comparator;
import com.rss.model.FEEDMessage;
public class PubDateComparator implements Comparator<FEEDMessage> {
@Override
public int compare(FEEDMessage o1, FEEDMessage o2) {
return o2.getFeedPubDate().compareTo(o1.getFeedPubDate());
}
}






No comments:

Post a Comment

Thanks for your comments/Suggestions.