cancel
Showing results for 
Search instead for 
Did you mean: 

How to access node value at run time using java mapping

Former Member
0 Kudos

Dear Experts ,

I am working on a proxy to HTTP scenario.Below is the output of my graphical  mapping.

Now I have to convert this XML request to multipart request .I have to fetch values of these 2 parameters (Attachment Name  ,  Attachment location)

at run time using java mapping and need to create a multipart MIME request as below. I have written code  to create multipart request for static   values.

my requirement is to parse payload and store values for these 2 nodes into 2 variables   so that I can pass these 2 variables into my code

and I can create boundaries request with run time value

As I am not a java guy...Can Anybody please share any sample code to parse this payload and store the node values into variable...??

Say for example: there are 2 attachment . so there will me 2 occurrence of node Attachment and there will be 2 sections get created .

--b2Lh0weilHCR3Aw6z30XSc-HGGYpw-LcKSA

Content-Disposition: form-data; name="C:\SDG\Temp\1.txt"; filename="Supplier Organization Creation.xlsx"

Content-Type: text/plain; charset=ISO-8859-1

Content-Transfer-Encoding: binary

Supplier Organization Creation

--b2Lh0weilHCR3Aw6z30XSc-HGGYpw-LcKSA

Content-Disposition: form-data; name="C:\SDG\Temp\1.txt"; filename="Supplier Organization Creation2.xlsx"

Content-Type: text/plain; charset=ISO-8859-1

Content-Transfer-Encoding: binary

Supplier Organization Creation2


Thanks

Sandeep sharma

Accepted Solutions (1)

Accepted Solutions (1)

PriyankaAnagani
Active Contributor
0 Kudos

Hi Sandeep,

Here is the sample code.

DocumentBuilderFactory dbFact  =  DocumentBuilderFactory.newInstance();

DocumetBuilder   dBuilder = dbFact.newDocumentBuilder();

Document document = dBuilder.parse(inputStream); // this input stream is the input you pass to java mapping

List attName = new ArrayList();

List attLocation  = new ArrayList();

NodeList nList  = document.getElementByTagName("Attachment");

//Loop through each occurrence of Attachment and add the attribute values to respective lists

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

          Element ele = (Element)nList.item(i);

          attName.add(i, ele.getChildNodes().item(0).getAttribute("AttachmentName"));

          attLocation.add(I,ele.getChildNodes().item(0).getAttribute("AttachmentLocation"));

}

Former Member
0 Kudos

Hi Priyanka,

for these 2 line I Am getting below error

attName.add(i, ele.getChildNodes().item(0).getAttribute("AttachmentName"));

          attLocation.add(I,ele.getChildNodes().item(0).getAttribute("AttachmentLocation"));

Error: The method getAttribute(String) is undefined for the type Node

Please suggest further

Thanks

former_member190293
Active Contributor
0 Kudos

Hi Sandeep!

Try following syntax:


Element eleChild = (Element)ele.getChildNodes().item(0);

attName.add(i, eleChild.getAttribute("AttachmentName");

Regards, Evgeniy.

Former Member
0 Kudos

Hi All,

Below is my Java code but I am not getting any output. Can anybody please let me know what should i change in this code  to get output

i

public class JMapping_Format_HTTPData extends AbstractTransformation {

/*

* Method transform interfaces with the SAP XI Container

*/

public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException {

getTrace().addDebugMessage("JAVA Mapping Called");

String inData = convertStreamToString(arg0.getInputPayload().getInputStream());

DynamicConfiguration conf = arg0.getDynamicConfiguration();

String out = "";

String boundary = "b2Lh0weilHCR3Aw6z30XSc-HGGYpw-LcKSA";

String CRLF = "\r\n";

String ioCtx ="1";

String ePASSRequestDocs ="C:\\SDG\\Temp\\Import.xml";

try

{

//Fetching run time parameter value

  DocumentBuilder domFactory = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 

  Transformer tf = TransformerFactory.newInstance().newTransformer(); 

  Document doc = domFactory.parse(arg0.getInputPayload().getInputStream()); 

  doc.getDocumentElement().normalize(); 

  DOMSource source = new DOMSource(doc); 

  List attName = new ArrayList();

  List attLocation  = new ArrayList();

  //NodeList nList  = doc.getElementByTagName("Attachment");

  NodeList nList = doc.getDocumentElement().getElementsByTagName("Attachment");

  

  //Loop through each occurrence of Attachment and add the attribute values to respective lists

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

  {

  Element ele = (Element)nList.item(i);

  Element eleChild = (Element)ele.getChildNodes().item(0);

  attName.add(i, eleChild.getAttribute("AttachmentName"));

  attLocation.add(i, eleChild.getAttribute("AttachmentLocation"));

          // Element ele = (Element)nList.item(i);

           //attName.add(i, ((DocumentBuilderFactory) ele.getChildNodes().item(0)).getAttribute("AttachmentName"));

           //attLocation.add(i,((DocumentBuilderFactory) ele.getChildNodes().item(0)).getAttribute("AttachmentLocation"));

         //out = "boundary=" + boundary + CRLF + CRLF;

    //Building first part : AttachmentName

    out = out + "--"+ boundary + CRLF + "Content-Disposition: form-data; name=\"attName\"" + CRLF;

    out = out + "Content-Type: text/plain; charset=US-ASCII" + CRLF;

    out = out + "Content-Transfer-Encoding: 8bit" + CRLF + CRLF;

    out = out + attLocation + CRLF;

  }

//Building first part : ioCtx

out = out + "--"+ boundary + CRLF + "Content-Disposition: form-data; name=\"ioCtx\"" + CRLF;

out = out + "Content-Type: text/plain; charset=US-ASCII" + CRLF;

out = out + "Content-Transfer-Encoding: 8bit" + CRLF + CRLF;

out = out + ioCtx + CRLF;

// Building the payload part

out = out + "--" + boundary + CRLF;

out = out + "Content-Disposition: form-data; name=\"C:\\SDG\\Temp\\Import.xml\"; filename=\"import.xml\"" + CRLF;

out = out + "Content-Type: text/xml; charset=ISO-8859-1" + CRLF;

out = out + "Content-Transfer-Encoding: binary" + CRLF + CRLF;

out = out + inData + CRLF + "--" + boundary + "--";

arg1.getOutputPayload().getOutputStream().write(out.getBytes());

//arg1.getOutputHeader().setContentType("multipart/form-data;boundary=b2Lh0weilHCR3Aw6z30XSc-HGGYpw-LcKSA");

}

catch(Exception exception1) {

  getTrace().addWarning(exception1.getMessage(),exception1);

}

}

/* Converts Input stream to String */

public String convertStreamToString(InputStream in){

StringBuffer sb = new StringBuffer();

try

{

InputStreamReader isr = new InputStreamReader(in);

Reader reader =

new BufferedReader(isr);

int ch;

while((ch = in.read()) > -1) {

sb.append((char)ch);}

reader.close();

}

catch(Exception exception) { getTrace().addWarning(exception.getMessage(),exception);}

return sb.toString();

}

public static void main(String[] args) {

}

}

Thanks

Sandeep Sharma

anupam_ghosh2
Active Contributor
0 Kudos

Hi Sandeep,

                   Please can you provide complete input xml and the output mime output you expect after java mapping.

Why are you not using mail packaging? That might resolve the issue.

Regards

Anupam

Former Member
0 Kudos

Hi anupam,

As i have to send request to a HTTP server which expects request in multipart form , I am using HTTP_AAE channel not mail .

The very first screen shot in my question is output of my graphical mapping. Then it feeds to Java mapping  output of java mapping should be  as below

--b2Lh0weilHCR3Aw6z30XSc-HGGYpw-LcKSA

Content-Disposition: form-data; name="C:\SDG\Temp\1.txt"; filename="Supplier Organization Creation.xlsx"

Content-Type: text/plain; charset=ISO-8859-1

Content-Transfer-Encoding: binary

Supplier Organization Creation

--b2Lh0weilHCR3Aw6z30XSc-HGGYpw-LcKSA

Content-Disposition: form-data; name="C:\SDG\Temp\1.txt"; filename="Supplier Organization Creation2.xlsx"

Content-Type: text/plain; charset=ISO-8859-1

Content-Transfer-Encoding: binary

Supplier Organization Creation2

--b2Lh0weilHCR3Aw6z30XSc-HGGYpw-LcKSA

Content-Disposition: form-data; name="ioCtx"

Content-Type: text/plain; charset=US-ASCII

Content-Transfer-Encoding: 8bit

1

--b2Lh0weilHCR3Aw6z30XSc-HGGYpw-LcKSA

Content-Disposition: form-data; name="C:\SDG\Temp\Import.xml"; filename="import.xml"

Content-Type: text/xml; charset=ISO-8859-1

Content-Transfer-Encoding: binary

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE ePASSRequest SYSTEM "ePASS3.dtd">

<ePASSRequest requestID="6000285962" submitterOrganization="6200" submitterUser="bam" submitterPassword="Test@1234">

<RFxRequest>

<createRFx>

<RFx RFxStyle="None" OrganizationCode="6200" RFxName="1081600039" RFxType="RFP" RFxBusinessUnit="(Unassigned)" RFxIsTemplate="FALSE" RFxTemplateCode="ZR01" RFxStatus="created" RFxDecimalPlaces="2" RFxDefaultCurrencyCode="INR" RFxCurrencyCodes="INR|USD|EUR" RFxPrimaryContact="bam">

<GeneralRFxBiddingRules RFxUseBundleBid="false" RFxVolumeDiscount="false" RFxUseItemSet="false" RFxEventNotifications="true"/>

<AttachmentRules RFxEventAttachment="true" RFxBidAttachment="true" RFxQuestionnaireResponseAttachment="true" RFxQuestionResponseAttachment="true"/>

<RFQBiddingRules RFxUseVolumeBid="false" RFxAllowOptimizationFeedback="false" RFxAllowSupplierConstraint="false" RFxAllowSingleBidPerItem="false"/>

<RFxBuyerRules RFxLockedBidIndicator="false"/>

<HostAssignment LoginName="bam" ResponderParticipationStatus="accepted" SubscriptionType="rfx" HostAssignmentRole="Lead"/>

<Subscription LoginName="xyz@abc.com" SupplierProfileName="Org Name" ResponderParticipationStatus="unacknowledged" SubscriptionType="rfx"/>

<Requisition RequisitionCode="Default" RequisitionName="Default">

<LineItem LineItemCategoryCode="root category" LineItemCode="10" LineItemLotName="Default" LineItemMinimumBidQuantity="0" LineItemMinimumDesiredQuantity="10.0" LineItemName="10" LineItemType="single">

<ExtendedAttribute ExtendedAttributeCode="Material Code" ExtendedAttributeValue="OPC43"/>

<ExtendedAttribute ExtendedAttributeCode="Material Group" ExtendedAttributeValue="Z200"/>

<ExtendedAttribute ExtendedAttributeCode="Required Delivery Date" ExtendedAttributeValue="30.07.2016"/>

<ExtendedAttribute ExtendedAttributeCode="UOM" ExtendedAttributeValue="Metric Ton"/>

</LineItem>

</Requisition>

</RFx>

</createRFx>

</RFxRequest>

</ePASSRequest>

--b2Lh0weilHCR3Aw6z30XSc-HGGYpw-LcKSA--

Thanks

former_member190293
Active Contributor
0 Kudos

Hi Sandeep!

Your mapping doesn't work because of some errors in your code.

First, I'm not sure about your method convertStreamToString as it produces corrupted XML output string.


Try to use following code instead:

public String streamToString(InputStream in) throws IOException {

  ByteArrayOutputStream result = new ByteArrayOutputStream();

  byte[] buffer = new byte[2048];

  int length;

  while ((length = in.read(buffer)) != -1) {

       result.write(buffer, 0, length);

  }

  return result.toString("UTF-8");

}

Second, for getting attributes values you use:

Element ele = (Element)nList.item(i);

Element eleChild = (Element)ele.getChildNodes().item(0);

attName.add(i, eleChild.getAttribute("AttachmentName"));

attLocation.add(i, eleChild.getAttribute("AttachmentLocation"));

"AttachmentName" and "AttachmentLocation" are the attributes of "ele" so you must use following code:

Element ele = (Element)nList.item(i);

Element eleChild = (Element)ele.getChildNodes().item(0);

attName.add(i, eleChild.getAttribute("AttachmentName"));

attLocation.add(i, eleChild.getAttribute("AttachmentLocation"));

Regards, Evgeniy.

Former Member
0 Kudos

Hi Anupam

Thanks a lot for helping me out.

Your code worked perfectly

sandeep

Answers (0)