Advertisement
ballchaichana

serviceTip

Aug 30th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.08 KB | None | 0 0
  1. package th.in.oneauthen;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.io.PrintWriter;
  12. import java.net.URISyntaxException;
  13. import java.nio.file.Files;
  14. import java.nio.file.Paths;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Base64;
  17. import java.util.Date;
  18. import java.util.Locale;
  19.  
  20. import org.apache.http.HttpEntity;
  21. import org.apache.http.HttpResponse;
  22. import org.apache.http.client.HttpClient;
  23. import org.apache.http.client.methods.HttpPost;
  24. import org.apache.http.client.utils.URIBuilder;
  25. import org.apache.http.entity.StringEntity;
  26. import org.apache.http.entity.mime.MultipartEntityBuilder;
  27. import org.apache.http.impl.client.HttpClientBuilder;
  28.  
  29. import com.google.gson.JsonObject;
  30. import com.google.gson.JsonParser;
  31.  
  32. public class serViceTip {
  33.  
  34.     public static String PATH_INPUT = "C:\\user\\Desktop\\dir1\\dir2\\";
  35.     public static String PATH_OUTPUT = "C:\\user\\Desktop\\dir1\\";
  36.     public static String PATH_FILECONFIG = "C:\\user\\Desktop\\dir1\\dir2\\";
  37.     public static String PATH_LOG = "C:\\temp\\";
  38.  
  39.     public static void main(String[] args) throws IOException {
  40.         File folder = new File(PATH_INPUT);
  41.         File[] listOfFiles = folder.listFiles();
  42.         if (folder.isDirectory()) {
  43.             if (folder.list().length > 0) {
  44.                 for (File file : listOfFiles) {
  45.                     if (file.isFile()) {
  46.                         System.out.println(file.getName());
  47.  
  48.                         byte[] input_file = Files.readAllBytes(Paths.get(PATH_INPUT + file.getName()));
  49.                         byte[] encodedBytes = Base64.getEncoder().encode(input_file);
  50.                         String encodedStringPdf = new String(encodedBytes);
  51.  
  52.                         // read config file and get value
  53.                         String conFig = readFile(PATH_FILECONFIG + "filename.ballza");// filename is file config
  54.  
  55.                         JsonObject jsonObject = new JsonParser().parse(conFig).getAsJsonObject();
  56.                         String access_token = jsonObject.get("token").getAsString();
  57.                         String profileIdStr = jsonObject.get("profileId").getAsString();
  58.                         int profileId = Integer.parseInt(profileIdStr);
  59.  
  60.                         System.out.println(access_token);
  61.                         System.out.println(profileId);
  62.  
  63.                         JsonObject json = new JsonObject();
  64.                         json.addProperty("profileId", profileId);
  65.                         json.addProperty("accessToken", access_token);
  66.                         json.addProperty("pdfData", encodedStringPdf);
  67.                         ////////////////////////////////////////////////////////////////
  68.  
  69.                         // send to api p'pae
  70.  
  71.                         BufferedReader br = null;
  72.                         String output;
  73.                         StringBuilder responseBuilder = null;
  74.  
  75.                         HttpClient httpClient = HttpClientBuilder.create().build();
  76.                         URIBuilder uriBuilder;
  77.                         try {
  78.                             uriBuilder = new URIBuilder("http://localhost:8080/OneESign/api/service/signing");
  79.                             HttpPost postMethod = new HttpPost(uriBuilder.build());
  80.                             StringEntity params = new StringEntity(json.toString());
  81.  
  82.                             postMethod.addHeader("content-type", "application/json");
  83.                             postMethod.setEntity(params);
  84.  
  85.                             HttpResponse httpResponse = httpClient.execute(postMethod);
  86.                             int responseCode = httpResponse.getStatusLine().getStatusCode();
  87.                             if (responseCode == 201 || responseCode == 200) {
  88.                                 br = new BufferedReader(new InputStreamReader((httpResponse.getEntity().getContent())));
  89.                                 responseBuilder = new StringBuilder();
  90.                                 while ((output = br.readLine()) != null) {
  91.                                     responseBuilder.append(output);
  92.                                 }
  93.  
  94.                             } else {
  95.                                 System.out.println(
  96.                                         "Failed : HTTP error code : " + httpResponse.getStatusLine().getStatusCode());
  97.                             }
  98.  
  99.                             // get response from api p'pae
  100.                             System.out.println(responseBuilder.toString());
  101.                             String get_response = responseBuilder.toString();
  102.                             JsonObject json_response = new JsonParser().parse(get_response).getAsJsonObject();
  103.                             String pdfDataResponse = json_response.get("pdfData").getAsString();
  104.                             String responseMessage = json_response.get("responseMessage").getAsString();
  105.                            
  106.                            
  107.  
  108.                             // decoder pdf from response
  109.                             byte[] decodedBytes = Base64.getDecoder().decode(pdfDataResponse.getBytes());
  110.                             FileOutputStream fos = new FileOutputStream(PATH_OUTPUT + file.getName());
  111.                             fos.write(decodedBytes);
  112.                             fos.flush();
  113.                             fos.close();
  114.                            
  115.                             //create .txt for log
  116.                             String STATUS = responseMessage;
  117.                             String log = STATUS + ":" + file.getName();
  118.                             logServer(log);
  119.  
  120.                             // delete old file
  121.                             String Del = PATH_INPUT + file.getName();
  122.                             Files.delete(Paths.get(Del));
  123.  
  124.                         } catch (URISyntaxException e1) {
  125.                             System.out.println("http://localhost:8080/OneESign/api/service/signing");
  126.                             e1.printStackTrace();
  127.                         }
  128.  
  129.                     } else {
  130.                         System.out.println("Empty");
  131.                     }
  132.  
  133.                 }
  134.             } else {
  135.  
  136.                 System.out.println("Directory is empty!");
  137.             }
  138.         } else {
  139.  
  140.             System.out.println("This is not a directory");
  141.         }
  142.         ///
  143.     }
  144.  
  145.     public static String readFile(String filename) throws IOException {
  146.         String content = null;
  147.         File file = new File(filename); // For example, foo.txt
  148.         FileReader reader = null;
  149.         try {
  150.             reader = new FileReader(file);
  151.             char[] chars = new char[(int) file.length()];
  152.             reader.read(chars);
  153.             content = new String(chars);
  154.             reader.close();
  155.         } catch (IOException e) {
  156.             e.printStackTrace();
  157.         } finally {
  158.             if (reader != null) {
  159.                 reader.close();
  160.             }
  161.         }
  162.         return content;
  163.     }
  164.    
  165.     private static void logServer(String str) {
  166.         Date dNow = new Date();
  167.         SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss ", Locale.ENGLISH);
  168.         SimpleDateFormat daily =  new SimpleDateFormat ("dd-MM-yyyy",Locale.ENGLISH);
  169.  
  170.         System.out.println("Current Date: " + ft.format(dNow));
  171.  
  172.         try (FileWriter fw = new FileWriter(PATH_LOG + daily.format(dNow)+".txt", true);
  173.                 BufferedWriter bw = new BufferedWriter(fw);
  174.                 PrintWriter out = new PrintWriter(bw)) {
  175.             out.println("["+ft.format(dNow)+"]"+" [API Resful service]" + str);
  176.             // more code
  177.         } catch (IOException e) {
  178.            
  179.         }
  180.     }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement