Coverage Report - org.mule.transport.servlet.MuleRESTReceiverServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleRESTReceiverServlet
0%
0/70
0%
0/26
0
 
 1  
 /*
 2  
  * $Id: MuleRESTReceiverServlet.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transport.servlet;
 12  
 
 13  
 import org.mule.api.MuleEvent;
 14  
 import org.mule.api.MuleException;
 15  
 import org.mule.api.MuleMessage;
 16  
 import org.mule.api.endpoint.InboundEndpoint;
 17  
 import org.mule.api.transport.MessageReceiver;
 18  
 
 19  
 import java.io.IOException;
 20  
 
 21  
 import javax.servlet.ServletException;
 22  
 import javax.servlet.http.HttpServletRequest;
 23  
 import javax.servlet.http.HttpServletResponse;
 24  
 
 25  
 
 26  
 /**
 27  
  * <code>MuleRESTReceiverServlet</code> is used for sending and receiving events from
 28  
  * the Mule server via a servlet container. The servlet uses the REST style of request
 29  
  * processing. <p/>
 30  
  * GET METHOD will do a request from an endpoint if an endpoint parameter is set otherwise 
 31  
  * it behaves the same way as POST. You can either specify the endpoint URL REST-style, 
 32  
  * e.g., to read from jms://orders.queue <p/>
 33  
  * http://www.mycompany.com/rest/jms/orders/queue <p/> or a logical Mule endpoint name, 
 34  
  * e.g., this would get the first email message received by the orderEmailInbox endpoint. <p/>
 35  
  * http://www.mycompany.com/rest/ordersEmailInbox <p/> 
 36  
  * POST METHOD Do a synchronous call and return a result 
 37  
  * http://www.clientapplication.com/service/clientquery?custId=1234 <p/> 
 38  
  * PUT METHOD Do an asynchronous call without returning a result (other than an http
 39  
  * status code) http://www.clientapplication.com/service/orders?payload=<order>more
 40  
  * beer</order> <p/> 
 41  
  * DELETE METHOD Same as GET only without returning a result
 42  
  */
 43  
 
 44  0
 public class MuleRESTReceiverServlet extends MuleReceiverServlet
 45  
 {
 46  
     /**
 47  
      * Serial version
 48  
      */
 49  
     private static final long serialVersionUID = -2395763805839859649L;
 50  
 
 51  
     @Override
 52  
     protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
 53  
         throws ServletException, IOException
 54  
     {
 55  
         try
 56  
         {
 57  0
             InboundEndpoint endpoint = getEndpointForURI(httpServletRequest);
 58  0
             if (endpoint != null)
 59  
             {
 60  0
                 String timeoutString = httpServletRequest.getParameter("timeout");
 61  0
                 long to = timeout;
 62  
 
 63  0
                 if (timeoutString != null)
 64  
                 {
 65  0
                     to = Long.parseLong(timeoutString);
 66  
                 }
 67  
 
 68  0
                 if (logger.isDebugEnabled())
 69  
                 {
 70  0
                     logger.debug("Making request using endpoint: " + endpoint.toString() + " timeout is: "
 71  
                                  + to);
 72  
                 }
 73  
 
 74  0
                 MuleMessage returnMessage = endpoint.request(to);
 75  0
                 writeResponse(httpServletResponse, returnMessage);
 76  0
             }
 77  
             else
 78  
             {
 79  0
                 MessageReceiver receiver = getReceiverForURI(httpServletRequest);
 80  
             
 81  0
                 httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME, payloadParameterName);
 82  
                 
 83  0
                 MuleMessage message = receiver.createMuleMessage(httpServletRequest);
 84  0
                 MuleEvent event = receiver.routeMessage(message);
 85  0
                 MuleMessage returnMessage = event == null ? null : event.getMessage();
 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  
     @Override
 96  
     protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
 97  
         throws ServletException, IOException
 98  
     {
 99  
         try
 100  
         {
 101  0
             MessageReceiver receiver = getReceiverForURI(httpServletRequest);
 102  
 
 103  0
             httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME, payloadParameterName);
 104  
 
 105  0
             MuleMessage message = receiver.createMuleMessage(httpServletRequest, 
 106  
                 receiver.getEndpoint().getEncoding());
 107  
             
 108  0
             MuleEvent event = receiver.routeMessage(message);
 109  0
             MuleMessage returnMessage = event == null ? null : event.getMessage();
 110  0
             writeResponse(httpServletResponse, returnMessage);
 111  
         }
 112  0
         catch (Exception e)
 113  
         {
 114  0
             handleException(e, "Failed to Post event to Mule", httpServletResponse);
 115  0
         }
 116  0
     }
 117  
 
 118  
     @Override
 119  
     protected void doPut(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
 120  
         throws ServletException, IOException
 121  
     {
 122  
         try
 123  
         {
 124  0
             MessageReceiver receiver = getReceiverForURI(httpServletRequest);
 125  
 
 126  0
             httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME, payloadParameterName);
 127  
 
 128  0
             MuleMessage message = receiver.createMuleMessage(httpServletRequest, 
 129  
                 receiver.getEndpoint().getEncoding());
 130  0
             receiver.routeMessage(message);
 131  
 
 132  0
             httpServletResponse.setStatus(HttpServletResponse.SC_CREATED);
 133  0
             if (feedback)
 134  
             {
 135  0
                 httpServletResponse.getWriter().write(
 136  
                     "Item was created at endpointUri: " + receiver.getEndpointURI());
 137  
             }
 138  
         }
 139  0
         catch (Exception e)
 140  
         {
 141  0
             handleException(e, "Failed to Post event to Mule" + e.getMessage(), httpServletResponse);
 142  0
         }
 143  0
     }
 144  
 
 145  
     @Override
 146  
     protected void doDelete(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
 147  
         throws ServletException, IOException
 148  
     {
 149  
         try
 150  
         {
 151  0
             InboundEndpoint endpoint = getEndpointForURI(httpServletRequest);
 152  0
             String timeoutString = httpServletRequest.getParameter("timeout");
 153  0
             long to = timeout;
 154  
 
 155  0
             if (timeoutString != null)
 156  
             {
 157  0
                 to = new Long(timeoutString).longValue();
 158  
             }
 159  
 
 160  0
             if (logger.isDebugEnabled())
 161  
             {
 162  0
                 logger.debug("Making request using endpoint: " + endpoint.toString() + " timeout is: " + to);
 163  
             }
 164  
 
 165  0
             MuleMessage returnMessage = endpoint.request(to);
 166  0
             if (returnMessage != null)
 167  
             {
 168  0
                 httpServletResponse.setStatus(HttpServletResponse.SC_OK);
 169  
             }
 170  
             else
 171  
             {
 172  0
                 httpServletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
 173  
             }
 174  
         }
 175  0
         catch (Exception e)
 176  
         {
 177  0
             handleException(e, "Failed to Delete mule event via receive using uri: "
 178  
                                + httpServletRequest.getPathInfo(), httpServletResponse);
 179  0
         }
 180  0
     }
 181  
 
 182  
     protected InboundEndpoint getEndpointForURI(HttpServletRequest httpServletRequest)
 183  
         throws MuleException
 184  
     {
 185  0
         String endpointName = httpServletRequest.getParameter("endpoint");
 186  0
         if (endpointName == null)
 187  
         {
 188  
             // Let's try stripping the path and only use the last path element
 189  0
             String uri = httpServletRequest.getPathInfo();
 190  0
             int i = uri.lastIndexOf("/");
 191  0
             if (i > -1)
 192  
             {
 193  0
                 endpointName = uri.substring(i + 1);
 194  
             }
 195  
         }
 196  
 
 197  0
         InboundEndpoint endpoint = muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(endpointName);
 198  0
         if (endpoint == null)
 199  
         {
 200  
             // if we dont find an endpoint for the given name, lets check the
 201  
             // servlet receivers
 202  0
             MessageReceiver receiver = getReceivers().get(endpointName);
 203  
             
 204  0
             if (receiver != null)
 205  
             {
 206  0
                 endpoint = receiver.getEndpoint();
 207  
             }
 208  
         }
 209  0
         return endpoint;
 210  
     }
 211  
 }