View Javadoc

1   /*
2    * Copyright (C) The Apache Software Foundation. All rights reserved.
3    *
4    * This software is published under the terms of the Apache Software License
5    * version 1.1, a copy of which has been included with this distribution in
6    * the LICENSE file.
7    * 
8    * $Id: MessageletRequestImpl.java 155459 2005-02-26 13:24:44Z dirkv $
9    */
10  package org.apache.commons.messagelet.impl;
11  
12  import java.io.BufferedReader;
13  import java.io.InputStreamReader;
14  
15  import javax.jms.JMSException;
16  import javax.jms.Message;
17  import javax.jms.TextMessage;
18  import javax.servlet.ServletInputStream;
19  import javax.servlet.ServletRequest;
20  import javax.servlet.ServletRequestWrapper;
21  
22  import org.apache.commons.messagelet.MessageletRequest;
23  import org.apache.commons.messenger.Messenger;
24  
25  /** <p><code>MessageletRequestImpl</code> represents a servlet request from
26    * a JMS Message source.</p>
27    *
28    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
29    * @version $Revision: 155459 $
30    */
31  public class MessageletRequestImpl extends ServletRequestWrapper implements MessageletRequest {
32  
33      /** The Message which caused this request */
34      private Message message;
35      /** The stream to read the body of the current Message */
36      private ServletInputStream stream;
37      
38      public MessageletRequestImpl(ServletRequest request) {
39          super(request);
40      }
41  
42      public void setMessage(Message message) throws JMSException {
43          this.message = message;
44          this.stream = createInputStream();
45          
46          // also publish the message as a request scope attribute
47          setAttribute( "message", message );
48      }
49  
50      public void setMessenger(Messenger messenger) {
51          setAttribute( "messenger", messenger );
52      }
53  
54      // MessageletRequest methods
55      //-------------------------------------------------------------------------    
56      
57      /** @return the Message which originated this request */
58      public Message getMessage() {
59          return message;
60      }
61  
62      
63      // ServletRequest methods
64      //-------------------------------------------------------------------------    
65  
66      public ServletInputStream getInputStream() {
67          return stream;
68      }
69  
70      public BufferedReader getReader() {
71          return new BufferedReader( new InputStreamReader( stream ) );
72      }
73  
74      // Implementation methods
75      //-------------------------------------------------------------------------    
76  
77  
78      protected ServletInputStream createInputStream() throws JMSException {
79          if ( message instanceof TextMessage ) {
80              TextMessage textMessage = (TextMessage) message;
81              return new BufferedServletInputStream( textMessage.getText() );
82          }
83          
84          // ##### handle ByteMessage and StreamMessage somehow one day?
85          return new BufferedServletInputStream();
86      }
87      
88  }
89