/**
* Retrieve the auth token from Adobe service
* @return
*/
private String getoauthToken() {
String token = "";
String tokenEndpoint = "https://oauthasservices-sXXXXXXXtrial.hanatrial.ondemand.com/oauth2/api/v1/token";
String client_id = <<ClientID from step 3>>;
String client_secret = <<Client secret from step 3>>;
String grant_type = "client_credentials";
String scope = "generate-ads-output";
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(tokenEndpoint);
String base64Credentials = Base64.getEncoder().encodeToString((client_id + ":" + client_secret).getBytes());
//1. Prepare the request headers
httpPost.addHeader("Authorization", "Basic " + base64Credentials);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
StringEntity input = null;
try {
input = new StringEntity("grant_type=" + grant_type + "&scope=" + scope);
httpPost.setEntity(input);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//2. Post the request
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
//3. Retrieve the token from the response
try {
JSONObject tokenjson = new JSONObject(IOUtils.toString(response.getEntity().getContent(), "UTF-8"));
token = tokenjson.getString("access_token");
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedOperationException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return token;
}
/**
* This method is used to encode the input files
* @param fileName
* @return
* @throws IOException
*/
private String encodeFileToBase64Binary(final String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource("").getPath();
String fullPath = URLDecoder.decode(path, "UTF-8");
File file = new File(fullPath + fileName);
return Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(file));
}
/**
* This method is used to call the Adobe service
* @return
*/
private String callService(){
//1. Get the oAuth token
String token = getoauthToken();
//2. Prepare the request headers
String url = "https://adsrestapiformsprocessing-sXXXXXXXtrial.hanatrial.ondemand.com/ads.restapi/v1/adsRender/pdf";
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(url);
request.addHeader("Authorization", "Bearer "+token);
request.addHeader("Content-Type", "application/json");
//3. Encode the form template file
String inputFileName = "adbforms\\Invoice.xdp";
String encxdp = "";
try {
encxdp = encodeFileToBase64Binary(inputFileName);
} catch (IOException e1) {
e1.printStackTrace();
}
//4. Encode the data xml file
inputFileName = "adbforms\\gendata.xml";
String encdata = "";
try {
encdata = encodeFileToBase64Binary(inputFileName);
} catch (IOException e1) {
e1.printStackTrace();
}
//5. Prepare the body of the request
String json = "{ "
+ "\"xdpTemplate\": \""+encxdp+"\", "
+ "\"xmlData\": \""+encdata+"\"}";
StringEntity input = null;
try
{
input = new StringEntity(json);
}catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
//6. Call the service and get the result
request.setEntity(input);
HttpResponse response = null;
try
{
response = httpClient.execute(request);
}catch(IOException e)
{
e.printStackTrace();
}
//7. Retrieve the file name and content from the response
String file = null;
String fileName = null;
try {
JSONObject tokenjson = new JSONObject(IOUtils.toString(response.getEntity().getContent(), "UTF-8"));
file = tokenjson.getString("fileContent");
fileName = tokenjson.getString("fileName");
//8. Decode and write the file.
writeUsingOutputStream(file, fileName);
} catch (IOException e) {
e.printStackTrace();
}catch(UnsupportedOperationException e)
{
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return file;
}
/**
* This method is used to decode and the write the output PDF file.
* @param data
* @param fileName
*/
private void writeUsingOutputStream(String data, String fileName) {
fileName = "adbforms/test.pdf";
byte[] decoded = Base64.getDecoder().decode(data);
String path = this.getClass().getClassLoader().getResource("").getPath();
try {
String fullPath = URLDecoder.decode(path, "UTF-8");
File file = new File(fullPath + fileName);
FileUtils.writeByteArrayToFile(file, decoded);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
9 | |
7 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 | |
3 |