title: Accessing a SOAP Header from JibX WS
body: <p>I have a tomcat web service that accepts opentravel.org OTA XML requests and responds accordingly.  It uses the JibX OTA classes.</p>
  
  <p>So far the users of the service have used POX, and it works really well, but a new user wants to use SOAP and add security credentials to the SOAP Header like this (instead of putting them the POS xml fragment)...</p>
  
  <pre><code>&lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;soap:Header&gt;
      &lt;wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org        /wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"&gt;
        &lt;wsse:UsernameToken&gt;
          &lt;wsse:Username&gt;USERNAME&lt;/wsse:Username&gt;
          &lt;wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-    wss-username-token-profile-1.0#PasswordText"&gt;SECRET&lt;/wsse:Password&gt;
        &lt;/wsse:UsernameToken&gt;
      &lt;/wsse:Security&gt;
    &lt;/soap:Header&gt;
  </code></pre>
  
  <p>
      
      </p>
  
  <p>So to authenticate the request I think I need to access the headers from within the service implementation class.</p>
  
  <p>I checked out the SOAP Headers example, which I think tells me that I can access the headers by also including a inContext, e.g.</p>
  
  <pre><code>public RoomListRS list(RoomListRQ roomListRQ, InContext inCtx){
  ....
  }
  </code></pre>
  
  <p>so within this method I can do this...</p>
  
  <pre><code>Security security = (Security ) inCtx.getAttribute("security");
  </code></pre>
  
  <p>so I can access the username token within, </p>
  
  <p>...having specified this in the service...</p>
  
  <pre><code>&lt;service name="OTAService"&gt;
    &lt;service-class&gt;com.xx.webservice.ota.HotelServiceImpl&lt;/service-class&gt;
    &lt;operation method="list"/&gt;
    &lt;handler-class class="org.jibx.ws.io.handler.ContextAttributeUnmarshallingInHandler"&gt;
      &lt;constructor-arg value="com.xx.shared.soap.security.Security"/&gt;
      &lt;constructor-arg value="security"/&gt;
    &lt;/handler-class&gt;
  &lt;/service&gt;
  </code></pre>
  
  <p>Have I got that right?</p>
  
  <p>So I created the Security class, but left out all the namespace stuff to start with just to get going and prove that I can access something in the header.  Based on having a fragment like this...</p>
  
  <pre><code>    &lt;Security&gt;
        &lt;UsernameToken&gt;
          &lt;Username&gt;USERNAME&lt;/Username&gt;
          &lt;Password&gt;SECRET&lt;/Password&gt;
        &lt;/UsernameToken&gt;
      &lt;/Security&gt;
  </code></pre>
  
  <p>So I created the binding with bindgen, then compiled, then called it with soapUI</p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.opentravel.org/OTA/2003/05"&gt;
       &lt;soapenv:Header&gt;
       &lt;Security&gt;
        &lt;UsernameToken&gt;
          &lt;Username&gt;USERNAME&lt;/Username&gt;
          &lt;Password&gt;SECRET&lt;/Password&gt;
        &lt;/UsernameToken&gt;
      &lt;/Security&gt;
    &lt;/soapenv:Header&gt;
     &lt;soapenv:Body&gt;
  &lt;OTA_HotelRoomListRQ xmlns="http://www.opentravel.org/OTA/2003/05" Version="2.0"&gt;
  ....
  &lt;/OTA_HotelRoomListRQ&gt;
  &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>but when I try to get the Security object from the context it is null.</p>
  
  <p>Have I got the wrong end of the stick?</p>
  
  <p>Should I just create another service with a different endpoint using something more SOAPY?</p>
  
  <p>Is what I am trying to do not possible with JibX WS and the inHandler?</p>
  
  <p>Any comments most welcome.</p>
  
  <hr>
  
  <p>thank you so much for taking the trouble to answer my question. </p>
  
  <p>I am trying to go through what you have added.  I used your customisation and the xsd to create the java source and binding.xml.  </p>
  
  <p>I have compiled the classes and I am now trying to bind them, but I am getting this error:    </p>
  
  <pre><code>C:\Java\wsse&gt;java org.jibx.binding.generator.BindGen org.oasisopen.docs.wss.oasis200401wsswssecuritysecext1.SecurityHeaderType
  Exception in thread "main" java.lang.IllegalStateException: No way to handle type java.lang.Object, referenced from org.oasisopen.docs.wss.oasis200401wsswssecuritysecext1.SecurityHeaderType
      at org.jibx.binding.generator.BindGen.expandReferences(BindGen.java:227)
      at org.jibx.binding.generator.BindGen.findReferences(BindGen.java:1010)
      at org.jibx.binding.generator.BindGen.generate(BindGen.java:1124)
      at org.jibx.binding.generator.BindGen.main(BindGen.java:1302)
  </code></pre>
  
  <p>I am going to take a look at bindgen customisations to see if that sheds any light, as that's the only clue given in response to this issue.  Could you tell me how you got around this?</p>
  
  <p>Thanks again.</p>