001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.http4;
018
019 import java.io.File;
020 import java.io.IOException;
021 import java.io.InputStream;
022 import java.io.PrintWriter;
023 import java.io.UnsupportedEncodingException;
024 import java.net.URLDecoder;
025 import java.util.Enumeration;
026 import java.util.Map;
027
028 import javax.activation.DataHandler;
029 import javax.activation.FileDataSource;
030 import javax.activation.FileTypeMap;
031 import javax.servlet.ServletOutputStream;
032 import javax.servlet.http.HttpServletRequest;
033 import javax.servlet.http.HttpServletResponse;
034
035 import org.apache.camel.Endpoint;
036 import org.apache.camel.Exchange;
037 import org.apache.camel.InvalidPayloadException;
038 import org.apache.camel.Message;
039 import org.apache.camel.StreamCache;
040 import org.apache.camel.component.http4.helper.CamelFileDataSource;
041 import org.apache.camel.component.http4.helper.GZIPHelper;
042 import org.apache.camel.converter.stream.CachedOutputStream;
043 import org.apache.camel.spi.HeaderFilterStrategy;
044 import org.apache.camel.util.IOHelper;
045 import org.apache.camel.util.MessageHelper;
046 import org.apache.camel.util.ObjectHelper;
047
048 /**
049 * Binding between {@link HttpMessage} and {@link HttpServletResponse}.
050 *
051 * @version $Revision: 966540 $
052 */
053 public class DefaultHttpBinding implements HttpBinding {
054
055 private boolean useReaderForPayload;
056 private HeaderFilterStrategy headerFilterStrategy = new HttpHeaderFilterStrategy();
057
058 public DefaultHttpBinding() {
059 }
060
061 public DefaultHttpBinding(HeaderFilterStrategy headerFilterStrategy) {
062 this.headerFilterStrategy = headerFilterStrategy;
063 }
064
065 public void readRequest(HttpServletRequest request, HttpMessage message) {
066
067 // lets force a parse of the body and headers
068 message.getBody();
069 // populate the headers from the request
070 Map<String, Object> headers = message.getHeaders();
071
072 //apply the headerFilterStrategy
073 Enumeration names = request.getHeaderNames();
074 while (names.hasMoreElements()) {
075 String name = (String)names.nextElement();
076 Object value = request.getHeader(name);
077 // mapping the content-type
078 if (name.toLowerCase().equals("content-type")) {
079 name = Exchange.CONTENT_TYPE;
080 }
081 if (headerFilterStrategy != null
082 && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, message.getExchange())) {
083 headers.put(name, value);
084 }
085 }
086
087 if (request.getCharacterEncoding() != null) {
088 headers.put(Exchange.HTTP_CHARACTER_ENCODING, request.getCharacterEncoding());
089 message.getExchange().setProperty(Exchange.CHARSET_NAME, request.getCharacterEncoding());
090 }
091
092 popluateRequestParameters(request, message);
093 // reset the stream cache
094 StreamCache cache = message.getBody(StreamCache.class);
095 if (cache != null) {
096 cache.reset();
097 }
098
099 // store the method and query and other info in headers
100 headers.put(Exchange.HTTP_METHOD, request.getMethod());
101 headers.put(Exchange.HTTP_QUERY, request.getQueryString());
102 headers.put(Exchange.HTTP_URL, request.getRequestURL());
103 headers.put(Exchange.HTTP_URI, request.getRequestURI());
104 headers.put(Exchange.HTTP_PATH, request.getPathInfo());
105 headers.put(Exchange.CONTENT_TYPE, request.getContentType());
106
107 popluateAttachments(request, message);
108 }
109
110 protected void popluateRequestParameters(HttpServletRequest request, HttpMessage message) {
111 //we populate the http request parameters without checking the request method
112 Map<String, Object> headers = message.getHeaders();
113 Enumeration names = request.getParameterNames();
114 while (names.hasMoreElements()) {
115 String name = (String)names.nextElement();
116 Object value = request.getParameter(name);
117 if (headerFilterStrategy != null
118 && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, message.getExchange())) {
119 headers.put(name, value);
120 }
121 }
122 if (request.getMethod().equals("POST") && request.getContentType() != null && request.getContentType().startsWith("application/x-www-form-urlencoded")) {
123 String charset = request.getCharacterEncoding();
124 if (charset == null) {
125 charset = "UTF-8";
126 }
127 // Push POST form params into the headers to retain compatibility with DefaultHttpBinding
128 String body = message.getBody(String.class);
129 try {
130 for (String param : body.split("&")) {
131 String[] pair = param.split("=", 2);
132 String name = URLDecoder.decode(pair[0], charset);
133 String value = URLDecoder.decode(pair[1], charset);
134 if (headerFilterStrategy != null
135 && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, message.getExchange())) {
136 headers.put(name, value);
137 }
138 }
139 } catch (UnsupportedEncodingException e) {
140 throw new RuntimeException(e);
141 }
142 }
143 }
144
145 protected void popluateAttachments(HttpServletRequest request, HttpMessage message) {
146 // check if there is multipart files, if so will put it into DataHandler
147 Enumeration names = request.getAttributeNames();
148 while (names.hasMoreElements()) {
149 String name = (String) names.nextElement();
150 Object object = request.getAttribute(name);
151 if (object instanceof File) {
152 String fileName = request.getParameter(name);
153 message.addAttachment(fileName, new DataHandler(new CamelFileDataSource((File)object, fileName)));
154 }
155 }
156 }
157
158 public void writeResponse(Exchange exchange, HttpServletResponse response) throws IOException {
159 if (exchange.isFailed()) {
160 if (exchange.getException() != null) {
161 doWriteExceptionResponse(exchange.getException(), response);
162 } else {
163 // it must be a fault, no need to check for the fault flag on the message
164 doWriteFaultResponse(exchange.getOut(), response, exchange);
165 }
166 } else {
167 // just copy the protocol relates header
168 copyProtocolHeaders(exchange.getIn(), exchange.getOut());
169 Message out = exchange.getOut();
170 if (out != null) {
171 doWriteResponse(out, response, exchange);
172 }
173 }
174 }
175
176 private void copyProtocolHeaders(Message request, Message response) {
177 if (request.getHeader(Exchange.CONTENT_ENCODING) != null) {
178 String contentEncoding = request.getHeader(Exchange.CONTENT_ENCODING, String.class);
179 response.setHeader(Exchange.CONTENT_ENCODING, contentEncoding);
180 }
181 if (checkChunked(response, response.getExchange())) {
182 response.setHeader(Exchange.TRANSFER_ENCODING, "chunked");
183 }
184 }
185
186 public void doWriteExceptionResponse(Throwable exception, HttpServletResponse response) throws IOException {
187 response.setStatus(500); // 500 for internal server error
188 response.setContentType("text/plain");
189
190 // append the stacktrace as response
191 PrintWriter pw = response.getWriter();
192 exception.printStackTrace(pw);
193
194 pw.flush();
195 }
196
197 public void doWriteFaultResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
198 doWriteResponse(message, response, exchange);
199 }
200
201 public void doWriteResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
202 // set the status code in the response. Default is 200.
203 if (message.getHeader(Exchange.HTTP_RESPONSE_CODE) != null) {
204 int code = message.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
205 response.setStatus(code);
206 }
207 // set the content type in the response.
208 String contentType = MessageHelper.getContentType(message);
209 if (MessageHelper.getContentType(message) != null) {
210 response.setContentType(contentType);
211 }
212
213 // append headers
214 for (String key : message.getHeaders().keySet()) {
215 String value = message.getHeader(key, String.class);
216 if (headerFilterStrategy != null
217 && !headerFilterStrategy.applyFilterToCamelHeaders(key, value, exchange)) {
218 response.setHeader(key, value);
219 }
220 }
221
222 // write the body.
223 if (message.getBody() != null) {
224 if (GZIPHelper.isGzip(message)) {
225 doWriteGZIPResponse(message, response, exchange);
226 } else {
227 doWriteDirectResponse(message, response, exchange);
228 }
229 }
230 }
231
232 protected void doWriteDirectResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
233 InputStream is = null;
234 if (checkChunked(message, exchange)) {
235 is = message.getBody(InputStream.class);
236 }
237 if (is != null) {
238 ServletOutputStream os = response.getOutputStream();
239 try {
240 // copy directly from input stream to output stream
241 IOHelper.copy(is, os);
242 } finally {
243 try {
244 os.close();
245 } catch (Exception e) {
246 // ignore, maybe client have disconnected or timed out
247 }
248 try {
249 is.close();
250 } catch (Exception e) {
251 // ignore, maybe client have disconnected or timed out
252 }
253 }
254 } else {
255 // not convertable as a stream so try as a String
256 String data = message.getBody(String.class);
257 if (data != null) {
258 // set content length before we write data
259 response.setContentLength(data.length());
260 response.getWriter().print(data);
261 response.getWriter().flush();
262 }
263 }
264 }
265
266 protected boolean checkChunked(Message message, Exchange exchange) {
267 boolean answer = true;
268 if (message.getHeader(Exchange.HTTP_CHUNKED) == null) {
269 // check the endpoint option
270 Endpoint endpoint = exchange.getFromEndpoint();
271 if (endpoint instanceof HttpEndpoint) {
272 answer = ((HttpEndpoint) endpoint).isChunked();
273 }
274 } else {
275 answer = message.getHeader(Exchange.HTTP_CHUNKED, boolean.class);
276 }
277 return answer;
278 }
279
280 protected void doWriteGZIPResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
281 byte[] bytes;
282 try {
283 bytes = message.getMandatoryBody(byte[].class);
284 } catch (InvalidPayloadException e) {
285 throw ObjectHelper.wrapRuntimeCamelException(e);
286 }
287
288 byte[] data = GZIPHelper.compressGZIP(bytes);
289 ServletOutputStream os = response.getOutputStream();
290 try {
291 response.setContentLength(data.length);
292 os.write(data);
293 os.flush();
294 } finally {
295 os.close();
296 }
297 }
298
299 public Object parseBody(HttpMessage httpMessage) throws IOException {
300 // lets assume the body is a reader
301 HttpServletRequest request = httpMessage.getRequest();
302 // Need to handle the GET Method which has no inputStream
303 if ("GET".equals(request.getMethod())) {
304 return null;
305 }
306 if (isUseReaderForPayload()) {
307 return request.getReader();
308 } else {
309 // otherwise use input stream and we need to cache it first
310 InputStream is = HttpConverter.toInputStream(request, httpMessage.getExchange());
311 if (is == null) {
312 return is;
313 }
314 // convert the input stream to StreamCache if the stream cache is not disabled
315 if (httpMessage.getExchange().getProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.FALSE, Boolean.class)) {
316 return is;
317 } else {
318 try {
319 CachedOutputStream cos = new CachedOutputStream(httpMessage.getExchange());
320 IOHelper.copy(is, cos);
321 return cos.getStreamCache();
322 } finally {
323 is.close();
324 }
325 }
326 }
327 }
328
329 public boolean isUseReaderForPayload() {
330 return useReaderForPayload;
331 }
332
333 public void setUseReaderForPayload(boolean useReaderForPayload) {
334 this.useReaderForPayload = useReaderForPayload;
335 }
336
337 public HeaderFilterStrategy getHeaderFilterStrategy() {
338 return headerFilterStrategy;
339 }
340
341 public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
342 this.headerFilterStrategy = headerFilterStrategy;
343 }
344
345 }