001/** 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2005 Sun Microsystems Inc. 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 * https://opensso.dev.java.net/public/CDDLv1.0.html or 013 * opensso/legal/CDDLv1.0.txt 014 * See the License for the specific language governing 015 * permission and limitations under the License. 016 * 017 * When distributing Covered Code, include this CDDL 018 * Header Notice in each file and include the License file 019 * at opensso/legal/CDDLv1.0.txt. 020 * If applicable, add the following below the CDDL Header, 021 * with the fields enclosed by brackets [] replaced by 022 * your own identifying information: 023 * "Portions Copyrighted [year] [name of copyright owner]" 024 * 025 * $Id: Client.java,v 1.4 2008/06/25 05:41:32 qcheng Exp $ 026 * 027 */ 028 029package com.iplanet.services.cdm; 030 031import com.iplanet.services.cdm.clientschema.AMClientCapData; 032import com.iplanet.sso.SSOToken; 033import com.sun.identity.shared.debug.Debug; 034import java.util.Collections; 035import java.util.HashMap; 036import java.util.HashSet; 037import java.util.Iterator; 038import java.util.Map; 039import java.util.Observable; 040import java.util.Set; 041 042/** 043 * Provides common access to client data. 044 * <p> 045 * <p> 046 * Client data is accessed for a particular client type. The underlying client 047 * data is stored in the profile service, but this interface should always used 048 * for accessing it (not by accessing the profile directly). 049 * @supported.api 050 */ 051 052public class Client extends Observable implements ICDMConstants { 053 private static final String ADD_PROP_ATTR = 054 AMClientCapData.ADDITIONAL_PROPERTIES_ATTR; 055 056 private static final String SEPARATOR = AMClientCapData.ADD_PROP_SEPARATOR; 057 058 private static G11NSettings g11nSettings = G11NSettings.getInstance(); 059 060 private static Debug debug = Debug.getInstance("amClientDetection"); 061 062 // 063 // The ClientType of this Object. 064 // 065 private String cType = null; 066 067 // 068 // Keep the attribute names & values in a Map. Key = name, 069 // value = (Set) of values. synchronizations around this Map are not 070 // required since the setProperty() is protected and the Map modifications 071 // are done only at object Construction time 072 // 073 private HashMap profileMap = null; 074 075 // 076 // Store the additionalProperties in a Set - used by the console plug-in 077 // only 078 // 079 private Set additionalProperties = null; 080 081 public Client() { 082 } 083 084 public Client(String clientType, Map data) { 085 profileMap = new HashMap(data.size()); 086 profileMap.putAll(data); 087 088 cType = clientType; // set our private var 089 additionalProperties = separateAdditionalProperties(profileMap); 090 } 091 092 /** 093 * Get Client instance for a specific client type . 094 * 095 * @param clientType 096 * Client Type. 097 * @return Requested Client instance. 098 * @throws ClientException 099 * if specified client type is null or not defined 100 * @deprecated Use ClientsManager#getInstance(String) 101 * @supported.api 102 */ 103 public static Client getInstance(String clientType) throws ClientException { 104 return ClientsManager.getInstance(clientType); 105 } 106 107 /** 108 * When setting client data, get a Client instance for a specific client 109 * type. A valid user session is required when setting client data. 110 * 111 * @param clientType 112 * Client type 113 * @param token 114 * SSO Token of the caller 115 * @return Client instance 116 * @throws ClientException 117 * if client type is null or not defined 118 * 119 * @deprecated Use ClientsManager#getInstance(String) 120 */ 121 protected static Client getInstance(String clientType, SSOToken token) 122 throws ClientException { 123 return getInstance(clientType); 124 } 125 126 /** 127 * Returns a Client instance for the default client type 128 * 129 * @return The Client instance corresponding to the default client type 130 * @deprecated Use ClientsManager#getDefaultInstance() 131 * @supported.api 132 */ 133 public static Client getDefaultInstance() { 134 return ClientsManager.getDefaultInstance(); 135 } 136 137 /** 138 * Returns an iterator of Client objects for all known client types. 139 * 140 * @return Iterator of Client objects 141 * @deprecated Use ClientsManager#getAllInstances() 142 * @supported.api 143 */ 144 public static Iterator getAllInstances() { 145 return ClientsManager.getAllInstances(); 146 } 147 148 /** 149 * When setting client data, returns an iterator of Client objects for all 150 * known client types. A valid user session is required when setting client 151 * data. 152 * 153 * @param token 154 * The user's SSO token 155 * @return Iterator of Client objects 156 * @deprecated Use ClientsManager#getAllInstances() 157 */ 158 protected static Iterator getAllInstances(SSOToken token) { 159 return getAllInstances(); 160 } 161 162 /** 163 * Gets the name of the client type for the data in this client instance. 164 * 165 * @return Name of the client type 166 * @supported.api 167 */ 168 public String getClientType() { 169 return cType; 170 } 171 172 /** 173 * Gets the client property for the specified key. 174 * 175 * @param name 176 * The key for the client property to be returned. 177 * @return The client property. Return null if name is null or an unknown 178 * key 179 * @supported.api 180 */ 181 public String getProperty(String name) { 182 String value = null; 183 Set properties = null; 184 185 if ((properties = getPropertiesInternal(name)) != null) { 186 Iterator iter = properties.iterator(); 187 value = (String) iter.next(); // get first element 188 } 189 190 return (value); 191 } 192 193 private Set getPropertiesInternal(String attributeName) { 194 Set properties = (Set) profileMap.get(attributeName); 195 return (properties); 196 } 197 198 /** 199 * Gets the client property for the specified key. 200 * 201 * @param name 202 * The key for the client property to be returned. 203 * @return The set of client property values. Returns null if name is null 204 * or an unknown key 205 * @supported.api 206 */ 207 public Set getProperties(String name) { 208 Set properties = getPropertiesInternal(name); 209 210 Set umSet = null; 211 if (properties != null) { 212 umSet = Collections.unmodifiableSet(properties); 213 } 214 215 return (umSet); 216 } 217 218 /** 219 * Returns a set of property names for this client data instance. 220 * 221 * @return The set of property names for this client data instance. 222 * @supported.api 223 */ 224 public Set getPropertyNames() { 225 Set keys = profileMap.keySet(); 226 return (keys); 227 } 228 229 public String getCharset(java.util.Locale loc) { 230 try { 231 return g11nSettings.getCharset(cType, loc); 232 } catch (ClientException ex) { 233 debug.error("Client.getCharset ", ex); 234 235 } 236 return CDM_DEFAULT_CHARSET; 237 } 238 239 /** 240 * used by the console plug-in (only) to get the additional properties. 241 */ 242 public Set getAdditionalProperties() { 243 return additionalProperties; 244 } 245 246 /** 247 * Removes the "additionalProperties" element from the Map, adds each of 248 * them to the Map with name and value (parsed with "=") and returns the 249 * values of the "additionalProperties in the Set. 250 * 251 * @return Set of the additionalProperties 252 */ 253 protected Set separateAdditionalProperties(Map m) { 254 Set addProps = null; 255 256 if ((m != null) && ((addProps = (Set) m.get(ADD_PROP_ATTR)) != null) 257 && (addProps.size() > 0)) { 258 m.remove(ADD_PROP_ATTR); // remove it 259 Iterator itr = addProps.iterator(); 260 while (itr.hasNext()) { 261 String property = (String) itr.next(); 262 int index = property.indexOf(SEPARATOR); 263 264 if (index <= 0) {// ignore props with no name 265 continue; 266 } 267 268 String name = property.substring(0, index); 269 String val = property.substring(index + 1); 270 271 Set set = new HashSet(1); 272 set.add(val); 273 274 m.put(name, set); 275 } 276 } 277 278 return addProps; 279 } 280 281}
Copyright © 2010-2017, ForgeRock All Rights Reserved.