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.portlet; 018 019import static java.lang.String.format; 020 021import java.io.IOException; 022import java.io.InputStream; 023 024import javax.portlet.ActionRequest; 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 * a portlet.</p> 032 * 033 * @since FileUpload 1.1 034 */ 035public class PortletRequestContext implements UploadContext { 036 037 // ----------------------------------------------------- Instance Variables 038 039 /** 040 * The request for which the context is being provided. 041 */ 042 private final ActionRequest request; 043 044 045 // ----------------------------------------------------------- Constructors 046 047 /** 048 * Construct a context for this request. 049 * 050 * @param request The request to which this context applies. 051 */ 052 public PortletRequestContext(ActionRequest request) { 053 this.request = request; 054 } 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.getProperty(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.getPortletInputStream(); 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}