如何在SOAPEnvelope中添加名称空间声明

如何在SOAPEnvelope中添加名称空间声明

本文介绍了如何在SOAPEnvelope中添加名称空间声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的soap应用程序中,我使用的是apache cxf。

In my soap application, I'm using apache cxf.

这是我的代码,它将数据提交到服务器。

Here's my code which will submit data to the server.

import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.bus.spring.SpringBusFactory;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;

SpringBusFactory bf = new SpringBusFactory();
URL              busFile = Submission.class.getResource(WSSEC_XML);
File             f = new File(busFile.getPath());

Bus              bus = bf.createBus(busFile.toString());
BusFactory.setDefaultBus(bus);
BusFactory.setThreadDefaultBus(bus);

DefaultCryptoCoverageChecker coverageChecker = new DefaultCryptoCoverageChecker();

coverageChecker.setSignBody(true);
coverageChecker.setSignTimestamp(true);
coverageChecker.setEncryptBody(true);
coverageChecker.setSignAddressingHeaders(true);

MyClaimservice  service = new MyClaimservice();
Myclaims        port = service.getMyClaimsSoap11();
BindingProvider provider = (BindingProvider) port;
provider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, ENDPOINT_ADDRESS);
provider.getRequestContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, SOAP_ACTION);

Client client = ClientProxy.getClient(port);

client.getOutInterceptors().add(new LoggingOutInterceptor());
client.getInInterceptors().add(new LoggingInInterceptor());
client.getInInterceptors().add(new WSS4JInInterceptor(getInProps()));
client.getOutInterceptors().add(new WSS4JOutInterceptor(getOutProps()));
client.getInInterceptors().add(coverageChecker);

SubmissionResponse response = port.submission(request);

我的请求数据是这样形成的。

My request data is formed like this.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    ....
    ....
    ....
</soap:Envelope>

我需要在< soap:Envelope> 。我的预期数据应该是这样的,

I need to add additional namespace to <soap:Envelope>. My expected data should be like this,

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prefix1="url1" xmlns:prefix2="url2" xmlns:prefix3:"url3">
    ....
    ....
    ....
</soap:Envelope>

我尝试将这些前缀和名称空间url添加到 DefaultCryptoCoverageChecker 使用Map和XpathExpressions。但是,似乎它并未对我的请求数据进行任何更改。

I tried adding those prefixes and namespace urls to DefaultCryptoCoverageChecker using Map and XpathExpressions. But seems like it does not made any changes to my request data.

Map<String, String> prefixes = new HashMap<String, String>();

prefixes.put("prefix1", "url1");
prefixes.put("prefix2", "url2");
prefixes.put("prefix3", "url3");
prefixes.put("prefix4", "url4");
prefixes.put("prefix5", "url5");

List<XPathExpression> xpaths = Arrays.asList(
        new XPathExpression("//prefix1:Envelope", CoverageType.SIGNED),
        new XPathExpression("//prefix2:Envelope", CoverageType.SIGNED),
        new XPathExpression("//prefix3:Envelope", CoverageType.SIGNED),
        new XPathExpression("//prefix4:Envelope", CoverageType.SIGNED),
        new XPathExpression("//prefix5:Envelope", CoverageType.SIGNED));

DefaultCryptoCoverageChecker coverageChecker = new DefaultCryptoCoverageChecker();

coverageChecker.addPrefixes(prefixes);
coverageChecker.addXPaths(xpaths);

coverageChecker.setSignBody(true);
coverageChecker.setSignTimestamp(true);
coverageChecker.setEncryptBody(true);
coverageChecker.setSignAddressingHeaders(true);

任何帮助将不胜感激。

更新2:

我已经尝试了Apache cxf的转换功能。

I've tried Apache cxf's transformation feature.

Map<String, String> outTransformMap = Collections.singletonMap("{http://schemas.xmlsoap.org/soap/envelope/}Envelope", "Envelope");
TransformOutInterceptor transformOutInterceptor = new TransformOutInterceptor();
transformOutInterceptor.setOutTransformElements(outTransformMap);
client.getOutInterceptors().add(transformOutInterceptor);

根据,以上代码将从<$中删除该特定名称空间c $ c>信封节点。而且我也可以通过查看 xmlns:soap = http://schemas.xmlsoap.org/soap/envelope/ 从< soap:Envelope>

According to http://cxf.apache.org/docs/transformationfeature.html#TransformationFeature-JAX-WS, above code will remove that specific namespace from Envelope node. And I can also confirm it by seeing the xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/ is being removed from <soap:Envelope>

我尝试了 transformOutInterceptor.setOutTransformElements(outTransformMap); ,将其替换为 transformOutInterceptor.setOutAppendElements(outTransformMap); ,其中 outTransformMap 现在将包含 Collections.singletonMap( {url1(我需要添加)} Envelope, Envelope); 。但是它没有将它作为新的命名空间附加到信封。我在这里做错什么..除了默认值以外,真的很难向< soap:Envelope> 添加其他名称空间吗?

I tried the reverse of transformOutInterceptor.setOutTransformElements(outTransformMap); by replacing it with transformOutInterceptor.setOutAppendElements(outTransformMap);, where the outTransformMap will now contains Collections.singletonMap("{url1 (which I need to add)}Envelope", "Envelope");. But it's not appending it as a new Namespace to Envelope. What am I doing wrong here.. Is it really difficult to add additional namespace to <soap:Envelope> other than the default namesapce.

推荐答案

在cxf论坛上进行挖掘后,我找到了答案。

After digging on cxf forums, I found the answer.

Map<String, String> nsMap = new HashMap<>();

nsMap.put("prefix1", "url1");
nsMap.put("prefix2", "url2");
nsMap.put("prefix3", "url3");
nsMap.put("prefix4", "url4");
nsMap.put("prefix5", "http://www.w3.org/2001/04/xmlenc#");

Client client = ClientProxy.getClient(port);
client.getRequestContext().put("soap.env.ns.map", nsMap);

如果有人知道如何为<$ c $做同样的事情,我将不胜感激。 c>< soap:Header>

I would be appreciated if anyone know on how to do the same for <soap:Header>

这篇关于如何在SOAPEnvelope中添加名称空间声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:13