KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > servlet > MuleRESTReceiverServlet


1 /*
2  * $Id: MuleRESTReceiverServlet.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.http.servlet;
12
13 import org.mule.MuleManager;
14 import org.mule.config.i18n.Message;
15 import org.mule.impl.MuleMessage;
16 import org.mule.providers.AbstractMessageReceiver;
17 import org.mule.umo.UMOMessage;
18 import org.mule.umo.endpoint.EndpointException;
19 import org.mule.umo.endpoint.EndpointNotFoundException;
20 import org.mule.umo.endpoint.MalformedEndpointException;
21 import org.mule.umo.endpoint.UMOEndpoint;
22 import org.mule.umo.provider.UMOMessageReceiver;
23
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import java.io.IOException JavaDoc;
29
30 /**
31  * <code>MuleRESTReceiverServlet</code> is used for sending a receiving events from
32  * the Mule server via a serlet container. The servlet uses the REST style of request
33  * processing GET METHOD will do a receive from an external source if an endpoint
34  * parameter is set otherwise it behaves the same way as POST. you can either specify
35  * the transport name i.e. to read from Jms orders.queue
36  * http://www.mycompany.com/rest/jms/orders/queue <p/> or a Mule endpoint name to
37  * target a specific endpoint config. This would get the first email message received
38  * by the orderEmailInbox endpoint. <p/>
39  * http://www.mycompany.com/rest/ordersEmailInbox <p/> POST Do a sysnchrous call and
40  * return a result http://www.clientapplication.com/service/clientquery?custId=1234
41  * <p/> PUT Do an asysnchrous call without returning a result (other than an http
42  * status code) http://www.clientapplication.com/service/orders?payload=<order>more
43  * beer</order> <p/> DELETE Same as GET only without returning a result
44  *
45  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
46  * @version $Revision: 3798 $
47  */

48
49 public class MuleRESTReceiverServlet extends MuleReceiverServlet
50 {
51     /**
52      * Serial version
53      */

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