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