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 /** 038 * The request for which the context is being provided. 039 */ 040 private final ActionRequest request; 041 042 /** 043 * Construct a context for this request. 044 * 045 * @param request The request to which this context applies. 046 */ 047 public PortletRequestContext(final ActionRequest request) { 048 this.request = request; 049 } 050 051 /** 052 * Retrieve the content length of the request. 053 * 054 * @return The content length of the request. 055 * @since 1.3 056 */ 057 @Override 058 public long contentLength() { 059 long size; 060 try { 061 size = Long.parseLong(request.getProperty(FileUploadBase.CONTENT_LENGTH)); 062 } catch (final NumberFormatException e) { 063 size = request.getContentLength(); 064 } 065 return size; 066 } 067 068 /** 069 * Retrieve the character encoding for the request. 070 * 071 * @return The character encoding for the request. 072 */ 073 @Override 074 public String getCharacterEncoding() { 075 return request.getCharacterEncoding(); 076 } 077 078 /** 079 * Retrieve the content length of the request. 080 * 081 * @return The content length of the request. 082 * @deprecated 1.3 Use {@link #contentLength()} instead 083 */ 084 @Override 085 @Deprecated 086 public int getContentLength() { 087 return request.getContentLength(); 088 } 089 090 /** 091 * Retrieve the content type of the request. 092 * 093 * @return The content type of the request. 094 */ 095 @Override 096 public String getContentType() { 097 return request.getContentType(); 098 } 099 100 /** 101 * Retrieve the input stream for the request. 102 * 103 * @return The input stream for the request. 104 * @throws IOException if a problem occurs. 105 */ 106 @Override 107 public InputStream getInputStream() throws IOException { 108 return request.getPortletInputStream(); 109 } 110 111 /** 112 * Returns a string representation of this object. 113 * 114 * @return a string representation of this object. 115 */ 116 @Override 117 public String toString() { 118 return format("ContentLength=%s, ContentType=%s", 119 Long.valueOf(contentLength()), 120 getContentType()); 121 } 122 123}