View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.shared.context;
20  
21  import javax.faces.context.ExceptionHandler;
22  import javax.faces.context.ExceptionHandlerWrapper;
23  import javax.faces.context.FacesContext;
24  import javax.faces.context.PartialViewContext;
25  import javax.faces.event.AbortProcessingException;
26  import javax.faces.event.ExceptionQueuedEvent;
27  import javax.faces.event.ExceptionQueuedEventContext;
28  import javax.faces.event.SystemEvent;
29  
30  /**
31   * This wrapper is a switch to choose in a lazy way between ajax and
32   * normal exceptionHandler wrapping, because FacesContext is initialized after
33   * ExceptionHandler, so it is not safe to get it when
34   * ExceptionHandlerFactory.getExceptionHandler() is called.
35   */
36  public class SwitchAjaxExceptionHandlerWrapperImpl extends ExceptionHandlerWrapper
37  {
38      private ExceptionHandler _requestExceptionHandler;
39      private ExceptionHandler _ajaxExceptionHandler;
40      private Boolean _isAjaxRequest;
41      
42      public SwitchAjaxExceptionHandlerWrapperImpl(
43              ExceptionHandler requestExceptionHandler,
44              ExceptionHandler ajaxExceptionHandler)
45      {
46          _requestExceptionHandler = requestExceptionHandler;
47          _ajaxExceptionHandler = ajaxExceptionHandler;
48      }
49      
50      @Override
51      public void processEvent(SystemEvent exceptionQueuedEvent)
52              throws AbortProcessingException
53      {
54          //Check if this is an ajax request, but take advantage of exceptionQueuedEvent facesContext
55          isAjaxRequest(exceptionQueuedEvent);
56          super.processEvent(exceptionQueuedEvent);
57      }
58  
59      protected boolean isAjaxRequest(SystemEvent exceptionQueuedEvent)
60      {
61          if (_isAjaxRequest == null)
62          {
63              if (exceptionQueuedEvent instanceof ExceptionQueuedEvent)
64              {
65                  ExceptionQueuedEvent eqe = (ExceptionQueuedEvent)exceptionQueuedEvent;
66                  ExceptionQueuedEventContext eqec = eqe.getContext();
67                  if (eqec != null)
68                  {
69                      FacesContext facesContext = eqec.getContext();
70                      if (facesContext != null)
71                      {
72                          return isAjaxRequest(facesContext);
73                      }
74                  }
75              }
76              return isAjaxRequest();
77          }
78          return _isAjaxRequest;
79      }
80      
81      protected boolean isAjaxRequest(FacesContext facesContext)
82      {
83          if (_isAjaxRequest == null)
84          {
85              facesContext = (facesContext == null) ? FacesContext.getCurrentInstance() : facesContext;
86              PartialViewContext pvc = facesContext.getPartialViewContext();
87              if (pvc == null)
88              {
89                  return false;
90              }
91              _isAjaxRequest = pvc.isAjaxRequest();
92          }
93          return _isAjaxRequest;
94      }
95      
96      protected boolean isAjaxRequest()
97      {
98          if (_isAjaxRequest == null)
99          {
100             FacesContext facesContext = FacesContext.getCurrentInstance();
101             PartialViewContext pvc = facesContext.getPartialViewContext();
102             if (pvc == null)
103             {
104                 return false;
105             }
106             _isAjaxRequest = pvc.isAjaxRequest();
107         }
108         return _isAjaxRequest;
109     }
110     
111     @Override
112     public ExceptionHandler getWrapped()
113     {
114         if (isAjaxRequest())
115         {
116             return _ajaxExceptionHandler;
117         }
118         else
119         {
120             return _requestExceptionHandler;
121         }
122     }
123 }