import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.bind.DatatypeConverter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class Base64Encoding extends AbstractTransformation {
/*Main method is used only for Testing purpose as PI compiler looks for Transform method if required it can be comment before deploying in PI*/
public static void main(String agrs[]) throws StreamTransformationException
{
try
{
FileInputStream inStream = new FileInputStream ("C:\\Users\\Newuser\\Desktop\\Input.xml"); //INPUT FILE (PAYLOAD)
FileInputStream ins = new FileInputStream ("C:\\Users\\Newuser\\Desktop\\Input.xml");
FileOutputStream outStream = new FileOutputStream ("C:\\Users\\Newuser\\Desktop\\Output.xml");
Base64Encoding mapping = new Base64Encoding();
mapping.execute(inStream,ins, outStream);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void execute(InputStream arg0,InputStream ins, OutputStream arg1) throws IOException{
try {
/*Creating a DOM document "doc" to read entire payload*/
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
Document doc;
dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(arg0);
doc.getDocumentElement().normalize();
/*Searching for the Node "DeliveryInfo" in source payload*/
NodeList nodeList = doc.getElementsByTagName("DeliveryInfo");
Node node = nodeList.item(0);
Element eElement = (Element) node;
/*Creating a DOM document "document" to creaet target payload*/
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
/*Below code create Namespace for target message */
document.setXmlVersion("1.0");/*****set the XML Version**********/
Element Namespace= document.createElementNS("http://mycompany.com/pi/mapping/INVOIC", "ns0:MT_INVOIC_SOA_Request");
document.appendChild(Namespace);
/*This will create the xml structure for target message type
and will get the values of required nodes from source payload to assign the same in Target structure*/
Element Element1 = document.createElement("BillerID");
Namespace.appendChild(Element1);
Element Element2 = document.createElement("TransactionID");
Namespace.appendChild(Element2);
Element Element3 = document.createElement("Data");
Namespace.appendChild(Element3);
Element1.setTextContent(eElement.getElementsByTagName("BillerID").item(0).getTextContent());
Element2.setTextContent(eElement.getElementsByTagName("TransactionID").item(0).getTextContent());
/*This will read the entire source payload and encode in Base64
and same will be assign to new field to passs in target structure*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[ins.read()];
int len;
while ((len = ins.read(buffer)) > -1 ) {
baos.write(buffer, 0, len);
}
baos.flush();
String Payload= new String(baos.toByteArray());
/*This will remove the Message Type Node from the Payload befor encoding the same in Base64*/
String removeNodes = Payload.substring(0,Payload.indexOf("<ns0:MT_INVOIC_SAP_Request xmlns:ns0=\"http://eaton.com/pi/OTTO_FISCHER/INVOIC\">")) + Payload.substring(Payload.indexOf("<ns0:MT_INVOIC_SAP_Request xmlns:ns0=\"http://mycompany.com/pi/mapping/INVOIC\">") + ("<ns0:MT_INVOIC_SAP_Request xmlns:ns0=\"http://mycompany.com/pi/mapping/INVOIC\">>".length()-1) , Payload.indexOf("</ns0:MT_INVOIC_SAP_Request>"));
String encodedString= DatatypeConverter.printBase64Binary(removeNodes.getBytes());
Element3.setTextContent(encodedString);
/* Assigning the created target message to "TransformationOutput"*/
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(arg1);
transformer.transform(domSource, streamResult);
arg1.flush();
arg1.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
/* The transform method which must be implemented */
public void transform(TransformationInput in, TransformationOutput out)
throws StreamTransformationException {
try {
execute(in.getInputPayload().getInputStream(),in.getInputPayload().getInputStream(),out.getOutputPayload().getOutputStream());
} catch (IOException e) {
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 |
---|---|
13 | |
10 | |
7 | |
7 | |
6 | |
5 | |
5 | |
5 | |
4 | |
4 |