Wednesday, October 19, 2011

Custom WebService Headers (DataControl and Proxy)


 I've recently stumbled across a situation where I have had the need to call a web service which required custom HTTP headers and noticed there wasn't a lot of information around on how to do this in ADF in regards to using Data Controls or a Web Service Proxy.
Below are the two snippets of code I used and a brief description of how to implement them.

1)      Simply copy and paste the below snippet of code into your class right before you call the webservice. (The below snippet of code adds two headers into the HTTP request.)


Map<String, Object> reqCtx = ((BindingProvider)iditInterface).getRequestContext(); Map<String, List> reqHttpHeader = (Map<String, List>)reqCtx.get(MessageContext.HTTP_REQUEST_HEADERS); if (null == reqHttpHeader) { reqHttpHeader = new Hashtable<String, List>(); } List userName = new ArrayList(); userName.add("header1Value"); reqHttpHeader.put("header1", header1Value); List password = new ArrayList(); password.add("header2Value "); reqHttpHeader.put("header2", header2Value); reqCtx.put(MessageContext.HTTP_REQUEST_HEADERS, reqHttpHeader);

In order to add additional HTTP headers to a web service call using a data control you must override the existing SOAPProvider class.

1)      Create a new SOAP Provider Class “MySOAPProvider”
2)      Use the below code to override the Provider class
3)      Override the existing references to the SOAPProvider in the DataBindings.


import javax.xml.soap.MimeHeaders; import javax.xml.soap.SOAPMessage; import oracle.adf.model.adapter.AdapterException; public class CustomSOAPProvider extends oracle.adfinternal.model.adapter.webservice.provider.soap.SOAPProvider{ public CustomSOAPProvider() { super(); } public void handleRequest(SOAPMessage soapMessage) throws AdapterException { MimeHeaders hd = soapMessage.getMimeHeaders(); hd.addHeader("[header1]", "[header1value]"); hd.addHeader("[header2]", "[header2value]"); super.handleRequest(soapMessage); } public void handleResponse(SOAPMessage soapMessage) throws AdapterException { super.handleResponse(soapMessage); } }



No comments:

Post a Comment