001/**
002 * Copyright 2005-2014 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 */
016package org.kuali.rice.kns.web.struts.action;
017
018import org.apache.log4j.Logger;
019import org.apache.struts.config.ModuleConfig;
020import org.apache.struts.upload.CommonsMultipartRequestHandler;
021
022import 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 * @deprecated KNS Struts deprecated, use KRAD and the Spring MVC framework.
031 */
032@Deprecated
033public class KualiMultipartRequestHandler extends CommonsMultipartRequestHandler {
034    private static final Logger LOG = Logger.getLogger(KualiMultipartRequestHandler.class);
035
036    private String sizeMax;
037    
038    /**
039     * Returns the maximum allowable size, in bytes, of an uploaded file. The
040     * value is obtained from the current module's controller configuration.
041     *
042     * @param mc The current module's configuration.
043     *
044     * @return The maximum allowable file size, in bytes.
045     */
046    public long getSizeMax(ModuleConfig mc) {
047        return convertSizeToBytes( sizeMax, super.getSizeMax(mc) );
048    }
049
050    public String getSizeMaxString() {
051        return sizeMax;
052    }    
053
054    public void setSizeMax( String sizeString ) {
055        this.sizeMax = sizeString;
056    }
057    
058//    public long convertSizeToBytes(String sizeString, long defaultSize) {
059//      return super.convertSizeToBytes(sizeString, defaultSize);
060//    }
061    
062    /**
063     * Sets the max size string to the item in the list that represents the largest size.
064     */
065    public void setMaxUploadSizeToMaxOfList( List<String> sizes ) {
066        long maxSize = 0L;
067        for ( String size : sizes ) {
068            long currSize = convertSizeToBytes(size, 0L);
069            if ( currSize == 0L ) {
070                LOG.warn( "Unable to parse max size (" + size + ").  Ignoring." );
071            }
072            if ( currSize > maxSize ) {
073                maxSize = currSize;
074                sizeMax = size;
075            }
076        }
077    }
078    
079    public long calculateMaxUploadSizeToMaxOfList( List<String> sizes ) {
080        long maxSize = 0L;
081        for ( String size : sizes ) {
082            long currSize = convertSizeToBytes(size, 0L);
083            if ( currSize == 0L ) {
084                LOG.warn( "Unable to parse max size (" + size + ").  Ignoring." );
085            }
086            if ( currSize > maxSize ) {
087                maxSize = currSize;             
088            }
089        }
090        return maxSize;
091    }
092    
093}