Coverage Report - org.mule.transport.servlet.jetty.JettyHttpMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
JettyHttpMessageReceiver
77%
27/35
67%
4/6
2.667
 
 1  
 /*
 2  
  * $Id: JettyHttpMessageReceiver.java 12238 2008-07-06 22:13:50Z rossmason $
 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.transport.servlet.jetty;
 12  
 
 13  
 import org.mule.MuleServer;
 14  
 import org.mule.RegistryContext;
 15  
 import org.mule.api.MuleContext;
 16  
 import org.mule.api.MuleException;
 17  
 import org.mule.api.endpoint.EndpointBuilder;
 18  
 import org.mule.api.endpoint.InboundEndpoint;
 19  
 import org.mule.api.lifecycle.CreateException;
 20  
 import org.mule.api.lifecycle.LifecycleException;
 21  
 import org.mule.api.service.Service;
 22  
 import org.mule.api.transport.Connector;
 23  
 import org.mule.config.i18n.CoreMessages;
 24  
 import org.mule.endpoint.EndpointURIEndpointBuilder;
 25  
 import org.mule.transport.AbstractMessageReceiver;
 26  
 import org.mule.transport.servlet.ServletConnector;
 27  
 import org.mule.transport.servlet.i18n.ServletMessages;
 28  
 import org.mule.util.StringUtils;
 29  
 
 30  
 /**
 31  
  * <code>JettyHttpMessageReceiver</code> is a simple http server that can be used to
 32  
  * listen for http requests on a particular port
 33  
  */
 34  
 public class JettyHttpMessageReceiver extends AbstractMessageReceiver
 35  
 {
 36  
     public static final String JETTY_SERVLET_CONNECTOR_NAME = "_jettyConnector";
 37  
 
 38  
     public JettyHttpMessageReceiver(Connector connector, Service service, InboundEndpoint endpoint)
 39  
             throws CreateException
 40  
     {
 41  
 
 42  42
         super(connector, service, endpoint);
 43  
 
 44  42
         if ("rest".equals(endpoint.getEndpointURI().getScheme()))
 45  
         {
 46  
             // We need to have a Servlet Connector pointing to our servlet so the Servlets can
 47  
             // find the listeners for incoming requests
 48  2
             ServletConnector scon = (ServletConnector) RegistryContext.getRegistry().lookupConnector(JETTY_SERVLET_CONNECTOR_NAME);
 49  2
             if (scon != null)
 50  
             {
 51  0
                 throw new CreateException(
 52  
                         ServletMessages.noServletConnectorFound(JETTY_SERVLET_CONNECTOR_NAME), this);
 53  
             }
 54  
 
 55  2
             scon = new ServletConnector();
 56  2
             scon.setName(JETTY_SERVLET_CONNECTOR_NAME);
 57  2
             scon.setServletUrl(endpoint.getEndpointURI().getAddress());
 58  
             try
 59  
             {
 60  2
                 MuleContext muleContext = MuleServer.getMuleContext();
 61  2
                 scon.setMuleContext(muleContext);
 62  
                 //muleContext.applyLifecycle(scon);
 63  2
                 muleContext.getRegistry().registerConnector(scon);
 64  
 
 65  2
                 String path = endpoint.getEndpointURI().getPath();
 66  2
                 if (StringUtils.isEmpty(path))
 67  
                 {
 68  0
                     path = "/";
 69  
                 }
 70  
 
 71  2
                 EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder("servlet://" + path.substring(1),
 72  
                     connector.getMuleContext());
 73  2
                 endpointBuilder.setTransformers(endpoint.getTransformers());
 74  2
                 InboundEndpoint ep = connector.getMuleContext()
 75  
                     .getRegistry()
 76  
                     .lookupEndpointFactory()
 77  
                     .getInboundEndpoint(endpointBuilder);
 78  2
                 scon.registerListener(service, ep);
 79  
             }
 80  0
             catch (Exception e)
 81  
             {
 82  0
                 throw new CreateException(e, this);
 83  2
             }
 84  
         }
 85  
 
 86  42
     }
 87  
 
 88  
     protected void doConnect() throws Exception
 89  
     {
 90  
 
 91  
 
 92  42
     }
 93  
 
 94  
     protected void doDisconnect() throws Exception
 95  
     {
 96  
 
 97  42
     }
 98  
 
 99  
 
 100  
     /**
 101  
      * Template method to dispose any resources associated with this receiver. There
 102  
      * is not need to dispose the connector as this is already done by the framework
 103  
      */
 104  
     protected void doDispose()
 105  
     {
 106  
         //Do nothing
 107  84
     }
 108  
 
 109  
     protected void doStart() throws MuleException
 110  
     {
 111  
         try
 112  
         {
 113  42
             ((JettyHttpConnector)connector).registerListener(this);
 114  
         }
 115  0
         catch (Exception e)
 116  
         {
 117  0
             throw new LifecycleException(CoreMessages.failedToStart("Jetty Http Receiver"), e, this);
 118  42
         }
 119  42
     }
 120  
 
 121  
     protected void doStop() throws MuleException
 122  
     {
 123  
         try
 124  
         {
 125  42
             ((JettyHttpConnector)connector).unregisterListener(this);
 126  
         }
 127  0
         catch (Exception e)
 128  
         {
 129  0
             throw new LifecycleException(CoreMessages.failedToStop("Jetty Http Receiver"), e, this);
 130  42
         }
 131  42
     }
 132  
 
 133  
 }