on 07-14-2017 7:13 PM
Dear Scn Users,
In a web-service response in a particular field PDF is sent as encoded string.
Requirement is to decode this string and write the PDF as file in the output directory.
Can this be accomplished using Java mapping or any other approach?
Raghu,
If you don't want to send any response to original sender then why not use File-to-File with SOAP lookup this will be much easier and convenient .
Here is the sample code you may need to re-tweak it bit lazy day today 😛
package com;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import sun.misc.BASE64Decoder;
public class PdftoBase64 {
public static void main(String[] args) {
try {
FileInputStream in = new FileInputStream("C:\\Users\\mkhavatkopp\\Desktop\\PI\\Test\\encoded.txt");
FileOutputStream out = new FileOutputStream("C:\\Users\\mkhavatkopp\\Desktop\\PI\\Test\\decoded.pdf");
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try
{
int rd;
byte[] data = new byte[16384];
while ((rd = in.read(data, 0, data.length)) != -1)
buffer.write(data, 0, rd);
buffer.flush();
System.out.println(buffer);
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(buffer.toString());
out.write(decodedBytes);
out.close();
}
catch (Exception e){
System.out.println((e.getMessage()));}
}
catch (Exception e){
System.out.println((e.getMessage()));}}
}
Br,
Manoj
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Raghuram,
We are having the same requirement. We have a synchronous flow, Proxy>PI>REST API. REST API is sending response xml in which one field has base64 string of PDF. PI has to convert the base64 string to PDF and place the PDF in nfsshare folder.
Could you please share the UDF code you used and step by step process in PI.
Thanks,
Hi,
As an alternative to the approach suggested and explained in detail by Evgeniy, you can also give a try to the custom adapter module FormatConversionBean developed and released by engswee.yeoh - you can find the landing page with module functionality description in Eng Swee's blog. In particular, this adapter module contains functionality for decoding Base64 encoding String and converting it to binary - refer to the corresponding blog on its usage. Provided XPath to the message payload's element that contains Base64 encoded String, the adapter module will replace the original message payload with the binary data that is an outcome of Base64 to binary conversion. The primary difference between these two approaches is, at which message processing step the conversion takes place: as a part of message mapping execution, or after the mapping, when the message already reaches communication channel and before it gets processed by the receiver adapter.
Regards,
Vadim
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Raghu!
After you've built DOM tree for your source XML in java mapping you can try this snippet:
...
XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); String xpathString = "//DocumentFiles/File/Contents"; XPathExpression expr = xpath.compile(xpathString); byte[] fileBytes = DatatypeConverter.parseBase64Binary(((Node)expr.evaluate(doc, XPathConstants.NODE)).getTextContent()); out.write(fileBytes);
Or you can use:
byte[] fileBytes = DatatypeConverter.parseBase64Binary(doc.getElementsByTagName("Name").item[0].getTextContent().trim());
instead of compiling XPath expression for getting the same result element's contents as byte array.
For building DOM tree of your source XML you can use this code:
DocumentBuilderFactory bldFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
bldFactory.setIgnoringElementContentWhitespace(true);
bldFactory.setNamespaceAware(true);
bldFactory.setValidating(false);
bldFactory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
Document inputDoc; try {
docBuilder = bldFactory.newDocumentBuilder();
inputDoc = docBuilder.parse(in);}
catch (Exception e) {
throw new StreamTransformationException("Exception caught while parsing input document: " + e.getMessage());
}
Regards, Evgeniy.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Eve, completed the Scenario,I replied the approach in Manoj's comment.
Hi Raghu!
If you need just to read PDF from response message and save it as file to any folder, it could be easily done using java mapping.
Regards, Evgeniy.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Raghuraman,
What is the overall end to end setup for this scenario? A client calls a web service which includes that data as part of the response? That data is intended to be delivered as a file to some other destination aside from the primary client?
Regards,
Ryan Crosby
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
94 | |
11 | |
11 | |
10 | |
9 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.