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