Tag: Java

Graphical Mapping with Plain Text to XML conversion

Graphical Mapping with Plain Text to XML conversion

Scenario:

Synchronous communication between a SAP system and a non SAP websystem. The SAP system sends a request via Proxy to SAP PI, which forwards the request via SOAP to a webservice. The webservice sends a synchronous XML response back to PI and PI forwards it back to the SAP system. So far so good.

But, in case of an error, the webservice responds with a plain text message only, which fails on PI during the mapping.

Solution:

Therefore, a mapping, which wraps non XML responds into an XML envelope is necessary. As the response is mapped to a different data structure, I decided to create a standard message mapping, which checks the first character for “<“. If the character is “<“, the standard message mapping is executed. In case it is any other character, the complete message is wrapped into a XML envelope.

To create this behavior, I added the following transform function into the “Attributes and Methods”:

public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {
	String strXML = new String();
	String outputPayload = null;
	// convert TransformationInput and TransformationOutput to InputStream and OutputStream
	InputStream i = (InputStream) in.getInputPayload().getInputStream();
	OutputStream o = (OutputStream) out.getOutputPayload().getOutputStream();

	// convert InputStream to String
	try {
		BufferedReader inpxml = new BufferedReader(new InputStreamReader(i));
		StringBuffer buffer = new StringBuffer();
		String line="";

		while ((line = inpxml.readLine()) != null) {
			buffer.append(line);
		}
		strXML=buffer.toString();

	} catch (Exception e) {
		System.out.println("Exception Occurred");
	}

	// check if input is XML
	if (strXML.startsWith("<")) {
		// if input is XML, use regular graphical mapping
		super.transform(in, out);
	} else {
		// if input is non XML, wrap input into XML envelope
		outputPayload =
		"<?xml version='1.0' encoding='UTF-8'?>"
		+ "<ns0:message xmlns:ns0='http://tempuri.org/message'>"
		+ strXML
		+ "</ns0:message>";

		// write payload into OutputStream
		try {
			o.write(outputPayload.getBytes("UTF-8"));
		} catch (Exception e) {
			System.out.println("Exception Occurred");
		}

	}

}
JAVA Mapping in Graphical Mapping
JAVA Mapping in Graphical Mapping