KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > servlet > MuleReceiverServlet


1 /*
2  * $Id: MuleReceiverServlet.java 3831 2006-11-07 22:29:59Z aperepel $
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 MuleSource MPL
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.config.i18n.Message;
14 import org.mule.impl.MuleMessage;
15 import org.mule.impl.endpoint.MuleEndpointURI;
16 import org.mule.providers.AbstractMessageReceiver;
17 import org.mule.providers.http.HttpConnector;
18 import org.mule.providers.service.ConnectorFactory;
19 import org.mule.umo.UMOMessage;
20 import org.mule.umo.endpoint.EndpointException;
21 import org.mule.umo.provider.NoReceiverForEndpointException;
22 import org.mule.util.PropertiesUtils;
23 import org.mule.MuleManager;
24
25 import javax.servlet.ServletConfig JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import java.io.IOException JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 /**
35  * Receives Http requests via a Servlet and routes the to listeners with servlet://
36  * endpoints
37  *
38  * There needs to be a ServletConnector configured on the Mule Server, this connector
39  * must have the servletUrl property set that matches the Url for the container that this
40  * Servlet is hosted in, i.e. something like http://192.168.10.21:8888
41  */

42
43 public class MuleReceiverServlet extends AbstractReceiverServlet
44 {
45     /**
46      * Serial version
47      */

48     private static final long serialVersionUID = 6631307373079767439L;
49
50     protected ServletConnector connector = null;
51
52     protected void doInit(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc
53     {
54         String JavaDoc servletConnectorName = servletConfig.getInitParameter(SERVLET_CONNECTOR_NAME_PROPERTY);
55         if(servletConnectorName==null)
56         {
57             connector = (ServletConnector)ConnectorFactory.getConnectorByProtocol("servlet");
58             if (connector == null)
59             {
60                 throw new ServletException JavaDoc(new Message("http", 9).toString());
61             }
62         }
63         else
64         {
65             connector = (ServletConnector)MuleManager.getInstance().lookupConnector(servletConnectorName);
66             if (connector == null)
67             {
68                 throw new ServletException JavaDoc(new Message("http", 10, servletConnectorName).toString());
69             }
70         }
71
72
73     }
74
75     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
76         throws ServletException JavaDoc, IOException JavaDoc
77     {
78         try
79         {
80             AbstractMessageReceiver receiver = getReceiverForURI(request);
81             UMOMessage responseMessage;
82             UMOMessage requestMessage = new MuleMessage(new HttpRequestMessageAdapter(request));
83             requestMessage.setProperty(HttpConnector.HTTP_METHOD_PROPERTY, "GET");
84             responseMessage = receiver.routeMessage(requestMessage, true);
85             writeResponse(response, responseMessage);
86         }
87         catch (Exception JavaDoc e)
88         {
89             handleException(e, e.getMessage(), response);
90         }
91     }
92
93     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
94         throws ServletException JavaDoc, IOException JavaDoc
95     {
96         try
97         {
98             AbstractMessageReceiver receiver = getReceiverForURI(request);
99             UMOMessage responseMessage;
100             UMOMessage requestMessage = new MuleMessage(new HttpRequestMessageAdapter(request));
101             requestMessage.setProperty(HttpConnector.HTTP_METHOD_PROPERTY, "POST");
102             responseMessage = receiver.routeMessage(requestMessage, true);
103             if (responseMessage != null)
104             {
105                 writeResponse(response, responseMessage);
106             }
107         }
108         catch (Exception JavaDoc e)
109         {
110             handleException(e, e.getMessage(), response);
111         }
112     }
113
114     protected AbstractMessageReceiver getReceiverForURI(HttpServletRequest JavaDoc httpServletRequest)
115         throws EndpointException
116     {
117         String JavaDoc uri = getReceiverName(httpServletRequest);
118         if (uri == null)
119         {
120             throw new EndpointException(new Message("http", 4, httpServletRequest.getRequestURI()));
121         }
122         AbstractMessageReceiver receiver = (AbstractMessageReceiver)getReceivers().get(uri);
123         if (receiver == null)
124         {
125             // Nothing found lets try stripping the path and only use the last
126
// path element
127
int i = uri.lastIndexOf("/");
128             if (i > -1)
129             {
130                 uri = uri.substring(i + 1);
131                 receiver = (AbstractMessageReceiver)getReceivers().get(uri);
132             }
133             if (receiver == null)
134             {
135                 throw new NoReceiverForEndpointException("No receiver found for endpointUri: " + uri);
136             }
137         }
138         receiver.getEndpoint().setEndpointURI(new MuleEndpointURI(getRequestUrl(httpServletRequest)));
139         return receiver;
140     }
141
142     protected String JavaDoc getRequestUrl(HttpServletRequest JavaDoc httpServletRequest)
143     {
144         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
145         url.append(connector.getProtocol().toLowerCase());
146         url.append(":");
147         url.append(httpServletRequest.getScheme());
148         url.append("://");
149         url.append(httpServletRequest.getServerName());
150         url.append(":");
151         url.append(httpServletRequest.getServerPort());
152         url.append("/");
153         url.append(getReceiverName(httpServletRequest));
154         if (httpServletRequest.getQueryString() != null)
155         {
156             url.append("?");
157             url.append(httpServletRequest.getQueryString());
158         }
159         return url.toString();
160     }
161
162     protected String JavaDoc getReceiverName(HttpServletRequest JavaDoc httpServletRequest)
163     {
164         String JavaDoc name = httpServletRequest.getPathInfo();
165         if (name == null)
166         {
167             name = httpServletRequest.getServletPath();
168             if (name == null)
169             {
170                 name = httpServletRequest.getParameter("endpoint");
171                 if (name == null)
172                 {
173                     Properties JavaDoc params = PropertiesUtils.getPropertiesFromQueryString(httpServletRequest.getQueryString());
174                     name = params.getProperty("endpoint");
175                     if (name == null)
176                     {
177                         return null;
178                     }
179                 }
180             }
181         }
182         
183         if (name.startsWith("/"))
184         {
185             name = name.substring(1);
186         }
187         return name;
188     }
189
190     protected Map JavaDoc getReceivers()
191     {
192         return connector.getReceivers();
193     }
194 }
195
Popular Tags