JavaとJavaEEプログラマのブログ

JavaEEを中心にしたをソフトウェア開発についてのブログ

slim3でのアップロードファイルのValidatorを作成する。

画像ファイル以外のファイルをエラーにするValidator。
ファイルサイズとコンテンツタイプをチェックしている。

import java.util.Map;

import org.datanucleus.util.StringUtils;
import org.slim3.controller.upload.FileItem;
import org.slim3.controller.validator.AbstractValidator;
import org.slim3.util.ApplicationMessage;

public class ImageFileValidator extends AbstractValidator {

    private static final int maxFileSize = 1024 * 1000;
    
    public ImageFileValidator() {
        super(null);
    }

    
    public ImageFileValidator(String message) {
        super(message);
    }

    public String validate(Map<String, Object> parameters, String name) {
        
        
       Object value = parameters.get(name);
        if (value == null || "".equals(value)) {
            return ApplicationMessage.get(
                "validator.required",
                getLabel(name) );
        }

        FileItem fi = (FileItem)value;
        if(fi == null || fi.getData() == null ){

            return ApplicationMessage.get(
                "validator.required",
                getLabel(name) );

        }

        
        
        if(! isImageContentType(fi)){
            if (message != null) {
                return message;
            }
            return ApplicationMessage.get(
                "validator.isNotImageFile",
                getLabel(name) );
        }
        
       
        if(fi.getData().length > maxFileSize ){
            if (message != null) {
                return message;
            }
            return ApplicationMessage.get(
                "validator.maxFileSize",
                getLabel(name) );
        }
        
        return null;
        

        
    }

    @Override
    protected String getMessageKey() {
        // TODO Auto-generated method stub
        return null;
    }


    private boolean isImageContentType(FileItem fi){
        if(StringUtils.isEmpty(fi.getContentType())){
            return false;
        }
        
        if(fi.getContentType().startsWith("image")){
            return true;
        }else{
            return false;
        }
                
    }

}

拡張したValidators内に独自Validatorを呼び出すメソッドを追加。

package com.mprog.photoBlog.validator;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slim3.controller.validator.Validators;

public class PhotoBlogValidators extends Validators {

    public PhotoBlogValidators(Map<String, Object> parameters)
            throws NullPointerException, IllegalStateException {
        super(parameters);
    }

    public PhotoBlogValidators(HttpServletRequest request) {
        super(request);
    }

    
    public ImageFileValidator isImageFile() {
        return new ImageFileValidator();
    }
    
    public ImageFileValidator isImageFile( String message) {
        return new ImageFileValidator(message);
    }
    

}

コントローラーの中で拡張したValidatorsを呼び出す。

    private boolean validate() {
        PhotoBlogValidators v = new PhotoBlogValidators(request);
        v.add(RequestKeys.TITLE, v.required(), v.maxlength(50));
        v.add(RequestKeys.USERNAME, v.required(), v.maxlength(50));
        v.add(RequestKeys.PHOTO, v.isImageFile() );

        if (!StringUtil.isEmpty(param(RequestKeys.PASSWORD))) {
            v.add(RequestKeys.PASSWORD, v.minlength(6), v.maxlength(20));
        }

        return v.validate();
    }