Author: hermanns
Date: Sat Jun 14 05:19:07 2008
New Revision: 667779

URL: http://svn.apache.org/viewvc?rev=667779&view=rev
Log:
WW-1725 FileUploadInterceptor and localized messages problem
o implemented requested feature based on a new config parameter:
   set useActionMessageBundle config parameter to true to use action message 
bundles
   The default behaviour is not changed at all, so this change is fully 
backwards compliant.

Modified:
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java?rev=667779&r1=667778&r2=667779&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java
 Sat Jun 14 05:19:07 2008
@@ -175,10 +175,18 @@
     private static final String DEFAULT_DELIMITER = ",";
     private static final String DEFAULT_MESSAGE = "no.message.found";
 
+    protected boolean useActionMessageBundle;
+
     protected Long maximumSize;
     protected String allowedTypes;
     protected Set allowedTypesSet = Collections.EMPTY_SET;
 
+
+
+    public void setUseActionMessageBundle(String value) {
+        this.useActionMessageBundle = Boolean.valueOf(value).booleanValue();
+    }
+
     /**
      * Sets the allowed mimetypes
      *
@@ -205,6 +213,7 @@
      */
     public String intercept(ActionInvocation invocation) throws Exception {
         ActionContext ac = invocation.getInvocationContext();
+
         HttpServletRequest request = (HttpServletRequest) 
ac.get(ServletActionContext.HTTP_REQUEST);
 
         if (!(request instanceof MultiPartRequestWrapper)) {
@@ -216,9 +225,10 @@
             return invocation.invoke();
         }
 
-        final Object action = invocation.getAction();
         ValidationAware validation = null;
 
+        Object action = invocation.getAction();
+        
         if (action instanceof ValidationAware) {
             validation = (ValidationAware) action;
         }
@@ -262,7 +272,7 @@
                         String contentTypeName = inputName + "ContentType";
                         String fileNameName = inputName + "FileName";
                         for (int index = 0; index < files.length; index++) {
-                            if (acceptFile(files[index], contentType[index], 
inputName, validation, ac.getLocale())) {
+                            if (acceptFile(action, files[index], 
contentType[index], inputName, validation, ac.getLocale())) {
                                 acceptedFiles.add(files[index]);
                                 acceptedContentTypes.add(contentType[index]);
                                 acceptedFileNames.add(fileName[index]);
@@ -275,10 +285,10 @@
                         }
                     }
                 } else {
-                    LOG.error(getTextMessage("struts.messages.invalid.file", 
new Object[]{inputName}, ActionContext.getContext().getLocale()));
+                    LOG.error(getTextMessage(action, 
"struts.messages.invalid.file", new Object[]{inputName}, 
ActionContext.getContext().getLocale()));
                 }
             } else {
-                
LOG.error(getTextMessage("struts.messages.invalid.content.type", new 
Object[]{inputName}, ActionContext.getContext().getLocale()));
+                LOG.error(getTextMessage(action, 
"struts.messages.invalid.content.type", new Object[]{inputName}, 
ActionContext.getContext().getLocale()));
             }
         }
 
@@ -293,7 +303,7 @@
             for (int index = 0; index < file.length; index++) {
                 File currentFile = file[index];
                 if (LOG.isInfoEnabled()) {
-                    LOG.info(getTextMessage("struts.messages.removing.file", 
new Object[]{inputValue, currentFile}, ActionContext.getContext().getLocale()));
+                    LOG.info(getTextMessage(action, 
"struts.messages.removing.file", new Object[]{inputValue, currentFile}, 
ActionContext.getContext().getLocale()));
                 }
                 if ((currentFile != null) && currentFile.isFile()) {
                     currentFile.delete();
@@ -316,25 +326,42 @@
      * @return true if the proposed file is acceptable by contentType and size.
      */
     protected boolean acceptFile(File file, String contentType, String 
inputName, ValidationAware validation, Locale locale) {
+        return acceptFile(null, file, contentType, inputName, validation, 
locale);
+
+    }
+    
+    /**
+     * Override for added functionality. Checks if the proposed file is 
acceptable based on contentType and size.
+     *
+     * @param action      - uploading action for message retrieval.
+     * @param file        - proposed upload file.
+     * @param contentType - contentType of the file.
+     * @param inputName   - inputName of the file.
+     * @param validation  - Non-null ValidationAware if the action implements 
ValidationAware, allowing for better
+     *                    logging.
+     * @param locale
+     * @return true if the proposed file is acceptable by contentType and size.
+     */
+    protected boolean acceptFile(Object action, File file, String contentType, 
String inputName, ValidationAware validation, Locale locale) {
         boolean fileIsAcceptable = false;
 
         // If it's null the upload failed
         if (file == null) {
-            String errMsg = getTextMessage("struts.messages.error.uploading", 
new Object[]{inputName}, locale);
+            String errMsg = getTextMessage(action, 
"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("struts.messages.error.file.too.large", new Object[]{inputName, 
file.getName(), "" + file.length()}, locale);
+            String errMsg = getTextMessage(action, 
"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("struts.messages.error.content.type.not.allowed", new 
Object[]{inputName, file.getName(), contentType}, locale);
+            String errMsg = getTextMessage(action, 
"struts.messages.error.content.type.not.allowed", new Object[]{inputName, 
file.getName(), contentType}, locale);
             if (validation != null) {
                 validation.addFieldError(inputName, errMsg);
             }
@@ -381,9 +408,20 @@
     }
 
     private String getTextMessage(String messageKey, Object[] args, Locale 
locale) {
+        return getTextMessage(null, messageKey, args, locale);
+
+    }
+    
+    private String getTextMessage(Object action, String messageKey, Object[] 
args, Locale locale) {
         if (args == null || args.length == 0) {
-            return LocalizedTextUtil.findText(this.getClass(), messageKey, 
locale);
+            if ( action != null && useActionMessageBundle) {
+                return LocalizedTextUtil.findText(action.getClass(), 
messageKey, locale);
+            }
+            return LocalizedTextUtil.findText(this.getClass(), messageKey, 
locale);                        
         } else {
+            if ( action != null && useActionMessageBundle) {
+                return LocalizedTextUtil.findText(action.getClass(), 
messageKey, locale, DEFAULT_MESSAGE, args);
+            }
             return LocalizedTextUtil.findText(this.getClass(), messageKey, 
locale, DEFAULT_MESSAGE, args);
         }
     }


Reply via email to