001/** 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2011 ForgeRock AS. All Rights Reserved 005 * 006 * The contents of this file are subject to the terms 007 * of the Common Development and Distribution License 008 * (the License). You may not use this file except in 009 * compliance with the License. 010 * 011 * You can obtain a copy of the License at 012 * http://forgerock.org/license/CDDLv1.0.html 013 * See the License for the specific language governing 014 * permission and limitations under the License. 015 * 016 * When distributing Covered Code, include this CDDL 017 * Header Notice in each file and include the License file 018 * at http://forgerock.org/license/CDDLv1.0.html 019 * If applicable, add the following below the CDDL Header, 020 * with the fields enclosed by brackets [] replaced by 021 * your own identifying information: 022 * "Portions Copyrighted [year] [name of copyright owner]" 023 * 024 */ 025package org.forgerock.openam.authentication.service; 026 027import com.iplanet.dpro.session.service.InternalSession; 028import java.util.Enumeration; 029 030/** 031 * This class is used in case of session upgrade for copying session properties 032 * from the old session into the new one. Subclasses should implement 033 * {@link #shouldCopy(java.lang.String)} in order to control which properties 034 * are needed to copy into the new session. 035 * In case you want to modify the copyable session property you are encouraged 036 * to override {@link #updateProperty(com.iplanet.dpro.session.service.InternalSession, 037 * java.lang.String, java.lang.String)} method. 038 * 039 * @author Peter Major 040 * @supported.all.api 041 */ 042public abstract class SessionPropertyUpgrader { 043 044 /** 045 * Entry point for LoginState. This method is called during session upgrade 046 * in order to copy session attributes from one session to another. 047 * 048 * @param oldSession The previous session 049 * @param newSession The new session 050 * @param forceAuth Whether the authentication was forced 051 */ 052 public final void populateProperties(InternalSession oldSession, InternalSession newSession, boolean forceAuth) { 053 Enumeration<String> allProperties = oldSession.getPropertyNames(); 054 while (allProperties.hasMoreElements()) { 055 String key = allProperties.nextElement(); 056 String value = (String) oldSession.getProperty(key); 057 if (shouldCopy(key)) { 058 if (!forceAuth) { 059 updateProperty(newSession, key, value); 060 } else { 061 updateProperty(oldSession, key, value); 062 } 063 } 064 } 065 } 066 067 /** 068 * This method updates a session property in the session with the given value. 069 * Override this method if you want to change some properties during the 070 * upgrade process. 071 * 072 * NOTE: If you override this, you SHOULD call super.updateProperty(..) 073 * at the end of your implementation with the updated values. 074 * 075 * @param session Session object where the property should be set 076 * @param property Name of the property to set 077 * @param value Value of the given session property 078 */ 079 public void updateProperty(InternalSession session, String property, String value) { 080 if (value != null) { 081 session.putProperty(property, value); 082 } 083 } 084 085 /** 086 * This method decides whether a given session property should be copied to 087 * the new session. 088 * 089 * @param key The name of the session property which we want to decide to copy 090 * @return <code>true</code> if the property with the given key should be 091 * copied into the new session 092 */ 093 public abstract boolean shouldCopy(String key); 094}
Copyright © 2010-2017, ForgeRock All Rights Reserved.