001 /** 002 * Copyright 2005-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.kns.web.struts.action; 017 018 import org.apache.log4j.Logger; 019 import org.apache.struts.config.ModuleConfig; 020 import org.apache.struts.upload.CommonsMultipartRequestHandler; 021 022 import java.util.List; 023 024 /** 025 * Subclass of the MultipartRequestHandler used by Struts. This one allows the maximum upload size to be set 026 * by the application rather than by an init parameter. 027 * 028 * @author Kuali Rice Team (rice.collab@kuali.org) 029 * 030 */ 031 public class KualiMultipartRequestHandler extends CommonsMultipartRequestHandler { 032 private static final Logger LOG = Logger.getLogger(KualiMultipartRequestHandler.class); 033 034 private String sizeMax; 035 036 /** 037 * Returns the maximum allowable size, in bytes, of an uploaded file. The 038 * value is obtained from the current module's controller configuration. 039 * 040 * @param mc The current module's configuration. 041 * 042 * @return The maximum allowable file size, in bytes. 043 */ 044 public long getSizeMax(ModuleConfig mc) { 045 return convertSizeToBytes( sizeMax, super.getSizeMax(mc) ); 046 } 047 048 public String getSizeMaxString() { 049 return sizeMax; 050 } 051 052 public void setSizeMax( String sizeString ) { 053 this.sizeMax = sizeString; 054 } 055 056 // public long convertSizeToBytes(String sizeString, long defaultSize) { 057 // return super.convertSizeToBytes(sizeString, defaultSize); 058 // } 059 060 /** 061 * Sets the max size string to the item in the list that represents the largest size. 062 */ 063 public void setMaxUploadSizeToMaxOfList( List<String> sizes ) { 064 long maxSize = 0L; 065 for ( String size : sizes ) { 066 long currSize = convertSizeToBytes(size, 0L); 067 if ( currSize == 0L ) { 068 LOG.warn( "Unable to parse max size (" + size + "). Ignoring." ); 069 } 070 if ( currSize > maxSize ) { 071 maxSize = currSize; 072 sizeMax = size; 073 } 074 } 075 } 076 077 public long calculateMaxUploadSizeToMaxOfList( List<String> sizes ) { 078 long maxSize = 0L; 079 for ( String size : sizes ) { 080 long currSize = convertSizeToBytes(size, 0L); 081 if ( currSize == 0L ) { 082 LOG.warn( "Unable to parse max size (" + size + "). Ignoring." ); 083 } 084 if ( currSize > maxSize ) { 085 maxSize = currSize; 086 } 087 } 088 return maxSize; 089 } 090 091 }