function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Drew1815Drew1815 

WSDL2Apex - adding my own SOAP Headers to the apex class

The WSDL I am generating through WSDL2Apex does not include the necessary SOAP Headers. Is there anyway to modify the generated apex code to include the SOAP Header properties? If so, how do I do that? I see the inputHttpHeaders__x but I am looking for the equivalent for SOAP Headers (if they exist)? 

 

 

Message Edited by Drew1815 on 08-07-2009 02:48 PM
cheenathcheenath

If the soap header is defined in the wsdl then, wsdl2apex will add a public field for it on the stub.

Drew1815Drew1815

Thanks for responding.  

 

Since the WSDL I am importing doesn't have the headers defined in the WSDL itself, can I manually add those public fields to the apex class myself? If so, can you share a simple example to get me started?  

  

cheenathcheenath

Do you want to send the header or receive the header?

 

It will be easy to add the header to the wsdl and run it through wsdl2apex.

Drew1815Drew1815

I want to send soap headers.

 

I can try to manually add the headers to the WSDL, but any chance you have sample Apex code that shows headers generated as Apex code? 

cheenathcheenath

Here is the wsdl with header:

 

 

<?xml version="1.0" encoding="utf-8"?> <wsdl:definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://doc.sample.com/docSample" targetNamespace="http://doc.sample.com/docSample" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <s:schema elementFormDefault="qualified" targetNamespace="http://doc.sample.com/docSample"> <s:element name="EchoString"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="input" type="s:string" /> </s:sequence> </s:complexType> </s:element> <s:element name="EchoStringResponse"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="EchoStringResult" type="s:string" /> </s:sequence> </s:complexType> </s:element> <s:element name="SessionHeader"> <s:complexType> <s:sequence> <s:element name="sessionId" type="s:string"/> </s:sequence> </s:complexType> </s:element> </s:schema> </wsdl:types> <wsdl:message name="EchoStringSoapIn"> <wsdl:part name="parameters" element="tns:EchoString" /> </wsdl:message> <wsdl:message name="EchoStringSoapOut"> <wsdl:part name="parameters" element="tns:EchoStringResponse" /> </wsdl:message> <wsdl:message name="MyHeader"> <wsdl:part name="SessionHeader" element="tns:SessionHeader" /> </wsdl:message> <wsdl:portType name="DocSamplePortType"> <wsdl:operation name="EchoString"> <wsdl:input message="tns:EchoStringSoapIn" /> <wsdl:output message="tns:EchoStringSoapOut" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="DocSampleBinding" type="tns:DocSamplePortType"> <wsdl:operation name="EchoString"> <soap:operation soapAction="urn:dotnet.callouttest.soap.sforce.com/EchoString" style="document" /> <wsdl:input> <soap:header use="literal" message="tns:MyHeader" part="SessionHeader" /> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="DocSample"> <wsdl:port name="DocSamplePort" binding="tns:DocSampleBinding"> <soap:address location="http://www.qaresponder.info/WebServices/DocSample.asmx" /> </wsdl:port> </wsdl:service> </wsdl:definitions>

 

 

cheenathcheenath

Here is the generated stub:

 

 

//Generated by wsdl2apex public class demo { public class EchoStringResponse_element { public String EchoStringResult; private String[] EchoStringResult_type_info = new String[]{'EchoStringResult','http://www.w3.org/2001/XMLSchema','string','0','1','false'}; private String[] apex_schema_type_info = new String[]{'http://doc.sample.com/docSample','true','false'}; private String[] field_order_type_info = new String[]{'EchoStringResult'}; } public class DocSamplePort { public String endpoint_x = 'http://www.qaresponder.info/WebServices/DocSample.asmx'; public Map<String,String> inputHttpHeaders_x; public Map<String,String> outputHttpHeaders_x; public String clientCert_x; public String clientCertPasswd_x; public Integer timeout_x; public demo.SessionHeader_element SessionHeader; private String SessionHeader_hns = 'SessionHeader=http://doc.sample.com/docSample'; private String[] ns_map_type_info = new String[]{'http://doc.sample.com/docSample', 'demo'}; public String EchoString(String input) { demo.EchoString_element request_x = new demo.EchoString_element(); demo.EchoStringResponse_element response_x; request_x.input = input; Map<String, demo.EchoStringResponse_element> response_map_x = new Map<String, demo.EchoStringResponse_element>(); response_map_x.put('response_x', response_x); WebServiceCallout.invoke( this, request_x, response_map_x, new String[]{endpoint_x, 'urn:dotnet.callouttest.soap.sforce.com/EchoString', 'http://doc.sample.com/docSample', 'EchoString', 'http://doc.sample.com/docSample', 'EchoStringResponse', 'demo.EchoStringResponse_element'} ); response_x = response_map_x.get('response_x'); return response_x.EchoStringResult; } } public class EchoString_element { public String input; private String[] input_type_info = new String[]{'input','http://www.w3.org/2001/XMLSchema','string','0','1','false'}; private String[] apex_schema_type_info = new String[]{'http://doc.sample.com/docSample','true','false'}; private String[] field_order_type_info = new String[]{'input'}; } public class SessionHeader_element { public String sessionId; private String[] sessionId_type_info = new String[]{'sessionId','http://www.w3.org/2001/XMLSchema','string','1','1','false'}; private String[] apex_schema_type_info = new String[]{'http://doc.sample.com/docSample','true','false'}; private String[] field_order_type_info = new String[]{'sessionId'}; } }

 

 

 

Drew1815Drew1815

Those samples were very helpful and I am getting closer.

 

But now I am getting duplicate elements in my SOAP Header element. 

Here is a snippet of my SOAP Header XML:

 

<AWSAccessKeyId xmlns="https://queue.amazonaws.com/doc/2009-02-01/"><AWSAccessKeyId>12345</AWSAccessKeyId></AWSAccessKeyId>

 Notice the AWSAccessKeyId is redundant. 

 

 

Here is my Apex class with the header element and namespace defined in the stub:

 

public queueAmazonawsComDoc20090201.AWSAccessKeyId_element AWSAccessKeyId; private String AWSAccessKeyId_hns = 'AWSAccessKeyId=https://queue.amazonaws.com/doc/2009-02-01/';

Lastly, in my web service method, before I invoke the call, I instantiate the header element, and set the public String property name AWSAccessKeyId:

 

AWSAccessKeyId = new queueAmazonawsComDoc20090201.AWSAccessKeyId_element(); AWSAccessKeyId.AWSAccessKeyId = key;

 

All of this compiles and executes the callout but incorrectly since the <AWSAccessKeyId> element is sent twice - one for the namespace and one with the actual string value set. 

 

Any ideas how to consolidate this into 1 soap header in the XML?   

 

sfdcFanBoysfdcFanBoy

Is this solved?

 

Please let me know, I am also looking for SOAP headers. I need to pass issuer name and key for wsdl binding.

 

Thanks