Tuesday, September 2, 2014

Using apache commons HttpClient-4.3.4


HttpClient API:

The most essential function of HttpClient is to execute HTTP methods.
Execution of an HTTP method involves one or several HTTP request / HTTP response exchanges,
usually handled internally by HttpClient.

The user is expected to provide a request object to execute and HttpClient is expected to transmit the request to the target server return a corresponding response object,
or throw an exception if execution was unsuccessful. The main entry point of the HttpClient API is
the HttpClient interface that defines the contract described above.

Reference: http://www.apache.org/

Prerequisites:----- 

Following jar files are required (httpcomponents-client-4.3.4):

 commons-codec-1.9.jar
 commons-logging-1.1.3.jar
 fluent-hc-4.3.4.jar
 httpclient-4.3.4.jar
 httpclient-cache-4.3.4.jar
 httpcore-4.3.2.jar
 httpmime-4.3.4.jar

----------------------------------------------------------------------------------------------------
  HttpUtils utility class
----------------------------------------------------------------------------------------------------


/*
 * Created By: Abhinav Kumar Mishra
 * Copyright © 2014. Abhinav Kumar Mishra.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.abhinavmishra14.http.utils;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;

/**
 * The Class HTTPUtils.
 */
public final class HTTPUtils {

       /** The Constant HTTP_CODE_401. */
       public static final int HTTP_CODE_401 = 401;

       /** The Constant HTTP_CODE_400. */
       public static final int HTTP_CODE_400 = 400;

       /** The Constant HTTP_CODE_403. */
       public static final int HTTP_CODE_403 = 403;

       /** The Constant HTTP_CODE_404. */
       public static final int HTTP_CODE_404 = 404;

       /** The Constant HTTP_CODE_500. */
       public static final int HTTP_CODE_500 = 500;

       /** The Constant HTTP_CODE_409. */
       public static final int HTTP_CODE_409 = 409;

       /** The Constant HTTP_CODE_200. */
       public static final int HTTP_CODE_200 = 200;

       /** The Constant HTTP_CODE_201. */
       public static final int HTTP_CODE_201 = 201;

       /** The Constant MIME_JSON. */
       public static final String MIME_JSON = "application/json";

       /** The Constant UTF8 */
       public static final String UTF8 = "UTF-8";

       /** The Constant LOG. */
       private final static Log LOG = LogFactory.getLog(HTTPUtils.class);

       /**
        * The Constructor.
        */
       private HTTPUtils() {
             super();
       }

       /**
        * Execute http get.
        *
        * @param url the url
        * @return the input stream
        * @throws ClientProtocolException the client protocol exception
        * @throws IOException the IO exception
        * @throws KeyManagementException the key management exception
        * @throws NoSuchAlgorithmException the no such algorithm exception
        */
       public static HttpResponse httpGet(final String url)
                    throws ClientProtocolException, IOException,
                    KeyManagementException, NoSuchAlgorithmException {
             LOG.info("Sending http get with url: "+ url);
             final CloseableHttpClient httpclient = getHttpClient();
             final HttpGet request = new HttpGet(url);
             return httpclient.execute(request);
       }

       /**
        * Execute http get.
        *
        * @param url the url
        * @param userName the user name
        * @param password the password
        * @return the input stream
        * @throws ClientProtocolException the client protocol exception
        * @throws IOException the IO exception
        * @throws KeyManagementException the key management exception
        * @throws NoSuchAlgorithmException the no such algorithm exception
        */
       public static HttpResponse httpGet(final String url, final String userName,
             final String password) throws ClientProtocolException, IOException,
                    KeyManagementException, NoSuchAlgorithmException {
             LOG.info("Sending http get with url: "+ url);
           final CloseableHttpClient httpclient = getHttpClient(userName, password);
             final HttpGet request = new HttpGet(url);
             return httpclient.execute(request);
       }

       /**
        * Http post.
        *
        * @param url the url
        * @param jsonReqData the json req data
        * @param userName the user name
        * @param password the password
        * @return the http response
        * @throws ClientProtocolException the client protocol exception
        * @throws IOException the IO exception
        * @throws KeyManagementException the key management exception
        * @throws NoSuchAlgorithmException the no such algorithm exception
        */
       public static HttpResponse httpPost(final String url,
             final String jsonReqData, final String userName,
             final String password) throws ClientProtocolException, IOException,
                    KeyManagementException, NoSuchAlgorithmException {
             LOG.info("Sending http post with url: "+ url);
       final CloseableHttpClient httpclient = getHttpClient(userName, password);
             final HttpPost httpPost = new HttpPost(url);
             final StringEntity httpEntity = new StringEntity(jsonReqData, UTF8);
             httpEntity.setContentType(MIME_JSON);
             httpPost.setEntity(httpEntity);        
             return httpclient.execute(httpPost);
       }

       /**
        * Http post.
        *
        * @param url the url
        * @param jsonReqData the json req data
        * @return the http response
        * @throws ClientProtocolException the client protocol exception
        * @throws IOException the IO exception
        * @throws NoSuchAlgorithmException
        * @throws KeyManagementException
        */
       public static HttpResponse httpPost(final String url,
                    final String jsonReqData) throws ClientProtocolException,
                    IOException, KeyManagementException, NoSuchAlgorithmException {
             LOG.info("Sending http post with url: "+ url);
             final CloseableHttpClient httpclient = getHttpClient();
             final HttpPost httpPost = new HttpPost(url);
             final StringEntity httpEntity = new StringEntity(jsonReqData, UTF8);
             httpEntity.setContentType(MIME_JSON);
             httpPost.setEntity(httpEntity);        
             return httpclient.execute(httpPost);
       }

       /**
        * Http post.
        *
        * @param url the url
        * @param params the params
        * @return the http response
        * @throws ClientProtocolException the client protocol exception
        * @throws IOException the IO exception
        * @throws KeyManagementException the key management exception
        * @throws NoSuchAlgorithmException the no such algorithm exception
        */
       public static HttpResponse httpPost(final String url,
                    final List<NameValuePair> params) throws ClientProtocolException,
                    IOException, KeyManagementException, NoSuchAlgorithmException {
             LOG.info("Sending http post with url: "+ url+ " params: "+params);
             final CloseableHttpClient httpclient = getHttpClient();
             final HttpPost httpPost = new HttpPost(url);
             httpPost.setEntity(new UrlEncodedFormEntity(params, UTF8));
             return httpclient.execute(httpPost);
       }

       /**
        * Http put.
        *
        * @param url the url
        * @param jsonReqData the json req data
        * @return the http response
        * @throws ClientProtocolException the client protocol exception
        * @throws IOException the IO exception
        * @throws KeyManagementException the key management exception
        * @throws NoSuchAlgorithmException the no such algorithm exception
        */
       public static HttpResponse httpPut(final String url,
                    final String jsonReqData) throws ClientProtocolException,
                    IOException, KeyManagementException, NoSuchAlgorithmException {
             LOG.info("Sending http put with url: "+ url);
             final CloseableHttpClient httpclient = getHttpClient();
             final HttpPut httpPut = new HttpPut(url);
             final StringEntity httpEntity = new StringEntity(jsonReqData, UTF8);
             httpEntity.setContentType(MIME_JSON);
             httpPut.setEntity(httpEntity);
             return httpclient.execute(httpPut);
       }

       /**
        * Http put.
        *
        * @param url the url
        * @param jsonReqData the json req data
        * @param userName the user name
        * @param password the password
        * @return the http response
        * @throws ClientProtocolException the client protocol exception
        * @throws IOException the IO exception
        * @throws NoSuchAlgorithmException
        * @throws KeyManagementException
        */
       public static HttpResponse httpPut(final String url,
             final String jsonReqData, final String userName,
             final String password) throws ClientProtocolException, IOException,
             KeyManagementException, NoSuchAlgorithmException {
             LOG.info("Sending http put with url: "+ url);
       final CloseableHttpClient httpclient = getHttpClient(userName, password);
             final HttpPut httpPut = new HttpPut(url);
             final StringEntity httpEntity = new StringEntity(jsonReqData, UTF8);
             httpEntity.setContentType(MIME_JSON);
             httpPut.setEntity(httpEntity);
             return httpclient.execute(httpPut);
       }

       /**
        * Http delete.
        *
        * @param url the url
        * @return the http response
        * @throws ClientProtocolException the client protocol exception
        * @throws IOException the IO exception
        * @throws KeyManagementException the key management exception
        * @throws NoSuchAlgorithmException the no such algorithm exception
        */
       public static HttpResponse httpDelete(final String url)
                    throws ClientProtocolException, IOException,
                    KeyManagementException, NoSuchAlgorithmException {
             LOG.info("Sending http delete with url: "+ url);
             final CloseableHttpClient httpclient = getHttpClient();
             final HttpDelete httpDelete = new HttpDelete(url);  
             return httpclient.execute(httpDelete);
       }

  
       /**
        * Http delete.
        *
        * @param url the url
        * @param jsonReqData the json req data
        * @param userName the user name
        * @param password the password
        * @return the http response
        * @throws ClientProtocolException the client protocol exception
        * @throws IOException the IO exception
        * @throws KeyManagementException the key management exception
        * @throws NoSuchAlgorithmException the no such algorithm exception
        */
       public static HttpResponse httpDelete(final String url,
       final String jsonReqData, final String userName,
             final String password) throws ClientProtocolException, IOException,
             KeyManagementException, NoSuchAlgorithmException {
             LOG.info("Sending http delete with url: "+ url);
       final CloseableHttpClient httpclient = getHttpClient(userName, password);
             final HttpDelete httpDelete = new HttpDelete(url);  
             return httpclient.execute(httpDelete);
       }

       /**
        * Gets the http client.
        *
        * @return the http client
        * @throws KeyManagementException the key management exception
        * @throws NoSuchAlgorithmException the no such algorithm exception
        */
       public static CloseableHttpClient getHttpClient()
             throws KeyManagementException, NoSuchAlgorithmException {
             final SSLContext sslContext = SSLContexts.custom().useTLS().build();
             final SSLConnectionSocketFactory sslConnFactory =
                 new SSLConnectionSocketFactory(sslContext,
                 new String[]{"TLSv1", "TLSv1.1", "TLSv1.2"},
                 null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
             //Returns the CloseableHttpClient
             return HttpClients.custom().setSSLSocketFactory(sslConnFactory).build(); 
       }

  
       /**
        * Gets the http client.
        *
        * @param username the username
        * @param password the password
        * @return the http client
        * @throws KeyManagementException the key management exception
        * @throws NoSuchAlgorithmException the no such algorithm exception
        */
       public static CloseableHttpClient getHttpClient(final String username,
             final String password) throws KeyManagementException,
             NoSuchAlgorithmException {
             final CredentialsProvider provider = new BasicCredentialsProvider();
             final UsernamePasswordCredentials credentials =
                new UsernamePasswordCredentials(username, password);
             provider.setCredentials(AuthScope.ANY, credentials);
             final SSLContext sslContext = SSLContexts.custom().useTLS().build();
             final SSLConnectionSocketFactory sslConnFactory =
new SSLConnectionSocketFactory(sslContext,  new String[]{"TLSv1", "TLSv1.1", "TLSv1.2"},  null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            
           return HttpClients.custom().setDefaultCredentialsProvider(
           provider).setSSLSocketFactory(sslConnFactory).build();
       }

       /**
        * Convert stream to string.
        *
        * @param responseStream the response stream
        * @return the string
        * @throws IOException the IO exception
        */
       public static String convertStreamToString(final InputStream responseStream)
                    throws IOException {
             return IOUtils.toString(responseStream,StandardCharsets.UTF_8);
       }

  
       /**
        * Gets the hashed password.
        *
        * @param password the password
        * @return the hashed password
        * @throws NoSuchAlgorithmException the no such algorithm exception
        */
       public static String getHashedPassword(final String password)
                    throws NoSuchAlgorithmException {
        final MessageDigest mDigest = MessageDigest.getInstance("sha1");
       final byte[] digestPassword = mDigest.digest(password.getBytes(StandardCharsets.UTF_8));
             final StringBuffer digestPassSb = new StringBuffer();
             for (int index = 0; index < digestPassword.length; index++) {
             digestPassSb.append(Integer.toString((digestPassword[index] & 0xff) +
                                 0x100,16).substring(1));
             }
             return digestPassSb.toString();
       }

       /**
        * Read binary content.
        *
        * @param filePath the file path
        * @return the byte[]
        * @throws IOException the IO exception
        */
       public static byte[] readBinaryContent(final String filePath)
                    throws IOException {
       byte[] binaryData = null;
       try (final FileInputStream inputStream = new FileInputStream(
                           filePath);) {
             try (final ByteArrayOutputStream outputStream =
                 new ByteArrayOutputStream();) {
                           final int bufferSize = 1 << 8; // 1KiB buffer
                           final byte[] buffer = new byte[bufferSize];
                           int bytesRead = -1;
                           while ((bytesRead = inputStream.read(buffer)) > -1) {
                                 outputStream.write(buffer, 0, bytesRead);
                           }
                           binaryData = outputStream.toByteArray();
                    }
             }
             return binaryData;
       }

  
       /**
        * Read binary content from input stream.
        *
        * @param inputStream the input stream
        * @return the byte[]
        * @throws IOException the IO exception
        */
       public static byte[] readBinaryContent(final InputStream inputStream )
                    throws IOException {
             byte[] binaryData = null;
             try (final ByteArrayOutputStream outputStream =
                    new ByteArrayOutputStream();) {
                    final int bufferSize = 1 << 8; // 1KiB buffer
                    final byte[] buffer = new byte[bufferSize];
                    int bytesRead = -1;
                    while ((bytesRead = inputStream.read(buffer)) > -1) {
                           outputStream.write(buffer, 0, bytesRead);
                    }
                    binaryData = outputStream.toByteArray();
             }
             return binaryData;
       }

       /**
        * Encode bytes to base64.
        *
        * @param binaryData the binary data
        * @return the string
        */
       public static String encodeBytesToBase64String(final byte[] binaryData){
             return Base64.encodeBase64String(binaryData);
       }

       /**
        * Encode bytes to base64.
        *
        * @param binaryData the binary data
        * @return the byte[]
        */
       public static byte[] encodeBytesToBase64(final byte[] binaryData){
             return Base64.encodeBase64(binaryData);
       }

       /**
        * Decode bse64 string to bytes.
        *
        * @param encodedStr the encoded str
        * @return the byte[]
        */
       public static byte[] decodeBse64StringToBytes(final String encodedStr){
             return Base64.decodeBase64(encodedStr);
       }
  
       /**
        * Write byte array to file.
        *
        * @param binaryData the binary data
        * @param filePath the file path
        * @throws FileNotFoundException the file not found exception
        * @throws IOException the IO exception
        */
       public static void writeByteArrayToFile(final byte[] binaryData,
                    final String filePath)
           throws FileNotFoundException, IOException {
             try (FileOutputStream fileOpStream = new FileOutputStream(filePath);) {
                    fileOpStream.write(binaryData);
             }
       }

       /**
        * Save stream to file.
        *
        * @param inputStream the input stream
        * @param fileName the file name
        * @throws FileNotFoundException the file not found exception
        * @throws IOException the IO exception
        */
       public static void saveStreamToFile(final InputStream inputStream,
                    final String fileName)
            throws FileNotFoundException, IOException {
             writeByteArrayToFile(readBinaryContent(inputStream),fileName);
       }
}



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());
}
}






How database drivers loaded to system ?

So, we all know how to create a JDBC connection, but what happens when you do
Class.forName (xxxx.xxxx.XXXDriver).

Let's have a look...


The DriverManager class maintains a list of Driver classes that have registered themselves by calling the method DriverManager.registerDriver. All Driver classes should be written with a static section (a static initializer) that creates an instance of the class and then registers it with the DriverManager class when it is loaded. Thus, a user would not normally call DriverManager.registerDriver directly; it should be called automatically by a Driver class when it is loaded. A Driver class is loaded, and therefore automatically registered with the DriverManager.

Most JDBC Driver classes register themselves in their static initializers by calling registerDriver().
registerDriver() is the real call that you hardly ever need to call yourself (unless you write your own JDBC driver).

It can be done using:
Class.forName (com.microsoft.jdbc.sqlserver.SQLServerDriver);
 
Calling the Class.forName automatically creates an instance of a driver and registers it with 
the DriverManager,  so you don't need to create an instance of the class,
If you were to create your own instance, you would be creating an unnecessary duplicate, 
but it would do no harm.

After this it is in the DriverManager's list of drivers and available for creating a connection.
 
So SQLServerDriver written with a static section (a static initializer) that
creates an instance of the class and then registers it with the DriverManager class when it is loaded.


See the given snippet
 
public final class SQLServerDriver implements Driver
{

    static final String driverProperties[] = {
        "databaseName", "user", "password", "serverName", "portNumber", "disableStatementPooling", "integratedSecurity", "lockTimeout", "sendStringParametersAsUnicode", "lastUpdateCount",
        "applicationName", "selectMethod", "loginTimeout", "instanceName", "workstationID", "xopenStates"
    };
    static final String driverPropertiesSynonyms[][] = {
        {
            "database", "databaseName"
        }, {
            "userName", "user"
        }, {
            "server", "serverName"
        }, {
            "port", "portNumber"
        }
    };

  static Properties connProperties;

  static
  {
        try
        {
            DriverManager.registerDriver(new SQLServerDriver());
        }
        catch(SQLException sqlexception)
        {
            sqlexception.printStackTrace();
        }
    }

    public SQLServerDriver()
    {
    }

…….all other methods..

}//end of SQLServerDriver   
 
 
Does loading of two driver classes at same time effect the connection and how to load a driver using static loading:
 
NO, It does not affect database connection it will just register them self with driver manager class by calling
DriverManager.registerDriver() method.
 
Because Vendor specific drivers are implemented in such a way that after loading them using
Class.forName(“”) will automatically register that driver to DriverManager which maintains the 
list of drivers available.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import com.mysql.jdbc.Driver;

public class DBConnection
{
  public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Class.forName("com.mysql.jdbc.Driver");
        //com.mysql.jdbc.Driver d= new Driver(); Drivers can be loaded in this way also.
        Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/usermgmtdb", "root", "root");
        System.out.println("MySQLConnection object: "+con);
        Enumeration e = DriverManager.getDrivers();
        while (e.hasMoreElements())
        {
              System.out.println("Registered jdbc drivers to driver manager: "+e.nextElement());
        }
  }
}
 
OUTPUT>>
 
MySQLConnection object: com.mysql.jdbc.Connection@18558d2
Registered jdbc drivers to driver manager: sun.jdbc.odbc.JdbcOdbcDriver@14318bb
Registered jdbc drivers to driver manager: com.mysql.jdbc.Driver@530daa


JDBC Drivers can also be loaded in this way also:
 
com.mysql.jdbc.Driver d= new Driver();
This will also load the mysql driver to DriverManager.
 
But Class.forName(“”) is recommended to load the driver.
 
Reason behind it is >>
 
A call to Class.forName("X") causes the class named X to be dynamically loaded (at runtime). 
A call to forName("X") causes the class named X to be initialized (i.e., JVM executes all
its static block after class loading). Class.forName("X") returns the Class object associated with the "X" class.
Class.forName("X") loads the class if it not already loaded. 
The JVM keeps track of all the classes that have been previously loaded.
This method uses the classloader of the class that invokes it. 
The "X" is the fully qualified name of the desired class.
For example,

class AClass { 
    static { 
        System.out.println("static block in AClass");
    }
}

public class Program { 

    public static void main(String[] args) {

        try { 

            Class c   = Class.forName("AClass"); 

        } catch (ClassNotFoundException e) {

        }
    }
}
The output is
static block in AClass
Here is one example that uses returned Class to create an instance of AClass:


class AClass {

  public AClass() {

    System.out.println("AClass's Constructor");
  }

  static { 
    System.out.println("static block in AClass");
  }

}


public class Program { 

  public static void main(String[] args) {

    try { 

      System.out.println("The first time calls forName:");
      Class c   = Class.forName("AClass"); 
      AClass a = (AClass)c.newInstance();

      System.out.println("The second time calls forName:");

      Class c1 = Class.forName("com.xyzws.AClass");

    } catch (ClassNotFoundException e) {
           e.printStacktrace();

    } catch (InstantiationException e) {
         e.printStacktrace();
    } catch (IllegalAccessException e) {            e.printStacktrace();     }   } }
The output is
The first time calls forName:

static block in AClass

AClass's Constructor

The second time calls forName:  

//Calss has been loaded so there is not "static block in AClass" printing out
 
 

 

Friday, August 29, 2014

Type conversion of integers in Java


Let's see how integer type are converted in java internally.


Type conversion of integers in Java:

Converting from int to byte and short >>
Example>
int num=132;
byte b= (byte)num;
short s= (short) num;
------------------------------------------------------------------------------------------------------------

Explanation---
Note :>> in java byte (8-bit), short (16-bit), int (32-bit), long (64-bit) are signed 2's complement Integers,
I.e. these data types stores data in form of 2's complement, because java supports signed    integers and to store the negative values it uses the 2’s complement of their binary representation.

byte b= (byte)num;

0000 0000 0000 0000 0000 0000 1000 0100 => 32 bit int

1000 0100 => 8 bit byte [Rest of 24 bits will be chopped off to make it 8 bit from 32 bit, because 132 will not fit within the range of byte which is -128 to 127]
Here 2's complement will be taken---

0111 1011  (1s compliment)
              +1   (2s complement)
0111 1100
----------------
- 124 (byte)
----------------
*************************************************************************************
short s= (short) num;

0000 0000 1000 0100 => 16 bit short [No loss of bit so result will be same, i.e. 132, because 132 is perfectly fits within the range of short which is -32768 to 32767]
Now  Let, short s1= (short) 32769;
So,
0000 0000 0000 0000 1000 0000 0000 0001  => 32769 (an int value)

1000 0000 0000 0001  => (Rest of the bits which are out of range are chopped off here)
0111 1111 1111 1110 (1's complement)
                                  +1 (2's complement)
----------------------------
0111 1111 1111 1111 => - 32767 (short)
----------------------------


Converting from int (hex) to byte and short >>
Example>
int a=0xf1;
byte b= (byte)a;
------------------------------------------------------------------------------------------------------------

Explanation---
Note :>> in java byte (8-bit), short (16-bit), int (32-bit), long (64-bit) are signed 2's complement Integers,
I.e. these data types stores data in form of 2's complement, because java supports signed    integers and to store the negative values it uses the 2’s complement of their binary representation.

Here, int a=0xf1 , represents hexadecimal representation of int value, which is equal to 241.

So,
0000 0000 0000 0000 0000 0000 1111 0001 => 241 (an int value 32 bit)

1111 0001 => bit byte [Rest of 24 bits will be chopped off to make it 8 bit from 32 bit, because 241 will not fit within the range of byte which is -128 to 127]

0000 1110  (1’s compliment)
              +1   (2’s complement)
0000 1111
----------------
- 15 (byte)
----------------
Remember: 2’s complement will be taken when you convert the int to byte and short, or long to int.

Converting from long (hex) to int >>
Example:
long a= 0xffffffff;
int b= (int)a;
-----------------------------------------------------------------------------------------------------------

Explanation---
Note :>> in java byte (8-bit), short (16-bit), int (32-bit), long (64-bit) are signed 2's complement Integers,
I.e. these data types stores data in form of 2's complement, because java supports signed    integers and to store the negative values it uses the 2’s complement of their binary representation.
Here, long a=0xffffffff, represents hexadecimal representation of long value,which is equal to 4294967281.
So,
0000 0000 0000 0000 0000 0000 0000 0000 1111 1111 1111 1111 1111 1111 1111 1111 >>>> 4294967281 [64 bit long]

In int>> 1111 1111 1111 1111 1111 1111 1111 1111 [Rest of the bit are chopped off to fit the digits in the range of int,which is 2-32 TO 2 31]

0000 0000 0000 0000 0000 0000 0000 0000 -- 1's complement
                                                                                +1  -- 2's complement
0000 0000 0000 0000 0000 0000 0000 0001
---------------------------------------------------------
  -1 (int)
----------------------------------------------------------