Java 将自定义 HTTP 标头添加到 Axis 1.4 Web 服务响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18787424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:03:27  来源:igfitidea点击:

Adding custom HTTP headers to Axis 1.4 web service responses

javaweb-servicesaxisjax-rpc

提问by mip

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

我正在尝试将自定义 HTTP 标头添加到 Axis 1.4 Web 服务器。

I've created a handler which extends BasicHandler:

我创建了一个扩展 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:

我已将以下内容添加到 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.

我可以看到 invoke 方法正在被调用,因为日志记录出现在日志文件中,但自定义标头未添加到响应中。

Any suggestions appreciated.

任何建议表示赞赏。

回答by Raul Lapeira Herrero

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

我记得使用生成的存根文件添加 HTTP 用户和密码,检查此链接并找到以下代码:

_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

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

That kind of modification works.

这种修改有效。

回答by Khush

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.

希望这可以帮助。

回答by romeara

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

org.apache.axis.Stub通过执行以下操作,我能够在实例上执行此操作:

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)

请注意,_setProperty() 的 value 参数必须为 a java.util.Hashtable(使用 Stub 时,Axis 稍后会对其进行强制转换)