Coverage Report - org.kuali.rice.kew.web.UserPreferencesFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
UserPreferencesFilter
0%
0/26
0%
0/8
1.833
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kew.web;
 17  
 
 18  
 import java.io.IOException;
 19  
 
 20  
 import javax.servlet.Filter;
 21  
 import javax.servlet.FilterChain;
 22  
 import javax.servlet.FilterConfig;
 23  
 import javax.servlet.ServletException;
 24  
 import javax.servlet.ServletRequest;
 25  
 import javax.servlet.ServletResponse;
 26  
 import javax.servlet.http.HttpServletRequest;
 27  
 import javax.servlet.http.HttpServletResponse;
 28  
 
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 import org.kuali.rice.kew.api.KewApiServiceLocator;
 32  
 import org.kuali.rice.kew.api.preferences.Preferences;
 33  
 import org.kuali.rice.kew.api.preferences.PreferencesService;
 34  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 35  
 import org.kuali.rice.kew.api.KewApiConstants;
 36  
 import org.kuali.rice.krad.UserSession;
 37  
 import org.kuali.rice.krad.util.KRADUtils;
 38  
 
 39  
 /**
 40  
  * This class establishes and initializes the KEW Preferences after a user logs in.
 41  
  * 
 42  
  * <p>
 43  
  * This filter assumes that a UserSession is already established.
 44  
  * </p>
 45  
  * 
 46  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 47  
  */
 48  0
 public class UserPreferencesFilter implements Filter {
 49  
         
 50  0
         private static final Log LOG = LogFactory.getLog(UserPreferencesFilter.class);
 51  
 
 52  
         private FilterConfig filterConfig;
 53  
         private PreferencesService preferencesService;
 54  
         
 55  
         @Override
 56  
         public void init(FilterConfig config) throws ServletException {
 57  0
                 this.filterConfig = config;
 58  0
         }
 59  
 
 60  
         @Override
 61  
         public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
 62  0
                 this.doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain);
 63  0
         }
 64  
         
 65  
         private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
 66  0
                 final UserSession session = KRADUtils.getUserSessionFromRequest(request);
 67  
                 
 68  0
                 if (session == null) {
 69  0
                         throw new IllegalStateException("A user session has not been established");
 70  
                 }
 71  
                 
 72  0
                 final String principalId = session.getPrincipalId();
 73  
                 
 74  0
                 if (session.retrieveObject(KewApiConstants.PREFERENCES) == null) {
 75  0
                         final Preferences preferences = retrievePreferences(principalId);
 76  0
                         session.addObject(KewApiConstants.PREFERENCES, preferences);
 77  
                 }
 78  0
                 chain.doFilter(request, response);
 79  0
         }
 80  
 
 81  
         @Override
 82  
         public void destroy() {
 83  0
                 filterConfig = null;
 84  0
         }
 85  
 
 86  
     private Preferences retrievePreferences(String principalId) {
 87  0
             Preferences preferences = this.getPreferenceService().getPreferences(principalId);
 88  0
         if (preferences.isRequiresSave()) {
 89  0
             LOG.info("Detected that user preferences require saving.");
 90  0
             this.getPreferenceService().savePreferences(principalId, preferences);
 91  0
             preferences = this.getPreferenceService().getPreferences(principalId);
 92  
         }
 93  
         
 94  0
         return preferences;
 95  
     }
 96  
     
 97  
     
 98  
     private PreferencesService getPreferenceService() {
 99  0
             if (this.preferencesService == null) {
 100  0
                     this.preferencesService = KewApiServiceLocator.getPreferencesService();
 101  
             }
 102  
             
 103  0
             return this.preferencesService;
 104  
     }
 105  
     
 106  
 }