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: MessageletRequestImpl.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.ServletRequest;
020    import javax.servlet.ServletRequestWrapper;
021    
022    import org.apache.commons.messagelet.MessageletRequest;
023    import org.apache.commons.messenger.Messenger;
024    
025    /** <p><code>MessageletRequestImpl</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 MessageletRequestImpl extends ServletRequestWrapper implements MessageletRequest {
032    
033        /** The Message which caused this request */
034        private Message message;
035        /** The stream to read the body of the current Message */
036        private ServletInputStream stream;
037        
038        public MessageletRequestImpl(ServletRequest request) {
039            super(request);
040        }
041    
042        public void setMessage(Message message) throws JMSException {
043            this.message = message;
044            this.stream = createInputStream();
045            
046            // also publish the message as a request scope attribute
047            setAttribute( "message", message );
048        }
049    
050        public void setMessenger(Messenger messenger) {
051            setAttribute( "messenger", messenger );
052        }
053    
054        // MessageletRequest methods
055        //-------------------------------------------------------------------------    
056        
057        /** @return the Message which originated this request */
058        public Message getMessage() {
059            return message;
060        }
061    
062        
063        // ServletRequest methods
064        //-------------------------------------------------------------------------    
065    
066        public ServletInputStream getInputStream() {
067            return stream;
068        }
069    
070        public BufferedReader getReader() {
071            return new BufferedReader( new InputStreamReader( stream ) );
072        }
073    
074        // Implementation methods
075        //-------------------------------------------------------------------------    
076    
077    
078        protected ServletInputStream createInputStream() throws JMSException {
079            if ( message instanceof TextMessage ) {
080                TextMessage textMessage = (TextMessage) message;
081                return new BufferedServletInputStream( textMessage.getText() );
082            }
083            
084            // ##### handle ByteMessage and StreamMessage somehow one day?
085            return new BufferedServletInputStream();
086        }
087        
088    }
089