【问题标题】:Axis 2 and rampart issue while adding cutom header to the request向请求添加自定义标头时出现轴 2 和壁垒问题
【发布时间】:2013-02-13 11:47:13
【问题描述】:
<soapenv:Envelope> 
<soapenv:Header>    
      <cor:india>test</cor:india>
     </soapenv:Header>

<soapenv:Body>
.
.
</soapenv:Body>
</soapenv:Envelope>

     OMFactory omFactory =OMAbstractFactory.getOMFactory();
    OMNamespace omNamespace = omFactory.createOMNamespace("http://example.com/...", "cor");
    OMElement header = omFactory.createOMElement("india", omNamespace);
    header.setText("test");
    stub._getServiceClient().addHeader(header);

我想将自定义标头添加到使用轴 2 和壁垒的肥皂请求中。 但下面是我得到的例外

Caused by: org.apache.rampart.RampartException: Error in extracting message properties
    at org.apache.rampart.RampartMessageData.<init>(RampartMessageData.java:379)
    at org.apache.rampart.MessageBuilder.build(MessageBuilder.java:61)
    at org.apache.rampart.handler.RampartSender.invoke(RampartSender.java:65)
    ... 10 more
Caused by: org.apache.ws.security.WSSecurityException: Error in converting SOAP Envelope to Document; nested exception is: 
    java.lang.ClassCastException: org.apache.axiom.om.impl.llom.OMElementImpl cannot be cast to org.apache.axiom.soap.SOAPHeaderBlock
    at org.apache.rampart.util.Axis2Util.getDocumentFromSOAPEnvelope(Axis2Util.java:191)
    at org.apache.rampart.RampartMessageData.<init>(RampartMessageData.java:270)
    ... 12 more
Caused by: java.lang.ClassCastException: org.apache.axiom.om.impl.llom.OMElementImpl cannot be cast to org.apache.axiom.soap.SOAPHeaderBlock
    at org.apache.rampart.util.Axis2Util.getDocumentFromSOAPEnvelope(Axis2Util.java:141)
    ... 13 more

【问题讨论】:

  • 我遇到了同样的错误。但我没有发现错误请给我示例应用程序
  • @sureshmanda,您是否正在通过轴 2 在soap 请求中添加额外的标头信息,例如:“test”?
  • 是的,但我不喜欢这种。
  • 您必须添加名称空间并将内容添加到答案部分中显示的标题中。这是它的代码。 OMNamespace ns = factory.createOMNamespace("google.com", "cor"); hdr.addHeaderBlock("印度", ns).setText("这里的值");

标签: java web-services axis2 axis rampart


【解决方案1】:

如果您尝试使用此处提供的提示添加标题 Example Custom Headers 并且您使用壁垒参与的位置,那么您将面临上述问题。

解决方案是在 META-INF 中添加您自己的 module.xml,并在添加自定义标头的位置添加输出流

<module name="test" class="exampleClass">

    <OutFlow>

            <handler name="handle" class="exampleClass">
                <order phase="Security"  />
            </handler>
        </OutFlow>
</module>

================================================ ====================================

public class exampleClass AbstractHandler implements org.apache.axis2.modules.Module {

public InvocationResponse invoke(MessageContext ctx) throws AxisFault {

SOAPEnvelope env = ctx.getEnvelope();
SOAPHeader hdr = env.getHeader();

SOAPFactory factory = (SOAPFactory) env.getOMFactory();

OMNamespace ns = factory.createOMNamespace("http://google.com", "cor");

//SOAPHeader head = factory.createSOAPHeader(env);

hdr.addHeaderBlock("india", ns).setText("value here");

return InvocationResponse.CONTINUE;
}


public void applyPolicy(Policy arg0, AxisDescription arg1) throws AxisFault {
    // TODO Auto-generated method stub

}

public boolean canSupportAssertion(Assertion arg0) {
    // TODO Auto-generated method stub
    return false;
}

public void engageNotify(AxisDescription arg0) throws AxisFault {
    // TODO Auto-generated method stub

}

public void init(ConfigurationContext arg0, AxisModule arg1) throws AxisFault {
    // TODO Auto-generated method stub

}

public void shutdown(ConfigurationContext arg0) throws AxisFault {
    // TODO Auto-generated method stub

} }

================================================ ===================================

在客户端加入你的模块之前,像壁垒一样

Options options = serviceClient.getOptions();
serviceClient.engageModule("test");

options.setProperty(RampartMessageData.KEY_RAMPART_POLICY,loadPolicy("policy.xml"));
serviceClient.engageModule("rampart");
..

..

【讨论】:

    猜你喜欢
    • 2012-11-15
    • 2015-10-02
    • 2017-11-06
    • 2013-11-13
    • 2021-08-25
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多