Uploadエラーメッセージの場所

FileUploadInterceptorで出力されるエラーメッセージはグローバルリソースに設定しておかなければいけないのだが、ソースを読むとリソース探索の起点としてInterceptorのクラスを渡しているけれど、本来はアクションのクラスで、どうもこの制限はバグなのではないかと思えてしまう。
そこで、アクションのプロパティ(Struts2の標準メッセージ探索ルール)にエラーメッセージを書けるように変更したFileUploadInterceptorのバージョンを書いた。
変更したいところはメッセージ探索開始のクラスだけなのだが、結構いじらないと駄目だった。

jp.co.hershe.struts2.util.FileUploadInterceptor2.java

package jp.co.hershe.struts2.util;

import java.io.File;
import java.util.Collection;
import java.util.Locale;

import org.apache.struts2.interceptor.FileUploadInterceptor;

import com.opensymphony.xwork2.ValidationAware;
import com.opensymphony.xwork2.util.LocalizedTextUtil;

public class FileUploadInterceptor2 extends FileUploadInterceptor {
    private static final String DEFAULT_MESSAGE = "no.message.found";

	protected boolean acceptFile(File file, String contentType, String inputName, ValidationAware validation, Locale locale) {
        boolean fileIsAcceptable = false;

        // If it's null the upload failed
        Class clz = validation!=null?validation.getClass():this.getClass();
        if (file == null) {
            String errMsg = getTextMessage(clz,"struts.messages.error.uploading", new Object[]{inputName}, locale);
            if (validation != null) {
                validation.addFieldError(inputName, errMsg);
            }

            log.error(errMsg);
        } else if (maximumSize != null && maximumSize.longValue() < file.length()) {
            String errMsg = getTextMessage(clz,"struts.messages.error.file.too.large", new Object[]{inputName, file.getName(), "" + file.length()}, locale);
            if (validation != null) {
                validation.addFieldError(inputName, errMsg);
            }

            log.error(errMsg);
        } else if ((! allowedTypesSet.isEmpty()) && (!containsItem(allowedTypesSet, contentType))) {
            String errMsg = getTextMessage(clz,"struts.messages.error.content.type.not.allowed", new Object[]{inputName, file.getName(), contentType}, locale);
            if (validation != null) {
                validation.addFieldError(inputName, errMsg);
            }

            log.error(errMsg);
        } else {
            fileIsAcceptable = true;
        }

        return fileIsAcceptable;
    }
    private static boolean containsItem(Collection itemCollection, String key) {
        return itemCollection.contains(key.toLowerCase());
    }
	
    private String getTextMessage(Class clas, String messageKey, Object[] args, Locale locale) {
        if (args == null || args.length == 0) {
            return LocalizedTextUtil.findText(clas, messageKey, locale);
        } else {
            return LocalizedTextUtil.findText(clas, messageKey, locale, DEFAULT_MESSAGE, args);
        }
    }
}

あと、struts.xml の必要箇所の変更。

  <interceptor name="fileUpload2"
        class="jp.co.hershe.struts2.util.FileUploadInterceptor2"/>
      :
      :
  <interceptor-ref name="fileUpload2">
        	<param name="allowedTypes">none</param>
        	<param name="maximumSize">0</param>
  </interceptor-ref>

これで、UploadAction.properties に

struts.messages.error.uploading={0}のアップロードができませんでした
struts.messages.error.file.too.large={0}のファイルが大きすぎます
struts.messages.error.content.type.not.allowed={0}の許可されていないファイルタイプです

などとできる