Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

2/25/2016

Zip File Using Java or ExecutorService Real time Example


From below two programs helps to do concurrent zipping of all files in folder using java.


import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import com.google.common.collect.Lists;

public class ZipIt {
 
public static void main(String[] args){
  
    
  
  try {
   
   File dir=new File("<DIR PATH>");
    File[] xmlFiles=null;
   if (dir.isDirectory()) {
              xmlFiles = dir.listFiles(new FilenameFilter() {
              @Override
              public boolean accept(File folder, String name) {
                  return name.toLowerCase().endsWith(".xml");
              }
          });
      }
   ExecutorService executorService = Executors.newScheduledThreadPool(60);
   List<List<File>> smallerLists = Lists.partition(Arrays.asList(xmlFiles), 60);
   List<Future<Integer>> futures=null;
   //20 independent threads used to generate 20 images.
   List<Callable<Integer>> callables = new ArrayList<Callable<Integer>>();
   
   for (List<File> list : smallerLists) {
    callables.add(new ZipFilesThread(list));
   }
   
   try {
    futures = executorService.invokeAll(callables);
    
   } finally {
    executorService.shutdown();
   }
   
   
   for (Future<Integer> future : futures) {
    
    System.out.println("File converted to Zip:"+future.isDone());
   }
   
   
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}





import java.io.File;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.logging.Logger;

import com.mercuria.etl.util.ZipUtil;

public class ZipFilesThread implements Callable<Integer> {

 
 List<File> files;
 
 
 private static Logger logger = Logger.getLogger(ZipFilesThread.class.getName());

 public ZipFilesThread(List<File> files) {
  super();
  this.files=files;
  
 }

 @Override
 public Integer call() throws Exception {
  
  for (File xmlFile : files) {
   ZipUtil.zipFile(xmlFile.getAbsolutePath());
   xmlFile.delete();
  }
  return files.size();
  
 }
 
 
 

}

9/01/2015

Execute putty (Plink) commands (On remote Linux machine) from java



Execute putty commands (On remote Linux machine) from java


1.       Before executing this program we need to download putty and Plink from below link. Put the downloaded files in one folder. (ex: C:\putty). After download istall or run once putty.exe and plink.exe


2.       We need to set this folder to class path  or In java program set the right folder path in below line. And execute the program.



Below is the example java program. you can test your commend replacing the correct host,username and password. and line # 26 with right command.

Note: Make sure that your commend must end with \n. 





 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.InputStream;
import java.io.OutputStream;

public class PuttyTest {
 private static String host = "***";
 private static String userName = "***";
 private static String password = "****";

 public static void main(String args[]) throws Exception
 {
  PuttyTest test=new PuttyTest();
  System.out.println(test.getLimServerStatus());
 }
 
 public String getLimServerStatus() throws Exception {

  try {
      String command = "c:/putty/plink -v "+host+" -l "+userName+" -pw "+password;
      Runtime r = Runtime.getRuntime ();
      Process p = r.exec (command);
      Thread.sleep (1000);
      InputStream std = p.getInputStream ();
      OutputStream out = p.getOutputStream ();
      //InputStream err = p.getErrorStream ();

      out.write ("tail -n 1000 config/load_updates.hst | grep \"Error\" | wc -l\n".getBytes ());
      out.flush ();

      Thread.sleep (3000);

      int value = 0;
      String otherString=null;
      if (std.available () > 0) {
          value = std.read();
          otherString=String.valueOf((char) value);
          while (std.available() > 0) {
              value = std.read();
              otherString+=String.valueOf((char) value);
          }
      }
     
      int count=0;
      String[] lines = otherString.split("\r\n|\r|\n");
      for (String string : lines) {
    System.out.println(string+" :"+count++);
   }
      p.destroy ();
      return lines[lines.length-2]; // needed output is in third line.
      
 }catch(Exception e)
 {
  e.printStackTrace();
 }
  return null;
 }
}

7/02/2015

Extract Text From (Image, PDF, Image embedded in PDF)

Extract Text From (Image, PDF, Image embedded in PDF)
----------------------------------------------------------------------------------------------------
Extracting text from the PDF is easy but extract text from the PDF that you received through scan is bit difficult. Because each scanned page is embedded in PDF as image.

Logic
-------
So in these kind of PDFs, first we have to extract images from PDF than extract text from images. 

step1:

If you have maven project add below dependency to your pom. This decency required to extract images from the PDF.

<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>2.0.0</version>
</dependency>

Step2:

To extract text from the image we need to install tesseract-ocr. Download .exe from the site and install the EXE   https://code.google.com/p/tesseract-ocr/

Once after installing the EXE  add home directory to the PATH. I added installed folder path to PATH  system variable (Properties->Advance settings->Environment Variable)    

C:\Program Files (x86)\Tesseract-OCR

Step3:

Open the windows commend prompt and  run the text "tesseract" . You should not get command not  exist error here . if you get error check for how to set the path.  

Step4:
 Once all set restart the eclipse and execute the below Program with your PDF. It generate the text file of pdfs in  given folder.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.File;

import net.sourceforge.tess4j.util.PdfUtilities;

import org.apache.commons.io.FileUtils;



public class TesseractExample {
 
 static String imageFolderPath="C:/santosh/PNG";
 static public void main(String[] args) {
  try {
   File[] imageFile = PdfUtilities.convertPdf2Png(new File(
     "C:/santosh/1999_001.pdf"));
   File dir=new File("C:/santosh/IMAGE_TEXT");
   if(!dir.exists())
   {
    if (dir.mkdir()) {
     System.out.println("Directory is created!");
    } 
   }
   else {
    FileUtils.cleanDirectory(dir); 
   }
   int i=1;
   for (File file : imageFile) {
    Runtime.getRuntime().exec("tesseract "+file.getAbsolutePath()+ " "+dir+File.separator+"imageText"+i);
    i++;
   }
  } catch (Exception e) {
   System.err.println(e.getMessage());
  }

 }

4/01/2015

SFTP/FTP JAVA file upload or download from FTP Server

SFTP/FTP  JAVA  file upload or download from FTP Server


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        private String host="host";
 private String user="user";
 private String pwd="password";
 private String localFileFullPath="C:/temp/file.txt";
 private String remoteFileName="ftpFileName";
 private String sftpWorkingDir="/ftpFolder/test;
 
 public void uploadFile() throws Exception{
  JSch jsch = new JSch();
  Session session = jsch.getSession(user, host,22);
  session.setPassword(pwd);
  java.util.Properties config = new java.util.Properties();
  config.put("StrictHostKeyChecking", "no");
  session.setConfig(config);
  session.connect();
  Channel channel = session.openChannel("sftp");
  channel.connect();
  ChannelSftp channelSftp = (ChannelSftp)channel;
  channelSftp.cd(sftpWorkingDir);
  channelSftp.put(new FileInputStream(new File(localFileFullPath)), remoteFileName);
  channelSftp.exit();
  session.disconnect();
 }
 
 public void downloadFile() throws Exception{
  JSch jsch = new JSch();
  Session session = jsch.getSession(user, host,22);
  session.setPassword(pwd);
  java.util.Properties config = new java.util.Properties();
  config.put("StrictHostKeyChecking", "no");
  session.setConfig(config);
  session.connect();
  Channel channel = session.openChannel("sftp");
  channel.connect();
  ChannelSftp channelSftp = (ChannelSftp)channel;
  channelSftp.cd(sftpWorkingDir);
  //channelSftp.put(new FileInputStream(new File(localFileFullPath)), remoteFileName);
  channelSftp.get(remoteFileName, new FileOutputStream(new File(localFileFullPath)));
  channelSftp.exit();
  session.disconnect();
 }

3/06/2015

Split large multi header .csv file to multiple files in Java

Split large multi header .csv file to multiple files in Java

Problem
----------------
if we have large .csv file with multiple header something like below. In multi Thread ETL process we need to split this file to multiple files on Header.

AAA
Column1 | Column2 | Column3 ..................
row1
row2
....
;;;;;

BBB
Column1 | Column2 | Column3 ..................
row1
row2








for the above format I used the regular expression is split point. 

String regex = "^.*[A-Z]$"

you can change this expression in below method as per your column header in below function before intended to use in your problem.

The below function go through the large file line by line and as header encounter move the lines to new file. Save function do the save or creation of new file.  

private String parentFolder = "C:/ETL/copy/";
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public void split(String fileName) throws IOException {
  try {
   // opens the file in a string buffer
   File headFile=new  File(parentFolder + fileName + ".csv");
   BufferedReader bufferedReader = new BufferedReader(new FileReader(headFile));
   StringBuffer stringBuffer = new StringBuffer();

   // performs the splitting
   String line;
   int row = 0;
   int counter = 1;
   while ((line = bufferedReader.readLine()) != null) {
    String regex = "^.*[A-Z]$";
    boolean isMatch = Pattern.matches(regex, line.trim());
    if (isMatch) {
     logger.info(line);
    }
    if (isMatch && row != 0) {
     saveFile(stringBuffer, fileName + counter + ".csv",headFile.lastModified());
     counter++;
     stringBuffer = new StringBuffer();
     stringBuffer.append(line);
     stringBuffer.append(NEWLINE);
    } else {
     stringBuffer.append(line);
     stringBuffer.append(NEWLINE);
    }

    row++;
   }
   saveFile(stringBuffer,fileName + counter + ".csv",headFile.lastModified());
   bufferedReader.close();

  } catch (IOException e) {
   e.printStackTrace();
  }
 }


 
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


  
 private void saveFile(StringBuffer stringBuffer, String filename,long lastModifiedTime)
   throws IOException {
  File file = new File(parentFolder + "splittedFile");
  file.mkdir();

  FileWriter output = null;
  try {
   file = new File(parentFolder + "splittedFile/" + filename);
   file.setLastModified(lastModifiedTime);
   output = new FileWriter(file);
   output.write(stringBuffer.toString());
   // System.out.println("file " + file.getAbsolutePath() +
   // " written");
  } catch (IOException e) {
   e.printStackTrace();
  } finally {

   try {
    output.close();
   } catch (IOException e) {
    // do nothing the file wasn't been even opened
   }
  }
 }

12/30/2014

FIX to javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair

Fix ERROR
javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair at sun.security.ssl.Alerts.getSSLException(Alerts.java:208) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1902) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1860) at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1843) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1362) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339) at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:535) at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:403) at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:472) at org.apache.http.conn.scheme.SchemeSocketFactoryAdaptor.connectSocket(SchemeSocketFactoryAdaptor.java:65) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177) at org

I struck with this error over two days. I would like to publish this, so that it may help someone struck with this error.
What cipher suite?
A cipher suite is a collection of symmetric and asymmetric encryption algorithms used by hosts to establish a secure communication. Supported cipher suites can be classified based on encryption algorithm strength, key length, key exchange and authentication mechanisms.

This issue looks like is java issue. The java class “SSLSocketFactory” looks not handing the CipherSuites that contains “_ECDHE_ “ .  


  







  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.List;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class SecureSocketFactory extends SSLSocketFactory{   

              private final SSLSocketFactory delegate;

              public SecureSocketFactory(SSLSocketFactory delegate) {

                  this.delegate = delegate;
              }

              @Override
              public String[] getDefaultCipherSuites() {

                  return  this.delegate.getDefaultCipherSuites();
              }

              @Override
              public String[] getSupportedCipherSuites() {

                  return this.delegate.getSupportedCipherSuites();
              }

              @Override
              public Socket createSocket(String arg0, int arg1) throws IOException,
                      UnknownHostException {

                  Socket socket = this.delegate.createSocket(arg0, arg1);
                  List<String> limited = new LinkedList<String>();
                  for(String suite : ((SSLSocket)socket).getEnabledCipherSuites())
                  {
                      if(!suite.contains("_ECDHE_"))
                      {
                          limited.add(suite);
                      }
                  }
                  ((SSLSocket)socket).setEnabledCipherSuites(limited.toArray(
                      new String[limited.size()]));

                  return socket;
              }

              @Override
              public Socket createSocket(InetAddress arg0, int arg1) throws IOException {

                  Socket socket = this.delegate.createSocket(arg0, arg1);
                  List<String> limited = new LinkedList<String>();
                  for(String suite : ((SSLSocket)socket).getEnabledCipherSuites())
                  {
                      if(!suite.contains("_ECDHE_"))
                      {
                          limited.add(suite);
                      }
                  }
                  ((SSLSocket)socket).setEnabledCipherSuites(limited.toArray(
                      new String[limited.size()]));

                  return socket;
              }

              @Override
              public Socket createSocket(Socket arg0, String arg1, int arg2, boolean arg3)
                      throws IOException {

                  Socket socket = this.delegate.createSocket(arg0, arg1, arg2, arg3);
                  List<String> limited = new LinkedList<String>();
                  for(String suite : ((SSLSocket)socket).getEnabledCipherSuites())
                  {
                      if(!suite.contains("_ECDHE_"))
                      {
                          limited.add(suite);
                      }
                  }
                  ((SSLSocket)socket).setEnabledCipherSuites(limited.toArray(
                      new String[limited.size()]));

                  return socket;
              }

              @Override
              public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3)
                      throws IOException, UnknownHostException {

                  Socket socket = this.delegate.createSocket(arg0, arg1, arg2, arg3);
                  List<String> limited = new LinkedList<String>();
                  for(String suite : ((SSLSocket)socket).getEnabledCipherSuites())
                  {
                      if(!suite.contains("_ECDHE_"))
                      {
                          limited.add(suite);
                      }
                  }
                  ((SSLSocket)socket).setEnabledCipherSuites(limited.toArray(
                      new String[limited.size()]));

                  return socket;
              }

              @Override
              public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2,
                      int arg3) throws IOException {

                  Socket socket = this.delegate.createSocket(arg0, arg1, arg2, arg3);
                  List<String> limited = new LinkedList<String>();
                  for(String suite : ((SSLSocket)socket).getEnabledCipherSuites())
                  {
                      if(!suite.contains("_ECDHE_"))
                      {
                          limited.add(suite);
                      }
                  }
                  ((SSLSocket)socket).setEnabledCipherSuites(limited.toArray(
                      new String[limited.size()]));

                  return socket;
              }

             
      
}
Below is the method to extract data from remote site. Most of the code is same how you extract data from remote HTTPS site. Only below line you use to replace use our custom ssl socket factory rather use java default one.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
HttpsURLConnection.setDefaultSSLSocketFactory(new SecureSocketFactory(sc.getSocketFactory()));


public byte[] getUrlContent(String url) throws Exception {
              URL dataUrl = new URL(url);
              Reader reader = null;
              if (url.startsWith("ftp:") || url.startsWith("file:")) {
                     InputStream ftpInputStream = dataUrl.openStream();
                     byte[] content = IOUtils.toByteArray(ftpInputStream);
                     IOUtils.closeQuietly(ftpInputStream);

                     return content;
              }

              try {
                     // Create a trust manager that does not validate certificate chains
                     TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                           public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                                  return null;
                           }

                           public void checkClientTrusted(X509Certificate[] certs,
                                         String authType) {
                           }

                           public void checkServerTrusted(X509Certificate[] certs,
                                         String authType) {
                           }
                     } };
                     // Install the all-trusting trust manager
                     final SSLContext sc = SSLContext.getInstance("SSL");
                     sc.init(null, trustAllCerts, new java.security.SecureRandom());
                     HttpsURLConnection
                                  .setDefaultSSLSocketFactory(new SecureSocketFactory(sc.getSocketFactory()));
                     // Create all-trusting host name verifier
                     HostnameVerifier allHostsValid = new HostnameVerifier() {
                           public boolean verify(String hostname, SSLSession session) {
                                  return true;
                           }
                     };

                     // Install the all-trusting host verifier
                     HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

                     URLConnection con = dataUrl.openConnection();
                     reader = new InputStreamReader(con.getInputStream());
                     byte[] bytes = IOUtils.toByteArray(reader);
                     return bytes;

              } catch (Exception e) {
                     logger.severe(ExceptionUtils.getStackTrace(e));
                     throw e;
              } finally {
                     if (reader != null) {
                           reader.close();
                     }
              }
             


       }

10/16/2014

Simplest way to connect remote Server using any private certificate (.pfx , .cer) file is very easy in Windows machine.

First step is to import the certificate in Windows.
Double click the certificate  you get below screen->next->next->give the right certificate Password  ->click till get the completing the certificate Wizard ->finish
Note: This is a one time job.



 


















Than just use the below method to get the client.  getHttpClientOnlyWindowsApp()

Below line  create the right keystore for you.  If we are going to use only WINDOWS than we don't need to create keystore as we did in other blog example. Below method create the 
KeyStore keyStore = KeyStore.getInstance("WINDOWS-MY");

Note: This works only for windows machine. If you are using Linux server follow other example.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
protected  DefaultHttpClient getHttpClientOnlyWindowsApp() throws Exception{
                    
                    
                     final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                           @Override
                           public void checkClientTrusted(final X509Certificate[] chain,
                                         final String authType) {
                           }
      
                           @Override
                           public void checkServerTrusted(final X509Certificate[] chain,
                                         final String authType) {
                           }
      
                           @Override
                           public X509Certificate[] getAcceptedIssuers() {
                                  return null;
                           }
                     } };
             
                     SSLContext context = SSLContext.getInstance("SSL");
               KeyManagerFactory keyFac = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
               KeyStore keyStore = KeyStore.getInstance("WINDOWS-MY");
               keyStore.load(null, null);
               keyFac.init(keyStore, null);
              context.init(keyFac.getKeyManagers(), trustAllCerts, null);
               SSLSocketFactory sslsf = new SSLSocketFactory(context);
                     Scheme sch = new Scheme("https", 443, sslsf);
                     DefaultHttpClient httpclient = new DefaultHttpClient();
                     httpclient.getConnectionManager().getSchemeRegistry().register(sch);
                     return httpclient;

              }

10/02/2014

How to do SSL connection using .pfx key file and password in java (HTTPClient)

How to do SSL connection using .pfx key file and password in java (HTTPClient)
If you are going to use HTTPClient to extract the java understanding TrustStore, KeyStore are very impotent.
KeyStore: Is nothing but private key (.pfx and its password) information that client is used to connect remote machine. Used by client.
Truststore : Which contains trusted certificates  chain. Used by server.  
   Why we need these certificates while connecting from HTTP client/java?
In client server communication first client send “hello” request. Server would replay to this with “hello” response. The response also contains the certificate usually “singed by CA” and ask the client something say do you trust this certificate?  Then client (java) looks for this certificate in the truststore (certificate chain) whether this certificate can be trusted or not?   If this certificate in trust store, client send “true” to Server.  Once client trust the certificate, server allows accessing to server method.  If server method access restricted it looks for private key and its password.  
This is all happens in fraction of mile seconds and this is called a HAND-SHAKE.    If anything goes wrong in handshake we get handshake exception.  
Connecting to server using httpclient is all about configuring the right sslcontext.

Below is the abstract java class you can use this directly by extending to your own class. This class would create write client for you.

If you don’t have “truststore” and just need to trust all certificates blindly use below method to create client.
getHttpClientWithoutTrustKey()
If you created trust store than use below method to get client
DefaultHttpClient getHttpClient()
In both methods it is important to understand how I am creating  keystore,truststore, and SSLContext.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.my.downloaders;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.X509Certificate;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.util.Assert;


/*
 * keytool -keystore <trustore name> -import -alias <alias name >-file certificate.cer -trustcacerts
 */
public abstract class AbstractHTTPSWithPrivateKeyDownloader {
       private static Logger logger = Logger.getLogger(AbstractHTTPSWithPrivateKeyDownloader.class.getName());
      
       public String trustStoreKey;//="truststore location";
       public String trustStorePass;//="trustostore";
       public String keyStoreKey;//=”absolute path to .pfx file”;
       public String keyStorePassowrd;//="";
       public String privatePassWord;//="it is same as keyStorePassowrd ";
      
       public void afterPropertiesSet() throws Exception {

              Assert.notNull(keyStorePassowrd, "keyStorePassowrd property cannot be NULL.");
              Assert.notNull(keyStoreKey, "keyStore property cannot be NULL.");
             
       }
      
       public abstract void doExecute() throws Exception;

       public void setTrustStoreKey(String trustStoreKey) {
              this.trustStoreKey = trustStoreKey;
       }
       public void setTrustStorePass(String trustStorePass) {
              this.trustStorePass = trustStorePass;
       }
       public void setKeyStoreKey(String keyStoreKey) {
              this.keyStoreKey = keyStoreKey;
       }
       public void setKeyStorePassowrd(String keyStorePassowrd) {
              this.keyStorePassowrd = keyStorePassowrd;
              this.privatePassWord=keyStorePassowrd;
       }

       protected  DefaultHttpClient getHttpClient() throws Exception{
             
              try
              {
              KeyStore trustStore = getTrustoStore(new File(trustStoreKey),
                           trustStorePass);
              KeyStore keyStore = getKeyStore(new File(keyStoreKey), keyStorePassowrd);
              SSLSocketFactory socketFactory = getSSLSocketFactory(
                           privatePassWord, trustStore, keyStore);

              Scheme sch = new Scheme("https", 443, socketFactory);
              DefaultHttpClient httpclient = new DefaultHttpClient();
              httpclient.getConnectionManager().getSchemeRegistry().register(sch);
              return httpclient;
              }
              catch(Exception e)
              {
                     logger.log(Level.WARNING, e.getMessage(), e);
                     return getHttpClientWithoutTrustKey();
              }
             
       }
      
       protected  DefaultHttpClient getHttpClientWithoutTrustKey() throws Exception{
             
             
              KeyStore keyStore = getKeyStore(new File(keyStoreKey), keyStorePassowrd);
             
              KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
              kmfactory.init(keyStore, keyStorePassowrd.toCharArray());
             
              SSLContext sslContext = SSLContext.getInstance("SSL");

              // set up a TrustManager that trusts everything
              sslContext.init(kmfactory.getKeyManagers(), new TrustManager[] { new X509TrustManager() {
                          public X509Certificate[] getAcceptedIssuers() {
                            logger.info("getAcceptedIssuers =============");
                                  return null;
                          }

                          public void checkClientTrusted(X509Certificate[] certs,
                                          String authType) {
                            logger.info("checkClientTrusted =============");
                          }

                          public void checkServerTrusted(X509Certificate[] certs,
                                          String authType) {
                            logger.info("checkServerTrusted =============");
                          }
              } }, new SecureRandom());
             
              SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext);
              Scheme sch = new Scheme("https", 443, socketFactory);
              DefaultHttpClient httpclient = new DefaultHttpClient();
              httpclient.getConnectionManager().getSchemeRegistry().register(sch);
              return httpclient;
       }

       protected  SSLSocketFactory getSSLSocketFactory(
                     String privateKeyPassword, KeyStore trustStore, KeyStore keyStore)
                     throws NoSuchAlgorithmException, KeyManagementException,
                     KeyStoreException {
              SSLSocketFactory socketFactory = null;
              try {
                    
                     socketFactory = new SSLSocketFactory(keyStore, privateKeyPassword,
                                  trustStore);
              } catch (UnrecoverableKeyException ke) {
                     logger.severe("Failed to create SSLSocketFactory, possible wrong password on client private key");
              }
              return socketFactory;
       }

       protected static KeyStore getTrustoStore(File truststoreFile,
                     String truststorePassword) throws Exception {
              KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
              FileInputStream trustStream = new FileInputStream(truststoreFile);
              try {
                     logger.info("Loading server truststore from file "
                                  + truststoreFile.getPath());
                     trustStore.load(trustStream, truststorePassword.toCharArray());
                     logger.info("Truststore certificate count: "
                                  + trustStore.size());
              } catch (Exception ex) {
                     logger.severe("Failed to load truststore: " + ex.toString());
                     throw ex;
              } finally {
                     try {
                           trustStream.close();
                     } catch (Exception ignore) {
                     }
              }
              return trustStore;
       }

       protected static KeyStore getKeyStore(File keystoreFile,
                     String keystorePassword) throws KeyStoreException,
                     NoSuchProviderException, FileNotFoundException {
              KeyStore keyStore = KeyStore.getInstance("PKCS12", "SunJSSE");
              FileInputStream keyStream = new FileInputStream(keystoreFile);
              try {
                     logger.info("Loading client keystore from file "
                                  + keystoreFile.getPath());
                     keyStore.load(keyStream, keystorePassword.toCharArray());
                     logger.info("Keystore certificate count: " + keyStore.size());
              } catch (Exception e) {
                     System.err.println("Failed to load keystore: " + e.toString());
                     logger.log(Level.SEVERE, e.getMessage(), e);

              } finally {
                     try {
                           keyStream.close();
                     } catch (Exception e) {
                           logger.log(Level.WARNING, e.getMessage(), e);
                     }
              }
              return keyStore;
       }

}


How to use the above class?
Create a URI and override the doExcecute() method.  

public class CliendDownloader extends AbstractHTTPSWithPrivateKeyDownloader implements Tasklet, InitializingBean {
      
       static {
              System.setProperty("sun.security.ssl.allowUnsafeRenegotiation","true");
       }
      
       private static Logger logger = Logger.getLogger(CliendDownloader.class.getName());
       public String url;
             
      
       public void setUrl(String url) {
              this.url = url;
       }

       public void doExecute() throws Exception {
              URI targetURI = new URI(url.trim());
              String protocol = targetURI.getScheme();
              if (!"https".equals(protocol)) {
                     logger.severe("URI does not begin with expected protocol name");
                     return;
              }
              logger.info("URI to fetch is " + targetURI.toString());

              DefaultHttpClient httpclient =null;
              try {
                    
                     if(trustStoreKey!=null && trustStorePass!=null)
                     {
                           httpclient = getHttpClient();
                     }
                     else
                     {
                           httpclient = getHttpClientWithoutTrustKey();
                     }
                    
                     HttpGet httpget = new HttpGet(targetURI);
                     HttpResponse response = null;
                     try {
                           response = httpclient.execute(httpget);
                            -----------
                           -------
                    
}



 }

How to create trust store?

If you would like to have trustore
Import the certificate from IE browser download in .cer format and run the below command.  

keytool -keystore <trustore name> -import -alias <alias name > -file certificate.cer -trustcacerts