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: HttpMessageletRequestImpl.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messagelet.impl;
011    
012    import java.io.BufferedReader;
013    import java.io.InputStreamReader;
014    
015    import javax.jms.JMSException;
016    import javax.jms.Message;
017    import javax.jms.TextMessage;
018    import javax.servlet.ServletInputStream;
019    import javax.servlet.http.HttpServletRequest;
020    import javax.servlet.http.HttpServletRequestWrapper;
021    
022    import org.apache.commons.messagelet.MessageletRequest;
023    import org.apache.commons.messenger.Messenger;
024    
025    /** <p><code>HttpMessageletRequestImpl</code> represents a servlet request from
026      * a JMS Message source which appears to be a HTTP request so that JSP can process
027      * the request as if it were a HTTP request.</p>
028      *
029      * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
030      * @version $Revision: 155459 $
031      */
032    public class HttpMessageletRequestImpl extends HttpServletRequestWrapper implements MessageletRequest {
033    
034        /** The Message which caused this request */
035        private Message message;
036        /** The stream to read the body of the current Message */
037        private ServletInputStream stream;
038        
039        public HttpMessageletRequestImpl(HttpServletRequest request) {
040            super(request);
041        }
042    
043        public void setMessage(Message message) throws JMSException {
044            this.message = message;
045            this.stream = createInputStream();
046            
047            // also publish the message as a request scope attribute
048            setAttribute( "message", message );
049        }
050    
051        public void setMessenger(Messenger messenger) {
052            setAttribute( "messenger", messenger );
053        }
054        
055        // MessageletRequest methods
056        //-------------------------------------------------------------------------    
057        
058        /** @return the Message which originated this request */
059        public Message getMessage() {
060            return message;
061        }
062    
063        
064        // ServletRequest methods
065        //-------------------------------------------------------------------------    
066    
067        public ServletInputStream getInputStream() {
068            return stream;
069        }
070    
071        public BufferedReader getReader() {
072            return new BufferedReader( new InputStreamReader( stream ) );
073        }
074    
075        
076        
077        // Implementation methods
078        //-------------------------------------------------------------------------    
079        
080        protected ServletInputStream createInputStream() throws JMSException {
081            if ( message instanceof TextMessage ) {
082                TextMessage textMessage = (TextMessage) message;
083                return new BufferedServletInputStream( textMessage.getText() );
084            }
085            
086            // ##### handle ByteMessage and StreamMessage somehow one day?
087            return new BufferedServletInputStream();
088        }
089        
090    }
091