4 Replies Latest reply on May 27, 2013 2:57 AM by hamzabenmansour

    ClassCastException when using Flash scope and Ajax

    hamzabenmansour

      Hi,

       

      I have a JSF 2.0 portlet running on eXo Platform 3.5 and JBoss EAP 5 using portletBridge 3.1.2.Final. I defined an error page for my portlet which may display the exception message. When an error occur, a custom ExceptionHandler extracts the message, puts it in the flash scope and redirects to the error page after calling the NavigationHandler (with a redirect).

       

      I have one complex page comprised of different forms submitted with a4j:commandLink components. when I submit one form and an unexpected Exception occurs, the error page is not diplayed and I have in the logs

       

       

      javax.faces.FacesException: javax.faces.FacesException:  .....

      .....

       

      Caused by: java.lang.ClassCastException: org.jboss.portletbridge.context.flash.FlashHttpServletAdapter cannot be cast to javax.portlet.PortletResponse

              at org.jboss.portletbridge.context.PortletExternalContextImpl.getPortletResponse(PortletExternalContextImpl.java:186)

              at org.jboss.portletbridge.context.PortletExternalContextImpl.getNamespace(PortletExternalContextImpl.java:195)

              at org.jboss.portletbridge.context.AbstractExternalContext.encodeNamespace(AbstractExternalContext.java:152)

              at javax.faces.context.ExternalContextWrapper.encodeNamespace(ExternalContextWrapper.java:115)

              at javax.portlet.faces.component.PortletNamingContainerUIViewRoot.setId(PortletNamingContainerUIViewRoot.java:59)

              at javax.faces.component.UIViewRoot.<init>(UIViewRoot.java:211)

              at javax.portlet.faces.component.PortletNamingContainerUIViewRoot.<init>(PortletNamingContainerUIViewRoot.java:47)

              at sun.reflect.GeneratedConstructorAccessor529.newInstance(Unknown Source)

              at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

              at java.lang.reflect.Constructor.newInstance(Constructor.java:532)

              at java.lang.Class.newInstance0(Class.java:372)

              at java.lang.Class.newInstance(Class.java:325)

              at com.sun.faces.application.ApplicationImpl.newThing(ApplicationImpl.java:1727)

       

       

       

      When, debugging, the implementation of the externalContext used is the ResourceRequestExternalContextImpl (due to the AJAX request) where we cast the response, which is in our case a FlashHttpServletAdapter to a PortletResponse.

       

      The work around in my case was to simply remove the use of the flash scope.

       

      Is this scope supported ? Or am I misusing the API ?

       

      Thanks a lot for your help

        • 1. Re: ClassCastException when using Flash scope and Ajax
          kenfinni

          It's a little difficult to tell from the above what the problem might actually be.

           

          Can you include the code where set the information into the flash scope and then how you retrieve it?

           

          Thanks

          Ken

          • 2. Re: ClassCastException when using Flash scope and Ajax
            hamzabenmansour

            Hi Ken,

             

            The code used in my ExceptionHandlerWrapper implementation is :

             

             

             

            try {

            ELFlash.getFlash().put("exceptionMessage", throwable.getMessage());

            throwable.printStackTrace(pw);

            ELFlash.getFlash().put("exceptionTrace", sw.toString());

            ELFlash.getFlash().put("afficherErreur", Boolean.valueOf(afficherException));

            nav.handleNavigation(fc, null, "/error?faces-redirect=true");

            fc.renderResponse();

            } finally {

            iterator.remove();

            }

             

             

            and in my error.xhtml :

             

             

            ....

            <h:outputText value="#{flash.exceptionTrace}" rendered="#{flash.afficherErreur == true}"/>

             

            ....

             

             

            Thanks for your reply.

             

            Regards

            • 3. Re: ClassCastException when using Flash scope and Ajax
              kenfinni

              Hamza,

               

              It looks like you're retrieving it through EL ok, but instead of calling ELFlash.getFlash(), you're better of getting the Flash with:

               

              FacesContext.getCurrentInstance().getExternalContext().getFlash().

               

              as then it's more portable.

               

              It's also possible that you're hitting this bug, which would mean needing to upgrade to PBR 3.2.0.Final to resolve it.

               

              Ken

              1 of 1 people found this helpful
              • 4. Re: ClassCastException when using Flash scope and Ajax
                hamzabenmansour

                Hi Ken,

                 

                Thank you for your helpfull answer. I'll try you suggestions.

                 

                Regards