View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.web.struts.action;
17  
18  import org.apache.log4j.Logger;
19  import org.apache.struts.config.ModuleConfig;
20  import org.apache.struts.upload.CommonsMultipartRequestHandler;
21  
22  import java.util.List;
23  
24  /**
25   * Subclass of the MultipartRequestHandler used by Struts.  This one allows the maximum upload size to be set
26   * by the application rather than by an init parameter. 
27   * 
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   *
30   */
31  public class KualiMultipartRequestHandler extends CommonsMultipartRequestHandler {
32      private static final Logger LOG = Logger.getLogger(KualiMultipartRequestHandler.class);
33  
34      private String sizeMax;
35      
36      /**
37       * Returns the maximum allowable size, in bytes, of an uploaded file. The
38       * value is obtained from the current module's controller configuration.
39       *
40       * @param mc The current module's configuration.
41       *
42       * @return The maximum allowable file size, in bytes.
43       */
44      public long getSizeMax(ModuleConfig mc) {
45          return convertSizeToBytes( sizeMax, super.getSizeMax(mc) );
46      }
47  
48      public String getSizeMaxString() {
49          return sizeMax;
50      }    
51  
52      public void setSizeMax( String sizeString ) {
53      	this.sizeMax = sizeString;
54      }
55      
56  //    public long convertSizeToBytes(String sizeString, long defaultSize) {
57  //	return super.convertSizeToBytes(sizeString, defaultSize);
58  //    }
59      
60      /**
61       * Sets the max size string to the item in the list that represents the largest size.
62       */
63      public void setMaxUploadSizeToMaxOfList( List<String> sizes ) {
64  	long maxSize = 0L;
65  	for ( String size : sizes ) {
66  	    long currSize = convertSizeToBytes(size, 0L);
67  	    if ( currSize == 0L ) {
68  		LOG.warn( "Unable to parse max size (" + size + ").  Ignoring." );
69  	    }
70  	    if ( currSize > maxSize ) {
71  		maxSize = currSize;
72  		sizeMax = size;
73  	    }
74  	}
75      }
76      
77      public long calculateMaxUploadSizeToMaxOfList( List<String> sizes ) {
78      	long maxSize = 0L;
79      	for ( String size : sizes ) {
80      	    long currSize = convertSizeToBytes(size, 0L);
81      	    if ( currSize == 0L ) {
82      		LOG.warn( "Unable to parse max size (" + size + ").  Ignoring." );
83      	    }
84      	    if ( currSize > maxSize ) {
85      		maxSize = currSize;    		
86      	    }
87      	}
88      	return maxSize;
89      }
90      
91  }