Advertisement
ballchaichana

Tip

Aug 29th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.84 KB | None | 0 0
  1. package th.in.oneauthen;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.net.URISyntaxException;
  10. import java.nio.file.Files;
  11. import java.nio.file.Paths;
  12. import java.util.Base64;
  13.  
  14. import org.apache.http.HttpEntity;
  15. import org.apache.http.HttpResponse;
  16. import org.apache.http.client.HttpClient;
  17. import org.apache.http.client.methods.HttpPost;
  18. import org.apache.http.client.utils.URIBuilder;
  19. import org.apache.http.entity.StringEntity;
  20. import org.apache.http.entity.mime.MultipartEntityBuilder;
  21. import org.apache.http.impl.client.HttpClientBuilder;
  22.  
  23. import com.google.gson.JsonObject;
  24. import com.google.gson.JsonParser;
  25.  
  26. public class serViceTip {
  27.    
  28.     public static String PATH_INPUT = "C:\\user\\Desktop\\dir1\\dir2\\";
  29.     public static String PATH_OUTPUT = "C:\\user\\Desktop\\dir1\\";
  30.     public static String PATH_FILECONFIG = "C:\\user\\Desktop\\dir1\\dir2\\";
  31.  
  32.     public static void main(String[] args) throws IOException {
  33.         File folder = new File(PATH_INPUT);
  34.         File[] listOfFiles = folder.listFiles();
  35.  
  36.         for (File file : listOfFiles) {
  37.             if (file.isFile()) {
  38.                 System.out.println(file.getName());
  39.                
  40.                 byte[] input_file =Files.readAllBytes(Paths.get(PATH_INPUT+file.getName()));
  41.                 byte[] encodedBytes = Base64.getEncoder().encode(input_file);  
  42.                 String encodedStringPdf =  new String(encodedBytes);
  43.                
  44.                
  45.                
  46.                
  47.                
  48.                 //read config file and get value
  49.                 String conFig = readFile(PATH_FILECONFIG+"filename.ballza");
  50.                
  51.                 JsonObject jsonObject = new JsonParser().parse(conFig).getAsJsonObject();
  52.                 String access_token = jsonObject.get("token").getAsString();
  53.                 String profileIdStr = jsonObject.get("profileId").getAsString();
  54.                 int profileId = Integer.parseInt(profileIdStr);
  55.                
  56.                 System.out.println(access_token);
  57.                 System.out.println(profileId);
  58.                
  59.                
  60.                 JsonObject json = new JsonObject();
  61.                 json.addProperty("profileId", profileId);  
  62.                 json.addProperty("accessToken", access_token);
  63.                 json.addProperty("pdfData", encodedStringPdf);
  64.                 ////////////////////////////////////////////////////////////////
  65.                
  66.                
  67.                
  68.                
  69.                
  70.                 //send to api p'pae
  71.                
  72.                 BufferedReader br = null;
  73.                 String output;
  74.                 StringBuilder responseBuilder = null;
  75.  
  76.                 HttpClient httpClient = HttpClientBuilder.create().build();
  77.                 URIBuilder uriBuilder;
  78.                 try {              
  79.                     uriBuilder = new URIBuilder("https://testoneid.inet.co.th/api/oauth/getpwd");
  80.                     HttpPost postMethod  = new HttpPost(uriBuilder.build());
  81.                     StringEntity params = new StringEntity(json.toString());
  82.                    
  83.                     postMethod.addHeader("content-type", "application/json");
  84.                     postMethod.setEntity(params);
  85.                    
  86.                     HttpResponse httpResponse = httpClient.execute(postMethod);
  87.                     int responseCode = httpResponse.getStatusLine().getStatusCode();
  88.                     if (responseCode == 201 || responseCode == 200) {
  89.                         br = new BufferedReader(new InputStreamReader((httpResponse.getEntity().getContent())));
  90.                         responseBuilder = new StringBuilder();
  91.                         while ((output = br.readLine()) != null) {
  92.                             responseBuilder.append(output);
  93.                         }
  94.  
  95.                     } else {
  96.                         System.out.println("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 pdfData = json_response.get("pdfData").getAsString();
  104.                    
  105.                    
  106.                     //decoder pdf from response
  107.                     byte[] decodedBytes = Base64.getDecoder().decode(encodedStringPdf.getBytes());
  108.                     FileOutputStream fos = new FileOutputStream(PATH_OUTPUT+file.getName());
  109.                     fos.write(decodedBytes);
  110.                     fos.flush();
  111.                     fos.close();
  112.                    
  113.                     //delete old file
  114.                    
  115.                      String Del = PATH_INPUT + file.getName();
  116.                      Files.delete(Paths.get(Del));
  117.                    
  118.                 } catch (URISyntaxException e1) {
  119.                     System.out.println("http://localhost:8080/OneESign/api/service/signing");
  120.                     e1.printStackTrace();
  121.                 }
  122.                
  123.             }
  124.             else {
  125.                 System.out.println("Empty");
  126.             }
  127.         }
  128.     }
  129.     public static String readFile(String filename) throws IOException
  130.     {
  131.         String content = null;
  132.         File file = new File(filename); // For example, foo.txt
  133.         FileReader reader = null;
  134.         try {
  135.             reader = new FileReader(file);
  136.             char[] chars = new char[(int) file.length()];
  137.             reader.read(chars);
  138.             content = new String(chars);
  139.             reader.close();
  140.         } catch (IOException e) {
  141.             e.printStackTrace();
  142.         } finally {
  143.             if(reader != null){
  144.                 reader.close();
  145.             }
  146.         }
  147.         return content;
  148.     }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement