001: /*
002: * $Id: AbstractReceiverServlet.java 11186 2008-03-05 23:11:10Z dandiep $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.http.servlet;
012:
013: import org.mule.RequestContext;
014: import org.mule.api.MuleMessage;
015: import org.mule.api.transport.OutputHandler;
016: import org.mule.transport.http.HttpConnector;
017: import org.mule.transport.http.HttpConstants;
018: import org.mule.transport.http.HttpResponse;
019:
020: import java.io.IOException;
021:
022: import javax.servlet.ServletConfig;
023: import javax.servlet.ServletException;
024: import javax.servlet.http.HttpServlet;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.commons.httpclient.Header;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: /**
032: * A base servlet used to receive requests from a servlet container and route
033: * them into Mule
034: */
035:
036: public abstract class AbstractReceiverServlet extends HttpServlet {
037: /**
038: * logger used by this class
039: */
040: protected transient Log logger = LogFactory.getLog(getClass());
041:
042: public static final String REQUEST_TIMEOUT_PROPERTY = "org.mule.servlet.timeout";
043: public static final String FEEDBACK_PROPERTY = "org.mule.servlet.feedback";
044: public static final String DEFAULT_CONTENT_TYPE_PROPERTY = "org.mule.servlet.default.content.type";
045:
046: /** The name of the servlet connector to use with this Servlet */
047: public static final String SERVLET_CONNECTOR_NAME_PROPERTY = "org.mule.servlet.connector.name";
048:
049: public static final String PAYLOAD_PARAMETER_NAME = "org.mule.servlet.payload.param";
050: public static final String DEFAULT_PAYLOAD_PARAMETER_NAME = "payload";
051:
052: public static final long DEFAULT_GET_TIMEOUT = 10000L;
053:
054: protected String payloadParameterName;
055: protected long timeout = DEFAULT_GET_TIMEOUT;
056: protected boolean feedback = true;
057: protected String defaultContentType = HttpConstants.DEFAULT_CONTENT_TYPE;
058:
059: public final void init() throws ServletException {
060: doInit();
061: }
062:
063: public final void init(ServletConfig servletConfig)
064: throws ServletException {
065: String timeoutString = servletConfig
066: .getInitParameter(REQUEST_TIMEOUT_PROPERTY);
067: if (timeoutString != null) {
068: timeout = Long.parseLong(timeoutString);
069: }
070: logger.info("Default request timeout for GET methods is: "
071: + timeout);
072:
073: String feedbackString = servletConfig
074: .getInitParameter(FEEDBACK_PROPERTY);
075: if (feedbackString != null) {
076: feedback = Boolean.valueOf(feedbackString).booleanValue();
077: }
078: logger.info("feedback is set to: " + feedback);
079:
080: String ct = servletConfig
081: .getInitParameter(DEFAULT_CONTENT_TYPE_PROPERTY);
082: if (ct != null) {
083: defaultContentType = ct;
084: }
085: logger.info("Default content type is: " + defaultContentType);
086:
087: payloadParameterName = servletConfig
088: .getInitParameter(PAYLOAD_PARAMETER_NAME);
089: if (payloadParameterName == null) {
090: payloadParameterName = DEFAULT_PAYLOAD_PARAMETER_NAME;
091: }
092: logger
093: .info("Using payload param name: "
094: + payloadParameterName);
095:
096: doInit(servletConfig);
097: }
098:
099: protected void doInit(ServletConfig servletConfig)
100: throws ServletException {
101: // nothing to do
102: }
103:
104: protected void doInit() throws ServletException {
105: // nothing to do
106: }
107:
108: protected void writeResponse(HttpServletResponse servletResponse,
109: MuleMessage message) throws Exception {
110: if (message == null) {
111: servletResponse
112: .setStatus(HttpServletResponse.SC_NO_CONTENT);
113: if (feedback) {
114: servletResponse.setStatus(HttpServletResponse.SC_OK);
115: servletResponse
116: .getWriter()
117: .write(
118: "Action was processed successfully. There was no result");
119: }
120: } else {
121: HttpResponse httpResponse;
122:
123: if (message.getPayload() instanceof HttpResponse) {
124: httpResponse = (HttpResponse) message.getPayload();
125: } else {
126: httpResponse = new HttpResponse();
127: String ct = message.getStringProperty(
128: HttpConstants.HEADER_CONTENT_TYPE, null);
129: if (ct != null) {
130: httpResponse.setHeader(new Header(
131: HttpConstants.HEADER_CONTENT_TYPE, ct));
132: }
133: httpResponse.setStatusLine(httpResponse
134: .getHttpVersion(), message.getIntProperty(
135: HttpConnector.HTTP_STATUS_PROPERTY,
136: HttpServletResponse.SC_OK));
137: httpResponse.setBody(message);
138:
139: }
140:
141: Header contentTypeHeader = httpResponse
142: .getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
143: String contentType = null;
144: if (contentTypeHeader == null) {
145: contentType = defaultContentType;
146: } else {
147: contentType = contentTypeHeader.getValue();
148: }
149:
150: if (!servletResponse.isCommitted()) {
151: servletResponse.setStatus(httpResponse.getStatusCode());
152: }
153: if (!contentType.startsWith("text")) {
154: servletResponse.setContentType(contentType);
155: OutputHandler outputHandler = httpResponse.getBody();
156:
157: outputHandler.write(RequestContext.getEvent(),
158: servletResponse.getOutputStream());
159: } else {
160: servletResponse.setContentType(contentType);
161: // Encoding: this method will check the charset on the content type
162: servletResponse.getWriter().write(
163: httpResponse.getBodyAsString());
164: }
165: }
166: servletResponse.flushBuffer();
167: }
168:
169: protected void handleException(Throwable exception, String message,
170: HttpServletResponse response) {
171: logger.error("message: " + exception.getMessage(), exception);
172: response
173: .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
174: try {
175: response.sendError(
176: HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
177: message + ": " + exception.getMessage());
178: } catch (IOException e) {
179: logger.error("Failed to sendError on response: "
180: + e.getMessage(), e);
181: }
182: }
183: }
|