1 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 ; 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServlet ; 23 import javax.servlet.http.HttpServletResponse ; 24 import java.io.ByteArrayInputStream ; 25 import java.io.IOException ; 26 27 31 32 public abstract class AbstractReceiverServlet extends HttpServlet 33 { 34 37 protected transient Log logger = LogFactory.getLog(getClass()); 38 39 public static final String REQUEST_TIMEOUT_PROPERTY = "org.mule.servlet.timeout"; 40 public static final String FEEDBACK_PROPERTY = "org.mule.servlet.feedback"; 41 public static final String DEFAULT_CONTENT_TYPE_PROPERTY = "org.mule.servlet.default.content.type"; 42 43 44 public static final String SERVLET_CONNECTOR_NAME_PROPERTY = "org.mule.servlet.connector.name"; 45 46 public static final String PAYLOAD_PARAMETER_NAME = "org.mule.servlet.payload.param"; 47 public static final String DEFAULT_PAYLOAD_PARAMETER_NAME = "payload"; 48 49 public static final long DEFAULT_GET_TIMEOUT = 10000L; 50 51 protected String payloadParameterName; 52 protected long timeout = DEFAULT_GET_TIMEOUT; 53 protected boolean feedback = true; 54 protected String defaultContentType = HttpConstants.DEFAULT_CONTENT_TYPE; 55 56 public final void init() throws ServletException 57 { 58 doInit(); 59 } 60 61 public final void init(ServletConfig servletConfig) throws ServletException 62 { 63 String 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 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 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 servletConfig) throws ServletException 95 { 96 } 98 99 protected void doInit() throws ServletException 100 { 101 } 103 104 protected void writeResponse(HttpServletResponse servletResponse, UMOMessage message) throws Exception 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 (message.getAdapter().getPayloadAsBytes())); 127 String 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 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 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 exception, String message, HttpServletResponse 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 e) 175 { 176 logger.error("Failed to sendError on response: " + e.getMessage(), e); 177 } 178 } 179 } 180 | Popular Tags |