56392

how add Message context headers to apache axis 2 Java

I am working on web services. I want to know how do we add headers to SOAP request in JAX-WS type web services.

Consider My header like this.

Map<String, List<String>> headers = new HashMap<String, List<String>>(); headers.put("Username", Collections.singletonList("aaaa")); headers.put("Password", Collections.singletonList("aaaa"));

I have stub object in my client class. I am using Apache Axis 2. All the classes are automatically generated.

SimpleSTub stub = new Simplestub();

I want to add this header information in client.

MessageContext.HTTP_REQUEST_HEADERS, headers

<strong>Edit</strong>

The actual implementation in a normal class found as

private static final String WS_URL = "http://localhost:9999/ws/hello?wsdl";

public static void main(String[] args) throws Exception {

URL url = new URL(WS_URL); QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService");

Service service = Service.create(url, qname); HelloWorld hello = service.getPort(HelloWorld.class); /*******************UserName & Password ******************************/ Map<String, Object> req_ctx = ((BindingProvider)hello).getRequestContext(); req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL); Map<String, List<String>> headers = new HashMap<String, List<String>>(); headers.put("Username", Collections.singletonList("mkyong")); headers.put("Password", Collections.singletonList("password")); req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers); /**********************************************************************/ System.out.println(hello.getHelloWorldAsString());

Can any one tell how to achieve this.

Thanks.

Answer1:

You're sort of on your way to the solution with what you already have. The most basic way to achieve this is

<ol> <li>

Within your client code, obtain a reference to the MessageContext through the BindingProvider on your SimpleStub

Map<String,Object> context = ((BindingProvder)stub).getRequestContext() Map<String,List> headers = context.get(MessageContext.HTTP_REQUEST_HEADERS) </li> <li>

Update the map and stuff it back in the request context object

context.put(MessageContext.HTTP_REQUEST_HEADERS,headers)

The above is all well and good. If however you're trying to do what I presume is add authentication parameters, the recommended way is

context.put(BindingProvder.USERNAME_PROPERTY,"username"); context.put(BindingProvder.PASSWORD_PROPERTY,"password"); </li> </ol>

Recommend

  • Exception occurs with JAX-RPC handler
  • Import a simple library from maven
  • How to call Class mediator property value inside another mediator Class
  • Howto extract data from the JSON body of REST request inside a WSO2 ESB Synapse handler
  • Exception in thread “AWT-EventQueue-0” java.lang.NoClassDefFoundError
  • Hadoop tasktracker issue
  • maven compile fails because i have a non-maven jar
  • JENKINS: ERROR when I try to use an older JDK for a specific maven project
  • Install ActiveMq in Apache Karaf 4.0.0.M2
  • Liferay Portlet Taglib Cast Exception
  • How to read xlsx file in an android application?
  • java program to calculate XIRR without using excel or any other library
  • How can I configure HTTP Response Headers in a Struts2 Interceptor?
  • how to exclude java sources from a third party jar?
  • Is it possible to extract table infomation using Apache Tika?
  • Why is the following two duplicate finder algorithms have different time complexity?
  • WSO2 ESB 4.0.3 - Error installing Data Services feature from 4.0.* repositories
  • detecting connection lost in spark streaming
  • How to make http call with file in groovy to upload a file and build arguments
  • How to Compose OSGi Based project with C++ based project?
  • three.js WebVR example code works on threejs.org but not on my local server
  • Phonegap GA plugin not working
  • Glassfish - java.lang.NoClassDefFoundError
  • File extension of zlib zipped html page?
  • Failed to find version-tag string. File must be updated
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • Problem deserializing objects from cache on MyBatis 3/Java
  • Cannot connect to cassandra from Spark
  • Why is an OPTIONS request sent to the server?
  • swift auto completion not working in Xcode6-Beta
  • Excel - Autoshape get it's name from cell (value)
  • How to set/get protobuf's extension field in Go?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Cassandra Data Model
  • Trying to switch camera back to front but getting exception
  • Importing jscolor library in angular 2
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Web-crawler for facebook in python
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent