1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.http.servlet; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.impl.MuleMessage; |
15 | |
import org.mule.providers.http.i18n.HttpMessages; |
16 | |
import org.mule.umo.UMOMessage; |
17 | |
import org.mule.umo.endpoint.EndpointException; |
18 | |
import org.mule.umo.endpoint.EndpointNotFoundException; |
19 | |
import org.mule.umo.endpoint.MalformedEndpointException; |
20 | |
import org.mule.umo.endpoint.UMOEndpoint; |
21 | |
import org.mule.umo.provider.UMOMessageReceiver; |
22 | |
|
23 | |
import java.io.IOException; |
24 | |
|
25 | |
import javax.servlet.ServletException; |
26 | |
import javax.servlet.http.HttpServletRequest; |
27 | |
import javax.servlet.http.HttpServletResponse; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 2 | public class MuleRESTReceiverServlet extends MuleReceiverServlet |
47 | |
{ |
48 | |
|
49 | |
|
50 | |
|
51 | |
private static final long serialVersionUID = -2395763805839859649L; |
52 | |
|
53 | |
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) |
54 | |
throws ServletException, IOException |
55 | |
{ |
56 | |
try |
57 | |
{ |
58 | 0 | if (httpServletRequest.getParameter("endpoint") != null) |
59 | |
{ |
60 | 0 | UMOEndpoint endpoint = getEndpointForURI(httpServletRequest); |
61 | 0 | String timeoutString = httpServletRequest.getParameter("timeout"); |
62 | 0 | long to = timeout; |
63 | |
|
64 | 0 | if (timeoutString != null) |
65 | |
{ |
66 | 0 | to = Long.parseLong(timeoutString); |
67 | |
} |
68 | |
|
69 | 0 | if (logger.isDebugEnabled()) |
70 | |
{ |
71 | 0 | logger.debug("Making request using endpoint: " + endpoint.toString() + " timeout is: " |
72 | |
+ to); |
73 | |
} |
74 | |
|
75 | 0 | UMOMessage returnMessage = endpoint.receive(to); |
76 | 0 | writeResponse(httpServletResponse, returnMessage); |
77 | 0 | } |
78 | |
else |
79 | |
{ |
80 | 0 | UMOMessageReceiver receiver = getReceiverForURI(httpServletRequest); |
81 | 0 | httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME, payloadParameterName); |
82 | 0 | UMOMessage message = new MuleMessage(receiver.getConnector().getMessageAdapter( |
83 | |
httpServletRequest)); |
84 | 0 | UMOMessage returnMessage = receiver.routeMessage(message, true); |
85 | 0 | writeResponse(httpServletResponse, returnMessage); |
86 | |
} |
87 | |
} |
88 | 0 | catch (Exception e) |
89 | |
{ |
90 | 0 | handleException(e, "Failed to route event through Servlet Receiver", httpServletResponse); |
91 | 0 | } |
92 | 0 | } |
93 | |
|
94 | |
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) |
95 | |
throws ServletException, IOException |
96 | |
{ |
97 | |
try |
98 | |
{ |
99 | 2 | UMOMessageReceiver receiver = getReceiverForURI(httpServletRequest); |
100 | 2 | httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME, payloadParameterName); |
101 | 2 | UMOMessage message = new MuleMessage(receiver.getConnector() |
102 | |
.getMessageAdapter(httpServletRequest)); |
103 | 2 | UMOMessage returnMessage = receiver.routeMessage(message, true); |
104 | 2 | writeResponse(httpServletResponse, returnMessage); |
105 | |
|
106 | |
} |
107 | 0 | catch (Exception e) |
108 | |
{ |
109 | 0 | handleException(e, "Failed to Post event to Mule", httpServletResponse); |
110 | 2 | } |
111 | 2 | } |
112 | |
|
113 | |
protected void doPut(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) |
114 | |
throws ServletException, IOException |
115 | |
{ |
116 | |
try |
117 | |
{ |
118 | 0 | UMOMessageReceiver receiver = getReceiverForURI(httpServletRequest); |
119 | 0 | httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME, payloadParameterName); |
120 | 0 | UMOMessage message = new MuleMessage(receiver.getConnector() |
121 | |
.getMessageAdapter(httpServletRequest)); |
122 | 0 | receiver.routeMessage(message, MuleManager.getConfiguration().isSynchronous()); |
123 | |
|
124 | 0 | httpServletResponse.setStatus(HttpServletResponse.SC_CREATED); |
125 | 0 | if (feedback) |
126 | |
{ |
127 | 0 | httpServletResponse.getWriter().write( |
128 | |
"Item was created at endpointUri: " + receiver.getEndpointURI()); |
129 | |
} |
130 | |
} |
131 | 0 | catch (Exception e) |
132 | |
{ |
133 | 0 | handleException(e, "Failed to Post event to Mule" + e.getMessage(), httpServletResponse); |
134 | 0 | } |
135 | 0 | } |
136 | |
|
137 | |
protected void doDelete(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) |
138 | |
throws ServletException, IOException |
139 | |
{ |
140 | |
try |
141 | |
{ |
142 | 0 | UMOEndpoint endpoint = getEndpointForURI(httpServletRequest); |
143 | 0 | String timeoutString = httpServletRequest.getParameter("timeout"); |
144 | 0 | long to = timeout; |
145 | |
|
146 | 0 | if (timeoutString != null) |
147 | |
{ |
148 | 0 | to = new Long(timeoutString).longValue(); |
149 | |
} |
150 | |
|
151 | 0 | if (logger.isDebugEnabled()) |
152 | |
{ |
153 | 0 | logger.debug("Making request using endpoint: " + endpoint.toString() + " timeout is: " + to); |
154 | |
} |
155 | |
|
156 | 0 | UMOMessage returnMessage = endpoint.receive(to); |
157 | 0 | if (returnMessage != null) |
158 | |
{ |
159 | 0 | httpServletResponse.setStatus(HttpServletResponse.SC_OK); |
160 | |
} |
161 | |
else |
162 | |
{ |
163 | 0 | httpServletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT); |
164 | |
} |
165 | |
} |
166 | 0 | catch (Exception e) |
167 | |
{ |
168 | 0 | handleException(e, "Failed to Delete mule event via receive using uri: " |
169 | |
+ httpServletRequest.getPathInfo(), httpServletResponse); |
170 | 0 | } |
171 | 0 | } |
172 | |
|
173 | |
protected UMOEndpoint getEndpointForURI(HttpServletRequest httpServletRequest) |
174 | |
throws EndpointException, MalformedEndpointException |
175 | |
{ |
176 | 0 | String endpointName = httpServletRequest.getParameter("endpoint"); |
177 | 0 | if (endpointName == null) |
178 | |
{ |
179 | 0 | throw new EndpointException(HttpMessages.httpParameterNotSet("endpoint")); |
180 | |
} |
181 | |
|
182 | 0 | UMOEndpoint endpoint = MuleManager.getInstance().lookupEndpoint(endpointName); |
183 | 0 | if (endpoint == null) |
184 | |
{ |
185 | |
|
186 | |
|
187 | 0 | UMOMessageReceiver receiver = (UMOMessageReceiver)getReceivers().get(endpointName); |
188 | |
|
189 | 0 | if (receiver == null) |
190 | |
{ |
191 | 0 | throw new EndpointNotFoundException(endpointName); |
192 | |
} |
193 | |
|
194 | 0 | endpoint = receiver.getEndpoint(); |
195 | |
|
196 | |
} |
197 | 0 | return endpoint; |
198 | |
} |
199 | |
} |