7
votes
java - Adding custom HTTP headers to Axis 1.4 web service responses - Stack Overflow
Asked
Active 11 months ago
Viewed 15k times
7

I'm trying to add custom HTTP headers to Axis 1.4 web servers.

I've created a handler which extends BasicHandler:

public class HttpHeaderHandler extends BasicHandler {

  .
  .
  .

  @Override
  public void invoke(org.apache.axis.MessageContext arg0) throws AxisFault {  
    LOG.trace("invoke called");     
    Hashtable ht = (Hashtable)ctx.getProperty(HTTPConstants.RESPONSE_HEADERS);
    if(ht == null) {
      ht = new Hashtable();
    }
    ht.put("custom-header", "Hello");
    ctx.setProperty(HTTPConstants.RESPONSE_HEADERS, ht);     
  }

  .
  .
  .

}

I've added the following to server-config.wsdd:

    .
    .
    .

<transport name="http">
    <requestFlow>
        <handler type="URLMapper" />
        <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
    </requestFlow>
    <responseFlow>
        <handler type="java:com.my.package.HttpHeaderHandler" />
    </responseFlow>
</transport>

    .
    .
    .

I can see that the invoke method is being called as the logging is appearing in the log file but the custom header is not being added to the response.

Any suggestions appreciated.

2
6

I was able to do this on a org.apache.axis.Stub instance by doing the following:

private Stub setHeaders(Stub stub, Hashtable<String, String> headers){
    stub._setProperty(HTTPConstants.REQUEST_HEADERS, headers);
    return stub;
}

Note that it is REQUIRED that the value argument to _setProperty() be a java.util.Hashtable (it gets cast later on by Axis when the Stub is used)

    2

    I added apikey for request header thanks for @romeara answer here . And it works. Axis 1.4 sending client request from java.

    YourStub stub = new YourStub();
    Hashtable<String, String> headers = new Hashtable<String, String>();
    headers.put("apikey", "xxxxxxxxxxxxxxxxxxxx");
    stub._setProperty(HTTPConstants.REQUEST_HEADERS, headers);
    
      1

      I remember using the stub files generated to add HTTP user and password, check this link and locate the code that says:

      _call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE);  
      

      http://www.coderanch.com/t/225102/Web-Services/java/Axis-username-password-auth-stubs

      That kind of modification works.

        -1

        This is what we have done

        import javax.xml.soap.SOAPElement;
        import javax.xml.soap.SOAPException;
        
         /**
         * This method is to be used for secure SOAP calls.
         * Method created as Axis 1.4 strips the security header which compiling the Java classes.
         * @param username
         * @param password
         * @return SOAP Header
         * @throws SOAPException
         */
        public static SOAPHeaderElement createCustomSOAPHeader(String username, String password) throws SOAPException {
            SOAPHeaderElement oHeaderElement;
            SOAPElement oElement;   
        
            //Header
            oHeaderElement = new SOAPHeaderElement("http://siebel.com/webservices", "Security");
            oHeaderElement.setPrefix("web");
            oHeaderElement.setMustUnderstand(false);
            //Elements for the Header
            oElement = oHeaderElement.addChildElement("UsernameToken");
            oElement.addTextNode(username);
            oElement = oHeaderElement.addChildElement("PasswordText");
            oElement.addTextNode(password);
            oElement = oHeaderElement.addChildElement("SessionType");
            oElement.addTextNode("None");
        
            return oHeaderElement;
        }
        

        Hope this helps.

        3
        • 2
          This is not HTTP Header, this is SOAP Header.
          – MariuszS
          Oct 1 2013 at 8:48
        • @MariuszS Isn't Axis 1.4 used for SOAP??? Can it be used for HTPP as well? Thats news to me.
          – Khush
          Oct 2 2013 at 0:28
        • Yes, Axis can be used for SOAP over HTTP Transport, and some real life scenerios requires adding http header. Look at this answer -> stackoverflow.com/questions/3925272/…
          – MariuszS
          Oct 3 2013 at 12:19

        Your Answer

        By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

        Not the answer you're looking for? Browse other questions tagged or ask your own question.

         
        4
        Found the solution here in the end stackoverflow.com/questions/3925272/….mip
        I have provided you with an answer.Khush

        4 Answers

        6
        votes

        I was able to do this on a org.apache.axis.Stub instance by doing the following:

        private Stub setHeaders(Stub stub, Hashtable<String, String> headers){
            stub._setProperty(HTTPConstants.REQUEST_HEADERS, headers);
            return stub;
        }
        

        Note that it is REQUIRED that the value argument to _setProperty() be a java.util.Hashtable (it gets cast later on by Axis when the Stub is used)

        2
        votes

        I added apikey for request header thanks for @romeara answer here . And it works. Axis 1.4 sending client request from java.

        YourStub stub = new YourStub();
        Hashtable<String, String> headers = new Hashtable<String, String>();
        headers.put("apikey", "xxxxxxxxxxxxxxxxxxxx");
        stub._setProperty(HTTPConstants.REQUEST_HEADERS, headers);
        
        1
        votes

        I remember using the stub files generated to add HTTP user and password, check this link and locate the code that says:

        _call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE);  
        

        http://www.coderanch.com/t/225102/Web-Services/java/Axis-username-password-auth-stubs

        That kind of modification works.

        -1
        votes

        This is what we have done

        import javax.xml.soap.SOAPElement;
        import javax.xml.soap.SOAPException;
        
         /**
         * This method is to be used for secure SOAP calls.
         * Method created as Axis 1.4 strips the security header which compiling the Java classes.
         * @param username
         * @param password
         * @return SOAP Header
         * @throws SOAPException
         */
        public static SOAPHeaderElement createCustomSOAPHeader(String username, String password) throws SOAPException {
            SOAPHeaderElement oHeaderElement;
            SOAPElement oElement;   
        
            //Header
            oHeaderElement = new SOAPHeaderElement("http://siebel.com/webservices", "Security");
            oHeaderElement.setPrefix("web");
            oHeaderElement.setMustUnderstand(false);
            //Elements for the Header
            oElement = oHeaderElement.addChildElement("UsernameToken");
            oElement.addTextNode(username);
            oElement = oHeaderElement.addChildElement("PasswordText");
            oElement.addTextNode(password);
            oElement = oHeaderElement.addChildElement("SessionType");
            oElement.addTextNode("None");
        
            return oHeaderElement;
        }
        

        Hope this helps.