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