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 2007 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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.preferences.Preferences;
 32  
 import org.kuali.rice.kew.preferences.service.PreferencesService;
 33  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 34  
 import org.kuali.rice.kew.util.KEWConstants;
 35  
 import org.kuali.rice.kns.UserSession;
 36  
 import org.kuali.rice.kns.util.WebUtils;
 37  
 
 38  
 /**
 39  
  * This class establishes and initializes the KEW Preferences after a user logs in.
 40  
  * 
 41  
  * <p>
 42  
  * This filter assumes that a UserSession is already established.
 43  
  * </p>
 44  
  * 
 45  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 46  
  */
 47  0
 public class UserPreferencesFilter implements Filter {
 48  
         
 49  0
         private static final Log LOG = LogFactory.getLog(UserPreferencesFilter.class);
 50  
 
 51  
         private FilterConfig filterConfig;
 52  
         private PreferencesService preferencesService;
 53  
         
 54  
         @Override
 55  
         public void init(FilterConfig config) throws ServletException {
 56  0
                 this.filterConfig = config;
 57  0
         }
 58  
 
 59  
         @Override
 60  
         public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
 61  0
                 this.doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain);
 62  0
         }
 63  
         
 64  
         private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
 65  0
                 final UserSession session = WebUtils.getUserSessionFromRequest(request);
 66  
                 
 67  0
                 if (session == null) {
 68  0
                         throw new IllegalStateException("A user session has not been established");
 69  
                 }
 70  
                 
 71  0
                 final String principalId = session.getPrincipalId();
 72  
                 
 73  0
                 if (session.retrieveObject(KEWConstants.PREFERENCES) == null) {
 74  0
                         final Preferences preferences = retrievePreferences(principalId);
 75  0
                         session.addObject(KEWConstants.PREFERENCES, preferences);
 76  
                 }
 77  0
                 chain.doFilter(request, response);
 78  0
         }
 79  
 
 80  
         @Override
 81  
         public void destroy() {
 82  0
                 filterConfig = null;
 83  0
         }
 84  
 
 85  
     private Preferences retrievePreferences(String principalId) {
 86  0
             Preferences preferences = this.getPreferenceService().getPreferences(principalId);
 87  0
         if (preferences.isRequiresSave()) {
 88  0
             LOG.info("Detected that user preferences require saving.");
 89  0
             this.getPreferenceService().savePreferences(principalId, preferences);
 90  0
             preferences = this.getPreferenceService().getPreferences(principalId);
 91  
         }
 92  
         
 93  0
         return preferences;
 94  
     }
 95  
     
 96  
     
 97  
     private PreferencesService getPreferenceService() {
 98  0
             if (this.preferencesService == null) {
 99  0
                     this.preferencesService = KEWServiceLocator.getPreferencesService();
 100  
             }
 101  
             
 102  0
             return this.preferencesService;
 103  
     }
 104  
     
 105  
 }