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