如何在Axis2中添加SOAP头
1个回答
展开全部
MathStub.java类中的add方法有下面这一段代码:
[java] view plaincopy
......
//其它构造函数最终都是调用这个函数创建MathStub对象
public MathStub(org.apache.axis2.context.ConfigurationContext configurationContext,
java.lang.String targetEndpoint, boolean useSeparateListener)
throws org.apache.axis2.AxisFault {
populateAxisService();
populateFaults();
_serviceClient = new org.apache.axis2.client.ServiceClient(
configurationContext, _service);
_serviceClient.getOptions().setTo(
new org.apache.axis2.addressing.EndpointReference(targetEndpoint));
_serviceClient.getOptions().setUseSeparateListener(useSeparateListener);
_serviceClient.getOptions().setSoapVersionURI(
org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
}
......
public com.sean.AddResponse add(com.sean.Add add0)
throws java.rmi.RemoteException{
......
// adding SOAP soap_headers
_serviceClient.addHeadersToEnvelope(env);
......
}
......
看了下ServiceClient的源码:
[java] view plaincopy
......
private ArrayList<OMElement> headers;
......
public void addHeader(OMElement header) {
if (headers == null) {
headers = new ArrayList<OMElement>();
}
headers.add(header);
}
/**
* Add SOAP Header to be sent with outgoing messages.
*
* @param header header to be sent (non-<code>null</code>)
*/
public void addHeader(SOAPHeaderBlock header) {
if (headers == null) {
headers = new ArrayList<OMElement>();
}
headers.add(header);
}
/** Remove all headers for outgoing message. */
public void removeHeaders() {
if (headers != null) {
headers.clear();
}
}
public void addStringHeader(QName headerName, String headerText) throws AxisFault {
if (headerName.getNamespaceURI() == null ||
"".equals(headerName.getNamespaceURI())) {
throw new AxisFault("Failed to add string header,"
+ " you have to have namespaceURI for the QName");
}
OMElement omElement = OMAbstractFactory.getOMFactory()
.createOMElement(headerName, null);
omElement.setText(headerText);
addHeader(omElement);
}
/**
* Add all configured headers to a SOAP envelope.
*
* @param envelope the SOAPEnvelope in which to write the headers
*/
public void addHeadersToEnvelope(SOAPEnvelope envelope) {
if (headers != null) {
SOAPHeader soapHeader = envelope.getHeader();
for (Object header : headers) {
soapHeader.addChild((OMElement)header);
}
}
}
......
修改方式已经很明确了,在MathStub类中创建私有方法,通过AXIOM(使用版本为:1.2.14)生成所需头对象,然后将该头对象添加至ServiceClient对象即可
[java] view plaincopy在CODE上查看代码片派生到我的代码片
......
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.client.ServiceClient;
......
public MathStub(org.apache.axis2.context.ConfigurationContext configurationContext,
java.lang.String targetEndpoint, boolean useSeparateListener)
throws org.apache.axis2.AxisFault {
populateAxisService();
populateFaults();
_serviceClient = new org.apache.axis2.client.ServiceClient(
configurationContext, _service);
//添加SOAP头
this.addHeaders(_serviceClient);
_serviceClient.getOptions().setTo(
new org.apache.axis2.addressing.EndpointReference(targetEndpoint));
_serviceClient.getOptions().setUseSeparateListener(useSeparateListener);
_serviceClient.getOptions().setSoapVersionURI(
org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
}
......
private void addHeaders(org.apache.axis2.client.ServiceClient _serviceClient){
OMFactory omFactory = OMAbstractFactory.getOMFactory();
OMNamespace omNS=omFactory.createOMNamespace("","sean");
OMElement head = omFactory.createOMElement("Security", omNS);
OMElement token = omFactory.createOMElement("Token", omNS);
head.addChild(token);
OMElement userName = omFactory.createOMElement("Username", omNS);
userName.addChild(omFactory.createOMText(userName, "root"));
token.addChild(userName);
OMElement password = omFactory.createOMElement("Password", omNS);
password.addAttribute(omFactory.createOMAttribute("Type", null, "PasswordText"));
password.addChild(omFactory.createOMText(password, "123"));
token.addChild(password);
_serviceClient.addHeader(head);
}
......
添加SOAP头之前的请求格式如下:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="">
<soapenv:Body>
<ns1:add xmlns:ns1="">
<ns1:x>1</ns1:x>
<ns1:y>1</ns1:y>
</ns1:add>
</soapenv:Body>
</soapenv:Envelope>
添加SOAP头之后的请求格式如下:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="">
<soapenv:Header>
<sean:Security xmlns:sean="">
<sean:Token>
<sean:Username>root</sean:Username>
<sean:Password Type="PasswordText">123</sean:Password>
</sean:Token>
</sean:Security>
</soapenv:Header>
<soapenv:Body>
<ns1:add xmlns:ns1="">
<ns1:x>1</ns1:x>
<ns1:y>1</ns1:y>
</ns1:add>
</soapenv:Body>
</soapenv:Envelope>
[java] view plaincopy
......
//其它构造函数最终都是调用这个函数创建MathStub对象
public MathStub(org.apache.axis2.context.ConfigurationContext configurationContext,
java.lang.String targetEndpoint, boolean useSeparateListener)
throws org.apache.axis2.AxisFault {
populateAxisService();
populateFaults();
_serviceClient = new org.apache.axis2.client.ServiceClient(
configurationContext, _service);
_serviceClient.getOptions().setTo(
new org.apache.axis2.addressing.EndpointReference(targetEndpoint));
_serviceClient.getOptions().setUseSeparateListener(useSeparateListener);
_serviceClient.getOptions().setSoapVersionURI(
org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
}
......
public com.sean.AddResponse add(com.sean.Add add0)
throws java.rmi.RemoteException{
......
// adding SOAP soap_headers
_serviceClient.addHeadersToEnvelope(env);
......
}
......
看了下ServiceClient的源码:
[java] view plaincopy
......
private ArrayList<OMElement> headers;
......
public void addHeader(OMElement header) {
if (headers == null) {
headers = new ArrayList<OMElement>();
}
headers.add(header);
}
/**
* Add SOAP Header to be sent with outgoing messages.
*
* @param header header to be sent (non-<code>null</code>)
*/
public void addHeader(SOAPHeaderBlock header) {
if (headers == null) {
headers = new ArrayList<OMElement>();
}
headers.add(header);
}
/** Remove all headers for outgoing message. */
public void removeHeaders() {
if (headers != null) {
headers.clear();
}
}
public void addStringHeader(QName headerName, String headerText) throws AxisFault {
if (headerName.getNamespaceURI() == null ||
"".equals(headerName.getNamespaceURI())) {
throw new AxisFault("Failed to add string header,"
+ " you have to have namespaceURI for the QName");
}
OMElement omElement = OMAbstractFactory.getOMFactory()
.createOMElement(headerName, null);
omElement.setText(headerText);
addHeader(omElement);
}
/**
* Add all configured headers to a SOAP envelope.
*
* @param envelope the SOAPEnvelope in which to write the headers
*/
public void addHeadersToEnvelope(SOAPEnvelope envelope) {
if (headers != null) {
SOAPHeader soapHeader = envelope.getHeader();
for (Object header : headers) {
soapHeader.addChild((OMElement)header);
}
}
}
......
修改方式已经很明确了,在MathStub类中创建私有方法,通过AXIOM(使用版本为:1.2.14)生成所需头对象,然后将该头对象添加至ServiceClient对象即可
[java] view plaincopy在CODE上查看代码片派生到我的代码片
......
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.client.ServiceClient;
......
public MathStub(org.apache.axis2.context.ConfigurationContext configurationContext,
java.lang.String targetEndpoint, boolean useSeparateListener)
throws org.apache.axis2.AxisFault {
populateAxisService();
populateFaults();
_serviceClient = new org.apache.axis2.client.ServiceClient(
configurationContext, _service);
//添加SOAP头
this.addHeaders(_serviceClient);
_serviceClient.getOptions().setTo(
new org.apache.axis2.addressing.EndpointReference(targetEndpoint));
_serviceClient.getOptions().setUseSeparateListener(useSeparateListener);
_serviceClient.getOptions().setSoapVersionURI(
org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
}
......
private void addHeaders(org.apache.axis2.client.ServiceClient _serviceClient){
OMFactory omFactory = OMAbstractFactory.getOMFactory();
OMNamespace omNS=omFactory.createOMNamespace("","sean");
OMElement head = omFactory.createOMElement("Security", omNS);
OMElement token = omFactory.createOMElement("Token", omNS);
head.addChild(token);
OMElement userName = omFactory.createOMElement("Username", omNS);
userName.addChild(omFactory.createOMText(userName, "root"));
token.addChild(userName);
OMElement password = omFactory.createOMElement("Password", omNS);
password.addAttribute(omFactory.createOMAttribute("Type", null, "PasswordText"));
password.addChild(omFactory.createOMText(password, "123"));
token.addChild(password);
_serviceClient.addHeader(head);
}
......
添加SOAP头之前的请求格式如下:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="">
<soapenv:Body>
<ns1:add xmlns:ns1="">
<ns1:x>1</ns1:x>
<ns1:y>1</ns1:y>
</ns1:add>
</soapenv:Body>
</soapenv:Envelope>
添加SOAP头之后的请求格式如下:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="">
<soapenv:Header>
<sean:Security xmlns:sean="">
<sean:Token>
<sean:Username>root</sean:Username>
<sean:Password Type="PasswordText">123</sean:Password>
</sean:Token>
</sean:Security>
</soapenv:Header>
<soapenv:Body>
<ns1:add xmlns:ns1="">
<ns1:x>1</ns1:x>
<ns1:y>1</ns1:y>
</ns1:add>
</soapenv:Body>
</soapenv:Envelope>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询