1 package org.apache.turbine.modules.actions.sessionvalidator; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.apache.commons.configuration.Configuration; 23 24 import org.apache.commons.lang.StringUtils; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.apache.turbine.Turbine; 30 import org.apache.turbine.TurbineConstants; 31 32 import org.apache.turbine.pipeline.PipelineData; 33 import org.apache.turbine.services.security.TurbineSecurity; 34 35 import org.apache.turbine.util.RunData; 36 import org.apache.turbine.util.TurbineException; 37 38 /** 39 * The SessionValidator attempts to retrieve the User object from the 40 * Servlet API session that is associated with the request. If the 41 * data cannot be retrieved, it is handled here. If the user has not 42 * been marked as being logged into the system, the user is rejected 43 * and the screen is set to the screen.homepage value in 44 * TurbineResources.properties. 45 * 46 * <p> 47 * 48 * Other systems generally have a database table which stores this 49 * information, but we take advantage of the Servlet API here to save 50 * a hit to the database for each and every connection that a user 51 * makes. 52 * 53 * <p> 54 * 55 * This action is special in that it should only be executed by the 56 * Turbine servlet. 57 * 58 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a> 59 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 60 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a> 61 * @version $Id: DefaultSessionValidator.java 1066518 2011-02-02 16:30:53Z ludwig $ 62 */ 63 public class DefaultSessionValidator 64 extends SessionValidator 65 { 66 /** Logging */ 67 private static Log log = LogFactory.getLog(DefaultSessionValidator.class); 68 69 /** 70 * Execute the action. The default is to populate the RunData 71 * object and, if the user is unknown, to force a login screen (as 72 * set in the tr.props). 73 * 74 * @deprecated Use PipelineData version instead. 75 * @see org.apache.turbine.modules.screens.error.InvalidState 76 * @param data Turbine RunData context information. 77 * @throws TurbineException The anonymous user could not be obtained 78 * from the security service 79 */ 80 @Deprecated 81 @Override 82 public void doPerform(RunData data) 83 throws TurbineException 84 { 85 Configuration conf = Turbine.getConfiguration(); 86 87 // Pull user from session. 88 data.populate(); 89 90 // The user may have not logged in, so create a "guest/anonymous" user. 91 if (data.getUser() == null) 92 { 93 log.debug("Fixing up empty User Object!"); 94 data.setUser(TurbineSecurity.getAnonymousUser()); 95 data.save(); 96 } 97 98 // Make sure the User has logged into the system. 99 if (!data.getUser().hasLoggedIn()) 100 { 101 // only set the message if nothing else has already set it 102 // (e.g. the LogoutUser action). 103 if (StringUtils.isEmpty(data.getMessage())) 104 { 105 data.setMessage(conf.getString(TurbineConstants.LOGIN_MESSAGE)); 106 } 107 108 // set the screen to be the login page 109 data.setScreen(conf.getString(TurbineConstants.SCREEN_LOGIN)); 110 111 // We're not doing any actions buddy! (except action.login which 112 // will have been performed already) 113 data.setAction(null); 114 } 115 116 if (!data.hasScreen()) 117 { 118 data.setMessage(conf.getString( 119 TurbineConstants.LOGIN_MESSAGE_NOSCREEN)); 120 data.setScreen(conf.getString(TurbineConstants.SCREEN_HOMEPAGE)); 121 } 122 123 if (data.getParameters().containsKey("_session_access_counter")) 124 { 125 // See comments in screens.error.InvalidState. 126 if (data.getParameters().getInt("_session_access_counter") 127 < (((Integer) data.getUser().getTemp( 128 "_session_access_counter")).intValue() - 1)) 129 { 130 data.getUser().setTemp("prev_screen", data.getScreen()); 131 data.getUser().setTemp("prev_parameters", data.getParameters()); 132 data.setScreen(conf.getString( 133 TurbineConstants.SCREEN_INVALID_STATE)); 134 data.setAction(""); 135 } 136 } 137 } 138 139 /** 140 * Execute the action. The default is to populate the RunData 141 * object and, if the user is unknown, to force a login screen (as 142 * set in the tr.props). 143 * 144 * @see org.apache.turbine.modules.screens.error.InvalidState 145 * @param pipelineData Turbine PipelineData context information. 146 * @throws TurbineException The anonymous user could not be obtained 147 * from the security service 148 */ 149 @Override 150 public void doPerform(PipelineData pipelineData) 151 throws TurbineException 152 { 153 RunData data = getRunData(pipelineData); 154 doPerform(data); 155 } 156 157 }