1 2 Previous Next 22 Replies Latest reply on Mar 9, 2008 9:09 AM by zjsun

    share my some fix for my portal impl

      I'v locally fixed some classes for my portal impl, but i'm not sure whether it's correct.

      FYI.

        • 1. Re: share my some fix for my portal impl

          all fix has a "zjsun fix" note.

          Follow me...

          org.jboss.seam.jsf.SeamPhaseListenerWrapper:

          private void beforePortletPhase(PhaseEvent event,
           FacesContext facesContext, PortletPhase portletPhase) {
          
           boolean notInitialised = false;
           if (event.getPhaseId() == RESTORE_VIEW) {
           _defaultListener.beforeRestoreView(facesContext);
           }
          
           // // TODO: zjsun fix here
           // boolean conversationFound = false;
           // Map parameters = facesContext.getExternalContext()
           // .getRequestParameterMap();
          
           if (event.getPhaseId() == RENDER_RESPONSE
           && !Contexts.isApplicationContextActive()) {
           _defaultListener.beforeRestoreView(facesContext);
          
           // TODO:zjsun fix here :restore conversation context, pages
           _defaultListener.afterRestoreView(facesContext);
           // FacesLifecycle.resumePage();
           // ConversationPropagation.instance()
           // .restoreConversationId(parameters);
           // conversationFound = Manager.instance().restoreConversation();
           // FacesLifecycle
           // .resumeConversation(facesContext.getExternalContext());
          
           notInitialised = true;
           }
           // delegate to subclass:
           _defaultListener.handleTransactionsBeforePhase(event);
          
           if (event.getPhaseId() == RENDER_RESPONSE) {
           if (notInitialised) {
           // TODO:zjsun fix here : restore pages?
           // _defaultListener.afterRestoreView(facesContext);
           }
           _defaultListener.beforeRenderResponse(facesContext);
           }
           }
          


          • 2. Re: share my some fix for my portal impl

            org.jboss.portletbridge.AjaxPortletBridge:

            public void doFacesRequest(RenderRequest request, RenderResponse response)
             throws BridgeException {
             initRequest(request, response, Bridge.PortletPhase.RenderPhase);
             FacesContext facesContext = getFacesContext(request, response);
             PortletViewState windowState = PortletStateHolder.getInstance(
             facesContext).getWindowState(facesContext);
             try {
             if (null == response.getContentType()) {
             String contenttype = request.getResponseContentType();
             if (contenttype == null) {
             contenttype = "text/html";
             }
             String encoding = response.getCharacterEncoding();
             if (encoding == null) {
             encoding = "UTF-8";
             }
             response.setContentType(contenttype);
            
             }
             // set portletbridge title if its set.
             ResourceBundle bundle = portletConfig.getResourceBundle(request
             .getLocale());
             if (bundle != null) {
             String title = null;
             try {
             title = bundle.getString("javax.portlet.title");
             response.setTitle(title);
             } catch (Exception e) {
             // Ignore MissingResourceException
             }
             }
             String namespace = response.getNamespace();
             windowState.restoreRequest(facesContext, true);
             renderResponse(facesContext, windowState);
             // TODO - detect redirect case. Reset response, clear request
             // variables as far as Seam state.
             // Perform new render phase with a new ViewId.
             String redirectViewId = (String) facesContext.getExternalContext()
             .getRequestMap().get(
             AbstractExternalContext.REDIRECT_VIEW_ID);
             if (null != redirectViewId) {
            
             windowState.reset();
             windowState.setViewId(redirectViewId);
            
             // TODO: zjsun fix here 先�出�定�时�留的�数
             Object redirectParams = request
             .getAttribute(PortletViewState.REDIRECT_REQUEST_PARAMETERS_ATTRIBUTE);
            
             // release old FacesContext.
             facesContext.release();
             // Reset attributes to initial state
             ArrayList initialAttributes = (ArrayList) request
             .getAttribute(AbstractExternalContext.EXCLUDED_PARAMETERS_ATTRIBUTE);
             ArrayList currentAttributes = Collections.list(request
             .getAttributeNames());
             currentAttributes.removeAll(initialAttributes);
             for (Object newAttribute : currentAttributes) {
             request.removeAttribute((String) newAttribute);
             }
             request.setAttribute(AbstractExternalContext.VIEW_ID_PARAMETER,
             redirectViewId);
            
             // TODO: zjsun fix here ���定�时的�数
             if (redirectParams != null) {
             request.setAttribute(
             PortletViewState.REQUEST_PARAMETERS_ATTRIBUTE,
             redirectParams);
             }
            
             // Create new FacesContext
             facesContext = getFacesContext(request, response);
            
             renderResponse(facesContext, windowState);
             }
             // Set important Portal parameters to window state.
             String actionURL = facesContext.getApplication().getViewHandler()
             .getActionURL(facesContext,
             facesContext.getViewRoot().getViewId());
            
            // // TODO:zjsun fix here 添加stateId�数
            // PortalActionURL portalActionUrl = new PortalActionURL(actionURL);
            // portalActionUrl.addParameter(PortletStateHolder.STATE_ID_PARAMETER,
            // windowState.getStateId());
            // // actionURL = facesContext.getExternalContext().encodeActionURL(
            // // actionURL);
            // // windowState.setPortalActionURL(new PortalActionURL(actionURL));
            // String encodedActionUrl = facesContext.getExternalContext()
            // .encodeActionURL(portalActionUrl.toString());
            // portalActionUrl = PortletStateHolder
            // .decodeCmstarUrl(new PortalActionURL(encodedActionUrl));
            // // ä¿�è¯�å˜å…¥çš„æ˜¯æ£å¸¸çš„url(包括æ£å¸¸çš„servlet路径)而ä¸�是ç»�过cmstarç¼–ç �过的
            // //portalActionUrl.setPath(actionURL);
            // windowState.setPortalActionURL(portalActionUrl);
             actionURL = facesContext.getExternalContext().encodeActionURL(actionURL);
             windowState.setPortalActionURL(new PortalActionURL(actionURL));
            
            // PortletURL portletURL = response.createRenderURL();
            // portletURL.setParameter(PortletStateHolder.STATE_ID_PARAMETER,
            // windowState.getStateId());
            // // String renderUrl = portletURL.toString();
            // PortalActionURL portalRenderUrl = new PortalActionURL(portletURL
            // .toString());
            // // ä¿�è¯�å˜å…¥çš„æ˜¯æ£å¸¸çš„url(包括æ£å¸¸çš„servlet路径)而ä¸�是ç»�过cmstarç¼–ç �过的
            // portalRenderUrl = PortletStateHolder
            // .decodeCmstarUrl(portalRenderUrl);
            // //portalRenderUrl.setPath(actionURL);
            // windowState.setPortalRenderURL(portalRenderUrl);
            
             PortletURL portletURL = response.createRenderURL();
             portletURL.setParameter(PortletStateHolder.STATE_ID_PARAMETER, windowState.getStateId());
             String renderUrl = portletURL.toString();
             windowState.setPortalRenderURL(new PortalActionURL(renderUrl));
            
             windowState.setNamespace(namespace);
             // writer.println("</div>");
            
             //TODO:zjsun fix here 用facesContext.getExternalContext替代request
             //PortletSession portletSession = request.getPortletSession(true);
             PortletSession portletSession = (PortletSession)facesContext.getExternalContext().getSession(true);
            
             WindowIDRetriver idRetriver = (WindowIDRetriver) portletSession
             .getAttribute(PortletStateHolder.WINDOW_ID_RETRIVER);
             if (null != idRetriver) {
             windowState.setWindowId(idRetriver.getWindowID());
             }
            
             //TODO:zjsun fix here 用facesContext.getExternalContext替代request
             //Principal userPrincipal = request.getUserPrincipal();
             Principal userPrincipal = facesContext.getExternalContext().getUserPrincipal();
            
             // TODO - get user roles, defined in the portletbridge.xml ( ???
             // parse it ??? ), and store all values for a
             // "isUserInRole(roleName)" calls
            
             portletSession.setAttribute(
             AbstractExternalContext.PORTAL_USER_PRINCIPAL,
             userPrincipal, PortletSession.APPLICATION_SCOPE);
            
             if (log.isDebugEnabled()) {
             log.debug("Finish rendering portletbridge for namespace "
             + namespace);
             }
             // Disable portletbridge caching.
             // TODO - detect ajax components on page, static views can be
             // cached.
             response.setProperty(RenderResponse.EXPIRATION_CACHE, "0");
             } catch (Exception e) {
             response.reset();
             // TODO - handle exception
             log.error("Error processing execute lifecycle", e);
             exceptionHandler.processRenderException(facesContext, windowState,
             e);
             } finally {
             facesContext.release();
             }
             }
            


            private void renderResponse(FacesContext facesContext,
             PortletViewState windowState) throws FacesException {
             if (null == facesContext.getViewRoot()) {
             execute(facesContext);
             // TODO - detect redirect case.
             }
             //
             AjaxContext ajaxContext = AjaxContext.getCurrentInstance(facesContext);
             Map commonAjaxParameters = ajaxContext.getCommonAjaxParameters();
             commonAjaxParameters.put(PortletStateHolder.STATE_ID_PARAMETER,
             windowState.getStateId());
            
             // TODO:zjsun fix here åˆ¤æ–æ˜¯å�¦å·²ç»�结æ�Ÿresponse
             if (!facesContext.getResponseComplete()) {
             render(facesContext);
             }
             }
            


            • 3. Re: share my some fix for my portal impl

              org.jboss.portletbridge.lifecycle.RestoreViewPhase:

              public void executePhase(FacesContext context) {
               // Check pre-created ViewRoot.
               UIViewRoot viewRoot = context.getViewRoot();
               if (null != viewRoot) {
               if (_log.isDebugEnabled()) {
               _log.debug("Found created UIViewRoot in facesContext");
               }
               viewRoot.setLocale(context.getExternalContext().getRequestLocale());
               processBindings(context, viewRoot);
               } else {
               // Restore or Create new view.
               String viewId = calculateViewId(context);
               ViewHandler viewHandler = context.getApplication().getViewHandler();
               if (isFacesRequest(context)) {
               // TODO - check for a null and throw exception.
               viewRoot = viewHandler.restoreView(context, viewId);
               if (null != viewRoot) {
               processBindings(context, viewRoot);
               } else {
               // Error restore view. Session Expired ?
               throw new ViewExpiredException("Error restore view "
               + viewId + ", session expired?");
               }
               } else {
               viewRoot = viewHandler.createView(context, viewId);
              
               // TODO: zjsun fix here?
               context.renderResponse();
               }
               context.setViewRoot(viewRoot);
               }
               }
              


              private String calculateViewId(FacesContext context) {
               String viewId = null;
              
               // TODO:zjsun fix here 优先判æ–PortletRequestå…ˆ
               Object externalRequest = context.getExternalContext().getRequest();
               if (externalRequest instanceof PortletRequest) {
               viewId = PortletStateHolder.getInstance(context).getViewId(context);
               } else if (context.getExternalContext().getRequest() instanceof ServletRequest) {
               viewId = context.getExternalContext().getRequestPathInfo();
               if (null == viewId) {
               viewId = context.getExternalContext().getRequestServletPath();
               }
               } else {
               throw new FacesException();
               }
               return viewId;
               }
              


              • 4. Re: share my some fix for my portal impl

                org.jboss.portletbridge.lifecycle.PortletLifecycle:

                public void render(FacesContext context) throws FacesException {
                 // TODO - in the portletbridge mode, restore state, request scope
                 // variables and Faces Messages.
                 // Object request = context.getExternalContext().getRequest();
                 // if(request instanceof PortletRequest){
                 // executePhase.executePhase(context);
                 // PortletViewState windowState =
                 // PortletStateHolder.getInstance(context).getWindowState(context);
                 // windowState.restoreMessages(context);
                 // Map requestScopeBeans = windowState.getRequestScopeBeans();
                 // if(null != requestScopeBeans){
                 // context.getExternalContext().getRequestMap().putAll(requestScopeBeans);
                 // }
                 // }
                
                 // TODO:zjsun fix here åˆ¤æ–æ˜¯å�¦å·²ç»�response complete
                 if (!context.getResponseComplete()) {
                 renderPhase.execute(context);
                 }
                 }
                


                • 5. Re: share my some fix for my portal impl

                  org.jboss.portletbridge.context.ServletExternalContextImpl:

                  @Override
                   protected String createActionUrl(Map<String, String> parameters) {
                   PortalActionURL actionUrl = new PortalActionURL(portalActionUrl);
                  
                   // TODO:zjsun fix here 对cmstar的url进行处�
                   if (CmstarURL.isCmstarUrl(actionUrl)) {
                   CmstarURL cmstarUrl;
                   try {
                   cmstarUrl = new CmstarURL(actionUrl);
                   for (Entry<String, String> parameterEntry : parameters
                   .entrySet()) {
                   cmstarUrl.addParameter(parameterEntry.getKey(),
                   parameterEntry.getValue());
                   }
                   return cmstarUrl.toString();
                   } catch (MalformedURLException e) {
                   e.printStackTrace();
                   }
                   }
                  
                   for (Entry<String, String> parameterEntry : parameters.entrySet()) {
                   actionUrl.addParameter(parameterEntry.getKey(), parameterEntry
                   .getValue());
                   }
                   return actionUrl.toString();
                   }
                  


                  // TODO:zjsun fix here é‡�å†™æ¤æ–¹æ³•,以便更新é‡�定å�‘çš„viewId
                   @Override
                   public String encodeActionURL(String url) {
                   try {
                   PortalActionURL actionURL = new PortalActionURL(url);
                   String viewId = actionURL.getParameter(VIEW_ID_PARAMETER);
                   if (viewId == null
                   && actionURL.isInContext(this.getHttpRequest()
                   .getContextPath())) {
                   viewId = FacesUtil.getViewIdFromPath(actionURL.getPath());
                   actionURL.addParameter(VIEW_ID_PARAMETER, viewId);
                   url = actionURL.toString();
                   }
                   } catch (MalformedURLException e) {
                   e.printStackTrace();
                   }
                   return super.encodeActionURL(url);
                   }
                  


                  • 6. Re: share my some fix for my portal impl

                    org.jboss.portletbridge.context.RenderPortletExternalContextImpl:

                    protected Object getRequestParameter(String name) {
                     // TODO: zjsun fix here 增加返回数组判æ–.
                     Object retObj = getRequestParameterValues(name);
                     if (retObj == null) {
                     return null;
                     }
                    
                     if (retObj.getClass().isArray()) {
                     Object[] values = (Object[]) retObj;
                     return values[0];
                     } else {
                     return retObj;
                     }
                     }
                    


                    @Override
                     public void redirect(String url) throws IOException {
                     if (null == url) {
                     throw new NullPointerException("Path to redirect is null");
                     }
                    
                     PortalActionURL actionURL = new PortalActionURL(url);
                    
                     // TODO: zjsun fix here 对CMSTAR的actionUrl进行解�处�先,
                     // CMSTAR Url:?.p=ASFWERISFSADF......
                     if (CmstarURL.isCmstarUrl(actionURL)) {
                     CmstarURL cmstarUrl = new CmstarURL(actionURL);
                     actionURL = cmstarUrl.toPortalActionURL();
                     // 强制设定actionURL.isInContext(getRequestContextPath()
                     actionURL.setPath(getRequestContextPath() + ACTION_URL_DO_NOTHITG);
                     }
                    
                     if ((!actionURL.isInContext(getRequestContextPath()) && null == actionURL
                     .getParameter(VIEW_ID_PARAMETER))
                     || "true".equalsIgnoreCase(actionURL
                     .getParameter(Bridge.DIRECT_LINK))) {
                     // dispatch(url);
                    
                     // throw new IllegalStateException(
                     // "Redirect to new url not at action phase");
                     } else {
                     // HACK - if page is in the context, just treat it as navigation
                     // case
                     internalRedirect(actionURL);
                     }
                     }
                    


                    @SuppressWarnings("unchecked")
                     private Map<String, String[]> getSavedRequestParameters() {
                     if (null == _requestParameters) {
                     // Get parameters ( all or a View state only ) restored as requered
                     // in the JSR 301 PLT 5.1
                     _requestParameters = (Map<String, String[]>) getRequestAttribute(PortletViewState.REQUEST_PARAMETERS_ATTRIBUTE);
                    
                     // TODO: zjsun fix here
                     if (_requestParameters == null) {
                     _requestParameters = Collections.EMPTY_MAP;
                     }
                     }
                     return _requestParameters;
                     }
                    


                    // TODO: zjsun fix here
                     @Override
                     public Map getRequestParameterValuesMap() {
                     return getSavedRequestParameters();
                     }
                    


                    • 7. Re: share my some fix for my portal impl

                      org.jboss.portletbridge.context.PortletExternalContextImpl:

                      public String getRemoteUser() {
                       // TODO:zjsun fix here
                       // return getPortletRequest().getRemoteUser();
                       String user = getPortletRequest().getRemoteUser();
                       if (user == null){
                       Principal userPrincipal = getUserPrincipal();
                       user = userPrincipal.getName();
                       }
                       return user;
                       }
                      


                      public Object getSession(boolean create) {
                       // TODO:zjsun fix here 传递create�数
                       return getPortletRequest().getPortletSession(create);
                       }
                      


                      protected void internalRedirect(PortalActionURL actionURL) {
                       // Detect ViewId from URL and create new view for them.
                       String viewId = actionURL.getParameter(VIEW_ID_PARAMETER);
                       if (null != viewId) {
                       try {
                       viewId = URLDecoder.decode(viewId, "UTF8");
                       // Save new viewId to restore after redirect.
                       getRequestMap().put(AbstractExternalContext.REDIRECT_VIEW_ID,
                       viewId);
                       // FacesContext facesContext =
                       // FacesContext.getCurrentInstance();
                       // ViewHandler viewHandler = facesContext.getApplication()
                       // .getViewHandler();
                       // facesContext.setViewRoot(viewHandler.createView(facesContext,
                       // viewId));
                       // setHasNavigationRedirect(true);
                      
                       // TODO: zjsun fix here �定�时需��时�留�数
                       Map<String, String> actionParams = actionURL.getParameters();
                       if (actionParams != null) {
                       Map<String, String[]> requestParameters = new HashMap<String, String[]>();
                       for (Iterator<Entry<String, String>> it = actionParams
                       .entrySet().iterator(); it.hasNext();) {
                       Entry<String, String> entry = it.next();
                       String key = entry.getKey();
                       if (key != null) {
                       String value = entry.getValue();
                       requestParameters.put(key.trim(),
                       new String[] { value });
                       }
                       }
                      
                       if (requestParameters.size() > 0) {
                       getRequestMap()
                       .put(
                       PortletViewState.REDIRECT_REQUEST_PARAMETERS_ATTRIBUTE,
                       requestParameters);
                       }
                       }
                       } catch (UnsupportedEncodingException e) {
                       // Do nothing - UTF-8 encoding is a default.
                       }
                       }
                       }
                      


                      • 8. Re: share my some fix for my portal impl

                        org.jboss.portletbridge.context.PortalActionURL:

                        //TODO:zjsun fix here 暴露这些属性,以便使用
                         public void setProtocol(String protocol) {
                         this.protocol = protocol;
                         }
                        
                         public void setHost(String host) {
                         this.host = host;
                         }
                        
                         public void setPort(int port) {
                         this.port = port;
                         }
                        
                         public void setAuthority(String authority) {
                         this.authority = authority;
                         }
                        
                         public void setPath(String path) {
                         this.path = path;
                         }
                        
                         public void setUserInfo(String userInfo) {
                         this.userInfo = userInfo;
                         }
                        
                         public void setParameters(Map<String, String> parameters) {
                         this.parameters = parameters;
                         }
                        
                         public void setQueryString(String queryString) {
                         this.queryString = queryString;
                         }
                        


                        • 9. Re: share my some fix for my portal impl

                          org.jboss.portletbridge.application.PortletViewState:

                          // TODO: zjsun fix here �定�时需��留�数
                           public static final String REDIRECT_REQUEST_PARAMETERS_ATTRIBUTE = PortletViewState.class
                           .getName()
                           + "REDIRECT_REQUEST_PARAMS";
                          


                          • 10. Re: share my some fix for my portal impl

                            org.jboss.portletbridge.application.PortletStateHolder:

                            public String getStateId(FacesContext context) {
                             ExternalContext externalContext = context.getExternalContext();
                             Object request = externalContext.getRequest();
                             String stateId = null;
                             // TODO: zjsun fix here 调整PortletRequest优先判æ–顺åº�
                             if (request instanceof PortletRequest) {
                             PortletRequest portletRequest = (PortletRequest) request;
                             PortletSession portletSession = portletRequest
                             .getPortletSession(true);
                             PortletConfig portletConfig = (PortletConfig) portletRequest
                             .getAttribute(AbstractExternalContext.PORTLET_CONFIG_ATTRIBUTE);
                             if (null == portletConfig) {
                             throw new FacesException();
                             }
                             String scopeId = portletConfig.getPortletName()
                             + portletSession.getId();
                             String portletModeName = portletRequest.getPortletMode().toString();
                             if (request instanceof RenderRequest) {
                             RenderRequest renderRequest = (RenderRequest) request;
                             stateId = renderRequest.getParameter(STATE_ID_PARAMETER);
                             if (null == stateId) {
                             RenderResponse response = (RenderResponse) externalContext
                             .getResponse();
                             stateId = scopeId + portletModeName
                             + response.getNamespace();
                             }
                             } else if (request instanceof ActionRequest) {
                             ActionRequest actionRequest = (ActionRequest) request;
                            
                             if (stateId == null) {
                             stateId = (String) actionRequest
                             .getAttribute(STATE_ID_PARAMETER);
                             }
                             if (null == stateId) {
                            
                             UUID uuid = UUID.randomUUID();
                             stateId = scopeId + portletModeName + uuid.toString();
                             actionRequest.setAttribute(STATE_ID_PARAMETER, stateId);
                             ActionResponse response = (ActionResponse) externalContext
                             .getResponse();
                             response.setRenderParameter(STATE_ID_PARAMETER, stateId);
                             }
                             } else {
                             throw new FacesException();
                             }
                             WindowIDRetriver idRetriver = (WindowIDRetriver) portletSession
                             .getAttribute(WINDOW_ID_RETRIVER);
                             if (null == idRetriver) {
                             idRetriver = new WindowIDRetriver(scopeId);
                             portletSession.setAttribute(WINDOW_ID_RETRIVER, idRetriver);
                             }
                             } else if (request instanceof HttpServletRequest) {
                             HttpServletRequest servletRequest = (HttpServletRequest) request;
                             stateId = servletRequest.getParameter(STATE_ID_PARAMETER);
                             // if (null == stateId) {
                             // throw new FacesException("No portletbridge stateId parameter for
                             // AJAX request");
                             // }
                             } else {
                             throw new FacesException();
                             }
                             return stateId;
                             }
                            


                            • 11. Re: share my some fix for my portal impl

                              after these local fix, i have s:link and ajax actions works well.

                              • 12. Re: share my some fix for my portal impl
                                wesleyhales

                                Thanks for the help!

                                Can you describe what problems you have solved first. then you or I can file a Jira and you can submit a patch. This will begin the formal bug fixing process and allow us to see what you have done clearer.

                                • 13. Re: share my some fix for my portal impl

                                   

                                  "zjsun" wrote:
                                  all fix has a "zjsun fix" note.

                                  Follow me...

                                  org.jboss.seam.jsf.SeamPhaseListenerWrapper:
                                  private void beforePortletPhase(PhaseEvent event,
                                   FacesContext facesContext, PortletPhase portletPhase) {
                                  
                                   boolean notInitialised = false;
                                   if (event.getPhaseId() == RESTORE_VIEW) {
                                   _defaultListener.beforeRestoreView(facesContext);
                                   }
                                  
                                   // // TODO: zjsun fix here
                                   // boolean conversationFound = false;
                                   // Map parameters = facesContext.getExternalContext()
                                   // .getRequestParameterMap();
                                  
                                   if (event.getPhaseId() == RENDER_RESPONSE
                                   && !Contexts.isApplicationContextActive()) {
                                   _defaultListener.beforeRestoreView(facesContext);
                                  
                                   // TODO:zjsun fix here :restore conversation context, pages
                                   _defaultListener.afterRestoreView(facesContext);
                                   // FacesLifecycle.resumePage();
                                   // ConversationPropagation.instance()
                                   // .restoreConversationId(parameters);
                                   // conversationFound = Manager.instance().restoreConversation();
                                   // FacesLifecycle
                                   // .resumeConversation(facesContext.getExternalContext());
                                  
                                   notInitialised = true;
                                   }
                                   // delegate to subclass:
                                   _defaultListener.handleTransactionsBeforePhase(event);
                                  
                                   if (event.getPhaseId() == RENDER_RESPONSE) {
                                   if (notInitialised) {
                                   // TODO:zjsun fix here : restore pages?
                                   // _defaultListener.afterRestoreView(facesContext);
                                   }
                                   _defaultListener.beforeRenderResponse(facesContext);
                                   }
                                   }
                                  


                                  problem:
                                  1) when the condition "(event.getPhaseId() == RENDER_RESPONSE
                                  && !Contexts.isApplicationContextActive())" is true, then must call "_defaultListener.afterRestoreView(facesContext)" to resore the conversation before calling "_defaultListener.handleTransactionsBeforePhase(event)"

                                  • 14. Re: share my some fix for my portal impl

                                     

                                    "zjsun" wrote:
                                    org.jboss.portletbridge.AjaxPortletBridge:
                                    public void doFacesRequest(RenderRequest request, RenderResponse response)
                                     throws BridgeException {
                                     initRequest(request, response, Bridge.PortletPhase.RenderPhase);
                                     FacesContext facesContext = getFacesContext(request, response);
                                     PortletViewState windowState = PortletStateHolder.getInstance(
                                     facesContext).getWindowState(facesContext);
                                     try {
                                     if (null == response.getContentType()) {
                                     String contenttype = request.getResponseContentType();
                                     if (contenttype == null) {
                                     contenttype = "text/html";
                                     }
                                     String encoding = response.getCharacterEncoding();
                                     if (encoding == null) {
                                     encoding = "UTF-8";
                                     }
                                     response.setContentType(contenttype);
                                    
                                     }
                                     // set portletbridge title if its set.
                                     ResourceBundle bundle = portletConfig.getResourceBundle(request
                                     .getLocale());
                                     if (bundle != null) {
                                     String title = null;
                                     try {
                                     title = bundle.getString("javax.portlet.title");
                                     response.setTitle(title);
                                     } catch (Exception e) {
                                     // Ignore MissingResourceException
                                     }
                                     }
                                     String namespace = response.getNamespace();
                                     windowState.restoreRequest(facesContext, true);
                                     renderResponse(facesContext, windowState);
                                     // TODO - detect redirect case. Reset response, clear request
                                     // variables as far as Seam state.
                                     // Perform new render phase with a new ViewId.
                                     String redirectViewId = (String) facesContext.getExternalContext()
                                     .getRequestMap().get(
                                     AbstractExternalContext.REDIRECT_VIEW_ID);
                                     if (null != redirectViewId) {
                                    
                                     windowState.reset();
                                     windowState.setViewId(redirectViewId);
                                    
                                     // TODO: zjsun fix here 先å�–出é‡�定å�‘æ—¶ä¿�留的å�‚æ•°
                                     Object redirectParams = request
                                     .getAttribute(PortletViewState.REDIRECT_REQUEST_PARAMETERS_ATTRIBUTE);
                                    
                                     // release old FacesContext.
                                     facesContext.release();
                                     // Reset attributes to initial state
                                     ArrayList initialAttributes = (ArrayList) request
                                     .getAttribute(AbstractExternalContext.EXCLUDED_PARAMETERS_ATTRIBUTE);
                                     ArrayList currentAttributes = Collections.list(request
                                     .getAttributeNames());
                                     currentAttributes.removeAll(initialAttributes);
                                     for (Object newAttribute : currentAttributes) {
                                     request.removeAttribute((String) newAttribute);
                                     }
                                     request.setAttribute(AbstractExternalContext.VIEW_ID_PARAMETER,
                                     redirectViewId);
                                    
                                     // TODO: zjsun fix here æ�¢å¤�é‡�定å�‘æ—¶çš„å�‚æ•°
                                     if (redirectParams != null) {
                                     request.setAttribute(
                                     PortletViewState.REQUEST_PARAMETERS_ATTRIBUTE,
                                     redirectParams);
                                     }
                                    
                                     // Create new FacesContext
                                     facesContext = getFacesContext(request, response);
                                    
                                     renderResponse(facesContext, windowState);
                                     }
                                     // Set important Portal parameters to window state.
                                     String actionURL = facesContext.getApplication().getViewHandler()
                                     .getActionURL(facesContext,
                                     facesContext.getViewRoot().getViewId());
                                    
                                    // // TODO:zjsun fix here æ·»åŠ stateIdå�‚æ•°
                                    // PortalActionURL portalActionUrl = new PortalActionURL(actionURL);
                                    // portalActionUrl.addParameter(PortletStateHolder.STATE_ID_PARAMETER,
                                    // windowState.getStateId());
                                    // // actionURL = facesContext.getExternalContext().encodeActionURL(
                                    // // actionURL);
                                    // // windowState.setPortalActionURL(new PortalActionURL(actionURL));
                                    // String encodedActionUrl = facesContext.getExternalContext()
                                    // .encodeActionURL(portalActionUrl.toString());
                                    // portalActionUrl = PortletStateHolder
                                    // .decodeCmstarUrl(new PortalActionURL(encodedActionUrl));
                                    // // ä¿Â�è¯Â�å˜入的是æÂ£å¸¸çš„url(包括æÂ£å¸¸çš„servlet路径)而ä¸Â�是ç»Â�过cmstar编ç Â�过的
                                    // //portalActionUrl.setPath(actionURL);
                                    // windowState.setPortalActionURL(portalActionUrl);
                                     actionURL = facesContext.getExternalContext().encodeActionURL(actionURL);
                                     windowState.setPortalActionURL(new PortalActionURL(actionURL));
                                    
                                    // PortletURL portletURL = response.createRenderURL();
                                    // portletURL.setParameter(PortletStateHolder.STATE_ID_PARAMETER,
                                    // windowState.getStateId());
                                    // // String renderUrl = portletURL.toString();
                                    // PortalActionURL portalRenderUrl = new PortalActionURL(portletURL
                                    // .toString());
                                    // // ä¿Â�è¯Â�å˜入的是æÂ£å¸¸çš„url(包括æÂ£å¸¸çš„servlet路径)而ä¸Â�是ç»Â�过cmstar编ç Â�过的
                                    // portalRenderUrl = PortletStateHolder
                                    // .decodeCmstarUrl(portalRenderUrl);
                                    // //portalRenderUrl.setPath(actionURL);
                                    // windowState.setPortalRenderURL(portalRenderUrl);
                                    
                                     PortletURL portletURL = response.createRenderURL();
                                     portletURL.setParameter(PortletStateHolder.STATE_ID_PARAMETER, windowState.getStateId());
                                     String renderUrl = portletURL.toString();
                                     windowState.setPortalRenderURL(new PortalActionURL(renderUrl));
                                    
                                     windowState.setNamespace(namespace);
                                     // writer.println("</div>");
                                    
                                     //TODO:zjsun fix here 用facesContext.getExternalContext替代request
                                     //PortletSession portletSession = request.getPortletSession(true);
                                     PortletSession portletSession = (PortletSession)facesContext.getExternalContext().getSession(true);
                                    
                                     WindowIDRetriver idRetriver = (WindowIDRetriver) portletSession
                                     .getAttribute(PortletStateHolder.WINDOW_ID_RETRIVER);
                                     if (null != idRetriver) {
                                     windowState.setWindowId(idRetriver.getWindowID());
                                     }
                                    
                                     //TODO:zjsun fix here 用facesContext.getExternalContext替代request
                                     //Principal userPrincipal = request.getUserPrincipal();
                                     Principal userPrincipal = facesContext.getExternalContext().getUserPrincipal();
                                    
                                     // TODO - get user roles, defined in the portletbridge.xml ( ???
                                     // parse it ??? ), and store all values for a
                                     // "isUserInRole(roleName)" calls
                                    
                                     portletSession.setAttribute(
                                     AbstractExternalContext.PORTAL_USER_PRINCIPAL,
                                     userPrincipal, PortletSession.APPLICATION_SCOPE);
                                    
                                     if (log.isDebugEnabled()) {
                                     log.debug("Finish rendering portletbridge for namespace "
                                     + namespace);
                                     }
                                     // Disable portletbridge caching.
                                     // TODO - detect ajax components on page, static views can be
                                     // cached.
                                     response.setProperty(RenderResponse.EXPIRATION_CACHE, "0");
                                     } catch (Exception e) {
                                     response.reset();
                                     // TODO - handle exception
                                     log.error("Error processing execute lifecycle", e);
                                     exceptionHandler.processRenderException(facesContext, windowState,
                                     e);
                                     } finally {
                                     facesContext.release();
                                     }
                                     }
                                    


                                    private void renderResponse(FacesContext facesContext,
                                     PortletViewState windowState) throws FacesException {
                                     if (null == facesContext.getViewRoot()) {
                                     execute(facesContext);
                                     // TODO - detect redirect case.
                                     }
                                     //
                                     AjaxContext ajaxContext = AjaxContext.getCurrentInstance(facesContext);
                                     Map commonAjaxParameters = ajaxContext.getCommonAjaxParameters();
                                     commonAjaxParameters.put(PortletStateHolder.STATE_ID_PARAMETER,
                                     windowState.getStateId());
                                    
                                     // TODO:zjsun fix here 判æ–Â是åÂ�¦å·²ç»Â�结æÂ�Ÿresponse
                                     if (!facesContext.getResponseComplete()) {
                                     render(facesContext);
                                     }
                                     }
                                    


                                    problem:
                                    1) when "(null != redirectViewId)", it's meaning a redirect(maybe using s:link + f:param),the bridge should restore the request params, otherwise the f:param and conversationId param is lost, that is
                                    Object redirectParams = request
                                     .getAttribute(PortletViewState.REDIRECT_REQUEST_PARAMETERS_ATTRIBUTE);

                                    and
                                    if (redirectParams != null) {
                                     request.setAttribute(
                                     PortletViewState.REQUEST_PARAMETERS_ATTRIBUTE,
                                     redirectParams);
                                     }
                                    

                                    2)
                                    Principal userPrincipal = request.getUserPrincipal();
                                    
                                    should be
                                    Principal userPrincipal = facesContext.getExternalContext().getUserPrincipal();
                                    

                                    because the bridge ExternalContext impl has the override version.

                                    3)
                                    render(facesContext);
                                    
                                    should check "if (!facesContext.getResponseComplete())" in case of response completed in execute phase.

                                    1 2 Previous Next