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.transport;
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.codehaus.activesoap.util.XMLStreamFactory;
023    
024    import javax.xml.stream.XMLStreamException;
025    import javax.xml.stream.XMLStreamReader;
026    import javax.xml.stream.XMLStreamWriter;
027    import java.io.BufferedReader;
028    import java.io.Reader;
029    import java.io.StringReader;
030    import java.io.StringWriter;
031    
032    /**
033     * Represents the invocation of an operation, whether REST or SOAP
034     *
035     * @version $Revision: 1.9 $
036     */
037    public class Invocation {
038        private static final Log log = LogFactory.getLog(Invocation.class);
039    
040        private TransportClient transport;
041        private XMLStreamFactory streamFactory;
042        private StringWriter buffer = new StringWriter();
043        private XMLStreamWriter out;
044    
045        public Invocation(TransportClient transport, XMLStreamFactory streamFactory) {
046            this.transport = transport;
047            this.streamFactory = streamFactory;
048        }
049    
050        public XMLStreamWriter getOut() throws XMLStreamException {
051            if (out == null) {
052                out = createXMLStreamWriter();
053            }
054            return out;
055        }
056    
057        /**
058         * Invokes a one way operation
059         */
060        public void invokeOneWay() throws Exception {
061            transport.invokeOneWay(this, createRequestReader());
062        }
063    
064        /**
065         * Performs a request/reply waiting for the reply
066         * and returning a parser on the response
067         */
068        public XMLStreamReader invokeRequest() throws Exception {
069            Reader reader = transport.invokeRequest(this, createRequestReader());
070            return createXMLStreamReader(reader);
071        }
072    
073    
074        /**
075         * A helper method which returns the reader as a {@link BufferedReader}
076         */
077        public BufferedReader asBufferedReader(Reader request) {
078            if (request instanceof BufferedReader) {
079                return (BufferedReader) request;
080            }
081            return new BufferedReader(request);
082        }
083    
084        // Implementation methods
085        //-------------------------------------------------------------------------
086        protected Reader createRequestReader() throws XMLStreamException {
087            out.close();
088    
089            String text = getRequestText();
090            //System.out.println(text);
091            if (log.isTraceEnabled()) {
092                log.trace("About to perform request: " + text);
093            }
094            return new StringReader(text);
095        }
096    
097        protected String getRequestText() {
098            return buffer.toString();
099        }
100    
101        protected XMLStreamReader createXMLStreamReader(Reader reader) throws XMLStreamException {
102            return streamFactory.getInputFactory().createXMLStreamReader(reader);
103        }
104    
105        protected XMLStreamWriter createXMLStreamWriter() throws XMLStreamException {
106            return streamFactory.getOutputFactory().createXMLStreamWriter(buffer);
107        }
108    }