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.soap.bindings.soap.interceptors;
018    
019    import java.util.Iterator;
020    
021    import javax.xml.stream.XMLStreamException;
022    import javax.xml.stream.XMLStreamWriter;
023    
024    import org.apache.servicemix.soap.api.Fault;
025    import org.apache.servicemix.soap.api.InterceptorChain;
026    import org.apache.servicemix.soap.api.Message;
027    import org.apache.servicemix.soap.bindings.soap.SoapVersion;
028    import org.apache.servicemix.soap.core.AbstractInterceptor;
029    import org.apache.servicemix.soap.util.DomUtil;
030    import org.apache.servicemix.soap.util.stax.DOMStreamReader;
031    import org.apache.servicemix.soap.util.stax.StaxUtil;
032    import org.w3c.dom.DocumentFragment;
033    import org.w3c.dom.Element;
034    
035    /**
036     * @author <a href="mailto:gnodet [at] gmail.com">Guillaume Nodet</a>
037     */
038    public class SoapOutInterceptor extends AbstractInterceptor {
039    
040        private final SoapVersion soapVersion;
041        
042        public SoapOutInterceptor() {
043            this(null);
044        }
045        
046        public SoapOutInterceptor(SoapVersion soapVersion) {
047            this.soapVersion = soapVersion;
048        }
049        
050        public void handleMessage(Message message) {
051            XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
052            if (writer == null) {
053                throw new NullPointerException("XMLStreamWriter content not found");
054            }
055            SoapVersion soapVersion = message.get(SoapVersion.class); 
056            if (soapVersion == null) {
057                soapVersion = this.soapVersion;
058                if (soapVersion == null) {
059                    throw new IllegalStateException("No soap version specified");
060                }
061            }
062    
063            try {
064                StaxUtil.writeStartElement(writer, soapVersion.getEnvelope());
065                // Write Header
066                if (message.getSoapHeaders().size() > 0) {
067                    StaxUtil.writeStartElement(writer, soapVersion.getHeader());
068                    for (Iterator it = message.getSoapHeaders().values().iterator(); it.hasNext();) {
069                        DocumentFragment df = (DocumentFragment) it.next();
070                        Element e = DomUtil.getFirstChildElement(df);
071                        StaxUtil.copy(new DOMStreamReader(e), writer);
072                    }
073                    writer.writeEndElement();
074                }
075                // Write Body
076                StaxUtil.writeStartElement(writer, soapVersion.getBody());
077                // Write content
078                InterceptorChain chain = message.get(InterceptorChain.class);
079                chain.doIntercept(message);
080                // Close elements
081                writer.writeEndElement();
082                writer.writeEndElement();
083            } catch (XMLStreamException e) {
084                throw new Fault(e);
085            }
086        }
087        
088        
089    }