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 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 if (httpServletRequest.getParameter("endpoint") != null)
59 {
60 UMOEndpoint endpoint = getEndpointForURI(httpServletRequest);
61 String timeoutString = httpServletRequest.getParameter("timeout");
62 long to = timeout;
63
64 if (timeoutString != null)
65 {
66 to = Long.parseLong(timeoutString);
67 }
68
69 if (logger.isDebugEnabled())
70 {
71 logger.debug("Making request using endpoint: " + endpoint.toString() + " timeout is: "
72 + to);
73 }
74
75 UMOMessage returnMessage = endpoint.receive(to);
76 writeResponse(httpServletResponse, returnMessage);
77 }
78 else
79 {
80 UMOMessageReceiver receiver = getReceiverForURI(httpServletRequest);
81 httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME, payloadParameterName);
82 UMOMessage message = new MuleMessage(receiver.getConnector().getMessageAdapter(
83 httpServletRequest));
84 UMOMessage returnMessage = receiver.routeMessage(message, true);
85 writeResponse(httpServletResponse, returnMessage);
86 }
87 }
88 catch (Exception e)
89 {
90 handleException(e, "Failed to route event through Servlet Receiver", httpServletResponse);
91 }
92 }
93
94 protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
95 throws ServletException, IOException
96 {
97 try
98 {
99 UMOMessageReceiver receiver = getReceiverForURI(httpServletRequest);
100 httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME, payloadParameterName);
101 UMOMessage message = new MuleMessage(receiver.getConnector()
102 .getMessageAdapter(httpServletRequest));
103 UMOMessage returnMessage = receiver.routeMessage(message, true);
104 writeResponse(httpServletResponse, returnMessage);
105
106 }
107 catch (Exception e)
108 {
109 handleException(e, "Failed to Post event to Mule", httpServletResponse);
110 }
111 }
112
113 protected void doPut(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
114 throws ServletException, IOException
115 {
116 try
117 {
118 UMOMessageReceiver receiver = getReceiverForURI(httpServletRequest);
119 httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME, payloadParameterName);
120 UMOMessage message = new MuleMessage(receiver.getConnector()
121 .getMessageAdapter(httpServletRequest));
122 receiver.routeMessage(message, MuleManager.getConfiguration().isSynchronous());
123
124 httpServletResponse.setStatus(HttpServletResponse.SC_CREATED);
125 if (feedback)
126 {
127 httpServletResponse.getWriter().write(
128 "Item was created at endpointUri: " + receiver.getEndpointURI());
129 }
130 }
131 catch (Exception e)
132 {
133 handleException(e, "Failed to Post event to Mule" + e.getMessage(), httpServletResponse);
134 }
135 }
136
137 protected void doDelete(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
138 throws ServletException, IOException
139 {
140 try
141 {
142 UMOEndpoint endpoint = getEndpointForURI(httpServletRequest);
143 String timeoutString = httpServletRequest.getParameter("timeout");
144 long to = timeout;
145
146 if (timeoutString != null)
147 {
148 to = new Long(timeoutString).longValue();
149 }
150
151 if (logger.isDebugEnabled())
152 {
153 logger.debug("Making request using endpoint: " + endpoint.toString() + " timeout is: " + to);
154 }
155
156 UMOMessage returnMessage = endpoint.receive(to);
157 if (returnMessage != null)
158 {
159 httpServletResponse.setStatus(HttpServletResponse.SC_OK);
160 }
161 else
162 {
163 httpServletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
164 }
165 }
166 catch (Exception e)
167 {
168 handleException(e, "Failed to Delete mule event via receive using uri: "
169 + httpServletRequest.getPathInfo(), httpServletResponse);
170 }
171 }
172
173 protected UMOEndpoint getEndpointForURI(HttpServletRequest httpServletRequest)
174 throws EndpointException, MalformedEndpointException
175 {
176 String endpointName = httpServletRequest.getParameter("endpoint");
177 if (endpointName == null)
178 {
179 throw new EndpointException(HttpMessages.httpParameterNotSet("endpoint"));
180 }
181
182 UMOEndpoint endpoint = MuleManager.getInstance().lookupEndpoint(endpointName);
183 if (endpoint == null)
184 {
185
186
187 UMOMessageReceiver receiver = (UMOMessageReceiver)getReceivers().get(endpointName);
188
189 if (receiver == null)
190 {
191 throw new EndpointNotFoundException(endpointName);
192 }
193
194 endpoint = receiver.getEndpoint();
195
196 }
197 return endpoint;
198 }
199 }