3

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

Edit

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.

Patan
  • 17,073
  • 36
  • 124
  • 198

1 Answers1

13

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

  1. 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)
    
  2. 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");   
    
kolossus
  • 20,559
  • 3
  • 52
  • 104
  • Thanks for the solution. It would be great if you can tell me the way to access it in service class also. – Patan Jan 10 '13 at 06:09
  • @User222, look at [this answer](http://stackoverflow.com/a/13302564/1530938) and just replace "response" with requests. There are other ways to get it done in the service bean though. If this answer above resolves the issue, please don't forget to accept it – kolossus Jan 10 '13 at 06:16
  • 1
    I am getting exception as stub cannot be type casted to BindingPort. I have edited the question. Please check. – Patan Jan 10 '13 at 06:48
  • Thanks again for the response.I think stub need to replaced with hello variable. But I do not know how to get it from stub. – Patan Jan 10 '13 at 06:51
  • @User222, i'm a little confused here. At what point exactly are you in this development? Have you been able to communicate with the web service at all? The code edit you've made in your question, have you actually run that and it works? – kolossus Jan 10 '13 at 12:19
  • I have developed a webservice without authentication. Its working fine. I am able to get the result. But I am not understanding how to add authentication in Axis2. – Patan Jan 10 '13 at 13:02
  • @User222 So securing the web service is the concrete requirement? You'll need to post a new question detailing that I'm afraid, an answer to that requirement won't make sense in this context – kolossus Jan 10 '13 at 14:34
  • @kolossus thanks for the solution; however, i am stuck with casting stub to bindingprovider. and i am using Axis2, too. (it throws an exception saying that my stub cannot be cast to javax.xml.ws.BindingProvider. Is that an extra step in generating the stub with Axis2? Thanks. – Gary Tsui Feb 06 '14 at 04:20