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.http;
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.http.helper.GZIPHelper;
041    import org.apache.camel.converter.stream.CachedOutputStream;
042    import org.apache.camel.spi.HeaderFilterStrategy;
043    import org.apache.camel.util.IOHelper;
044    import org.apache.camel.util.MessageHelper;
045    import org.apache.camel.util.ObjectHelper;
046    
047    /**
048     * Binding between {@link HttpMessage} and {@link HttpServletResponse}.
049     *
050     * @version $Revision: 946605 $
051     */
052    public class DefaultHttpBinding implements HttpBinding {
053    
054        private boolean useReaderForPayload;
055        private HeaderFilterStrategy headerFilterStrategy = new HttpHeaderFilterStrategy();
056    
057        public DefaultHttpBinding() {
058        }
059    
060        public DefaultHttpBinding(HeaderFilterStrategy headerFilterStrategy) {
061            this.headerFilterStrategy = headerFilterStrategy;
062        }
063    
064        public void readRequest(HttpServletRequest request, HttpMessage message) {
065            
066            // lets force a parse of the body and headers
067            message.getBody();
068            // populate the headers from the request
069            Map<String, Object> headers = message.getHeaders();
070            
071            //apply the headerFilterStrategy
072            Enumeration names = request.getHeaderNames();
073            while (names.hasMoreElements()) {
074                String name = (String)names.nextElement();
075                Object value = request.getHeader(name);
076                // mapping the content-type 
077                if (name.toLowerCase().equals("content-type")) {
078                    name = Exchange.CONTENT_TYPE;
079                }
080                if (headerFilterStrategy != null
081                    && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, message.getExchange())) {
082                    headers.put(name, value);
083                }
084            }
085                    
086            if (request.getCharacterEncoding() != null) {
087                headers.put(Exchange.HTTP_CHARACTER_ENCODING, request.getCharacterEncoding());
088                message.getExchange().setProperty(Exchange.CHARSET_NAME, request.getCharacterEncoding());
089            }
090    
091            popluateRequestParameters(request, message);        
092            
093            Object body = message.getBody();
094            // reset the stream cache if the body is the instance of StreamCache
095            if (body instanceof StreamCache) {
096                ((StreamCache)body).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            
123            if (request.getMethod().equals("POST") && request.getContentType() != null && request.getContentType().equals("application/x-www-form-urlencoded")) {
124                String charset = request.getCharacterEncoding();
125                if (charset == null) {
126                    charset = "UTF-8";
127                }
128                // Push POST form params into the headers to retain compatibility with DefaultHttpBinding
129                String body = message.getBody(String.class);
130                try {
131                    for (String param : body.split("&")) {
132                        String[] pair = param.split("=", 2);
133                        String name = URLDecoder.decode(pair[0], charset);
134                        String value = URLDecoder.decode(pair[1], charset);
135                        if (headerFilterStrategy != null
136                            && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, message.getExchange())) {
137                            headers.put(name, value);
138                        }
139                    }
140                } catch (UnsupportedEncodingException e) {
141                    throw new RuntimeException(e);
142                }
143            }
144            
145        }
146        
147        protected void popluateAttachments(HttpServletRequest request, HttpMessage message) {
148            // check if there is multipart files, if so will put it into DataHandler
149            Enumeration names = request.getAttributeNames();
150            while (names.hasMoreElements()) {
151                String name = (String) names.nextElement();
152                Object object = request.getAttribute(name);
153                if (object instanceof File) {
154                    String fileName = request.getParameter(name);
155                    message.addAttachment(fileName, new DataHandler(new FileDataSource((File)object), FileTypeMap.getDefaultFileTypeMap().getContentType(fileName)));
156                }
157            }
158        }
159    
160        public void writeResponse(Exchange exchange, HttpServletResponse response) throws IOException {
161            if (exchange.isFailed()) {
162                if (exchange.getException() != null) {
163                    doWriteExceptionResponse(exchange.getException(), response);
164                } else {
165                    // it must be a fault, no need to check for the fault flag on the message
166                    doWriteFaultResponse(exchange.getOut(), response, exchange);
167                }
168            } else {
169                // just copy the protocol relates header
170                copyProtocolHeaders(exchange.getIn(), exchange.getOut());
171                Message out = exchange.getOut();            
172                if (out != null) {
173                    doWriteResponse(out, response, exchange);
174                }
175            }
176        }
177    
178        private void copyProtocolHeaders(Message request, Message response) {
179            if (request.getHeader(Exchange.CONTENT_ENCODING) != null) {
180                String contentEncoding = request.getHeader(Exchange.CONTENT_ENCODING, String.class);
181                response.setHeader(Exchange.CONTENT_ENCODING, contentEncoding);
182            }        
183            if (checkChunked(response, response.getExchange())) {
184                response.setHeader(Exchange.TRANSFER_ENCODING, "chunked");
185            }
186        }
187    
188        public void doWriteExceptionResponse(Throwable exception, HttpServletResponse response) throws IOException {
189            response.setStatus(500); // 500 for internal server error
190            response.setContentType("text/plain");
191    
192            // append the stacktrace as response
193            PrintWriter pw = response.getWriter();
194            exception.printStackTrace(pw);
195    
196            pw.flush();
197        }
198    
199        public void doWriteFaultResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
200            doWriteResponse(message, response, exchange);
201        }
202    
203        public void doWriteResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
204            // set the status code in the response. Default is 200.
205            if (message.getHeader(Exchange.HTTP_RESPONSE_CODE) != null) {
206                int code = message.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
207                response.setStatus(code);
208            }
209            // set the content type in the response.
210            String contentType = MessageHelper.getContentType(message);
211            if (MessageHelper.getContentType(message) != null) {
212                response.setContentType(contentType);
213            }
214    
215            // append headers
216            for (String key : message.getHeaders().keySet()) {
217                String value = message.getHeader(key, String.class);
218                if (headerFilterStrategy != null
219                        && !headerFilterStrategy.applyFilterToCamelHeaders(key, value, exchange)) {
220                    response.setHeader(key, value);
221                }
222            }
223    
224            // write the body.
225            if (message.getBody() != null) {
226                if (GZIPHelper.isGzip(message)) {
227                    doWriteGZIPResponse(message, response, exchange);
228                } else {
229                    doWriteDirectResponse(message, response, exchange);
230                }
231            }
232        }
233    
234        protected void doWriteDirectResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
235            InputStream is = null;
236            if (checkChunked(message, exchange)) {
237                is = message.getBody(InputStream.class);
238            }
239            if (is != null) {
240                ServletOutputStream os = response.getOutputStream();
241                try {
242                    // copy directly from input stream to output stream
243                    IOHelper.copy(is, os);
244                } finally {
245                    try {
246                        os.close();
247                    } catch (Exception e) {
248                        // ignore, maybe client have disconnected or timed out
249                    }
250                    try {
251                        is.close();
252                    } catch (Exception e) {
253                        // ignore, maybe client have disconnected or timed out
254                    }
255                }
256            } else {
257                // not convertable as a stream so try as a String
258                String data = message.getBody(String.class);
259                if (data != null) {
260                    // set content length before we write data
261                    response.setContentLength(data.length());
262                    response.getWriter().print(data);
263                    response.getWriter().flush();
264                }
265            }
266        }
267    
268        protected boolean checkChunked(Message message, Exchange exchange) {
269            boolean answer = true;
270            if (message.getHeader(Exchange.HTTP_CHUNKED) == null) {
271                // check the endpoint option
272                Endpoint endpoint = exchange.getFromEndpoint();
273                if (endpoint instanceof HttpEndpoint) {
274                    answer = ((HttpEndpoint)endpoint).isChunked();
275                }
276            } else {
277                answer = message.getHeader(Exchange.HTTP_CHUNKED, boolean.class);
278            }
279            return answer;
280        }
281    
282        protected void doWriteGZIPResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
283            byte[] bytes;
284            try {
285                bytes = message.getMandatoryBody(byte[].class);
286            } catch (InvalidPayloadException e) {
287                throw ObjectHelper.wrapRuntimeCamelException(e);
288            }
289    
290            byte[] data = GZIPHelper.compressGZIP(bytes);
291            ServletOutputStream os = response.getOutputStream();
292            try {
293                response.setContentLength(data.length);
294                os.write(data);
295                os.flush();
296            } finally {
297                os.close();
298            }
299        }
300    
301        public Object parseBody(HttpMessage httpMessage) throws IOException {
302            // lets assume the body is a reader
303            HttpServletRequest request = httpMessage.getRequest();
304            // Need to handle the GET Method which has no inputStream
305            if ("GET".equals(request.getMethod())) {
306                return null;
307            }
308            if (isUseReaderForPayload()) {
309                return request.getReader();
310            } else {
311                // otherwise use input stream and we need to cache it first
312                InputStream is = HttpConverter.toInputStream(request, httpMessage.getExchange());
313                if (is == null) {
314                    return is;
315                }
316                // convert the input stream to StreamCache if the stream cache is not disabled
317                if (httpMessage.getExchange().getProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.FALSE, Boolean.class)) {
318                    return is;
319                } else {
320                    try {
321                        CachedOutputStream cos = new CachedOutputStream(httpMessage.getExchange());
322                        IOHelper.copy(is, cos);
323                        return cos.getStreamCache();
324    
325                    } finally {
326                        is.close();
327                    }
328                }
329                 
330            }
331        }
332    
333        public boolean isUseReaderForPayload() {
334            return useReaderForPayload;
335        }
336    
337        public void setUseReaderForPayload(boolean useReaderForPayload) {
338            this.useReaderForPayload = useReaderForPayload;
339        }
340    
341        public HeaderFilterStrategy getHeaderFilterStrategy() {
342            return headerFilterStrategy;
343        }
344    
345        public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
346            this.headerFilterStrategy = headerFilterStrategy;
347        }
348    
349    }