cancel
Showing results for 
Search instead for 
Did you mean: 

Renaming the PAYLOAD-NAME

0 Kudos

Hi,

I need to know is posible rename the Payload (MainDocument) By (Document).

I have a Scenario SOAP Sender - PI - REST Receiver, and I sending a Payload with PDF Attachment and JSON Document but I need rename this.

I found information to rename the attachment but can not find how to do to change the MainDocument.

best regards,

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hi George and All,

Thank you so much. I have resolved the issue.

I solved by applying a mapping program JAVA, which converts the content-type of the two parts of the message body, adding the data I needed.

To do this, I build on the following Blogs:

And here's the solution:

File CreateForm.java

package com.sap.map.multipart;

import java.io.*;

import java.util.Collection;

import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import com.sap.aii.mapping.api.AbstractTrace;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.Attachment;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

public class CreateForm extends AbstractTransformation {

    private static final String LINE_FEED = "\r\n";

    private String contentType1;

    private String contentType2;

  @SuppressWarnings("unchecked")

  public void transform (TransformationInput in, TransformationOutput out)

  throws StreamTransformationException {

  AbstractTrace trace = getTrace();

  String json = new String();

  InputStream ins = in.getInputPayload().getInputStream();

  try{

  //***************************************************************

  //Get document XML from input stream.

  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

  DocumentBuilder builder = factory.newDocumentBuilder();

  Document doc = builder.parse(ins);

  //***************************************************************

  //***************************************************************

  //Convertir XML a JSON - y Escribir nuevo Payload con content-type editado

  //***************************************************************

         //Instanciamos la clase para convertir el Payload

  ParseDocument pDoc = new ParseDocument();

  //Guardamos el nuevo Payload en variable String

         json = pDoc.ConvertDocumentXML2JSON(doc);

  //Editamos un nuevo content-type para el encabezado del Payload

          contentType2 = "text/plain;charset=us-ascii" + LINE_FEED

            + "content-transfer-encoding:7bit" + LINE_FEED

            + "content-disposition:form-data;name=\"document\"";

           //Grabamos el nuevo contentype en el Header del Payload

         out.getOutputHeader().setContentType(contentType2);

        //Convertimos en Bytes el String nuevo y lo grabamos en la salida

         byte[] message = json.getBytes("UTF-8");

         //Escribimo el Output Pay-load

  out.getOutputPayload().getOutputStream().write(message);

  //***************************************************************

  //***************************************************************

  //Reescribimos el Header del Adjunto y grabamos el nuevo

  //***************************************************************

         //Se obtienen los ContentID de los Adjuntos

  Collection<String> contId = in.getInputAttachments().getAllContentIds(false);

         String contentId = null;

         //Iteramos para obtener el String del ContentID

         for (Iterator iterator = contId.iterator(); iterator.hasNext();) {

          contentId = (String) iterator.next();

         }

         //Creamos un Objeto Adjunto de entrada y lo tomamos con el ContentID obtenido

         Attachment adjuntoIn = in.getInputAttachments().getAttachment(contentId);

         //Guardamos en Variables los 3 parámetros del adjunto de entrada

         //1) ContentID ya lo teniamos

         //2) el contenido del archivo en Bytes[]

         byte[] content = adjuntoIn.getContent();

         //3) el content-type del header del adjunto

         contentType1 = adjuntoIn.getContentType();

         //Editamos el content-type del header del adjunto al valor nuevo deseado

         contentType1 = contentType1 + LINE_FEED

              + "content-transfer-encoding:binary" + LINE_FEED

              + "content-disposition:form-data;name=\"" + contentId +"\";filename=\"" + contentId + "\"";

         //Eliminamos de la Salida el Adjunto que teniamos

         out.getOutputAttachments().removeAttachment(contentId);

         //Creamos un nuevo adjunto con los valores deseados

         Attachment adjuntoOut = out.getOutputAttachments().create(contentId, contentType1, content);

         //Seteamos el nuevo adjunto como adjunto de Salida

         out.getOutputAttachments().setAttachment(adjuntoOut);

  //***************************************************************

  //***************************************************************

  } catch (Exception e) {

  trace.addDebugMessage(e.getMessage());

  }

  }

  }

I used a helper class to convert the XML document into JSON:

File ParseDocument.java

package com.sap.map.multipart;

import org.w3c.dom.CharacterData;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.Document;

import org.w3c.dom.NodeList;

public class ParseDocument {

  public String ConvertDocumentXML2JSON(Document doc) {

  String xmlParse = "";

  try {

  doc.getDocumentElement().normalize();

  //Get the node List that contains the node "document"...

  Element nodoRaiz = null;

  NodeList listaNodos = null;

  Node nodo = null;

  Element elem = null;

  xmlParse = "{ ";

  NodeList documentNode = doc.getElementsByTagName("n0:Document");

  nodoRaiz = (Element) documentNode.item(0);

  listaNodos = nodoRaiz.getChildNodes();

  for (int i = 0; i < listaNodos.getLength(); i++) {

  nodo = listaNodos.item(i);

  if (nodo instanceof Element) {

            elem = (Element) nodo;

    if (xmlParse != "{ "){xmlParse = xmlParse + ", ";}

    xmlParse = xmlParse +"\""

  +elem.getNodeName()

  +"\": \""

  +getCharacterDataFromElement(elem)

  + "\"";

  } 

  }

  xmlParse = xmlParse + " }";

  } catch (Exception e) {

  System.out.println(e.getMessage());

  }

  return xmlParse;

  }

  public static String getCharacterDataFromElement(Element e) {

  String texto = "";

  Node child = e.getFirstChild();

  if (child instanceof CharacterData) {

  CharacterData cd = (CharacterData) child;

  texto = cd.getData();

  return texto;

  }

  return "";

  }

}

Answers (4)

Answers (4)

0 Kudos

sorry folks, I think I generated a confusing question, this is what I really need ...

I saved the whole message I am sending, this message is "Multipart" and need to change the "header" of the part "MainDocument" in the "Body" message.

Add real example:

george_credland
Participant
0 Kudos

Hi Patricio,

Sorry for the delay.

I don't have a way to remove the "content-id" header.

"Content-Type" can be manipulated by several means:

There's an option to do this on the "Parameter" tab of the Advanced http adapter:

Alternatively it can also be re-written using an SAP supplied Bean. e.g. MessageTransformBean.

Inserting MessageTransformBean in Module Processor -  Adding Modules to the Module Processor - SAP ...

On the "Module" tab.

Module Name = AF_Modules/MessageTransformBean

Give it a module key (unique label) or let it default an id number. Sort the line to the top of the "Processing Sequence".

In the Module Configuration supply the same "module key".

Parameter name=Transform.ContentType

Parameter value=text/plain

n.b. this won't character set convert utf-8 and us-ascii. Rather its just changing the declaration.

There's a separate TextCodepageConversionBean for that. Follow the SAP Help link, then look for the Bean name in the list on the left.

george_credland
Participant
0 Kudos

Hi Gaurav,

How do the headers appear on the receiver side? I use a free utility TCPMon to create a listener on my local machine, then point the PI channel to that over http. That way it shows the full request including headers.

Have you tried adding PayloadSwapBean?

Adding PayloadSwapBean in the Module Processor -  Adding Modules to the Module Processor - SAP Libr...

On the Module tab of the communication channel insert it into the "Processing Sequence" table

so that its the first item in the list.

The help page talks about the Mail adapter, but the beans often apply in other cases so its worth trying it.

George

GauravKant
Contributor
0 Kudos

Hi Patricio,

i don't think, we can change this.  As we don't have ASMA parameter filename for SOAP adapter. And the Maindocument we are getting by default as we cannot pass name for the data processing from application.

Regards,

Gaurav

former_member190293
Active Contributor
0 Kudos

Hi Patricio!

Wouldn't you please explain why do you need it?

Regards, Evgeniy.