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    
023    import javax.servlet.ServletInputStream;
024    import javax.servlet.http.HttpServletRequest;
025    
026    import org.apache.camel.Converter;
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: 743649 $
034     */
035    @Converter
036    public class HttpConverter {
037    
038        @Converter
039        public HttpServletRequest toServletRequest(HttpMessage message) {
040            if (message == null) {
041                return null;
042            }
043            return message.getRequest();
044        }
045    
046        @Converter
047        public ServletInputStream toServletInputStream(HttpMessage message) throws IOException {
048            HttpServletRequest request = toServletRequest(message);
049            if (request != null) {
050                return request.getInputStream();
051            }
052            return null;
053        }
054    
055        @Converter
056        public InputStream toInputStream(HttpMessage message) throws Exception {
057            HttpServletRequest request = toServletRequest(message);
058            if (request != null) {
059                return GZIPHelper.getInputStream(request);
060            }
061            return null;
062        }
063    
064        @Converter
065        public BufferedReader toReader(HttpMessage message) throws IOException {
066            HttpServletRequest request = toServletRequest(message);
067            if (request != null) {
068                return request.getReader();
069            }
070            return null;
071        }
072    
073    }