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