1 /* 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.jndi.ldap; 27 28 import java.io.IOException; 29 import java.util.Hashtable; 30 import java.util.Vector; 31 import javax.naming.*; 32 import javax.naming.directory.*; 33 34 /** 35 * This subclass is used by LDAP to implement the schema calls. 36 * Basically, it keeps track of which context it is an attribute of 37 * so it can get the schema for that cotnext. 38 * 39 * @author Jon Ruiz 40 */ 41 final class LdapAttribute extends BasicAttribute { 42 43 static final long serialVersionUID = -4288716561020779584L; 44 45 private transient DirContext baseCtx = null; 46 private Name rdn = new CompositeName(); 47 48 // these two are used to reconstruct the baseCtx if this attribute has 49 // been serialized ( 50 private String baseCtxURL; 51 private Hashtable<String, ? super String> baseCtxEnv; 52 53 public Object clone() { 54 LdapAttribute attr = new LdapAttribute(this.attrID, baseCtx, rdn); 55 attr.values = values.clone(); 56 return attr; 57 } 58 59 /** 60 * Adds a new value to this attribute. 61 * 62 * @param attrVal The value to be added. If null, a null value is added to 63 * the attribute. 64 * @return true Always returns true. 65 */ 66 public boolean add(Object attrVal) { 67 // LDAP attributes don't contain duplicate values so there's no need 68 // to check if the value already exists before adding it. 69 values.addElement(attrVal); 70 return true; 71 } 72 73 /** 74 * Constructs a new instance of an attribute. 75 * 76 * @param id The attribute's id. It cannot be null. 77 */ 78 LdapAttribute(String id) { 79 super(id); 80 } 81 82 /** 83 * Constructs a new instance of an attribute. 84 * 85 * @param id The attribute's id. It cannot be null. 86 * @param baseCtx the baseCtx object of this attribute 87 * @param rdn the RDN of the entry (relative to baseCtx) 88 */ 89 private LdapAttribute(String id, DirContext baseCtx, Name rdn) { 90 super(id); 91 this.baseCtx = baseCtx; 92 this.rdn = rdn; 93 } 94 95 /** 96 * Sets the baseCtx and rdn used to find the attribute's schema 97 * Used by LdapCtx.setParents(). 98 */ 99 void setParent(DirContext baseCtx, Name rdn) { 100 this.baseCtx = baseCtx; 101 this.rdn = rdn; 102 } 103 104 /** 105 * returns the ctx this attribute came from. This call allows 106 * LDAPAttribute to be serializable. 'baseCtx' is transient so if 107 * it is null, the `baseCtxURL` is used to reconstruct the context 108 * to which calls are made. 109 */ 110 private DirContext getBaseCtx() throws NamingException { 111 if(baseCtx == null) { 112 if (baseCtxEnv == null) { 113 baseCtxEnv = new Hashtable<String, String>(3); 114 } 115 baseCtxEnv.put(Context.INITIAL_CONTEXT_FACTORY, 116 "com.sun.jndi.ldap.LdapCtxFactory"); 117 baseCtxEnv.put(Context.PROVIDER_URL,baseCtxURL); 118 baseCtx = (new InitialDirContext(baseCtxEnv)); 119 } 120 return baseCtx; 121 } 122 123 /** 124 * This is called when the object is serialized. It is 125 * overridden so that the appropriate class variables can be set 126 * to re-construct the baseCtx when deserialized. Setting these 127 * variables is costly, so it is only done if the object 128 * is actually serialized. 129 */ 130 private void writeObject(java.io.ObjectOutputStream out) 131 throws IOException { 132 133 // setup internal state 134 this.setBaseCtxInfo(); 135 136 // let the ObjectOutpurStream do the real work of serialization 137 out.defaultWriteObject(); 138 } 139 140 /** 141 * sets the information needed to reconstruct the baseCtx if 142 * we are serialized. This must be called _before_ the object is 143 * serialized!!! 144 */ 145 @SuppressWarnings("unchecked") // clone() 146 private void setBaseCtxInfo() { 147 Hashtable<String, Object> realEnv = null; 148 Hashtable<String, Object> secureEnv = null; 149 150 if (baseCtx != null) { 151 realEnv = ((LdapCtx)baseCtx).envprops; 152 this.baseCtxURL = ((LdapCtx)baseCtx).getURL(); 153 } 154 155 if(realEnv != null && realEnv.size() > 0 ) { 156 // remove any security credentials - otherwise the serialized form 157 // would store them in the clear 158 for (String key : realEnv.keySet()){ 159 if (key.indexOf("security") != -1 ) { 160 161 //if we need to remove props, we must do it to a clone 162 //of the environment. cloning is expensive, so we only do 163 //it if we have to. 164 if(secureEnv == null) { 165 secureEnv = realEnv.clone(); 166 } 167 secureEnv.remove(key); 168 } 169 } 170 } 171 172 // set baseCtxEnv depending on whether we removed props or not 173 this.baseCtxEnv = (secureEnv == null ? realEnv : secureEnv); 174 } 175 176 /** 177 * Retrieves the syntax definition associated with this attribute. 178 * @return This attribute's syntax definition. 179 */ 180 public DirContext getAttributeSyntaxDefinition() throws NamingException { 181 // get the syntax id from the attribute def 182 DirContext schema = getBaseCtx().getSchema(rdn); 183 DirContext attrDef = (DirContext)schema.lookup( 184 LdapSchemaParser.ATTRIBUTE_DEFINITION_NAME + "/" + getID()); 185 186 Attribute syntaxAttr = attrDef.getAttributes("").get("SYNTAX"); 187 188 if(syntaxAttr == null || syntaxAttr.size() == 0) { 189 throw new NameNotFoundException( 190 getID() + "does not have a syntax associated with it"); 191 } 192 193 String syntaxName = (String)syntaxAttr.get(); 194 195 // look in the schema tree for the syntax definition 196 return (DirContext)schema.lookup( 197 LdapSchemaParser.SYNTAX_DEFINITION_NAME + "/" + syntaxName); 198 } 199 200 /** 201 * Retrieves this attribute's schema definition. 202 * 203 * @return This attribute's schema definition. 204 */ 205 public DirContext getAttributeDefinition() throws NamingException { 206 DirContext schema = getBaseCtx().getSchema(rdn); 207 208 return (DirContext)schema.lookup( 209 LdapSchemaParser.ATTRIBUTE_DEFINITION_NAME + "/" + getID()); 210 } 211 }