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