Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Read zipped Base64-Stream and convert it to xml

Hello exprts,

I'm currently working on an interface where I have to read a zip-file from a webservice. This zp files ist base64-encoded and contains an xml-list that I need to populate to the target structure. I got the scenario so far, that it is working in the eclipse enviroment as clean java code. There I can decode the base64-string, uzipp the archive and save the inherit xml-file on my filesystem.

But I don't get it working as java mapping in a udf. I don't know how to convert the (unzipped) bytesream in order to return the xml file to the repsonse structure. Currently I'm only haveing the zip-entry as outputstream:

Does anyone of you has a sugesstion on how to get xml file in the zip-archive as output of the mapping?

regards

Christian

Tags:
Former Member
replied

Hi Christian

Your requirement is much clearer, now that we can see that the Base64 value is actually embedded in one of the fields in the XML source.

You can try out the following source code for the Java mapping. With this you won't need a graphical mapping with UDF. If you don't have a Java IDE environment to compile the archive, you can try the technique I mentioned earlier on writing the Java mapping directly in ESR. If you do that, just make sure you add the necessary imports.

package com.equalize.xpi.esr.mapping.refactor;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

import javax.xml.bind.DatatypeConverter;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

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 Base64DecodeAndUnzip extends AbstractTransformation {

  @Override

  public void transform(TransformationInput input, TransformationOutput output) throws StreamTransformationException {

  try {

  // Get base64 string from DOM input

  InputStream is = input.getInputPayload().getInputStream();

  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

  DocumentBuilder builder = factory.newDocumentBuilder();

  Document docIn = builder.parse(is);

  NodeList details = docIn.getElementsByTagName("CustomerList");

  String b64 = details.item(0).getFirstChild().getNodeValue();

  // First decode the base 64 string

  byte[] decoded = DatatypeConverter.parseBase64Binary(b64);

  // Next, unzip the file (assumption only 1 entry in zip file)

  ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(decoded));

  ByteArrayOutputStream baos = new ByteArrayOutputStream();

  ZipEntry ze = zis.getNextEntry();

  if (ze != null) {

  byte[] buffer = new byte[1024];

  int read = 0;

  while ((read = zis.read(buffer, 0, buffer.length)) != -1) {

  baos.write(buffer, 0, read);

  }

  baos.flush();

  zis.closeEntry();

  }

  zis.close();

  // Write to output stream

  OutputStream os = output.getOutputPayload().getOutputStream();

  os.write(baos.toByteArray()); 

  } catch (Exception e) {

  throw new StreamTransformationException("Exception: " + e.getMessage(), e); 

  }

  }

}

Rgds

Eng Swee

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question