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