001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     **/
018    package org.codehaus.activesoap.handler.xstream;
019    
020    import com.thoughtworks.xstream.XStream;
021    import com.thoughtworks.xstream.io.xml.StaxDriver;
022    import org.codehaus.activesoap.Handler;
023    import org.codehaus.activesoap.MessageExchange;
024    
025    import javax.xml.stream.XMLStreamReader;
026    import javax.xml.stream.XMLStreamWriter;
027    
028    /**
029     * @version $Revision: 1.5 $
030     */
031    public class XStreamHandler implements Handler {
032        private Object object;
033        protected StaxDriver driver = new StaxDriver();
034        protected XStream xstream = new XStream(driver);
035    
036        public void invoke(MessageExchange exchange) throws Exception {
037            XMLStreamReader in = exchange.getIn();
038            XMLStreamWriter out = exchange.getOut();
039    
040            // lets reset first in case we get a fault
041            object = null;
042            object = xstream.unmarshal(driver.createStaxReader(in));
043            handleBody(exchange, object, out);
044        }
045    
046        /**
047         * Sends the reply object to the output body
048         */
049        public void reply(MessageExchange exchange, Object reply, XMLStreamWriter out) throws Exception {
050            xstream.marshal(reply, driver.createStaxWriter(out));
051        }
052    
053    
054        // Properties
055        //-------------------------------------------------------------------------
056    
057        /**
058         * Returns the body of the SOAP request converted to the native POJO
059         */
060        public Object getObject() {
061            return object;
062        }
063    
064        public StaxDriver getDriver() {
065            return driver;
066        }
067    
068        public XStream getXstream() {
069            return xstream;
070        }
071    
072        // Implementation methods
073        //-------------------------------------------------------------------------
074        protected void handleBody(MessageExchange exchange, Object body, XMLStreamWriter out) throws Exception {
075        }
076    }