4 Replies Latest reply on Apr 2, 2008 9:30 AM by mbakkali

    redirect to current page after a NotLoggedInException

      I would like to display an error message on the page
      saying that This operation requires to be logged in
      and I don't want to be redirected to any other page.


      In my pages.xml, I have somewhere:


      <exception class=....>


      <redirect view-id="#{some EL}"...>
          <message .../>
      </redirect>
      </exception/>


      I would like to replace the some EL by an EL
      that would be my current page.


      Is this possible?


      Thanks in advance!

        • 1. Re: redirect to current page after a NotLoggedInException
          norman

          How about #{facesContext.viewRoot.viewId}?


          • 2. Re: redirect to current page after a NotLoggedInException

            Nope. Didn't work.


            In pages.xml, I tried to also add this at the topic at the login-view-id attribute. It didn't even resolve EL.
            (I saw the same #{facesContext...} in the browser
            for a brief instant).


            Also I had to add / before EL because Seam complained about
            the fact that a view-id must start with /.


            Maybe there is another way to do this?

            • 3. Re: redirect to current page after a NotLoggedInException
              norman

              Ah.  I see.  The reason that expression doesn't work is that we are in the ExceptionFilter, which operates outside of the faces lifecycle and uses the Mock contexts, not the real ones. The mock contexts don't remember the viewId you were accessing.  If you look  at RedirectHandler.java, you can see the logic we use to reconstruct the viewId from the the servlet path.  It's not terribly robust, but it should work for you. Here's what I tested:



              @Name("viewId")
              @Scope(ScopeType.STATELESS)
              public class ViewId
              {
                  public String getCurrentValue() {
                      String servletPath = ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getServletPath();
                      return servletPath.substring(0, servletPath.lastIndexOf('.')) + Pages.getSuffix();    }
              }
              



              That works with:


              <redirect view-id="#{viewId.currentValue}"> 
              




              There should be a way to do this without crazy hacks.  If you open a JIRA, I'll make sure we provide a better solution.



              • 4. Re: redirect to current page after a NotLoggedInException

                That sounds like a good hack!


                However the only thing is that the method getCurrentValue()
                resolves to the page I was trying to access (instead of the current page I was at).


                I guess you tested this calling a method with a
                @Restrict("#{identity.loggedIn}")
                annotation.


                In my case, I am clicking on a link to a restricted page (not method/class) so the current page is not the same I guess from Seam's stand point.


                I guess now the goal is to find out how to get the current page.