1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.apache.commons.messagelet.impl; |
11 | |
|
12 | |
import javax.jms.JMSException; |
13 | |
import javax.jms.Message; |
14 | |
import javax.servlet.ServletException; |
15 | |
|
16 | |
import org.apache.commons.messagelet.MessageDrivenObjectSupport; |
17 | |
import org.apache.commons.messenger.Messenger; |
18 | |
import org.apache.commons.messenger.MessengerListener; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class MessageHttpServletDispatcher extends MessageDrivenObjectSupport implements MessengerListener { |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
private HttpMessageletRequestImpl request; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
private HttpMessageletResponseImpl response; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
private Messenger messenger; |
42 | |
|
43 | |
|
44 | |
private String path; |
45 | |
|
46 | |
|
47 | 0 | public MessageHttpServletDispatcher() { |
48 | |
|
49 | |
|
50 | 0 | } |
51 | |
|
52 | |
public MessageHttpServletDispatcher(String path) { |
53 | 0 | this(); |
54 | 0 | this.path = path; |
55 | 0 | } |
56 | |
|
57 | |
|
58 | |
public void init() throws ServletException { |
59 | 0 | request = new HttpMessageletRequestImpl( createHttpServletRequest() ); |
60 | 0 | response = new HttpMessageletResponseImpl( new HttpServletResponseImpl() ); |
61 | 0 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
public void setMessenger(Messenger messenger) { |
66 | 0 | this.messenger = messenger; |
67 | 0 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public void onMessage(Message message) { |
75 | |
try { |
76 | 0 | response.setReplyToDestination( message.getJMSReplyTo() ); |
77 | |
} |
78 | 0 | catch (JMSException e) { |
79 | 0 | log( "Could not find JMS replyTo destination", e ); |
80 | 0 | response.setReplyToDestination( null ); |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
try { |
85 | |
|
86 | 0 | request.setMessage( message ); |
87 | 0 | request.setMessenger( messenger ); |
88 | 0 | response.setReplyMessenger( messenger ); |
89 | 0 | response.reset(); |
90 | |
|
91 | |
|
92 | 0 | getServletContext().getRequestDispatcher( getPath() ).include(request, response); |
93 | |
|
94 | |
|
95 | 0 | response.finish(); |
96 | |
} |
97 | 0 | catch (Throwable e) { |
98 | 0 | handleException( message, e ); |
99 | 0 | } |
100 | 0 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public String getPath() { |
109 | 0 | return path; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public void setPath(String path) { |
116 | 0 | this.path = path; |
117 | |
|
118 | |
|
119 | 0 | } |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
protected void handleException(Message message, Throwable t) { |
126 | 0 | log( "Caught exception processing message: " + message, t); |
127 | 0 | } |
128 | |
|
129 | |
protected HttpServletRequestImpl createHttpServletRequest() { |
130 | 0 | HttpServletRequestImpl request = new HttpServletRequestImpl( getServletContext() ); |
131 | 0 | request.setRequestURI( path ); |
132 | 0 | int idx = path.indexOf( '?' ); |
133 | 0 | if ( idx >= 0 ) { |
134 | 0 | request.setQueryString( path.substring( idx + 1 ) ); |
135 | 0 | request.setPathInfo( path.substring( 0, idx ) ); |
136 | |
} |
137 | 0 | return request; |
138 | |
} |
139 | |
} |