0 Replies Latest reply on Sep 30, 2009 4:12 AM by Walter White

    FacesMessages in servlet filter

    Walter White Novice
      I am trying to use FacesMessages within a servlet filter inside a ContextualHttpServletRequest.  My reasoning for interacting with the FacesMessages component here is that I can notify the user of asynchronous events that occur.  Here is my filter:

      public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain)
                                              throws IOException, ServletException
                LOGGER.debug("processing event notifications");

                FacesMessages facesMessages = FacesMessages.instance();

                HttpRequest httpRequest = HttpRequestUtil.getHttpRequest((HttpServletRequest)servletRequest);
                Map<String, List<StatusMessage>> statusMessageMap = (Map<String, List<StatusMessage>>)Component.getInstance("statusMessageMap");

                if(statusMessageMap != null)
                     LOGGER.debug("adding status messages to faces context.");

                     List<StatusMessage> sessionStatusMessages = StatusMessageUtil.removeAll(httpRequest, statusMessageMap);

                     if(sessionStatusMessages != null)
                          LOGGER.debug("adding status messages to faces messages");
                          for(StatusMessage statusMessage : sessionStatusMessages)
                               LOGGER.debug("adding status message:" + statusMessage.getMessageKey() + "@" + statusMessage.getSeverity());
                               facesMessages.addFromResourceBundle(statusMessage.getSeverity(), statusMessage.getMessageKey(), statusMessage.getParameters());
                          LOGGER.debug("no status messages to add to faces context.");
                     LOGGER.debug("status message map is null.");

                filterChain.doFilter(servletRequest, servletResponse);

      Right now, I'm using this to notify users if another user attempted to hijack their session.  In the logs, I see the message is being added, but I never actually see it added on the screen.  For my other Seam components, I am adding them the same way:

      facesMessages.addFromResourceBundle(StatusMessage.Severity.INFO, "Thanks for contacting us, we will read your message shortly.");

      As a note, I would like to use the actual message key, but it doesn't seem that Seam is resolving them even though they're in my messages_en.properties file.

      facesMessages.addFromResourceBundle(StatusMessage.Severity.INFO, "contactAction.successMessage");

      Also, I don't know if it means anything, but when I try to add the messages within my servlet filter, the FacesContext.getCurrentInstance() is null.  The code above doesn't reference it presently, but I had a log statement earlier to see if that was null.  I was thinking that if the FacesContext is null, how can I add messages?

      Can I manipulate the FacesMessages this way?  Also, what is the proper way to reference messages in messages.properties?