Repository: primefaces/primefaces (java)

Issue:
MessageFactory: Pass FacesContext into all methods
### Description

Right now MessageFactory does `FacesContext.currentInstance()` and it should really take FacesContext as a parameter.

### Describe the solution you would like

pass the FacesContext

### Additional context

_No response_

Pull request:
Fix #12904: MessageFactory pass in FacesContext
Fix #12904: MessageFactory pass in FacesContext

Patch:
diff --git a/primefaces/src/main/java/org/primefaces/component/api/UICalendar.java b/primefaces/src/main/java/org/primefaces/component/api/UICalendar.java
index d02c2b79fa..3febc5ce67 100644
--- a/primefaces/src/main/java/org/primefaces/component/api/UICalendar.java
+++ b/primefaces/src/main/java/org/primefaces/component/api/UICalendar.java
@@ -376,19 +376,19 @@ public abstract class UICalendar extends AbstractPrimeHtmlInputText implements I
                 case OK:
                     break;
                 case INVALID_DISABLED_DATE:
-                    msg = MessageFactory.getFacesMessage(DATE_INVALID_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, params);
+                    msg = MessageFactory.getFacesMessage(context, DATE_INVALID_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, params);
                     break;
                 case INVALID_RANGE_DATES_SEQUENTIAL:
-                    msg = MessageFactory.getFacesMessage(DATE_INVALID_RANGE_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, params);
+                    msg = MessageFactory.getFacesMessage(context, DATE_INVALID_RANGE_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, params);
                     break;
                 case INVALID_MIN_DATE:
-                    msg = MessageFactory.getFacesMessage(DATE_MIN_DATE_ID, FacesMessage.SEVERITY_ERROR, params);
+                    msg = MessageFactory.getFacesMessage(context, DATE_MIN_DATE_ID, FacesMessage.SEVERITY_ERROR, params);
                     break;
                 case INVALID_MAX_DATE:
-                    msg = MessageFactory.getFacesMessage(DATE_MAX_DATE_ID, FacesMessage.SEVERITY_ERROR, params);
+                    msg = MessageFactory.getFacesMessage(context, DATE_MAX_DATE_ID, FacesMessage.SEVERITY_ERROR, params);
                     break;
                 case INVALID_OUT_OF_RANGE:
-                    msg = MessageFactory.getFacesMessage(DATE_OUT_OF_RANGE_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, params);
+                    msg = MessageFactory.getFacesMessage(context, DATE_OUT_OF_RANGE_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, params);
                     break;
             }
         }
diff --git a/primefaces/src/main/java/org/primefaces/component/api/UIPageableData.java b/primefaces/src/main/java/org/primefaces/component/api/UIPageableData.java
index 8e63cb5b1b..8cdf227ef3 100644
--- a/primefaces/src/main/java/org/primefaces/component/api/UIPageableData.java
+++ b/primefaces/src/main/java/org/primefaces/component/api/UIPageableData.java
@@ -81,7 +81,7 @@ public class UIPageableData extends PrimeUIData implements Pageable, TouchAware
     }
 
     public String getEmptyMessage() {
-        return (String) getStateHelper().eval(PropertyKeys.emptyMessage, MessageFactory.getMessage(EMPTY_MESSAGE));
+        return (String) getStateHelper().eval(PropertyKeys.emptyMessage, MessageFactory.getMessage(getFacesContext(), EMPTY_MESSAGE));
     }
 
     public void setEmptyMessage(String emptyMessage) {
diff --git a/primefaces/src/main/java/org/primefaces/component/api/UITree.java b/primefaces/src/main/java/org/primefaces/component/api/UITree.java
index 9b490a3f0e..6e2379df08 100644
--- a/primefaces/src/main/java/org/primefaces/component/api/UITree.java
+++ b/primefaces/src/main/java/org/primefaces/component/api/UITree.java
@@ -558,7 +558,7 @@ public abstract class UITree extends UIComponentBase implements NamingContainer
                     msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
                 }
                 else {
-                    msg = MessageFactory.getFacesMessage(REQUIRED_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, getClientId(context));
+                    msg = MessageFactory.getFacesMessage(context, REQUIRED_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, getClientId(context));
                 }
 
                 context.addMessage(getClientId(context), msg);
diff --git a/primefaces/src/main/java/org/primefaces/component/calendar/BaseCalendarRenderer.java b/primefaces/src/main/java/org/primefaces/component/calendar/BaseCalendarRenderer.java
index 9731c0b709..6d64235563 100644
--- a/primefaces/src/main/java/org/primefaces/component/calendar/BaseCalendarRenderer.java
+++ b/primefaces/src/main/java/org/primefaces/component/calendar/BaseCalendarRenderer.java
@@ -282,13 +282,13 @@ public abstract class BaseCalendarRenderer extends InputRenderer {
         params[2] = ComponentUtils.getLabel(context, calendar);
 
         if (calendar.isTimeOnly()) {
-            message = MessageFactory.getFacesMessage("javax.faces.converter.DateTimeConverter.TIME", FacesMessage.SEVERITY_ERROR, params);
+            message = MessageFactory.getFacesMessage(context, "javax.faces.converter.DateTimeConverter.TIME", FacesMessage.SEVERITY_ERROR, params);
         }
         else if (calendar.hasTime()) {
-            message = MessageFactory.getFacesMessage("javax.faces.converter.DateTimeConverter.DATETIME", FacesMessage.SEVERITY_ERROR, params);
+            message = MessageFactory.getFacesMessage(context, "javax.faces.converter.DateTimeConverter.DATETIME", FacesMessage.SEVERITY_ERROR, params);
         }
         else {
-            message = MessageFactory.getFacesMessage("javax.faces.converter.DateTimeConverter.DATE", FacesMessage.SEVERITY_ERROR, params);
+            message = MessageFactory.getFacesMessage(context, "javax.faces.converter.DateTimeConverter.DATE", FacesMessage.SEVERITY_ERROR, params);
         }
 
         return new ConverterException(message);
diff --git a/primefaces/src/main/java/org/primefaces/component/captcha/Captcha.java b/primefaces/src/main/java/org/primefaces/component/captcha/Captcha.java
index 9b02f88c43..d15821048c 100644
--- a/primefaces/src/main/java/org/primefaces/component/captcha/Captcha.java
+++ b/primefaces/src/main/java/org/primefaces/component/captcha/Captcha.java
@@ -122,7 +122,7 @@ public class Captcha extends CaptchaBase {
                     params[0] = ComponentUtils.getLabel(context, this);
                     params[1] = value;
 
-                    msg = MessageFactory.getFacesMessage(Captcha.INVALID_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, params);
+                    msg = MessageFactory.getFacesMessage(context, Captcha.INVALID_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, params);
                 }
 
                 context.addMessage(getClientId(context), msg);
diff --git a/primefaces/src/main/java/org/primefaces/component/datatable/DataTableRenderer.java b/primefaces/src/main/java/org/primefaces/component/datatable/DataTableRenderer.java
index ac184ce956..dcc8bf97a7 100644
--- a/primefaces/src/main/java/org/primefaces/component/datatable/DataTableRenderer.java
+++ b/primefaces/src/main/java/org/primefaces/component/datatable/DataTableRenderer.java
@@ -1653,7 +1653,7 @@ public class DataTableRenderer extends DataRenderer {
             writer.writeAttribute("id", reflowId + "_label", null);
             writer.writeAttribute("for", reflowId, null);
             writer.writeAttribute("class", "ui-reflow-label", null);
-            writer.writeText(MessageFactory.getMessage(DataTable.SORT_LABEL), null);
+            writer.writeText(MessageFactory.getMessage(context, DataTable.SORT_LABEL), null);
             writer.endElement("label");
 
             writer.startElement("select", null);
@@ -1665,8 +1665,8 @@ public class DataTableRenderer extends DataRenderer {
             for (Map.Entry<SortMeta, String> header : headers.entrySet()) {
                 for (int sortOrder = 0; sortOrder < 2; sortOrder++) {
                     String sortOrderLabel = (sortOrder == 0)
-                                      ? MessageFactory.getMessage(DataTable.SORT_ASC)
-                                      : MessageFactory.getMessage(DataTable.SORT_DESC);
+                                      ? MessageFactory.getMessage(context, DataTable.SORT_ASC)
+                                      : MessageFactory.getMessage(context, DataTable.SORT_DESC);
 
                     writer.startElement("option", null);
                     writer.writeAttribute("value", header.getKey().getColumnKey() + "_" + sortOrder, null);
diff --git a/primefaces/src/main/java/org/primefaces/component/fileupload/FileUpload.java b/primefaces/src/main/java/org/primefaces/component/fileupload/FileUpload.java
index bb26634f61..7f0bc838fb 100644
--- a/primefaces/src/main/java/org/primefaces/component/fileupload/FileUpload.java
+++ b/primefaces/src/main/java/org/primefaces/component/fileupload/FileUpload.java
@@ -112,7 +112,7 @@ public class FileUpload extends FileUploadBase {
 
     public void validateFilename(FacesContext context, UploadedFile file) {
         try {
-            FileUploadUtils.requireValidFilename(file.getFileName());
+            FileUploadUtils.requireValidFilename(context, file.getFileName());
         }
         catch (ValidatorException ve) {
             setValid(false);
diff --git a/primefaces/src/main/java/org/primefaces/component/password/Password.java b/primefaces/src/main/java/org/primefaces/component/password/Password.java
index 6fc36f22a8..91beb90edb 100644
--- a/primefaces/src/main/java/org/primefaces/component/password/Password.java
+++ b/primefaces/src/main/java/org/primefaces/component/password/Password.java
@@ -74,7 +74,7 @@ public class Password extends PasswordBase {
                     params[0] = ComponentUtils.getLabel(context, this);
                     params[1] = ComponentUtils.getLabel(context, matchWith);
 
-                    msg = MessageFactory.getFacesMessage(Password.INVALID_MATCH_KEY, FacesMessage.SEVERITY_ERROR, params);
+                    msg = MessageFactory.getFacesMessage(context, Password.INVALID_MATCH_KEY, FacesMessage.SEVERITY_ERROR, params);
                 }
 
                 context.addMessage(getClientId(context), msg);
diff --git a/primefaces/src/main/java/org/primefaces/component/picklist/PickList.java b/primefaces/src/main/java/org/primefaces/component/picklist/PickList.java
index c53fdc5be5..b3135fdef1 100644
--- a/primefaces/src/main/java/org/primefaces/component/picklist/PickList.java
+++ b/primefaces/src/main/java/org/primefaces/component/picklist/PickList.java
@@ -139,7 +139,7 @@ public class PickList extends PickListBase {
                 message = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
             }
             else {
-                message = MessageFactory.getFacesMessage(REQUIRED_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, label);
+                message = MessageFactory.getFacesMessage(facesContext, REQUIRED_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, label);
             }
             facesContext.addMessage(clientId, message);
             setValid(false);
@@ -173,7 +173,7 @@ public class PickList extends PickListBase {
             Object targetItem = targetEntries.get(i);
             // Check if target item exists in source list
             if (!oldSource.contains(targetItem) && !oldTarget.contains(targetItem)) {
-                FacesMessage message = MessageFactory.getFacesMessage(UPDATE_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, label);
+                FacesMessage message = MessageFactory.getFacesMessage(facesContext, UPDATE_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, label);
                 facesContext.addMessage(clientId, message);
                 setValid(false);
                 break;
@@ -207,7 +207,7 @@ public class PickList extends PickListBase {
             boolean itemDisabled = isItemDisabled();
             // Check if disabled item has been moved from its former/original list
             if (itemDisabled && !oldEntries.contains(item)) {
-                FacesMessage message = MessageFactory.getFacesMessage(UPDATE_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, label);
+                FacesMessage message = MessageFactory.getFacesMessage(facesContext, UPDATE_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, label);
                 facesContext.addMessage(clientId, message);
                 setValid(false);
                 break;
diff --git a/primefaces/src/main/java/org/primefaces/component/selectonemenu/SelectOneMenu.java b/primefaces/src/main/java/org/primefaces/component/selectonemenu/SelectOneMenu.java
index 375713fd5e..8f25becd12 100644
--- a/primefaces/src/main/java/org/primefaces/component/selectonemenu/SelectOneMenu.java
+++ b/primefaces/src/main/java/org/primefaces/component/selectonemenu/SelectOneMenu.java
@@ -149,7 +149,7 @@ public class SelectOneMenu extends SelectOneMenuBase {
                             requiredMessageStr);
                 }
                 else {
-                    message = MessageFactory.getFacesMessage(REQUIRED_MESSAGE_ID,
+                    message = MessageFactory.getFacesMessage(context, REQUIRED_MESSAGE_ID,
                             FacesMessage.SEVERITY_ERROR,
                             ComponentUtils.getLabel(context, this));
                 }
diff --git a/primefaces/src/main/java/org/primefaces/component/slider/Slider.java b/primefaces/src/main/java/org/primefaces/component/slider/Slider.java
index 3ed4589ac4..50050c4963 100644
--- a/primefaces/src/main/java/org/primefaces/component/slider/Slider.java
+++ b/primefaces/src/main/java/org/primefaces/component/slider/Slider.java
@@ -156,7 +156,7 @@ public class Slider extends SliderBase {
             }
             else {
                 Object params = ComponentUtils.getLabel(context, this);
-                msg = MessageFactory.getFacesMessage(VALUE_OUT_OF_RANGE, FacesMessage.SEVERITY_ERROR, params);
+                msg = MessageFactory.getFacesMessage(context, VALUE_OUT_OF_RANGE, FacesMessage.SEVERITY_ERROR, params);
             }
             context.addMessage(getClientId(context), msg);
         }
diff --git a/primefaces/src/main/java/org/primefaces/component/treetable/TreeTableBase.java b/primefaces/src/main/java/org/primefaces/component/treetable/TreeTableBase.java
index b8886533fe..34a8d8d693 100644
--- a/primefaces/src/main/java/org/primefaces/component/treetable/TreeTableBase.java
+++ b/primefaces/src/main/java/org/primefaces/component/treetable/TreeTableBase.java
@@ -178,7 +178,7 @@ public abstract class TreeTableBase extends UITree implements Widget, ClientBeha
     }
 
     public String getEmptyMessage() {
-        return (String) getStateHelper().eval(PropertyKeys.emptyMessage, MessageFactory.getMessage(UIPageableData.EMPTY_MESSAGE));
+        return (String) getStateHelper().eval(PropertyKeys.emptyMessage, MessageFactory.getMessage(getFacesContext(), UIPageableData.EMPTY_MESSAGE));
     }
 
     public void setEmptyMessage(String emptyMessage) {
diff --git a/primefaces/src/main/java/org/primefaces/util/FileUploadUtils.java b/primefaces/src/main/java/org/primefaces/util/FileUploadUtils.java
index d6518c4554..088ae62d7e 100644
--- a/primefaces/src/main/java/org/primefaces/util/FileUploadUtils.java
+++ b/primefaces/src/main/java/org/primefaces/util/FileUploadUtils.java
@@ -90,31 +90,31 @@ public class FileUploadUtils {
      * @param filename the name of the file to check
      * @return the extracted file name
      */
-    public static String requireValidFilename(String filename) {
+    public static String requireValidFilename(FacesContext context, String filename) {
         if (LangUtils.isBlank(filename)) {
-            throw validationError(MessageFactory.getMessage(FILENAME_EMPTY));
+            throw validationError(MessageFactory.getMessage(context, FILENAME_EMPTY));
         }
 
         // use java.nio Paths to detect a bad character
         Character ch = containsInvalidCharacters(filename);
         if (ch != null) {
-            throw validationError(MessageFactory.getMessage(FILENAME_INVALID_CHAR, filename, ch));
+            throw validationError(MessageFactory.getMessage(context, FILENAME_INVALID_CHAR, filename, ch));
         }
 
         // Windows and Linux have different allowed characters
         if (isSystemWindows()) {
             if (!INVALID_FILENAME_WINDOWS.matcher(filename).find()) {
-                throw validationError(MessageFactory.getMessage(FILENAME_INVALID_WINDOWS, filename));
+                throw validationError(MessageFactory.getMessage(context, FILENAME_INVALID_WINDOWS, filename));
             }
         }
         else if (INVALID_FILENAME_LINUX.matcher(filename).find()) {
-            throw validationError(MessageFactory.getMessage(FILENAME_INVALID_LINUX, filename));
+            throw validationError(MessageFactory.getMessage(context, FILENAME_INVALID_LINUX, filename));
         }
 
         // URL encoded characters like %20 are invalid
         Matcher encodedMatcher = ENCODED_CHARS_PAT.matcher(filename);
         if (encodedMatcher.find() ) {
-            throw validationError(MessageFactory.getMessage(FILENAME_INVALID_CHAR, filename, encodedMatcher.group()));
+            throw validationError(MessageFactory.getMessage(context, FILENAME_INVALID_CHAR, filename, encodedMatcher.group()));
         }
 
         return FilenameUtils.getName(filename);
diff --git a/primefaces/src/main/java/org/primefaces/util/MessageFactory.java b/primefaces/src/main/java/org/primefaces/util/MessageFactory.java
index 28fbd359d9..aeb91b4cdd 100644
--- a/primefaces/src/main/java/org/primefaces/util/MessageFactory.java
+++ b/primefaces/src/main/java/org/primefaces/util/MessageFactory.java
@@ -55,16 +55,15 @@ public class MessageFactory {
         // NOOP
     }
 
-    public static FacesMessage getFacesMessage(String messageId, FacesMessage.Severity severity, Object... params) {
-        FacesMessage facesMessage = getFacesMessage(LocaleUtils.getCurrentLocale(), messageId, params);
+    public static FacesMessage getFacesMessage(FacesContext context, String messageId, FacesMessage.Severity severity, Object... params) {
+        FacesMessage facesMessage = getFacesMessage(context, LocaleUtils.getCurrentLocale(context), messageId, params);
         facesMessage.setSeverity(severity);
         return facesMessage;
     }
 
-    public static FacesMessage getFacesMessage(Locale locale, String messageId, Object... params) {
+    public static FacesMessage getFacesMessage(FacesContext facesContext, Locale locale, String messageId, Object... params) {
         String summary = null;
         String detail = null;
-        FacesContext facesContext = FacesContext.getCurrentInstance();
         Application application = facesContext.getApplication();
         String userBundleName = application.getMessageBundle();
         ResourceBundle bundle = null;
@@ -132,13 +131,12 @@ public class MessageFactory {
         return new FacesMessage(summary, detail);
     }
 
-    public static String getMessage(String messageId, Object... params) {
-        return getMessage(LocaleUtils.getCurrentLocale(), messageId, params);
+    public static String getMessage(FacesContext facesContext, String messageId, Object... params) {
+        return getMessage(facesContext, LocaleUtils.getCurrentLocale(facesContext), messageId, params);
     }
 
-    public static String getMessage(Locale locale, String messageId, Object... params) {
+    public static String getMessage(FacesContext facesContext, Locale locale, String messageId, Object... params) {
         String summary = null;
-        FacesContext facesContext = FacesContext.getCurrentInstance();
         Application application = facesContext.getApplication();
         String userBundleName = application.getMessageBundle();
         ResourceBundle bundle = null;
diff --git a/primefaces/src/main/java/org/primefaces/validate/FileValidator.java b/primefaces/src/main/java/org/primefaces/validate/FileValidator.java
index 65b865875b..8227f6c1f1 100644
--- a/primefaces/src/main/java/org/primefaces/validate/FileValidator.java
+++ b/primefaces/src/main/java/org/primefaces/validate/FileValidator.java
@@ -108,7 +108,7 @@ public class FileValidator extends AbstractPrimeValidator implements ClientValid
         Integer fileLimit = getFileLimit();
         if (fileLimit != null && uploadedFiles.getFiles().size() > fileLimit) {
             throw new ValidatorException(
-                    MessageFactory.getFacesMessage(FILE_LIMIT_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, fileLimit));
+                    MessageFactory.getFacesMessage(context, FILE_LIMIT_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, fileLimit));
         }
 
         long totalSize = 0;
@@ -120,7 +120,7 @@ public class FileValidator extends AbstractPrimeValidator implements ClientValid
         Long sizeLimit = getSizeLimit();
         if (sizeLimit != null && totalSize > sizeLimit) {
             throw new ValidatorException(
-                    MessageFactory.getFacesMessage(SIZE_LIMIT_MESSAGE_ID, FacesMessage.SEVERITY_ERROR,
+                    MessageFactory.getFacesMessage(context, SIZE_LIMIT_MESSAGE_ID, FacesMessage.SEVERITY_ERROR,
                             "*", FileUploadUtils.formatBytes(sizeLimit, LocaleUtils.getCurrentLocale(context))));
         }
     }
@@ -131,14 +131,14 @@ public class FileValidator extends AbstractPrimeValidator implements ClientValid
         Long sizeLimit = getSizeLimit();
         if (sizeLimit != null && uploadedFile.getSize() > sizeLimit) {
             throw new ValidatorException(
-                    MessageFactory.getFacesMessage(SIZE_LIMIT_MESSAGE_ID, FacesMessage.SEVERITY_ERROR,
+                    MessageFactory.getFacesMessage(context, SIZE_LIMIT_MESSAGE_ID, FacesMessage.SEVERITY_ERROR,
                             uploadedFile.getFileName(), FileUploadUtils.formatBytes(sizeLimit, LocaleUtils.getCurrentLocale(context))));
         }
 
         String allowTypes = getAllowTypes();
         if (!FileUploadUtils.isValidType(applicationContext, uploadedFile, allowTypes, accept)) {
             throw new ValidatorException(
-                    MessageFactory.getFacesMessage(ALLOW_TYPES_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, uploadedFile.getFileName(),
+                    MessageFactory.getFacesMessage(context, ALLOW_TYPES_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, uploadedFile.getFileName(),
                             FileUploadUtils.formatAllowTypes(allowTypes)));
         }
 
diff --git a/primefaces/src/main/java/org/primefaces/virusscan/impl/ClamDaemonScanner.java b/primefaces/src/main/java/org/primefaces/virusscan/impl/ClamDaemonScanner.java
index ad706ff96f..16e8122b24 100644
--- a/primefaces/src/main/java/org/primefaces/virusscan/impl/ClamDaemonScanner.java
+++ b/primefaces/src/main/java/org/primefaces/virusscan/impl/ClamDaemonScanner.java
@@ -116,7 +116,7 @@ public class ClamDaemonScanner implements VirusScanner {
     }
 
     protected String createErrorMessage(UploadedFile file, String response) {
-        return MessageFactory.getMessage("primefaces.fileupload.CLAM_AV_FILE", file.getFileName(), response);
+        return MessageFactory.getMessage(FacesContext.getCurrentInstance(), "primefaces.fileupload.CLAM_AV_FILE", file.getFileName(), response);
     }
 
     /**
diff --git a/primefaces/src/main/java/org/primefaces/virusscan/impl/VirusTotalReportScanner.java b/primefaces/src/main/java/org/primefaces/virusscan/impl/VirusTotalReportScanner.java
index 0e6a4eee37..58e6057096 100644
--- a/primefaces/src/main/java/org/primefaces/virusscan/impl/VirusTotalReportScanner.java
+++ b/primefaces/src/main/java/org/primefaces/virusscan/impl/VirusTotalReportScanner.java
@@ -107,7 +107,7 @@ public class VirusTotalReportScanner implements VirusScanner {
     }
 
     protected String createErrorMessage(UploadedFile file, JSONObject json) {
-        return MessageFactory.getMessage("primefaces.fileupload.VIRUS_TOTAL_FILE", file.getFileName());
+        return MessageFactory.getMessage(FacesContext.getCurrentInstance(), "primefaces.fileupload.VIRUS_TOTAL_FILE", file.getFileName());
     }
 
     protected URLConnection openConnection(UploadedFile file) throws IOException {


Test patch:
diff --git a/primefaces/src/test/java/org/primefaces/util/FileUploadUtilsTest.java b/primefaces/src/test/java/org/primefaces/util/FileUploadUtilsTest.java
index 9cb38ce95a..5d267e9899 100644
--- a/primefaces/src/test/java/org/primefaces/util/FileUploadUtilsTest.java
+++ b/primefaces/src/test/java/org/primefaces/util/FileUploadUtilsTest.java
@@ -315,7 +315,7 @@ class FileUploadUtilsTest {
         String filename = "~myfile.txt";
 
         // Act
-        String result = FileUploadUtils.requireValidFilename(filename);
+        String result = FileUploadUtils.requireValidFilename(context, filename);
 
         // Assert
         assertEquals(filename, result);
@@ -324,10 +324,10 @@ class FileUploadUtilsTest {
     @Test
     void requireValidFilename_Valid() {
         // valid files
-        assertEquals("someFileWithUmlautsßöäü.txt", FileUploadUtils.requireValidFilename("someFileWithUmlautsßöäü.txt"));
-        assertEquals("StringFinder.exe", FileUploadUtils.requireValidFilename("StringFinder.exe"));
-        assertEquals("January.xlsx", FileUploadUtils.requireValidFilename("January.xlsx"));
-        assertEquals("TravelBrochure.pdf", FileUploadUtils.requireValidFilename("TravelBrochure.pdf"));
+        assertEquals("someFileWithUmlautsßöäü.txt", FileUploadUtils.requireValidFilename(context, "someFileWithUmlautsßöäü.txt"));
+        assertEquals("StringFinder.exe", FileUploadUtils.requireValidFilename(context, "StringFinder.exe"));
+        assertEquals("January.xlsx", FileUploadUtils.requireValidFilename(context, "January.xlsx"));
+        assertEquals("TravelBrochure.pdf", FileUploadUtils.requireValidFilename(context, "TravelBrochure.pdf"));
     }
 
     @Test
@@ -339,7 +339,7 @@ class FileUploadUtilsTest {
             String[] invalidCharacters = {"/", "\0", ":", "*", "?", "\"", "<", ">", "|"};
             for (String character : invalidCharacters) {
                 String invalidFileName = "filename_with_" + character;
-                assertThrows(FacesException.class, () -> FileUploadUtils.requireValidFilename(invalidFileName));
+                assertThrows(FacesException.class, () -> FileUploadUtils.requireValidFilename(context, invalidFileName));
             }
         }
     }
@@ -354,85 +354,72 @@ class FileUploadUtilsTest {
             String[] invalidCharacters = {"/", "\\\\", ":", "*", "?", "\"", "<", ">", "|"};
             for (String character : invalidCharacters) {
                 String invalidFileName = "filename_with_" + character;
-                assertThrows(FacesException.class, () -> FileUploadUtils.requireValidFilename(invalidFileName));
+                assertThrows(FacesException.class, () -> FileUploadUtils.requireValidFilename(context, invalidFileName));
             }
 
             // Add reserved device names as invalid filenames
             String[] reservedDeviceNames = {"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2",
                 "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };
             for (String deviceName : reservedDeviceNames) {
-                assertThrows(FacesException.class, () -> FileUploadUtils.requireValidFilename(deviceName));
+                assertThrows(FacesException.class, () -> FileUploadUtils.requireValidFilename(context, deviceName));
             }
         }
     }
 
     @Test
     void requireValidFilename_EncodedChars() {
-        try (MockedStatic<FacesContext> facesContext = Mockito.mockStatic(FacesContext.class)) {
-            facesContext.when(FacesContext::getCurrentInstance).thenReturn(context);
-
-            try (MockedStatic<FileUploadUtils> fileUploadUtilsMockedStatic = Mockito.mockStatic(FileUploadUtils.class, Mockito.CALLS_REAL_METHODS)) {
-                fileUploadUtilsMockedStatic.when(FileUploadUtils::isSystemWindows).thenReturn(false);
-
-                // Check each invalid character individually
-                String[] invalidCharacters = {"%20", "%21"};
-                for (String ch : invalidCharacters) {
-                    String invalidFileName = "filename_with_" + ch;
-                    ValidatorException ex = assertThrows(ValidatorException.class, () -> FileUploadUtils.requireValidFilename(invalidFileName));
-                    assertEquals("Invalid filename: filename_with_" + ch + " contains invalid character: " + ch, ex.getFacesMessage().getDetail());
-                }
+        try (MockedStatic<FileUploadUtils> fileUploadUtilsMockedStatic = Mockito.mockStatic(FileUploadUtils.class, Mockito.CALLS_REAL_METHODS)) {
+            fileUploadUtilsMockedStatic.when(FileUploadUtils::isSystemWindows).thenReturn(false);
+
+            // Check each invalid character individually
+            String[] invalidCharacters = {"%20", "%21"};
+            for (String ch : invalidCharacters) {
+                String invalidFileName = "filename_with_" + ch;
+                ValidatorException ex = assertThrows(ValidatorException.class, () -> FileUploadUtils.requireValidFilename(context, invalidFileName));
+                assertEquals("Invalid filename: filename_with_" + ch + " contains invalid character: " + ch, ex.getFacesMessage().getDetail());
             }
         }
     }
 
     @Test
     void requireValidFilename_NotNull() {
-        try (MockedStatic<FacesContext> facesContext = Mockito.mockStatic(FacesContext.class)) {
-            facesContext.when(FacesContext::getCurrentInstance).thenReturn(context);
-            // Arrange
-            String invalidFileName = null;
+        // Arrange
+        String invalidFileName = null;
 
-            // Act
-            ValidatorException ex = assertThrows(ValidatorException.class, () -> FileUploadUtils.requireValidFilename(invalidFileName));
+        // Act
+        ValidatorException ex = assertThrows(ValidatorException.class, () -> FileUploadUtils.requireValidFilename(context, invalidFileName));
 
-            // Assert
-            assertEquals("Filename cannot be empty or null", ex.getFacesMessage().getDetail());
-        }
+        // Assert
+        assertEquals("Filename cannot be empty or null", ex.getFacesMessage().getDetail());
     }
 
     @Test
     void requireValidFilename_NotBlank() {
-        try (MockedStatic<FacesContext> facesContext = Mockito.mockStatic(FacesContext.class)) {
-            facesContext.when(FacesContext::getCurrentInstance).thenReturn(context);
-            // Arrange
-            String invalidFileName = " ";
+        // Arrange
+        String invalidFileName = " ";
 
-            // Act
-            ValidatorException ex = assertThrows(ValidatorException.class, () -> FileUploadUtils.requireValidFilename(invalidFileName));
+        // Act
+        ValidatorException ex = assertThrows(ValidatorException.class, () -> FileUploadUtils.requireValidFilename(context, invalidFileName));
 
-            // Assert
-            assertEquals("Filename cannot be empty or null", ex.getFacesMessage().getDetail());
-        }
+        // Assert
+        assertEquals("Filename cannot be empty or null", ex.getFacesMessage().getDetail());
     }
 
     @Test
     void requireValidFilename_InvalidCharacters() {
-        try (MockedStatic<FacesContext> facesContext = Mockito.mockStatic(FacesContext.class)) {
-            facesContext.when(FacesContext::getCurrentInstance).thenReturn(context);
-            // Arrange
-            String invalidFileName = "file:*";
-            String os = System.getProperty("os.name").toLowerCase();
+        // Arrange
+        String invalidFileName = "file:*";
+        String os = System.getProperty("os.name").toLowerCase();
 
-            // Act
-            ValidatorException ex = assertThrows(ValidatorException.class, () -> FileUploadUtils.requireValidFilename(invalidFileName));
+        // Act
+        ValidatorException ex = assertThrows(ValidatorException.class, () -> FileUploadUtils.requireValidFilename(context, invalidFileName));
 
-            // Assert
-            if (os.contains("win")) {
-                assertEquals("Invalid filename: file:* contains invalid character: :", ex.getFacesMessage().getDetail());
-            }
-            else {
-                assertEquals("Invalid Linux filename: file:*", ex.getFacesMessage().getDetail());
-            }
+        // Assert
+        if (os.contains("win")) {
+            assertEquals("Invalid filename: file:* contains invalid character: :", ex.getFacesMessage().getDetail());
+        }
+        else {
+            assertEquals("Invalid Linux filename: file:*", ex.getFacesMessage().getDetail());
         }
     }
 
@@ -444,11 +431,11 @@ class FileUploadUtilsTest {
 
     @Test
     void requireValidFilename() {
-        assertDoesNotThrow(() -> FileUploadUtils.requireValidFilename("someFileWithUmlautsßöäü.txt"));
+        assertDoesNotThrow(() -> FileUploadUtils.requireValidFilename(context, "someFileWithUmlautsßöäü.txt"));
         // validate fileName which contains /, which is invalid in a filename
-        assertThrows(FacesException.class, () -> FileUploadUtils.requireValidFilename("someFil/eWithUmlautsßöäü.txt"));
+        assertThrows(FacesException.class, () -> FileUploadUtils.requireValidFilename(context, "someFil/eWithUmlautsßöäü.txt"));
         // validate fileName which contains <, which is invalid in a filename
-        assertThrows(FacesException.class, () -> FileUploadUtils.requireValidFilename("someFil<eWithUmlautsßöäü.txt"));
+        assertThrows(FacesException.class, () -> FileUploadUtils.requireValidFilename(context, "someFil<eWithUmlautsßöäü.txt"));
     }
 
 }
\ No newline at end of file