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