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 @Override 065 public String getCharacterEncoding() { 066 return request.getCharacterEncoding(); 067 } 068 069 /** 070 * Retrieve the content type of the request. 071 * 072 * @return The content type of the request. 073 */ 074 @Override 075 public String getContentType() { 076 return request.getContentType(); 077 } 078 079 /** 080 * Retrieve the content length of the request. 081 * 082 * @return The content length of the request. 083 * @deprecated 1.3 Use {@link #contentLength()} instead 084 */ 085 @Override 086 @Deprecated 087 public int getContentLength() { 088 return request.getContentLength(); 089 } 090 091 /** 092 * Retrieve the content length of the request. 093 * 094 * @return The content length of the request. 095 * @since 1.3 096 */ 097 @Override 098 public long contentLength() { 099 long size; 100 try { 101 size = Long.parseLong(request.getProperty(FileUploadBase.CONTENT_LENGTH)); 102 } catch (NumberFormatException e) { 103 size = request.getContentLength(); 104 } 105 return size; 106 } 107 108 /** 109 * Retrieve the input stream for the request. 110 * 111 * @return The input stream for the request. 112 * 113 * @throws IOException if a problem occurs. 114 */ 115 @Override 116 public InputStream getInputStream() throws IOException { 117 return request.getPortletInputStream(); 118 } 119 120 /** 121 * Returns a string representation of this object. 122 * 123 * @return a string representation of this object. 124 */ 125 @Override 126 public String toString() { 127 return format("ContentLength=%s, ContentType=%s", 128 Long.valueOf(this.contentLength()), 129 this.getContentType()); 130 } 131 132}