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: HttpMessageletRequestImpl.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.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletRequestWrapper;
21
22 import org.apache.commons.messagelet.MessageletRequest;
23 import org.apache.commons.messenger.Messenger;
24
25 /** <p><code>HttpMessageletRequestImpl</code> represents a servlet request from
26 * a JMS Message source which appears to be a HTTP request so that JSP can process
27 * the request as if it were a HTTP request.</p>
28 *
29 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
30 * @version $Revision: 155459 $
31 */
32 public class HttpMessageletRequestImpl extends HttpServletRequestWrapper implements MessageletRequest {
33
34 /** The Message which caused this request */
35 private Message message;
36 /** The stream to read the body of the current Message */
37 private ServletInputStream stream;
38
39 public HttpMessageletRequestImpl(HttpServletRequest request) {
40 super(request);
41 }
42
43 public void setMessage(Message message) throws JMSException {
44 this.message = message;
45 this.stream = createInputStream();
46
47 // also publish the message as a request scope attribute
48 setAttribute( "message", message );
49 }
50
51 public void setMessenger(Messenger messenger) {
52 setAttribute( "messenger", messenger );
53 }
54
55 // MessageletRequest methods
56 //-------------------------------------------------------------------------
57
58 /** @return the Message which originated this request */
59 public Message getMessage() {
60 return message;
61 }
62
63
64 // ServletRequest methods
65 //-------------------------------------------------------------------------
66
67 public ServletInputStream getInputStream() {
68 return stream;
69 }
70
71 public BufferedReader getReader() {
72 return new BufferedReader( new InputStreamReader( stream ) );
73 }
74
75
76
77 // Implementation methods
78 //-------------------------------------------------------------------------
79
80 protected ServletInputStream createInputStream() throws JMSException {
81 if ( message instanceof TextMessage ) {
82 TextMessage textMessage = (TextMessage) message;
83 return new BufferedServletInputStream( textMessage.getText() );
84 }
85
86 // ##### handle ByteMessage and StreamMessage somehow one day?
87 return new BufferedServletInputStream();
88 }
89
90 }
91