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 */ 017package org.apache.commons.fileupload.servlet; 018 019import static java.lang.String.format; 020 021import java.io.IOException; 022import java.io.InputStream; 023 024import javax.servlet.http.HttpServletRequest; 025 026import org.apache.commons.fileupload.FileUploadBase; 027import org.apache.commons.fileupload.UploadContext; 028 029/** 030 * <p>Provides access to the request information needed for a request made to 031 * an HTTP servlet.</p> 032 * 033 * @since FileUpload 1.1 034 * 035 * @version $Id: ServletRequestContext.java 1564788 2014-02-05 14:36:41Z markt $ 036 */ 037public class ServletRequestContext implements UploadContext { 038 039 // ----------------------------------------------------- Instance Variables 040 041 /** 042 * The request for which the context is being provided. 043 */ 044 private final HttpServletRequest request; 045 046 // ----------------------------------------------------------- Constructors 047 048 /** 049 * Construct a context for this request. 050 * 051 * @param request The request to which this context applies. 052 */ 053 public ServletRequestContext(HttpServletRequest request) { 054 this.request = request; 055 } 056 057 // --------------------------------------------------------- Public Methods 058 059 /** 060 * Retrieve the character encoding for the request. 061 * 062 * @return The character encoding for the request. 063 */ 064 public String getCharacterEncoding() { 065 return request.getCharacterEncoding(); 066 } 067 068 /** 069 * Retrieve the content type of the request. 070 * 071 * @return The content type of the request. 072 */ 073 public String getContentType() { 074 return request.getContentType(); 075 } 076 077 /** 078 * Retrieve the content length of the request. 079 * 080 * @return The content length of the request. 081 * @deprecated 1.3 Use {@link #contentLength()} instead 082 */ 083 @Deprecated 084 public int getContentLength() { 085 return request.getContentLength(); 086 } 087 088 /** 089 * Retrieve the content length of the request. 090 * 091 * @return The content length of the request. 092 * @since 1.3 093 */ 094 public long contentLength() { 095 long size; 096 try { 097 size = Long.parseLong(request.getHeader(FileUploadBase.CONTENT_LENGTH)); 098 } catch (NumberFormatException e) { 099 size = request.getContentLength(); 100 } 101 return size; 102 } 103 104 /** 105 * Retrieve the input stream for the request. 106 * 107 * @return The input stream for the request. 108 * 109 * @throws IOException if a problem occurs. 110 */ 111 public InputStream getInputStream() throws IOException { 112 return request.getInputStream(); 113 } 114 115 /** 116 * Returns a string representation of this object. 117 * 118 * @return a string representation of this object. 119 */ 120 @Override 121 public String toString() { 122 return format("ContentLength=%s, ContentType=%s", 123 Long.valueOf(this.contentLength()), 124 this.getContentType()); 125 } 126 127}