users@javaserverfaces.java.net

Re: Problem calling HttpServletRequest authenticate() within preRenderView listener

From: Kraffmiller, Ellen <ekraffmiller_at_hmdc.harvard.edu>
Date: Thu, 11 Aug 2011 22:24:44 -0400

I'm not sure I understand your question, but here is my full example - I'm not calling processEvent() directly, but I'm registering the listener in my xhtml page:

<ui:composition template="/template.xhtml"
           xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ice="http://www.icesoft.com/icefaces/component">
<ui:param name="pageTitle" value="Text Clustering Home Page"/>
<ui:define name="metadata">
    <f:metadata>
       
        <f:event type="preRenderView" listener="#{permissionBean.preRender}"/>
    </f:metadata>
</ui:define>
<ui:define name="body">
        
           Text Clustering Home Page
           <h:form>
               <h:panelGrid columns="1">
                   <ice:outputText rendered="#{loginService.loggedIn}" value="Logged in User: #{loginService.currentUser.username}"/>
                   <h:link rendered="#{!loginService.loggedIn}" value="Register" outcome="Register"/>

                   <h:commandLink value="Logout" rendered="#{loginService.loggedIn}" action="#{loginService.logout}"/>
                   <h:link value="Login" rendered="#{!loginService.loggedIn}" outcome="Login"/>
               </h:panelGrid>
           </h:form>
        
</ui:define>
</ui:composition>

Here is my bean that contains the listener method:

@Named
@RequestScoped
public class PermissionBean implements java.io.Serializable {
   
    public void preRender(ComponentSystemEvent event) {

       HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
       HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
       try {
            
           if (!request.authenticate(response)) {
               System.out.println("After authenticate, context = " +FacesContext.getCurrentInstance());
               if (FacesContext.getCurrentInstance()!=null) {
                        FacesContext.getCurrentInstance().responseComplete();
               }
            }
        
       } catch (Exception e) { // may throw ServletException or IOException
           System.out.println("EXCEPTION calling authenticate");
           e.printStackTrace();
       }
   }
    
}

On Aug 11, 2011, at 6:41 PM, Roger Kitain wrote:

> Where is your processEvent(SystemEvent event) method?
>
> On 8/11/11 5:48 PM, ekraffmiller_at_hmdc.harvard.edu wrote:
>> Hi,
>> I'm getting an error when I call authenticate() within a preRenderView
>> listener in a managed bean. It looks like a bug in Mojarra, but I just
>> wanted to ask here before creating an Issue in Jira.
>>
>> Here is my code:
>> public void preRender(ComponentSystemEvent event) {
>> HttpServletRequest request =
>> (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContex
>> t().getRequest();
>> HttpServletResponse response =
>> (HttpServletResponse)FacesContext.getCurrentInstance().getExternalConte
>> xt().getResponse();
>> try {
>> if (!request.authenticate(response)) {
>> // can't call responseComplete() here, because
>> FacesContext.getCurrentInstance() == null.
>> // FacesContext.getCurrentInstance().responseComplete();
>>
>> }
>>
>> } catch (Exception e) { // may throw ServletException or
>> IOException
>> System.out.println("EXCEPTION calling authenticate");
>>
>> e.printStackTrace();
>> }
>>
>>
>> When the method is called I get this error:
>>
>> javax.enterprise.resource.webcontainer.jsf.context|_ThreadID=23;_Thread
>> Name=Thread-3;|Exception when handling error trying to reset the
>> response.
>> java.lang.NullPointerException
>> at
>> com.sun.faces.facelets.tag.jsf.core.DeclarativeSystemEventListener.proc
>> essEvent(EventHandler.java:126)
>>
>> I tried calling responseComplete() to fix the error, but after the call
>> to authenticate(), the FacesContext.getCurrentInstance() returns null
>>
>> Is this a bug in Mojarra, or am I calling authenticate incorrectly?
>>
>> Thanks,
>> Ellen
>
>
> --
> roger.kitain_at_oracle.com
> https://twitter.com/rogerk09
> http://www.java.net/blogs/rogerk
>