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.BufferedReader;
020    import java.io.IOException;
021    import java.io.InputStream;
022    import javax.servlet.ServletInputStream;
023    import javax.servlet.http.HttpServletRequest;
024    
025    import org.apache.camel.Converter;
026    import org.apache.camel.Exchange;
027    import org.apache.camel.component.http.helper.GZIPHelper;
028    
029    /**
030     * Some converter methods making it easy to convert the body of a message to servlet types or to switch between
031     * the underlying {@link ServletInputStream} or {@link BufferedReader} payloads etc.
032     *
033     * @version $Revision: 790811 $
034     */
035    @Converter
036    public final class HttpConverter {
037    
038        private HttpConverter() {
039        }
040    
041        @Converter
042        public static HttpServletRequest toServletRequest(HttpMessage message) {
043            if (message == null) {
044                return null;
045            }
046            return message.getRequest();
047        }
048    
049        @Converter
050        public static ServletInputStream toServletInputStream(HttpMessage message) throws IOException {
051            HttpServletRequest request = toServletRequest(message);
052            if (request != null) {
053                return request.getInputStream();
054            }
055            return null;
056        }
057    
058        @Converter
059        public static InputStream toInputStream(HttpMessage message) throws Exception {
060            return toInputStream(toServletRequest(message));
061        }
062    
063        @Converter
064        public static BufferedReader toReader(HttpMessage message) throws IOException {
065            HttpServletRequest request = toServletRequest(message);
066            if (request != null) {
067                return request.getReader();
068            }
069            return null;
070        }
071    
072        @Converter
073        public static InputStream toInputStream(HttpServletRequest request) throws IOException {
074            if (request == null) {
075                return null;
076            }
077            String contentEncoding = request.getHeader(Exchange.CONTENT_ENCODING);
078            return GZIPHelper.toGZIPInputStream(contentEncoding, request.getInputStream());
079        }
080    
081    }