001/**
002 * Copyright 2005-2017 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.web;
017
018import java.io.IOException;
019
020import javax.servlet.Filter;
021import javax.servlet.FilterChain;
022import javax.servlet.FilterConfig;
023import javax.servlet.ServletException;
024import javax.servlet.ServletRequest;
025import javax.servlet.ServletResponse;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.kuali.rice.kew.api.KewApiServiceLocator;
032import org.kuali.rice.kew.api.preferences.Preferences;
033import org.kuali.rice.kew.api.preferences.PreferencesService;
034import org.kuali.rice.kew.service.KEWServiceLocator;
035import org.kuali.rice.kew.api.KewApiConstants;
036import org.kuali.rice.krad.UserSession;
037import org.kuali.rice.krad.util.KRADUtils;
038
039/**
040 * This class establishes and initializes the KEW Preferences after a user logs in.
041 * 
042 * <p>
043 * This filter assumes that a UserSession is already established.
044 * </p>
045 * 
046 * @author Kuali Rice Team (rice.collab@kuali.org)
047 */
048public class UserPreferencesFilter implements Filter {
049        
050        private static final Log LOG = LogFactory.getLog(UserPreferencesFilter.class);
051
052        private FilterConfig filterConfig;
053        private PreferencesService preferencesService;
054        
055        @Override
056        public void init(FilterConfig config) throws ServletException {
057                this.filterConfig = config;
058        }
059
060        @Override
061        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
062                this.doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain);
063        }
064        
065        private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
066                final UserSession session = KRADUtils.getUserSessionFromRequest(request);
067                
068                if (session == null) {
069                        throw new IllegalStateException("A user session has not been established");
070                }
071                
072                final String principalId = session.getPrincipalId();
073                
074                if (session.retrieveObject(KewApiConstants.PREFERENCES) == null) {
075                        final Preferences preferences = retrievePreferences(principalId);
076                        session.addObject(KewApiConstants.PREFERENCES, preferences);
077                }
078                chain.doFilter(request, response);
079        }
080
081        @Override
082        public void destroy() {
083                filterConfig = null;
084        }
085
086    private Preferences retrievePreferences(String principalId) {
087        Preferences preferences = this.getPreferenceService().getPreferences(principalId);
088        if (preferences.isRequiresSave()) {
089            LOG.info("Detected that user preferences require saving.");
090            this.getPreferenceService().savePreferences(principalId, preferences);
091            preferences = this.getPreferenceService().getPreferences(principalId);
092        }
093        
094        return preferences;
095    }
096    
097    
098    private PreferencesService getPreferenceService() {
099        if (this.preferencesService == null) {
100                this.preferencesService = KewApiServiceLocator.getPreferencesService();
101        }
102        
103        return this.preferencesService;
104    }
105    
106}