KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: AbstractReceiverServlet.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.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.apache.commons.httpclient.Header;
16 import org.mule.providers.http.HttpConstants;
17 import org.mule.providers.http.HttpResponse;
18 import org.mule.umo.UMOMessage;
19
20 import javax.servlet.ServletConfig JavaDoc;
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServlet JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28  * A base servlet used to receive requests from a servlet container and route
29  * them into Mule
30  */

31
32 public abstract class AbstractReceiverServlet extends HttpServlet JavaDoc
33 {
34     /**
35      * logger used by this class
36      */

37     protected transient Log logger = LogFactory.getLog(getClass());
38
39     public static final String JavaDoc REQUEST_TIMEOUT_PROPERTY = "org.mule.servlet.timeout";
40     public static final String JavaDoc FEEDBACK_PROPERTY = "org.mule.servlet.feedback";
41     public static final String JavaDoc DEFAULT_CONTENT_TYPE_PROPERTY = "org.mule.servlet.default.content.type";
42
43     /** The name of the servlet connector to use with this Servlet */
44     public static final String JavaDoc SERVLET_CONNECTOR_NAME_PROPERTY = "org.mule.servlet.connector.name";
45
46     public static final String JavaDoc PAYLOAD_PARAMETER_NAME = "org.mule.servlet.payload.param";
47     public static final String JavaDoc DEFAULT_PAYLOAD_PARAMETER_NAME = "payload";
48
49     public static final long DEFAULT_GET_TIMEOUT = 10000L;
50
51     protected String JavaDoc payloadParameterName;
52     protected long timeout = DEFAULT_GET_TIMEOUT;
53     protected boolean feedback = true;
54     protected String JavaDoc defaultContentType = HttpConstants.DEFAULT_CONTENT_TYPE;
55
56     public final void init() throws ServletException JavaDoc
57     {
58         doInit();
59     }
60
61     public final void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc
62     {
63         String JavaDoc timeoutString = servletConfig.getInitParameter(REQUEST_TIMEOUT_PROPERTY);
64         if (timeoutString != null)
65         {
66             timeout = Long.parseLong(timeoutString);
67         }
68         logger.info("Default request timeout for GET methods is: " + timeout);
69
70         String JavaDoc feedbackString = servletConfig.getInitParameter(FEEDBACK_PROPERTY);
71         if (feedbackString != null)
72         {
73             feedback = Boolean.valueOf(feedbackString).booleanValue();
74         }
75         logger.info("feedback is set to: " + feedback);
76
77         String JavaDoc ct = servletConfig.getInitParameter(DEFAULT_CONTENT_TYPE_PROPERTY);
78         if (ct != null)
79         {
80             defaultContentType = ct;
81         }
82         logger.info("Default content type is: " + defaultContentType);
83
84         payloadParameterName = servletConfig.getInitParameter(PAYLOAD_PARAMETER_NAME);
85         if (payloadParameterName == null)
86         {
87             payloadParameterName = DEFAULT_PAYLOAD_PARAMETER_NAME;
88         }
89         logger.info("Using payload param name: " + payloadParameterName);
90
91         doInit(servletConfig);
92     }
93
94     protected void doInit(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc
95     {
96         // nothing to do
97
}
98
99     protected void doInit() throws ServletException JavaDoc
100     {
101         // nothing to do
102
}
103
104     protected void writeResponse(HttpServletResponse JavaDoc servletResponse, UMOMessage message) throws Exception JavaDoc
105     {
106         if (message == null)
107         {
108             servletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
109             if (feedback)
110             {
111                 servletResponse.setStatus(HttpServletResponse.SC_OK);
112                 servletResponse.getWriter().write("Action was processed successfully. There was no result");
113             }
114         }
115         else
116         {
117             HttpResponse httpResponse;
118
119             if (message.getPayload() instanceof HttpResponse)
120             {
121                 httpResponse = (HttpResponse)message.getPayload();
122             }
123             else
124             {
125                 httpResponse = new HttpResponse();
126                 httpResponse.setBody(new ByteArrayInputStream JavaDoc(message.getAdapter().getPayloadAsBytes()));
127                 String JavaDoc ct = message.getStringProperty(HttpConstants.HEADER_CONTENT_TYPE, null);
128                 if(ct!=null)
129                 {
130                     httpResponse.setHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, ct));
131                 }
132             }
133
134             Header contentTypeHeader = httpResponse.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
135             String JavaDoc contentType = null;
136             if (contentTypeHeader == null)
137             {
138                 contentType = defaultContentType;
139             }
140             else
141             {
142                 contentType = contentTypeHeader.getValue();
143             }
144
145             if (!contentType.startsWith("text"))
146             {
147                 servletResponse.setContentType(contentType);
148                 servletResponse.getOutputStream().write(httpResponse.getBodyBytes());
149             }
150             else
151             {
152
153                 servletResponse.setContentType(contentType);
154                 // Encoding: this method will check the charset on the content type
155
servletResponse.getWriter().write(httpResponse.getBodyString());
156             }
157             if (!servletResponse.isCommitted())
158             {
159                 servletResponse.setStatus(httpResponse.getStatusCode());
160             }
161         }
162         servletResponse.flushBuffer();
163     }
164
165     protected void handleException(Throwable JavaDoc exception, String JavaDoc message, HttpServletResponse JavaDoc response)
166     {
167         logger.error("message: " + exception.getMessage(), exception);
168         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
169         try
170         {
171             response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message + ": "
172                                                                              + exception.getMessage());
173         }
174         catch (IOException JavaDoc e)
175         {
176             logger.error("Failed to sendError on response: " + e.getMessage(), e);
177         }
178     }
179 }
180
Popular Tags