通过处理程序使用 JAX-RPC 或 XSLT-ing 响应进行类型化

发布于 2024-12-13 11:44:56 字数 761 浏览 3 评论 0原文

所以我们已经有了一个可爱的老式 PHP WS,我们必须通过 JAX-RPC 集成,这个 WS 提供一些响应,如下所示:

 <return SOAP-ENC:arrayType="SOAP-ENC:Array[1]" xsi:type="SOAP-ENC:Array">
    <item SOAP-ENC:arrayType="xsd:ur-type[3]" xsi:type="SOAP-ENC:Array">
       <item xsi:type="xsd:string">...</item>
       <item xsi:type="xsd:float">...</item>
       <item xsi:type="xsd:int">...</item>
    </item>
 </return>

现在的问题是,JAX-RPC 似乎不知道 ur-type code>

我们考虑使用 Handler 来搜索并替换响应的 XML 或通过 XSLT 转换它,以便 ur-type > => anyType 但我们似乎不知道如何做到这一点。具体来说,我们可以使用什么 OutputStream / StreamResult 进行转换?

任何其他建议都非常受欢迎:)

So we've got ourselves a lovely vintage PHP WS we must integrate via JAX-RPC and this WS delivers some responses as follows:

 <return SOAP-ENC:arrayType="SOAP-ENC:Array[1]" xsi:type="SOAP-ENC:Array">
    <item SOAP-ENC:arrayType="xsd:ur-type[3]" xsi:type="SOAP-ENC:Array">
       <item xsi:type="xsd:string">...</item>
       <item xsi:type="xsd:float">...</item>
       <item xsi:type="xsd:int">...</item>
    </item>
 </return>

Now the problem is, JAX-RPC doesn't seem to know about ur-type

We thought about using a Handler for doing a search and replace on the the responses' XML or transforming it via XSLT so that ur-type => anyType but we can't seem to figure it out how to do it. Specifically what OutputStream / StreamResult could we use for the transformation?

Any other suggestion is more than welcomed :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も让我眼熟你 2024-12-20 11:44:56

因此,如果最终我们放弃了使用 XSLT 或搜索并替换原始 XML 的想法。

我们使用 SOAPBody 来操作响应的 DOM 并将 ur-type 替换为 anyType ,一切顺利很好。尽管如此,如果您有更好的想法,请发布。

编辑:这就是我们基本上是如何做到的。

这在您获取端口的类中,您需要注册进行搜索和替换的处理程序:

private TestSOAPPort getPort() {
    TestSOAPService service = new TestSOAPServiceImpl();
    try {
        registerResponseHandler(service);
        TestSOAPPort port = service.getTestSOAPPort();
        ((TestSOAPPortStub) port)._setProperty(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, TestConstants.ENDPOINT_URL);
        return port;
    } catch (Exception e) {
        // ...
    }

    return null;
}

private void registerResponseHandler(TestSOAPService service) {
    HandlerRegistry hr = service.getHandlerRegistry();
    QName portName = new QName(TestConstants.NAMESPACE_URI, TestConstants.PORT_NAME);
    List handlerChain = hr.getHandlerChain(portName);
    HandlerInfo hi = new HandlerInfo();
    hi.setHandlerClass(TestResponseHandler.class);
    handlerChain.add(hi);
}

处理程序如下所示:

import javax.xml.rpc.handler.GenericHandler;

public class TestResponseHandler extends GenericHandler {
    private static final String XSD_UR_TYPE = "xsd:ur-type";
    private static final String XSD_ANY_TYPE = "xsd:anyType";
    private static final String XSD_INT_ARRAY = "xsd:int[";
    private static final String XSD_ANY_TYPE_ARRAY = "xsd:anyType[";

    @Override
    public boolean handleResponse(MessageContext context) {
        SOAPMessageContext smc = (SOAPMessageContext) context;
        SOAPMessage sm = smc.getMessage();
        try {
            SOAPBody sb = sm.getSOAPBody();
            handleNodes(sb.getElementsByTagName("problemTag"));
            sm.saveChanges();
        } catch (Exception e) {
            // ...
        }

        return super.handleResponse(context);
    }

    private void handleNodes(NodeList nodes) {
        // do your search and replace here
        if (nodes == null) {
            return;
        }
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            NamedNodeMap attributes = node.getAttributes();
            for (int j = 0; j < attributes.getLength(); j++) {
                Node attribute = attributes.item(j);
                if (attribute.getNodeValue().startsWith(XSD_UR_TYPE)) {
                    attribute.setNodeValue(attribute.getNodeValue().replace(XSD_UR_TYPE, XSD_ANY_TYPE));
                } else if (attribute.getNodeValue().startsWith(XSD_INT_ARRAY)) {
                    attribute.setNodeValue(attribute.getNodeValue().replace(XSD_INT_ARRAY, XSD_ANY_TYPE_ARRAY));
                }
            }
        }
    }

}

So if the end we gave up with our idea of using a XSLT or doing a search and replace on the raw XML.

We're using SOAPBody to manipulate the responses' DOM and replace ur-type with anyType and everything works out just fine. Nonetheless, if you have any better idea, please do post it.

Edit: here's how we basically did it.

This goes in the class where you get the port, you need to register the handler where you do your search and replace:

private TestSOAPPort getPort() {
    TestSOAPService service = new TestSOAPServiceImpl();
    try {
        registerResponseHandler(service);
        TestSOAPPort port = service.getTestSOAPPort();
        ((TestSOAPPortStub) port)._setProperty(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, TestConstants.ENDPOINT_URL);
        return port;
    } catch (Exception e) {
        // ...
    }

    return null;
}

private void registerResponseHandler(TestSOAPService service) {
    HandlerRegistry hr = service.getHandlerRegistry();
    QName portName = new QName(TestConstants.NAMESPACE_URI, TestConstants.PORT_NAME);
    List handlerChain = hr.getHandlerChain(portName);
    HandlerInfo hi = new HandlerInfo();
    hi.setHandlerClass(TestResponseHandler.class);
    handlerChain.add(hi);
}

And this is how the handler looks like:

import javax.xml.rpc.handler.GenericHandler;

public class TestResponseHandler extends GenericHandler {
    private static final String XSD_UR_TYPE = "xsd:ur-type";
    private static final String XSD_ANY_TYPE = "xsd:anyType";
    private static final String XSD_INT_ARRAY = "xsd:int[";
    private static final String XSD_ANY_TYPE_ARRAY = "xsd:anyType[";

    @Override
    public boolean handleResponse(MessageContext context) {
        SOAPMessageContext smc = (SOAPMessageContext) context;
        SOAPMessage sm = smc.getMessage();
        try {
            SOAPBody sb = sm.getSOAPBody();
            handleNodes(sb.getElementsByTagName("problemTag"));
            sm.saveChanges();
        } catch (Exception e) {
            // ...
        }

        return super.handleResponse(context);
    }

    private void handleNodes(NodeList nodes) {
        // do your search and replace here
        if (nodes == null) {
            return;
        }
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            NamedNodeMap attributes = node.getAttributes();
            for (int j = 0; j < attributes.getLength(); j++) {
                Node attribute = attributes.item(j);
                if (attribute.getNodeValue().startsWith(XSD_UR_TYPE)) {
                    attribute.setNodeValue(attribute.getNodeValue().replace(XSD_UR_TYPE, XSD_ANY_TYPE));
                } else if (attribute.getNodeValue().startsWith(XSD_INT_ARRAY)) {
                    attribute.setNodeValue(attribute.getNodeValue().replace(XSD_INT_ARRAY, XSD_ANY_TYPE_ARRAY));
                }
            }
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文