title: Access SOAP message header in the requested method
body: <p>EDIT : Message cleared and code added</p>
  
  <p>I develop a jax-ws based web service with a basic Java client.</p>
  
  <p>I use SOAP handlers to authenticate the user. One on the client side which add the userId and the token to the SOAP header and another one on the server side which get these informations and authenticate the user with the database.</p>
  
  <p>Client side (simplified) :</p>
  
  <pre><code>import static modules.auth.AuthClient.userID;
  import static modules.auth.AuthClient.token;
  
  public boolean handleMessage(SOAPMessageContext context) {
  
      //Getting SOAP headers
      SOAPMessage soapMsg = context.getMessage();
      SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
      SOAPHeader soapHeader = soapEnv.getHeader();
  
      QName qname;
      SOAPHeaderElement soapHeaderElement;
  
      //Add userID in SOAP header
      qname = new QName("****","UserID");
      soapHeaderElement = soapHeader.addHeaderElement(qname);
      soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
      soapHeaderElement.addTextNode(userID);
  
      //Add token in SOAP header
      qname = new QName("****","Token");
      soapHeaderElement = soapHeader.addHeaderElement(qname);
      soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
      soapHeaderElement.addTextNode(token);
  
      soapMsg.saveChanges();
  
      return true;
  }
  </code></pre>
  
  <p>Server side (simplified) :</p>
  
  <pre><code>public boolean handleMessage(SOAPMessageContext context) {  
      Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
  
      if (!isRequest) {
          //Getting SOAP headers
          SOAPMessage soapMsg = context.getMessage();
          SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
          SOAPHeader soapHeader = soapEnv.getHeader();
  
          Iterator it = soapHeader.extractHeaderElements(SOAPConstants.URI_SOAP_ACTOR_NEXT);
  
          Node userIDNode = (Node) it.next();
          String userID = (userIDNode == null) ? null : userIDNode.getValue();
  
          Node tokenNode = (Node) it.next();
          String token = (tokenNode == null) ? null : tokenNode.getValue();
  
          //Return if the user is connected
          User u = AuthWS.validToken(userID, token);
  
          //Here I have my user but I don't know how to get it in my requested method
      }
      return true;
  }
  </code></pre>
  
  <p>In order to manage users rights I would like to access my user directly in the method requested.</p>
  
  <p>Example of method that can be requested :</p>
  
  <pre><code>public Project getProject(@WebParam(name = "name") String name) throws WebServiceFailure, EntityNotFoundException {
  
      //Here I would like to verify the user's rights
  
      try {
          PreparedStatement ps = DBConnect.getStatement("SELECT name FROM projects WHERE name = '" + name + "'");
          ResultSet res = ps.executeQuery();
          if(res.next()){
              Project p = new Project(res.getString("name"));
              ps.close();
              return p;
          } else {
              ps.close();
              throw new EntityNotFoundException("Can't find the project '"+name+"'");
          }
      } catch (SQLException ex) {
          throw new WebServiceFailure(ex.getMessage());
      }
  }
  </code></pre>
  
  <p>Many thanks</p>