com.gantzgulch.sharing.form.FileUploadValidator.java Source code

Java tutorial

Introduction

Here is the source code for com.gantzgulch.sharing.form.FileUploadValidator.java

Source

/*
 * Copyright 2011 GantzGulch, Inc.
 * 
 * This file is part of GantzFileSharing.
 * 
 * GantzFileSharing is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GantzFileSharing 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GantzFileSharing.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.gantzgulch.sharing.form;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class FileUploadValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return SharedFileUploadForm.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {

        SharedFileUploadForm form = (SharedFileUploadForm) target;

        if (StringUtils.isBlank(form.getFile().getOriginalFilename())) {
            errors.rejectValue("file", "file.no_name", "File must have a name.");
        }

        if (form.getFile().getSize() == 0) {
            errors.rejectValue("file", "file.empty", "No empty files.");
        }

    }

}