使用SOAPHandler更改请求中的namespaceURI

时间:2013-12-30 10:27:53

标签: java soap jax-ws xml-namespaces

我尝试用SOAPHandler更改soap消息的正文(请求部分的namespaceURI),但这似乎不起作用。 更改的原因是,我必须与不同的客户端合作,并且有一个旧的无法更改,所以我想更改此客户端消息以使其与我的服务器一起使用。

这是我的代码:

@Override
    public boolean handleMessage(SOAPMessageContext context) {

        boolean isResponse = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        try {
            if (!isResponse) {
                SOAPMessage msg = context.getMessage();
                SOAPBody body = msg.getSOAPBody();
                Iterator<?> iter = body.getChildElements();
                boolean implChanged = false;
                while (iter.hasNext()) {
                    Object next = iter.next();
                    if (next instanceof SOAPElement) {
                        SOAPElement se = (SOAPElement) next;
                        String uri = se.getNamespaceURI();
                        if (uri.equalsIgnoreCase("http://service.plr.mycomp.com/BruttoNettoRechner/1.0")) {
                            if (!se.removeNamespaceDeclaration("impl")) {

                                System.out.println("FEHLER beim entfernen");
                            }
                            if (se.addNamespaceDeclaration("impl", "http://service.plr.mycomp.com") == null) {
                                System.out.println("Fehler beim hinzufügen");
                            }
                            System.out.println(se.getNamespaceURI());
                            System.out.println(se.lookupNamespaceURI("impl"));

                            implChanged = true;

                        }
                    }
                }
                if (msg.saveRequired()) {
                    msg.saveChanges();
                }
                msg.writeTo(System.out);

            }
        } catch (SOAPException e) {
            System.out.println("SOAP FEHLER");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

这是输入:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <impl:calculateBruttoNetto xmlns:impl="http://service.plr.mycomp.com/BruttoNettoRechner/1.0">
         <bruttoNettoIn>
            <ns1:aktBruttoEink xmlns:ns1="http://model.plr.mycomp.com">4000.0</ns1:aktBruttoEink>
            <ns1:aktNettoEink ns2:nil="true" xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://model.plr.mycomp.com"/>
            <ns1:alleinverdienerJN xmlns:ns1="http://model.plr.mycomp.com">false</ns1:alleinverdienerJN>
            <ns1:anzGehaelter xmlns:ns1="http://model.plr.mycomp.com">2</ns1:anzGehaelter>
            <ns1:berufsgruppe xmlns:ns1="http://model.plr.mycomp.com">2</ns1:berufsgruppe>
            <ns1:bruttoNetto xmlns:ns1="http://model.plr.mycomp.com">1</ns1:bruttoNetto>
         </bruttoNettoIn>
      </impl:calculateBruttoNetto>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

控制台输出说URI没有改变,但是当我要求整个消息时它似乎工作。但之后的警告显示没有任何改变:

11:08:52,801 INFO  [stdout] (http-/0.0.0.0:8080-1) http://service.plr.mycomp.com/BruttoNettoRechner/1.0

11:08:52,801 INFO  [stdout] (http-/0.0.0.0:8080-1) http://service.plr.mycomp.com/BruttoNettoRechner/1.0

11:08:52,803 INFO  [stdout] (http-/0.0.0.0:8080-1) <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:impl="http://service.plr.mycomp.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Header/><SOAP-ENV:Body>

11:08:52,803 INFO  [stdout] (http-/0.0.0.0:8080-1)       <impl:calculateBruttoNetto xmlns:impl="http://service.plr.mycomp.com">

11:08:52,803 INFO  [stdout] (http-/0.0.0.0:8080-1)          <bruttoNettoIn>

11:08:52,803 INFO  [stdout] (http-/0.0.0.0:8080-1)             <ns1:aktBruttoEink xmlns:ns1="http://model.plr.mycomp.com">4000.0</ns1:aktBruttoEink>

11:08:52,803 INFO  [stdout] (http-/0.0.0.0:8080-1)             <ns1:aktNettoEink xmlns:ns1="http://model.plr.mycomp.com" xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance" ns2:nil="true"/>

11:08:52,803 INFO  [stdout] (http-/0.0.0.0:8080-1)             <ns1:alleinverdienerJN xmlns:ns1="http://model.plr.mycomp.com">false</ns1:alleinverdienerJN>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)             <ns1:anzGehaelter xmlns:ns1="http://model.plr.mycomp.com">2</ns1:anzGehaelter>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)             <ns1:berufsgruppe xmlns:ns1="http://model.plr.mycomp.com">2</ns1:berufsgruppe>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)             <ns1:bruttoNetto xmlns:ns1="http://model.plr.mycomp.com">1</ns1:bruttoNetto>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)          </bruttoNettoIn>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)       </impl:calculateBruttoNetto>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)    </SOAP-ENV:Body>

11:08:52,804 INFO  [stdout] (http-/0.0.0.0:8080-1)    

11:08:52,805 WARNING [org.apache.cxf.phase.PhaseInterceptorChain] (http-/0.0.0.0:8080-1) Interceptor for {http://service.plr.mycomp.com/}BruttoNettoRechnerService#{http://service.plr.mycomp.com}calculateBruttoNetto has thrown exception, unwinding now: org.apache.cxf.interceptor.Fault: Unexpected wrapper element {http://service.plr.mycomp.com/BruttoNettoRechner/1.0}calculateBruttoNetto found.   Expected {http://service.plr.mycomp.com}calculateBruttoNetto.
    at org.apache.cxf.interceptor.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:106)

所以我不明白为什么我对命名空间的更改不起作用。 (我不希望消息有效,还有其他任务要做)。

THX

1 个答案:

答案 0 :(得分:1)

我没有找到为什么我的代码无效,但我找到了一个满足我需求的解决方案:

public boolean handleMessage(SOAPMessageContext context) {

    boolean isResponse = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    try {
        if (!isResponse) {
            SOAPMessage msg = context.getMessage();
            SOAPBody body = msg.getSOAPBody();
            Iterator<?> iter = body.getChildElements();
            while (iter.hasNext()) {
                Object next = iter.next();
                if (next instanceof SOAPElement) {
                    SOAPElement se = (SOAPElement) next;
                    if (se.getNamespaceURI().equalsIgnoreCase("http://service.plr.mycomp.com/BruttoNettoRechner/1.0")) {
                        QName qName = new QName("http://service.plr.mycomp.com", "calculateBruttoNetto", "impl");
                        se.setElementQName(qName);
                        break;
                    }
                }
            }
            if (msg.saveRequired()) {
                msg.saveChanges();
            }
            // msg.writeTo(System.out);

        }
    } catch (SOAPException e) {
        log.error("Fehler beim Bearbeiten des Requests im ServerSOAPHandler", e);
    }

    return true;
}