001    /*
002     * Copyright (C) The Apache Software Foundation. All rights reserved.
003     *
004     * This software is published under the terms of the Apache Software License
005     * version 1.1, a copy of which has been included with this distribution in
006     * the LICENSE file.
007     * 
008     * $Id: MessageletResponseImpl.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messagelet.impl;
011    
012    import java.io.IOException;
013    import java.io.PrintWriter;
014    
015    import javax.jms.Destination;
016    import javax.jms.JMSException;
017    import javax.jms.Message;
018    import javax.servlet.ServletOutputStream;
019    import javax.servlet.ServletResponse;
020    import javax.servlet.ServletResponseWrapper;
021    
022    import org.apache.commons.messagelet.MessageletResponse;
023    import org.apache.commons.messenger.Messenger;
024    
025    /** <p><code>MessageletResponseImpl</code> represents a servlet request from
026      * a JMS Message source.</p>
027      *
028      * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
029      * @version $Revision: 155459 $
030      */
031    public class MessageletResponseImpl extends ServletResponseWrapper implements MessageletResponse {
032    
033        /** the messenger used to send replies to */
034        private Messenger messenger;
035        /** the reply to destination to send replies to */
036        private Destination replyToDestination;
037        
038        /** The BufferedServletOutputStream that is given to the servlet to capture
039         * the response 
040         */
041        private BufferedServletOutputStream stream = null;
042        /** The PrintWriter that has been returned by getWriter(), if any */
043        protected PrintWriter writer;
044        
045        public MessageletResponseImpl(ServletResponse response) {
046            super(response);
047        }
048    
049        /** Resets the response, ready for a new request */
050        public void reset() {
051            writer = null;
052            stream = null;
053        }
054        
055        /** Called to finish the request */
056        public void finish() throws IOException, JMSException {
057            try {
058                if ( writer != null ) {
059                    writer.flush();
060                    writer.close();
061                }
062                if ( stream != null ) {
063                    byte[] data = stream.toByteArray();
064    
065                    // for now assume a text message
066                    String text = new String(data);
067                    Message message = getReplyMessenger().createTextMessage( text );
068                    sendReply( message );
069                }
070            }
071            finally {
072                writer = null;
073                stream = null;
074            }
075        }
076        
077        // MessageletResponse methods
078        //-------------------------------------------------------------------------    
079        
080        /** Sends a reply to the original message */
081        public void sendReply(Message replyMessage) throws JMSException {
082            getReplyMessenger().send( getReplyToDestination(), replyMessage );
083        }
084        
085        public Messenger getReplyMessenger() {
086            return messenger;
087        }
088        
089        public Destination getReplyToDestination() {
090            return replyToDestination;
091        }
092        
093        public void setReplyMessenger(Messenger messenger) {
094            this.messenger = messenger;
095        }
096        
097        public void setReplyToDestination(Destination replyToDestination) {
098            this.replyToDestination = replyToDestination;
099        }
100        
101        
102        // Implementation methods
103        //-------------------------------------------------------------------------    
104        
105        public void flushBuffer() throws IOException {
106            if ( stream != null ) {
107                stream.flush();
108            }
109        }
110        
111        public ServletOutputStream getOutputStream() throws IOException {
112            if (writer != null) {
113                throw new IllegalStateException("getWriter() has already been called " +
114                "for this response");
115            }
116            if (stream == null) {
117                stream = createOutputStream();
118            }
119            return stream;
120        }
121        
122        public PrintWriter getWriter() throws IOException {
123            if (writer != null) {
124                return writer;
125            }        
126            if (stream != null) {
127                throw new IllegalStateException("getOutputStream() has already been called for this response");
128            }        
129            stream = createOutputStream();
130            writer = new PrintWriter(stream);
131            return writer;
132        }
133        
134        protected BufferedServletOutputStream createOutputStream() {
135            return new BufferedServletOutputStream();
136        }
137    }
138