View Javadoc

1   /*
2    * $Id: MuleRESTReceiverServlet.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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 CPAL v1.0
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.impl.MuleMessage;
15  import org.mule.providers.AbstractMessageReceiver;
16  import org.mule.providers.http.i18n.HttpMessages;
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 java.io.IOException;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
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  
46  public class MuleRESTReceiverServlet extends MuleReceiverServlet
47  {
48      /**
49       * Serial version
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                  AbstractMessageReceiver 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              AbstractMessageReceiver 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             AbstractMessageReceiver 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             // if we dont find an endpoint for the given name, lets check the
186             // servlet receivers
187             UMOMessageReceiver receiver = (UMOMessageReceiver)getReceivers().get(endpointName);
188             if (receiver == null)
189             {
190                 throw new EndpointNotFoundException(endpointName);
191             }
192             endpoint = receiver.getEndpoint();
193 
194         }
195         return endpoint;
196     }
197 }