Skip to Content
0

Base 64 Decoding And Sending as PDF File

Jul 14, 2017 at 06:13 PM

279

avatar image

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?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

5 Answers

Best Answer
Manoj K Jul 17, 2017 at 09:41 AM
1

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 :P

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

Show 3 Share
10 |10000 characters needed characters left characters exceeded

Let me try this Manoj.

But the output file which will be written by the file adapter needs to be deleted somehow?

0

Raghu , this code is just the desktop version you may need to re-tweak it to use it in PI and the output of this Code will be your decoded PDF .

0

Thanks Manoj for the idea.

Completed the scenario modifying the code.

For End-end I formulated an empty XML using message transform bean and selected ignore in empty file processing mode.

So no file written using file channel , only the decoded PDF using the code in the UDF.:)

0
Evgeniy Kolmakov Jul 17, 2017 at 10:19 AM
2

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.

Show 7 Share
10 |10000 characters needed characters left characters exceeded

Let me try this Eve,

So this code will convert the PDF in to string in XML and then I have to write it or how it works?

0

No, this above code will build a DOM Tree from your source XML and the using either one of the above mentioned method i.e either by xpath execution of directly reading the field content from 'Name' which is assumed here to have the encoded data then the the decoded output will be written to your outputstream i.e nothing but your output of your mapping then you later handover this output to other ICO with File Reciever using modules.

But however just have a query for Evgeniy Kolmakov does RequestResponseBean works for Dynamic-configuration too i.e Can dynamic config values be handovered from one ICO to another using these modules ? or is it just works for main paylaod?

0

Hi Manoj!

Please clarify, what is the aim of using RequestResponse bean in given scenario? Or did I miss anything?

Regards, Evgeniy.

0

Hello Eve,

The given scenario is File to WS using Req/Res bean i.e Async -sync bridge you may check Raghu's reply to Ryan's answer.

So my query was if in case have implemented some Dynamic config in the java mapping then will this be handovered to the other ICO which has file adapter as receiver ? have you tried this anytime ?

Br,

Manoj

0

Hi Manoj!

As far as I remember Dynamic Configuration data is lost when you use adapter bridge in ICo configuration. But you can save DC value to module context variable before calling standard adapter bean and restore it back to DC after call.

Regards, Evgeniy.

0

Hi Raghi!

This code will convert Base64-encoded string from given XML element into byte array which, in turn, will be written to output payload of your mapping. So you'll get your PDF file as binary payload after mapping.

Regards, Evgeniy.

0

Thanks Eve, completed the Scenario,I replied the approach in Manoj's comment.

1
Vadim Klimov Jul 17, 2017 at 01:59 PM
2

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 Eng Swee - 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

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Thanks Vadim.

0
Ryan Crosby Jul 14, 2017 at 06:20 PM
1

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

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Hello Ryan,

Its an File-WS Sync scenario using req/resp beans.

Once I get the output(one field will have encoded string) that needs to be decoded which will generate a PDF.

This PDF is required in the output directory.

0
Evgeniy Kolmakov Jul 15, 2017 at 03:29 PM
1

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.

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Eve can you please share the code if possible.

Requirement is just to decode and write the PDF as file.

0