001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.cxfbc.interceptors;
018    
019    import java.io.InputStream;
020    
021    import javax.xml.namespace.QName;
022    import javax.xml.stream.XMLStreamException;
023    import javax.xml.stream.XMLStreamReader;
024    import javax.xml.transform.stream.StreamSource;
025    
026    import org.apache.cxf.binding.soap.SoapMessage;
027    import org.apache.cxf.interceptor.Fault;
028    import org.apache.cxf.message.Message;
029    import org.apache.cxf.phase.AbstractPhaseInterceptor;
030    import org.apache.cxf.phase.Phase;
031    import org.apache.cxf.staxutils.DepthXMLStreamReader;
032    import org.apache.cxf.staxutils.StaxUtils;
033    
034    public class RetrievePayLoadInterceptor extends AbstractPhaseInterceptor<Message> {
035    
036        public RetrievePayLoadInterceptor() {
037            super(Phase.POST_STREAM);
038        }
039        
040        public void handleMessage(Message message) throws Fault {
041            InputStream is = message.getContent(InputStream.class);
042            XMLStreamReader xmlReader = null;
043            if (is != null) {
044                StreamSource bodySource = new StreamSource(message.getContent(InputStream.class));
045                xmlReader = StaxUtils.createXMLStreamReader(bodySource);
046                findBody(message, xmlReader);
047                message.setContent(XMLStreamReader.class, xmlReader);
048            }
049        }
050    
051        private void findBody(Message message, XMLStreamReader xmlReader) {
052            DepthXMLStreamReader reader = new DepthXMLStreamReader(xmlReader);
053            try {
054                int depth = reader.getDepth();
055                int event = reader.getEventType();
056                while (reader.getDepth() >= depth && reader.hasNext()) {
057                    QName name = null;
058                    if (event == XMLStreamReader.START_ELEMENT) {
059                        name = reader.getName();
060                    }
061                    if (event == XMLStreamReader.START_ELEMENT && name.equals(((SoapMessage)message).getVersion().getBody())) {
062                        reader.nextTag();
063                        return;
064                    }
065                    event = reader.next();
066                }
067                return;
068            } catch (XMLStreamException e) {
069                throw new RuntimeException("Couldn't parse stream.", e);
070            }
071        }
072    }