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