Coverage Report - org.apache.myfaces.shared.context.SwitchAjaxExceptionHandlerWrapperImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
SwitchAjaxExceptionHandlerWrapperImpl
0%
0/35
0%
0/20
3.5
 
 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  0
 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  0
     {
 46  0
         _requestExceptionHandler = requestExceptionHandler;
 47  0
         _ajaxExceptionHandler = ajaxExceptionHandler;
 48  0
     }
 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  0
         isAjaxRequest(exceptionQueuedEvent);
 56  0
         super.processEvent(exceptionQueuedEvent);
 57  0
     }
 58  
 
 59  
     protected boolean isAjaxRequest(SystemEvent exceptionQueuedEvent)
 60  
     {
 61  0
         if (_isAjaxRequest == null)
 62  
         {
 63  0
             if (exceptionQueuedEvent instanceof ExceptionQueuedEvent)
 64  
             {
 65  0
                 ExceptionQueuedEvent eqe = (ExceptionQueuedEvent)exceptionQueuedEvent;
 66  0
                 ExceptionQueuedEventContext eqec = eqe.getContext();
 67  0
                 if (eqec != null)
 68  
                 {
 69  0
                     FacesContext facesContext = eqec.getContext();
 70  0
                     if (facesContext != null)
 71  
                     {
 72  0
                         return isAjaxRequest(facesContext);
 73  
                     }
 74  
                 }
 75  
             }
 76  0
             return isAjaxRequest();
 77  
         }
 78  0
         return _isAjaxRequest;
 79  
     }
 80  
     
 81  
     protected boolean isAjaxRequest(FacesContext facesContext)
 82  
     {
 83  0
         if (_isAjaxRequest == null)
 84  
         {
 85  0
             facesContext = (facesContext == null) ? FacesContext.getCurrentInstance() : facesContext;
 86  0
             PartialViewContext pvc = facesContext.getPartialViewContext();
 87  0
             if (pvc == null)
 88  
             {
 89  0
                 return false;
 90  
             }
 91  0
             _isAjaxRequest = pvc.isAjaxRequest();
 92  
         }
 93  0
         return _isAjaxRequest;
 94  
     }
 95  
     
 96  
     protected boolean isAjaxRequest()
 97  
     {
 98  0
         if (_isAjaxRequest == null)
 99  
         {
 100  0
             FacesContext facesContext = FacesContext.getCurrentInstance();
 101  0
             PartialViewContext pvc = facesContext.getPartialViewContext();
 102  0
             if (pvc == null)
 103  
             {
 104  0
                 return false;
 105  
             }
 106  0
             _isAjaxRequest = pvc.isAjaxRequest();
 107  
         }
 108  0
         return _isAjaxRequest;
 109  
     }
 110  
     
 111  
     @Override
 112  
     public ExceptionHandler getWrapped()
 113  
     {
 114  0
         if (isAjaxRequest())
 115  
         {
 116  0
             return _ajaxExceptionHandler;
 117  
         }
 118  
         else
 119  
         {
 120  0
             return _requestExceptionHandler;
 121  
         }
 122  
     }
 123  
 }