View Javadoc

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          super(connector, service, endpoint);
43  
44          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              ServletConnector scon = (ServletConnector) RegistryContext.getRegistry().lookupConnector(JETTY_SERVLET_CONNECTOR_NAME);
49              if (scon != null)
50              {
51                  throw new CreateException(
52                          ServletMessages.noServletConnectorFound(JETTY_SERVLET_CONNECTOR_NAME), this);
53              }
54  
55              scon = new ServletConnector();
56              scon.setName(JETTY_SERVLET_CONNECTOR_NAME);
57              scon.setServletUrl(endpoint.getEndpointURI().getAddress());
58              try
59              {
60                  MuleContext muleContext = MuleServer.getMuleContext();
61                  scon.setMuleContext(muleContext);
62                  //muleContext.applyLifecycle(scon);
63                  muleContext.getRegistry().registerConnector(scon);
64  
65                  String path = endpoint.getEndpointURI().getPath();
66                  if (StringUtils.isEmpty(path))
67                  {
68                      path = "/";
69                  }
70  
71                  EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder("servlet://" + path.substring(1),
72                      connector.getMuleContext());
73                  endpointBuilder.setTransformers(endpoint.getTransformers());
74                  InboundEndpoint ep = connector.getMuleContext()
75                      .getRegistry()
76                      .lookupEndpointFactory()
77                      .getInboundEndpoint(endpointBuilder);
78                  scon.registerListener(service, ep);
79              }
80              catch (Exception e)
81              {
82                  throw new CreateException(e, this);
83              }
84          }
85  
86      }
87  
88      protected void doConnect() throws Exception
89      {
90  
91  
92      }
93  
94      protected void doDisconnect() throws Exception
95      {
96  
97      }
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     }
108 
109     protected void doStart() throws MuleException
110     {
111         try
112         {
113             ((JettyHttpConnector)connector).registerListener(this);
114         }
115         catch (Exception e)
116         {
117             throw new LifecycleException(CoreMessages.failedToStart("Jetty Http Receiver"), e, this);
118         }
119     }
120 
121     protected void doStop() throws MuleException
122     {
123         try
124         {
125             ((JettyHttpConnector)connector).unregisterListener(this);
126         }
127         catch (Exception e)
128         {
129             throw new LifecycleException(CoreMessages.failedToStop("Jetty Http Receiver"), e, this);
130         }
131     }
132 
133 }