View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Batch step to download a text file from an HTTPS URL to the specified target directory.
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       * @see org.kuali.ole.sys.batch.Step#execute(java.lang.String, java.util.Date)
46       */
47      @Override
48      public boolean execute(String jobName, Date jobRunDate) throws InterruptedException {
49          //String sourceFileUrl = "https://www.fededirectory.frb.org/FedACHdir.txt";
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  }