View Javadoc

1   /*
2    * $Id: AbstractReceiverServlet.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.providers.http.servlet;
12  
13  import org.mule.providers.http.HttpConstants;
14  import org.mule.providers.http.HttpResponse;
15  import org.mule.umo.UMOMessage;
16  
17  import java.io.ByteArrayInputStream;
18  import java.io.IOException;
19  
20  import javax.servlet.ServletConfig;
21  import javax.servlet.ServletException;
22  import javax.servlet.http.HttpServlet;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.commons.httpclient.Header;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * A base servlet used to receive requests from a servlet container and route
31   * them into Mule
32   */
33  
34  public abstract class AbstractReceiverServlet extends HttpServlet
35  {
36      /**
37       * logger used by this class
38       */
39      protected transient Log logger = LogFactory.getLog(getClass());
40  
41      public static final String REQUEST_TIMEOUT_PROPERTY = "org.mule.servlet.timeout";
42      public static final String FEEDBACK_PROPERTY = "org.mule.servlet.feedback";
43      public static final String DEFAULT_CONTENT_TYPE_PROPERTY = "org.mule.servlet.default.content.type";
44  
45      /** The name of the servlet connector to use with this Servlet */
46      public static final String SERVLET_CONNECTOR_NAME_PROPERTY = "org.mule.servlet.connector.name";
47  
48      public static final String PAYLOAD_PARAMETER_NAME = "org.mule.servlet.payload.param";
49      public static final String DEFAULT_PAYLOAD_PARAMETER_NAME = "payload";
50  
51      public static final long DEFAULT_GET_TIMEOUT = 10000L;
52  
53      protected String payloadParameterName;
54      protected long timeout = DEFAULT_GET_TIMEOUT;
55      protected boolean feedback = true;
56      protected String defaultContentType = HttpConstants.DEFAULT_CONTENT_TYPE;
57  
58      public final void init() throws ServletException
59      {
60          doInit();
61      }
62  
63      public final void init(ServletConfig servletConfig) throws ServletException
64      {
65          String timeoutString = servletConfig.getInitParameter(REQUEST_TIMEOUT_PROPERTY);
66          if (timeoutString != null)
67          {
68              timeout = Long.parseLong(timeoutString);
69          }
70          logger.info("Default request timeout for GET methods is: " + timeout);
71  
72          String feedbackString = servletConfig.getInitParameter(FEEDBACK_PROPERTY);
73          if (feedbackString != null)
74          {
75              feedback = Boolean.valueOf(feedbackString).booleanValue();
76          }
77          logger.info("feedback is set to: " + feedback);
78  
79          String ct = servletConfig.getInitParameter(DEFAULT_CONTENT_TYPE_PROPERTY);
80          if (ct != null)
81          {
82              defaultContentType = ct;
83          }
84          logger.info("Default content type is: " + defaultContentType);
85  
86          payloadParameterName = servletConfig.getInitParameter(PAYLOAD_PARAMETER_NAME);
87          if (payloadParameterName == null)
88          {
89              payloadParameterName = DEFAULT_PAYLOAD_PARAMETER_NAME;
90          }
91          logger.info("Using payload param name: " + payloadParameterName);
92  
93          doInit(servletConfig);
94      }
95  
96      protected void doInit(ServletConfig servletConfig) throws ServletException
97      {
98          // nothing to do
99      }
100 
101     protected void doInit() throws ServletException
102     {
103         // nothing to do
104     }
105 
106     protected void writeResponse(HttpServletResponse servletResponse, UMOMessage message) throws Exception
107     {
108         if (message == null)
109         {
110             servletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
111             if (feedback)
112             {
113                 servletResponse.setStatus(HttpServletResponse.SC_OK);
114                 servletResponse.getWriter().write("Action was processed successfully. There was no result");
115             }
116         }
117         else
118         {
119             HttpResponse httpResponse;
120 
121             if (message.getPayload() instanceof HttpResponse)
122             {
123                 httpResponse = (HttpResponse)message.getPayload();
124             }
125             else
126             {
127                 httpResponse = new HttpResponse();
128                 httpResponse.setBody(new ByteArrayInputStream(message.getAdapter().getPayloadAsBytes()));
129                 String ct = message.getStringProperty(HttpConstants.HEADER_CONTENT_TYPE, null);
130                 if(ct!=null)
131                 {
132                     httpResponse.setHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, ct));
133                 }
134             }
135 
136             Header contentTypeHeader = httpResponse.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE);
137             String contentType = null;
138             if (contentTypeHeader == null)
139             {
140                 contentType = defaultContentType;
141             }
142             else
143             {
144                 contentType = contentTypeHeader.getValue();
145             }
146 
147             if (!contentType.startsWith("text"))
148             {
149                 servletResponse.setContentType(contentType);
150                 servletResponse.getOutputStream().write(httpResponse.getBodyBytes());
151             }
152             else
153             {
154 
155                 servletResponse.setContentType(contentType);
156                 // Encoding: this method will check the charset on the content type
157                 servletResponse.getWriter().write(httpResponse.getBodyString());
158             }
159             if (!servletResponse.isCommitted())
160             {
161                 servletResponse.setStatus(httpResponse.getStatusCode());
162             }
163         }
164         servletResponse.flushBuffer();
165     }
166 
167     protected void handleException(Throwable exception, String message, HttpServletResponse response)
168     {
169         logger.error("message: " + exception.getMessage(), exception);
170         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
171         try
172         {
173             response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message + ": "
174                                                                              + exception.getMessage());
175         }
176         catch (IOException e)
177         {
178             logger.error("Failed to sendError on response: " + e.getMessage(), e);
179         }
180     }
181 }