Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package th.in.oneauthen;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URISyntaxException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.Base64;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.client.utils.URIBuilder;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.entity.mime.MultipartEntityBuilder;
- import org.apache.http.impl.client.HttpClientBuilder;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- public class serViceTip {
- public static String PATH_INPUT = "C:\\user\\Desktop\\dir1\\dir2\\";
- public static String PATH_OUTPUT = "C:\\user\\Desktop\\dir1\\";
- public static String PATH_FILECONFIG = "C:\\user\\Desktop\\dir1\\dir2\\";
- public static void main(String[] args) throws IOException {
- File folder = new File(PATH_INPUT);
- File[] listOfFiles = folder.listFiles();
- for (File file : listOfFiles) {
- if (file.isFile()) {
- System.out.println(file.getName());
- byte[] input_file =Files.readAllBytes(Paths.get(PATH_INPUT+file.getName()));
- byte[] encodedBytes = Base64.getEncoder().encode(input_file);
- String encodedStringPdf = new String(encodedBytes);
- //read config file and get value
- String conFig = readFile(PATH_FILECONFIG+"filename.ballza");
- JsonObject jsonObject = new JsonParser().parse(conFig).getAsJsonObject();
- String access_token = jsonObject.get("token").getAsString();
- String profileIdStr = jsonObject.get("profileId").getAsString();
- int profileId = Integer.parseInt(profileIdStr);
- System.out.println(access_token);
- System.out.println(profileId);
- JsonObject json = new JsonObject();
- json.addProperty("profileId", profileId);
- json.addProperty("accessToken", access_token);
- json.addProperty("pdfData", encodedStringPdf);
- ////////////////////////////////////////////////////////////////
- //send to api p'pae
- BufferedReader br = null;
- String output;
- StringBuilder responseBuilder = null;
- HttpClient httpClient = HttpClientBuilder.create().build();
- URIBuilder uriBuilder;
- try {
- uriBuilder = new URIBuilder("https://testoneid.inet.co.th/api/oauth/getpwd");
- HttpPost postMethod = new HttpPost(uriBuilder.build());
- StringEntity params = new StringEntity(json.toString());
- postMethod.addHeader("content-type", "application/json");
- postMethod.setEntity(params);
- HttpResponse httpResponse = httpClient.execute(postMethod);
- int responseCode = httpResponse.getStatusLine().getStatusCode();
- if (responseCode == 201 || responseCode == 200) {
- br = new BufferedReader(new InputStreamReader((httpResponse.getEntity().getContent())));
- responseBuilder = new StringBuilder();
- while ((output = br.readLine()) != null) {
- responseBuilder.append(output);
- }
- } else {
- System.out.println("Failed : HTTP error code : " + httpResponse.getStatusLine().getStatusCode());
- }
- //get response from api p'pae
- System.out.println(responseBuilder.toString());
- String get_response = responseBuilder.toString();
- JsonObject json_response = new JsonParser().parse(get_response).getAsJsonObject();
- String pdfData = json_response.get("pdfData").getAsString();
- //decoder pdf from response
- byte[] decodedBytes = Base64.getDecoder().decode(encodedStringPdf.getBytes());
- FileOutputStream fos = new FileOutputStream(PATH_OUTPUT+file.getName());
- fos.write(decodedBytes);
- fos.flush();
- fos.close();
- //delete old file
- String Del = PATH_INPUT + file.getName();
- Files.delete(Paths.get(Del));
- } catch (URISyntaxException e1) {
- System.out.println("http://localhost:8080/OneESign/api/service/signing");
- e1.printStackTrace();
- }
- }
- else {
- System.out.println("Empty");
- }
- }
- }
- public static String readFile(String filename) throws IOException
- {
- String content = null;
- File file = new File(filename); // For example, foo.txt
- FileReader reader = null;
- try {
- reader = new FileReader(file);
- char[] chars = new char[(int) file.length()];
- reader.read(chars);
- content = new String(chars);
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(reader != null){
- reader.close();
- }
- }
- return content;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement