1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.servlet; |
8 | |
|
9 | |
import org.mule.RequestContext; |
10 | |
import org.mule.api.MuleContext; |
11 | |
import org.mule.api.MuleMessage; |
12 | |
import org.mule.api.config.MuleProperties; |
13 | |
import org.mule.api.lifecycle.InitialisationException; |
14 | |
import org.mule.api.transformer.TransformerException; |
15 | |
import org.mule.api.transport.OutputHandler; |
16 | |
import org.mule.config.ExceptionHelper; |
17 | |
import org.mule.transport.http.HttpConstants; |
18 | |
import org.mule.transport.http.HttpResponse; |
19 | |
import org.mule.transport.http.transformers.MuleMessageToHttpResponse; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
|
23 | |
import javax.servlet.ServletException; |
24 | |
import javax.servlet.http.HttpServlet; |
25 | |
import javax.servlet.http.HttpServletResponse; |
26 | |
|
27 | |
import org.apache.commons.httpclient.Header; |
28 | |
import org.apache.commons.logging.Log; |
29 | |
import org.apache.commons.logging.LogFactory; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public abstract class AbstractReceiverServlet extends HttpServlet |
37 | |
{ |
38 | |
|
39 | |
|
40 | |
|
41 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
42 | |
|
43 | |
public static final String REQUEST_TIMEOUT_PROPERTY = "org.mule.servlet.timeout"; |
44 | |
public static final String FEEDBACK_PROPERTY = "org.mule.servlet.feedback"; |
45 | |
public static final String DEFAULT_CONTENT_TYPE_PROPERTY = "org.mule.servlet.default.content.type"; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
@Deprecated |
52 | |
public static final String SERVLET_CONNECTOR_NAME_PROPERTY = "org.mule.servlet.connector.name"; |
53 | |
|
54 | |
public static final String PAYLOAD_PARAMETER_NAME = "org.mule.servlet.payload.param"; |
55 | |
public static final String DEFAULT_PAYLOAD_PARAMETER_NAME = "payload"; |
56 | |
|
57 | |
public static final long DEFAULT_GET_TIMEOUT = 10000L; |
58 | |
|
59 | |
protected String payloadParameterName; |
60 | 0 | protected long timeout = DEFAULT_GET_TIMEOUT; |
61 | 0 | protected boolean feedback = true; |
62 | 0 | protected String defaultContentType = HttpConstants.DEFAULT_CONTENT_TYPE; |
63 | |
protected MuleContext muleContext; |
64 | |
|
65 | 0 | private MuleMessageToHttpResponse responseTransformer = new MuleMessageToHttpResponse(); |
66 | |
|
67 | |
@Override |
68 | |
public final void init() throws ServletException |
69 | |
{ |
70 | 0 | String timeoutString = getServletConfig().getInitParameter(REQUEST_TIMEOUT_PROPERTY); |
71 | 0 | if (timeoutString != null) |
72 | |
{ |
73 | 0 | timeout = Long.parseLong(timeoutString); |
74 | |
} |
75 | 0 | if (logger.isInfoEnabled()) |
76 | |
{ |
77 | 0 | logger.info("Default request timeout for GET methods is: " + timeout); |
78 | |
} |
79 | |
|
80 | 0 | String feedbackString = getServletConfig().getInitParameter(FEEDBACK_PROPERTY); |
81 | 0 | if (feedbackString != null) |
82 | |
{ |
83 | 0 | feedback = Boolean.valueOf(feedbackString); |
84 | |
} |
85 | 0 | if (logger.isInfoEnabled()) |
86 | |
{ |
87 | 0 | logger.info("feedback is set to: " + feedback); |
88 | |
} |
89 | |
|
90 | 0 | String ct = getServletConfig().getInitParameter(DEFAULT_CONTENT_TYPE_PROPERTY); |
91 | 0 | if (ct != null) |
92 | |
{ |
93 | 0 | if (logger.isDebugEnabled()) |
94 | |
{ |
95 | 0 | logger.debug("Using default content type configured on the servlet (" + DEFAULT_CONTENT_TYPE_PROPERTY + ") = " + ct); |
96 | |
} |
97 | 0 | defaultContentType = ct; |
98 | |
} |
99 | 0 | if (logger.isInfoEnabled()) |
100 | |
{ |
101 | 0 | logger.info("Default content type is: " + defaultContentType); |
102 | |
} |
103 | |
|
104 | 0 | payloadParameterName = getServletConfig().getInitParameter(PAYLOAD_PARAMETER_NAME); |
105 | 0 | if (payloadParameterName == null) |
106 | |
{ |
107 | 0 | payloadParameterName = DEFAULT_PAYLOAD_PARAMETER_NAME; |
108 | |
} |
109 | 0 | if (logger.isInfoEnabled()) |
110 | |
{ |
111 | 0 | logger.info("Using payload param name: " + payloadParameterName); |
112 | |
} |
113 | |
|
114 | 0 | muleContext = setupMuleContext(); |
115 | 0 | setupResponseTransformer(); |
116 | 0 | doInit(); |
117 | 0 | } |
118 | |
|
119 | |
protected MuleContext setupMuleContext() throws ServletException |
120 | |
{ |
121 | 0 | MuleContext context = (MuleContext) getServletContext().getAttribute(MuleProperties.MULE_CONTEXT_PROPERTY); |
122 | 0 | if (context == null) |
123 | |
{ |
124 | 0 | throw new ServletException("Property " + MuleProperties.MULE_CONTEXT_PROPERTY + " not set on ServletContext"); |
125 | |
} |
126 | 0 | return context; |
127 | |
} |
128 | |
|
129 | |
protected void setupResponseTransformer() throws ServletException |
130 | |
{ |
131 | 0 | responseTransformer.setMuleContext(muleContext); |
132 | |
|
133 | |
try |
134 | |
{ |
135 | 0 | responseTransformer.initialise(); |
136 | |
} |
137 | 0 | catch (InitialisationException e) |
138 | |
{ |
139 | 0 | throw new ServletException(e); |
140 | 0 | } |
141 | 0 | } |
142 | |
|
143 | |
protected void doInit() throws ServletException |
144 | |
{ |
145 | |
|
146 | 0 | } |
147 | |
|
148 | |
protected void writeResponse(HttpServletResponse servletResponse, MuleMessage message) throws Exception |
149 | |
{ |
150 | 0 | if (message == null) |
151 | |
{ |
152 | 0 | writeEmptyResponse(servletResponse); |
153 | |
} |
154 | |
else |
155 | |
{ |
156 | 0 | writeResponseFromMessage(servletResponse, message); |
157 | |
} |
158 | 0 | servletResponse.flushBuffer(); |
159 | 0 | } |
160 | |
|
161 | |
protected void writeEmptyResponse(HttpServletResponse servletResponse) throws IOException |
162 | |
{ |
163 | 0 | servletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT); |
164 | 0 | if (feedback) |
165 | |
{ |
166 | 0 | servletResponse.setStatus(HttpServletResponse.SC_OK); |
167 | 0 | servletResponse.getWriter().write("Action was processed successfully. There was no result"); |
168 | |
} |
169 | 0 | } |
170 | |
|
171 | |
protected void writeResponseFromMessage(HttpServletResponse servletResponse, MuleMessage message) throws Exception |
172 | |
{ |
173 | 0 | HttpResponse httpResponse = convertToHttpResponse(message); |
174 | 0 | setHttpHeadersOnServletResponse(httpResponse, servletResponse); |
175 | |
|
176 | 0 | if (!servletResponse.isCommitted()) |
177 | |
{ |
178 | 0 | servletResponse.setStatus(httpResponse.getStatusCode()); |
179 | |
} |
180 | |
|
181 | 0 | if (httpResponse.hasBody()) |
182 | |
{ |
183 | 0 | OutputHandler outputHandler = httpResponse.getBody(); |
184 | 0 | outputHandler.write(RequestContext.getEvent(), servletResponse.getOutputStream()); |
185 | |
} |
186 | 0 | } |
187 | |
|
188 | |
protected HttpResponse convertToHttpResponse(MuleMessage message) throws TransformerException |
189 | |
{ |
190 | 0 | if (message.getPayload() instanceof HttpResponse) |
191 | |
{ |
192 | 0 | return (HttpResponse) message.getPayload(); |
193 | |
|
194 | |
} |
195 | |
else |
196 | |
{ |
197 | 0 | return (HttpResponse) responseTransformer.transform(message); |
198 | |
} |
199 | |
} |
200 | |
|
201 | |
protected HttpServletResponse setHttpHeadersOnServletResponse(HttpResponse httpResponse, HttpServletResponse servletResponse) |
202 | |
{ |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | 0 | httpResponse.removeHeaders(HttpConstants.HEADER_TRANSFER_ENCODING); |
209 | |
|
210 | 0 | Header[] headers = httpResponse.getHeaders(); |
211 | 0 | for (Header header : headers) |
212 | |
{ |
213 | 0 | servletResponse.addHeader(header.getName(), header.getValue()); |
214 | |
} |
215 | |
|
216 | 0 | ensureContentTypeHeaderIsSet(servletResponse, httpResponse); |
217 | |
|
218 | 0 | return servletResponse; |
219 | |
} |
220 | |
|
221 | |
protected void ensureContentTypeHeaderIsSet(HttpServletResponse servletResponse, HttpResponse httpResponse) |
222 | |
{ |
223 | 0 | Header contentTypeHeader = httpResponse.getFirstHeader(HttpConstants.HEADER_CONTENT_TYPE); |
224 | 0 | String contentType = defaultContentType; |
225 | 0 | if (contentTypeHeaderIsValid(contentTypeHeader)) |
226 | |
{ |
227 | 0 | if (logger.isDebugEnabled()) |
228 | |
{ |
229 | 0 | logger.debug("Using Content-Type from message header = " + contentTypeHeader.getValue()); |
230 | |
} |
231 | 0 | contentType = contentTypeHeader.getValue(); |
232 | |
} |
233 | 0 | servletResponse.setContentType(contentType); |
234 | 0 | } |
235 | |
|
236 | |
protected boolean contentTypeHeaderIsValid(Header header) |
237 | |
{ |
238 | 0 | return (header != null) && (header.getValue() != null); |
239 | |
} |
240 | |
|
241 | |
protected void handleException(Throwable exception, String message, HttpServletResponse response) |
242 | |
{ |
243 | 0 | logger.error("message: " + exception.getMessage(), exception); |
244 | 0 | int code = Integer.valueOf(ExceptionHelper.getErrorMapping("http", exception.getClass())); |
245 | 0 | response.setStatus(code); |
246 | |
try |
247 | |
{ |
248 | 0 | response.sendError(code, message + ": " + exception.getMessage()); |
249 | |
} |
250 | 0 | catch (IOException e) |
251 | |
{ |
252 | 0 | logger.error("Failed to sendError on response: " + e.getMessage(), e); |
253 | 0 | } |
254 | 0 | } |
255 | |
} |