Uploaded image for project: 'MyFaces Core'
  1. MyFaces Core
  2. MYFACES-4354

ExternalContext#redirect is not add the windowId to url params.

    XMLWordPrintableJSON

Details

    • Wish
    • Status: Closed
    • Major
    • Resolution: Won't Fix
    • 2.3-next-M3
    • None
    • JSR-372
    • None
    • MyFaces 2.3-next-M3
      PrimeFaces 9.0-SNAPSHOT (local install from https://github.com/primefaces/primefaces)

    Description

      When I navigate to another view from standard navigation, windowId is a part of url.

       

      action="/AnotherView.xhtml"

       

      URL contains windowId ...AnotherView.xhtml?jfwid=XXXX

       

      But when i redirect from code like this

       

      String requestContextPath = facesContext.getExternalContext().getRequestContextPath();
      facesContext.getExternalContext().redirect(requestContextPath + "/myTarget");
      

      in my HttpServlet request does not contain a windowId parameter.

       

      @WebServlet(name = "MyServlet", urlPatterns = { "/myTarget" })
      public class MyServlet extends HttpServlet {
      
        @Override
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          final String windowId = request.getParameter(ConversationConsts.WINDOW_ID_PARAM); // null
          // 
      }
      

       

       

       

      The ExternalContext#redirect method should add jfwid parameter to the url if the URL does not already contain jfwid parameter.

      This patch works for me.

       

      public void redirect(String url) throws IOException
      {
        FacesContext facesContext = getCurrentFacesContext();
        PartialViewContext partialViewContext = facesContext.getPartialViewContext(); 
        if (partialViewContext.isPartialRequest())
        {
          PartialResponseWriter writer = partialViewContext.getPartialResponseWriter();
          this.setResponseContentType("text/xml");
          this.setResponseCharacterEncoding("UTF-8");
          this.addResponseHeader("Cache-control", "no-cache");
          writer.startDocument();
          writer.redirect(url);
          writer.endDocument();
          facesContext.responseComplete();
        }
        else if (_servletResponse instanceof HttpServletResponse)
        {
          ExternalContext externalContext = facesContext.getExternalContext();
          Map<String, String> requestParameterMap = externalContext.getRequestParameterMap();
          String windowId = requestParameterMap.get(ResponseStateManager.CLIENT_WINDOW_URL_PARAM);
          if (windowId != null)
          {
            url = addWindowIdIfNecessary(url, windowId);
          }
          ((HttpServletResponse) _servletResponse).sendRedirect(url);
          facesContext.responseComplete();
        }
        else
        {
          throw new IllegalArgumentException("Only HttpServletResponse supported");
        }
      }
      
      private String addWindowIdIfNecessary(String url, String windowId)
      {
        if(url.contains(ResponseStateManager.CLIENT_WINDOW_URL_PARAM + "="))
        {
          return url;
        }
            
        StringBuilder newUrl = new StringBuilder(url);
        if(url.contains("?"))
        {
          newUrl.append("&");
        }
        else
        {
          newUrl.append("?");
        }
        newUrl.append(ResponseStateManager.CLIENT_WINDOW_URL_PARAM);
        newUrl.append("=");
        newUrl.append(windowId);
        return newUrl.toString();
      }
      

       

       

       

      My settings:

      <factory> 
        <lifecycle-factory>org.primefaces.clientwindow.PrimeClientWindowLifecycleFactory</lifecycle-factory>
      </factory>
      
      <context-param>
        <param-name>javax.faces.CLIENT_WINDOW_MODE</param-name>
        <param-value>url</param-value>
      </context-param>
      

      MyFaces 2.3-next-M3
      PrimeFaces 9.0-SNAPSHOT (local install from https://github.com/primefaces/primefaces)

      Attachments

        Activity

          People

            Unassigned Unassigned
            krpata Jan Krpata
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: