001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0 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 * Contributors: 017 * <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 018 * 019 * $Id: InputFileSizeValidator.java 28460 2008-01-03 15:34:05Z sfermigier $ 020 */ 021 022package org.nuxeo.ecm.platform.ui.web.component.file; 023 024import javax.faces.component.StateHolder; 025import javax.faces.component.UIComponent; 026import javax.faces.context.FacesContext; 027import javax.faces.validator.Validator; 028import javax.faces.validator.ValidatorException; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.nuxeo.ecm.core.api.Blob; 033 034import com.sun.faces.util.MessageFactory; 035 036/** 037 * Input file size validator. 038 * <p> 039 * Validates an {@link InputFileInfo} blob value in case it's been uploaded. Value is set using the "maxSize" attribute 040 * and setting it to (for instance) "10Ko", "10Mo" or "10Go". 041 * 042 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 043 */ 044public class InputFileSizeValidator implements Validator, StateHolder { 045 046 public static final String VALIDATOR_ID = "InputFileSizeValidator"; 047 048 private static final Log log = LogFactory.getLog(InputFileSizeValidator.class); 049 050 private String maxSize = null; 051 052 private boolean maximumSet = false; 053 054 private boolean transientValue = false; 055 056 /** 057 * The message identifier of the {@link javax.faces.application.FacesMessage} to be created if the maximum size 058 * check fails. The message format string for this message may optionally include the following placeholders: 059 * <ul> 060 * <li><code>{0}</code> replaced by the configured maximum length.</li> 061 * </ul> 062 */ 063 public static final String MAXIMUM_MESSAGE_ID = "error.inputFile.maxSize"; 064 065 @Override 066 public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { 067 if (!maximumSet) { 068 return; 069 } 070 if (context == null || component == null) { 071 throw new IllegalArgumentException(); 072 } 073 if (value != null) { 074 if (value instanceof InputFileInfo) { 075 InputFileInfo info = (InputFileInfo) value; 076 String choice = info.getConvertedChoice(); 077 if (!(InputFileChoice.isUploadOrKeepTemp(choice))) { 078 return; 079 } 080 Blob blob = info.getConvertedBlob(); 081 long finalMaxSize = 0L; 082 String maxString = null; 083 if (maxSize != null) { 084 finalMaxSize = getMaxSizeBytes(); 085 maxString = maxSize; 086 } 087 if (finalMaxSize != 0L && blob.getLength() > finalMaxSize) { 088 throw new ValidatorException(MessageFactory.getMessage(context, MAXIMUM_MESSAGE_ID, maxString)); 089 } 090 } 091 } 092 } 093 094 private static long parseMaxSizeString(String maxSize) { 095 long res = 0L; 096 if (maxSize != null) { 097 maxSize = maxSize.trim(); 098 if (maxSize.length() < 2) { 099 log.error("Invalid maximum size " + maxSize); 100 return res; 101 } 102 Integer maxSizeInt; 103 String suffix = maxSize.substring(maxSize.length() - 2); 104 String maxSizeIntStr = maxSize.substring(0, maxSize.length() - 2).trim(); 105 try { 106 maxSizeInt = Integer.valueOf(maxSizeIntStr); 107 } catch (NumberFormatException e) { 108 log.error("Invalid maximum size " + maxSize); 109 return res; 110 } 111 // Using IS units 112 if ("Ko".equals(suffix)) { 113 res = maxSizeInt * 1000; 114 } else if ("Mo".equals(suffix)) { 115 res = maxSizeInt * 1000 * 1000; 116 } else if (maxSize.endsWith("Go")) { 117 res = maxSizeInt * 1000 * 1000 * 1000; 118 } else { 119 log.error("Invalid maximum size " + maxSize); 120 } 121 } 122 return res; 123 } 124 125 public String getMaxSize() { 126 return maxSize; 127 } 128 129 public long getMaxSizeBytes() { 130 return parseMaxSizeString(maxSize); 131 } 132 133 public void setMaxSize(String maxSizeString) { 134 maxSize = maxSizeString; 135 maximumSet = true; 136 } 137 138 @Override 139 public boolean isTransient() { 140 return transientValue; 141 } 142 143 @Override 144 public void setTransient(boolean newTransientValue) { 145 transientValue = newTransientValue; 146 } 147 148 @Override 149 public Object saveState(FacesContext context) { 150 Object[] values = new Object[2]; 151 values[0] = maxSize; 152 values[1] = maximumSet ? Boolean.TRUE : Boolean.FALSE; 153 return values; 154 } 155 156 @Override 157 public void restoreState(FacesContext context, Object state) { 158 Object[] values = (Object[]) state; 159 maxSize = (String) values[0]; 160 maximumSet = (Boolean) values[1]; 161 } 162 163}