how add Message context headers to apache axis 2 Java

13,192

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");   
    
Share:
13,192
Patan
Author by

Patan

Updated on June 04, 2022

Comments

  • Patan
    Patan almost 2 years

    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
    Patan over 11 years
    Thanks for the solution. It would be great if you can tell me the way to access it in service class also.
  • kolossus
    kolossus over 11 years
    @User222, look at this answer 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
  • Patan
    Patan over 11 years
    I am getting exception as stub cannot be type casted to BindingPort. I have edited the question. Please check.
  • Patan
    Patan over 11 years
    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.
  • kolossus
    kolossus over 11 years
    @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?
  • Patan
    Patan over 11 years
    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.
  • kolossus
    kolossus over 11 years
    @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
  • Gary Tsui
    Gary Tsui about 10 years
    @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.