1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.apache.commons.messenger; |
11 | |
|
12 | |
import java.io.BufferedReader; |
13 | |
import java.io.IOException; |
14 | |
import java.io.PrintWriter; |
15 | |
|
16 | |
import javax.jms.Destination; |
17 | |
import javax.jms.JMSException; |
18 | |
import javax.jms.Message; |
19 | |
import javax.jms.TextMessage; |
20 | |
import javax.servlet.ServletException; |
21 | |
import javax.servlet.http.HttpServlet; |
22 | |
import javax.servlet.http.HttpServletRequest; |
23 | |
import javax.servlet.http.HttpServletResponse; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class MessengerServlet extends HttpServlet { |
34 | |
|
35 | |
private static final String KEY_MESSENGER = "messenger"; |
36 | |
private static final String KEY_DESTINATION= "destination"; |
37 | |
|
38 | |
|
39 | |
private Messenger messenger; |
40 | |
|
41 | |
|
42 | |
private Destination destination; |
43 | |
|
44 | 0 | public MessengerServlet() { |
45 | 0 | } |
46 | |
|
47 | |
public void init() throws ServletException { |
48 | |
try { |
49 | 0 | String name = getRequiredInitParmeter( KEY_MESSENGER, "Name of the Messenger to use for this servlet" ); |
50 | 0 | String subject = getRequiredInitParmeter( KEY_DESTINATION, "Destination to be used for this servlet" ); |
51 | 0 | Messenger messenger = MessengerManager.get( name ); |
52 | 0 | if ( messenger == null ) { |
53 | 0 | throw new ServletException( "No Messenger configuration exists for name: " + name ); |
54 | |
} |
55 | 0 | Destination destination = messenger.getDestination( subject ); |
56 | 0 | if ( destination == null ) { |
57 | 0 | throw new ServletException( "No Destination exists for subject: " + subject ); |
58 | |
} |
59 | 0 | setMessenger( messenger ); |
60 | 0 | setDestination( destination ); |
61 | |
} |
62 | 0 | catch (JMSException e) { |
63 | 0 | throw new ServletException( "Failed to initialise Messenger and Destination: " + e, e ); |
64 | 0 | } |
65 | 0 | } |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public Messenger getMessenger() { |
75 | 0 | return messenger; |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public void setMessenger(Messenger messenger) { |
82 | 0 | this.messenger = messenger; |
83 | 0 | } |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public Destination getDestination() { |
89 | 0 | return destination; |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public void setDestination(Destination destination) { |
96 | 0 | this.destination = destination; |
97 | 0 | } |
98 | |
|
99 | |
|
100 | |
|
101 | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException { |
102 | |
try { |
103 | 0 | Message message = getMessenger().receiveNoWait( getDestination() ); |
104 | 0 | if ( message != null ) { |
105 | |
|
106 | 0 | writeMessage(message, request, response); |
107 | |
} |
108 | |
} |
109 | 0 | catch (IOException e) { |
110 | 0 | throw new ServletException(e); |
111 | |
} |
112 | 0 | catch (JMSException e) { |
113 | 0 | throw new ServletException(e); |
114 | 0 | } |
115 | 0 | } |
116 | |
|
117 | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException { |
118 | |
try { |
119 | 0 | Message message = readMessage(request, response); |
120 | 0 | if ( message != null ) { |
121 | 0 | getMessenger().send( getDestination(), message ); |
122 | |
} |
123 | |
} |
124 | 0 | catch (IOException e) { |
125 | 0 | throw new ServletException(e); |
126 | |
} |
127 | 0 | catch (JMSException e) { |
128 | 0 | throw new ServletException(e); |
129 | 0 | } |
130 | 0 | } |
131 | |
|
132 | |
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException { |
133 | |
try { |
134 | 0 | Message message = readMessage(request, response); |
135 | 0 | if ( message != null ) { |
136 | 0 | getMessenger().send( getDestination(), message ); |
137 | |
} |
138 | |
} |
139 | 0 | catch (IOException e) { |
140 | 0 | throw new ServletException(e); |
141 | |
} |
142 | 0 | catch (JMSException e) { |
143 | 0 | throw new ServletException(e); |
144 | 0 | } |
145 | 0 | } |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
protected Message readMessage(HttpServletRequest request, HttpServletResponse response) throws IOException, JMSException, ServletException { |
152 | |
|
153 | 0 | StringBuffer buffer = new StringBuffer(); |
154 | 0 | BufferedReader reader = request.getReader(); |
155 | 0 | for ( String line; (line = reader.readLine()) != null; ) { |
156 | 0 | buffer.append(line); |
157 | |
} |
158 | 0 | String text = buffer.toString(); |
159 | 0 | Message message = null; |
160 | 0 | if ( text.length() == 0 ) { |
161 | 0 | message = getMessenger().createMessage(); |
162 | |
} |
163 | |
else { |
164 | 0 | message = getMessenger().createTextMessage(text); |
165 | |
} |
166 | 0 | return message; |
167 | |
} |
168 | |
|
169 | |
protected void writeMessage(Message message, HttpServletRequest request, HttpServletResponse response) throws IOException, JMSException, ServletException { |
170 | |
|
171 | 0 | PrintWriter writer = response.getWriter(); |
172 | 0 | if ( message instanceof TextMessage ) { |
173 | 0 | TextMessage textMessage = (TextMessage) message; |
174 | 0 | writer.write( textMessage.getText() ); |
175 | |
} |
176 | 0 | } |
177 | |
|
178 | |
|
179 | |
protected String getRequiredInitParmeter(String key, String description) throws ServletException { |
180 | 0 | String value = getInitParameter( key ); |
181 | 0 | if ( value == null || value.length() == 0 ) { |
182 | 0 | throw new ServletException( |
183 | |
"No initialization parameter for parameter: " + key |
184 | |
+ " description: " + description |
185 | |
); |
186 | |
} |
187 | 0 | return value; |
188 | |
} |
189 | |
} |
190 | |
|