/** * This file is part of Faktotum. * * * Faktotum is free software: you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. * * Faktotum is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with Faktotum. * * If not, see <http://www.gnu.org/licenses/>. */ package de.romankreisel.faktotum.helper; import java.util.ArrayList; import java.util.List; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.FacesValidator; import javax.faces.validator.Validator; import javax.faces.validator.ValidatorException; import javax.servlet.http.Part; /** * @author roman * */ @FacesValidator(value = "profilePictureValidator") public class ProfilePictureValidator implements Validator { /* * (non-Javadoc) * * @see * javax.faces.validator.Validator#validate(javax.faces.context.FacesContext * , javax.faces.component.UIComponent, java.lang.Object) */ @Override public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { List<FacesMessage> msgs = new ArrayList<FacesMessage>(); Part file = (Part) value; // max 10mbyte - more than enough! // TODO: configurable if (file.getSize() > 1024 * 1024 * 10) { msgs.add(new FacesMessage("The uploaded file exceeds the maximum filesize")); } // TODO: more file types if (!"image/jpeg".equals(file.getContentType())) { msgs.add(new FacesMessage("Only JPG-Files are allowed")); } if (!msgs.isEmpty()) { throw new ValidatorException(msgs); } } }