on 07-07-2015 11:39 PM
Hi
I have a scenario whereby a ABAP Proxy is being used to send a PDF from ECC6 to PI in the form of a XML containing a Base64 string within a pair of XML tags.
The XPath to the Base64 String is /ns0:Base64Transmission/Row/Data
I have used the following code taken from other blogs on the same subject, to try and write the PDF using the file adapter:
package domDemo;
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.codec.binary.Base64;
import org.w3c.dom.Document;
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 JMDecodeDetails extends AbstractTransformation {
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document docOld = builder.parse(arg0.getInputPayload()
.getInputStream());
// XPath to tag containing Base64 PDF is:
// /ns0:Base64Transmission/Row/Data
NodeList details = docOld.getElementsByTagName("Data");
String data = details.item(0).getChildNodes().item(0)
.getNodeValue();
byte[] decodedBytes = Base64.decodeBase64(data.getBytes());
Document docNew = builder.parse(new ByteArrayInputStream(
decodedBytes));
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(docNew);
StreamResult result = new StreamResult(arg1.getOutputPayload()
.getOutputStream());
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The mapping executes successfully, but the result is a 0-byte sized file.
When I execute the test tool in the ESR, I get an error "Unable to display tree view; Error when parsing an XML document (Premature end of file.)"
Im not sure if this is relevant, or just because the result of the mapping is not an XML document.
Can anyone suggest what the issue is here please?
Many thanks in advance.
Steve
Stephen,
PDF can not be written to output as XML.
Here is updated code. Please test the interface end to end.
public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException {
try {
Document docOld = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(arg0.getInputPayload().getInputStream());
// XPath to tag containing Base64 PDF is: /ns0:Base64Transmission/Row/Data
NodeList details = docOld.getElementsByTagName("Data");
String data = details.item(0).getChildNodes().item(0).getNodeValue();
byte[] decodedBytes = Base64.decodeBase64(data.getBytes());
//Write transformed PDF to outputsteam.
arg1.getOutputPayload().getOutputStream().write(decodedBytes);
} catch (Exception 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.
ok, so using the revised code, and the OM testing 'trick', everything looks good.
When I extract the results of the OM and rename to .pdf, I see the PDF document.
However, in the end-to-end test, which is definately using the same OM, I am still getting a zero-byte file.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
25 | |
12 | |
9 | |
6 | |
6 | |
5 | |
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.