如何从cxf soap消息中提取元素

时间:2014-08-25 06:36:38

标签: java web-services spring-mvc soap cxf

我有下面的soap消息。我想提取Id和chargeBoxIdentity元素。

ID: 2
Address: http://localhost:8080/ocppserver/services/CentralSystemService
Encoding: UTF-8
Http-Method: POST
Content-Type: application/soap+xml;charset=UTF-8;action="/Authorize"
Headers: {accept-encoding=[gzip,deflate], connection=[Keep-Alive], Content-Length= [852], content-type=[application/soap+xml;charset=UTF-8;action="/Authorize"], host=[localhost:8080], user-agent=[Apache-HttpClient/4.1.1 (java 1.5)]}
Payload: <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
  <h:chargeBoxIdentity xmlns:h="urn://Ocpp/Cs/2012/06/" xmlns="urn://Ocpp/Cs/2012/06/">REE001</h:chargeBoxIdentity>
  <a:From>
     <a:Address>http://127.0.0.1:8081/ChargeBox/Ocpp</a:Address>
  </a:From>
  <a:MessageID>urn:uuid:2ae9d933-4f92-4628-84fe-35024a858450</a:MessageID>
  <a:ReplyTo>
     <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
  </a:ReplyTo>
  <a:Action>/Authorize</a:Action></s:Header>
 <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <authorizeRequest xmlns="urn://Ocpp/Cs/2012/06/">
     <idTag>EF1234</idTag>
  </authorizeRequest>
</s:Body>
</s:Envelope>

我在下面写了拦截器.Below拦截器打印Header和payload字段。如何提取Id?如何从有效载荷中提取chargeboxIdentity?我正在使用log4j框架。

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;

public class InBoundMessage extends AbstractSoapInterceptor {

    public InBoundMessage() {
        super(Phase.RECEIVE);
    }

    public void handleMessage(SoapMessage message) throws Fault {
            Map<String, List<String>> headers = CastUtils.cast((Map)message.get(Message.PROTOCOL_HEADERS));
            if (headers != null) {
                 Set<Entry<String,List<String>>> hashSet=headers.entrySet();
                 for(Entry<String,List<String>> entry:hashSet ) {
                     System.out.println("Key="+entry.getKey()+", Value="+entry.getValue());
                 }
            }
            System.out.println("Message id = "+message.getId());
            try{
            InputStream is = message.getContent ( InputStream.class );
            CachedOutputStream os = new CachedOutputStream ( );
            IOUtils.copy ( is, os );
            os.flush();
            message.setContent (  InputStream.class, os.getInputStream ( ) );
            is.close();
            String payload=IOUtils.toString ( os.getInputStream ( ) );
            System.out.println ("The request is: " + payload);

            }catch(Exception e){
                e.printStackTrace();
            }
    }

}

1 个答案:

答案 0 :(得分:0)

我使用SAX解析器解析了soap消息。

import java.io.StringReader;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ParseInBoundSoapMessage {

    private static InBoundSoapMessage inBoundSoapMessage=null;


    public static InBoundSoapMessage getInBoundSoapMessage(String xml) {
        try {
            inBoundSoapMessage=new InBoundSoapMessage();
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            SAXParser saxParser = factory.newSAXParser();
            DefaultHandler handler = new DefaultHandler() {

                boolean ischargeBoxIdentityPresent = false;
                boolean isAddressPresent = false;
                boolean isFromPresent=false;
                boolean isActionPresent=false;
                boolean isMessageID=false;
                boolean isRelatesTo=false;
                public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                    if (localName.equals("chargeBoxIdentity")) {
                        ischargeBoxIdentityPresent = true;
                    }
                    if (localName.equals("From")) {
                        isFromPresent = true;
                    }
                    if (localName.equals("Address")) {
                        isAddressPresent = true;
                    }
                    if(localName.equals("Action")){
                        isActionPresent=true;
                    }
                    if(localName.equals("MessageID")){
                        isMessageID=true;
                    }
                    if(localName.equals("RelatesTo")){
                        isRelatesTo=true;
                    }

                }

                public void characters(char ch[], int start, int length) throws SAXException {
                    if (ischargeBoxIdentityPresent) {
                        inBoundSoapMessage.setChargeBoxIdentity(new String(ch, start, length));
                        ischargeBoxIdentityPresent = false;
                    }
                    if(isAddressPresent && isFromPresent){
                        inBoundSoapMessage.setAddressInFrom(new String(ch, start, length));
                        isAddressPresent = false;
                        isFromPresent=false;
                    }
                    if(isActionPresent){
                        inBoundSoapMessage.setAction(new String(ch, start, length));
                        isActionPresent=false;
                    }
                    if(isMessageID){
                        inBoundSoapMessage.setMessageId(new String(ch, start, length));
                        isMessageID=false;
                    }
                    if(isRelatesTo){
                        inBoundSoapMessage.setRelatesTo(new String(ch, start, length));
                        isRelatesTo=false;
                    }
                }
            };
            saxParser.parse(new InputSource(new StringReader(xml)), handler);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return inBoundSoapMessage;
    }
}
相关问题