001 package org.apache.myfaces.tobago.validator; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 import org.apache.myfaces.tobago.component.UIFileInput; 021 import org.apache.myfaces.tobago.util.MessageFactory; 022 import org.apache.myfaces.tobago.util.ContentType; 023 import org.apache.commons.fileupload.FileItem; 024 025 import javax.faces.validator.Validator; 026 import javax.faces.validator.ValidatorException; 027 import javax.faces.validator.DoubleRangeValidator; 028 import javax.faces.component.StateHolder; 029 import javax.faces.component.UIComponent; 030 import javax.faces.context.FacesContext; 031 import javax.faces.application.FacesMessage; 032 033 /* 034 * Created by IntelliJ IDEA. 035 * User: bommel 036 * Date: Oct 30, 2006 037 * Time: 9:59:13 PM 038 */ 039 040 /** 041 * <p><strong>FileItemValidator</strong> is a {@link Validator} that checks 042 * the FileItem in the value of the associated component. 043 */ 044 045 @org.apache.myfaces.tobago.apt.annotation.Validator(id = FileItemValidator.VALIDATOR_ID) 046 public class FileItemValidator implements Validator, StateHolder { 047 public static final String VALIDATOR_ID = "org.apache.myfaces.tobago.FileItem"; 048 private Integer maxSize; 049 private String contentType; 050 private boolean transientValue; 051 052 public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { 053 if (value != null && component instanceof UIFileInput) { 054 FileItem file = (FileItem) value; 055 056 if (maxSize != null && file.getSize() > maxSize) { 057 // TODO better error text i18n 058 Object[] args = {maxSize, component.getId()}; 059 FacesMessage facesMessage = MessageFactory.createFacesMessage(context, 060 DoubleRangeValidator.MAXIMUM_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, args); 061 throw new ValidatorException(facesMessage); 062 } 063 // Check only a valid file 064 if (file.getSize() > 0 && contentType != null 065 && !ContentType.valueOf(contentType).match(ContentType.valueOf(file.getContentType()))) { 066 // TODO i18n 067 String text = "ContentType " + file.getContentType() + " not expected."; 068 FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, text, text); 069 throw new ValidatorException(facesMessage); 070 } 071 } 072 } 073 074 public int getMaxSize() { 075 return maxSize; 076 } 077 078 public void setMaxSize(int maxSize) { 079 if (maxSize > 0) { 080 this.maxSize = maxSize; 081 } 082 } 083 084 public String getContentType() { 085 return contentType; 086 } 087 088 public void setContentType(String contentType) { 089 this.contentType = contentType; 090 } 091 092 public Object saveState(FacesContext context) { 093 Object[] values = new Object[2]; 094 values[0] = maxSize; 095 values[1] = contentType; 096 return values; 097 } 098 099 public void restoreState(FacesContext context, Object state) { 100 Object[] values = (Object[]) state; 101 maxSize = (Integer) values[0]; 102 contentType = (String) values[1]; 103 } 104 105 public boolean isTransient() { 106 return transientValue; 107 } 108 109 public void setTransient(boolean newTransientValue) { 110 this.transientValue = newTransientValue; 111 } 112 }