1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.pdp.batch;
17  
18  import java.io.BufferedReader;
19  import java.io.InputStreamReader;
20  import java.io.PrintWriter;
21  import java.net.URL;
22  import java.util.Date;
23  
24  import javax.net.ssl.HttpsURLConnection;
25  import javax.net.ssl.SSLContext;
26  import javax.net.ssl.SSLSocketFactory;
27  import javax.net.ssl.TrustManager;
28  import javax.net.ssl.X509TrustManager;
29  
30  import org.kuali.ole.pdp.PdpConstants;
31  import org.kuali.ole.pdp.PdpParameterConstants;
32  import org.kuali.ole.pdp.util.HttpsTrustManager;
33  import org.kuali.ole.sys.batch.AbstractStep;
34  import org.kuali.ole.sys.service.impl.OleParameterConstants;
35  
36  
37  
38  
39  public class DownLoadFileViaHttpsStep extends AbstractStep {
40      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DownLoadFileViaHttpsStep.class);
41  
42      private String targetDirectoryPath;
43  
44      
45  
46  
47      @Override
48      public boolean execute(String jobName, Date jobRunDate) throws InterruptedException {
49          
50          String sourceFileUrl = getParameterService().getParameterValueAsString(OleParameterConstants.PRE_DISBURSEMENT_BATCH.class, PdpParameterConstants.FEDERAL_ACH_BANK_FILE_URL);
51          String targetFileName = getParameterService().getParameterValueAsString(OleParameterConstants.PRE_DISBURSEMENT_BATCH.class, PdpParameterConstants.ACH_BANK_INPUT_FILE);
52          String targetFilePath = targetDirectoryPath + targetFileName;
53  
54          LOG.info("Downloading file from " + sourceFileUrl + " to " + targetFilePath);
55  
56          try {
57              X509TrustManager manager = new HttpsTrustManager();
58              TrustManager managers[] = {manager};
59              SSLContext context = SSLContext.getInstance(PdpConstants.SECURE_SOCKET_PROTOCOL);
60              context.init(null, managers, null);
61              SSLSocketFactory factory = context.getSocketFactory();
62  
63              URL url = new URL(sourceFileUrl);
64              HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
65              connection.setDoOutput(true);
66              connection.setSSLSocketFactory(factory);
67  
68              BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
69              PrintWriter out = new PrintWriter(targetFilePath);
70  
71              String line;
72              int count = 0;
73              while ((line = in.readLine()) != null) {
74                 out.println(line);
75                 count++;
76              }
77  
78              in.close();
79              out.close();
80  
81              LOG.info("Total number of ACH Bank records downloaded: " + count);
82          }
83          catch (Exception e) {
84              throw new RuntimeException(e);
85          }
86  
87          return true;
88      }
89  
90      public void setTargetDirectoryPath(String targetDirectoryPath) {
91          this.targetDirectoryPath = targetDirectoryPath;
92      }
93  
94  }