| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 1999 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap; |
| 39 | |
| 40 | import java.io.Serializable; |
| 41 | |
| 42 | /** |
| 43 | * Represents an entry in the directory. |
| 44 | * |
| 45 | * @version 1.0 |
| 46 | */ |
| 47 | public class LDAPEntry implements Serializable { |
| 48 | |
| 49 | static final long serialVersionUID = -5563306228920012807L; |
| 50 | private String dn = null; |
| 51 | private LDAPAttributeSet attrSet = null; |
| 52 | |
| 53 | /** |
| 54 | * Constructs an empty entry. |
| 55 | */ |
| 56 | public LDAPEntry() { |
| 57 | dn = null; |
| 58 | attrSet = new LDAPAttributeSet(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Constructs a new entry with the specified distinguished name and with |
| 63 | * an empty attribute set. |
| 64 | * @param distinguishedName the distinguished name of the new entry |
| 65 | */ |
| 66 | public LDAPEntry( String distinguishedName ) { |
| 67 | dn = distinguishedName; |
| 68 | attrSet = new LDAPAttributeSet(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Constructs a new entry with the specified distinguished name and |
| 73 | * set of attributes. |
| 74 | * @param distinguishedName the distinguished name of the new entry |
| 75 | * @param attrs the set of attributes to assign to the new entry |
| 76 | * @see org.ietf.ldap.LDAPAttributeSet |
| 77 | */ |
| 78 | public LDAPEntry( String distinguishedName, LDAPAttributeSet attrs ) { |
| 79 | dn = distinguishedName; |
| 80 | attrSet = attrs; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the distinguished name of the current entry. |
| 85 | * @return distinguished name of the current entry. |
| 86 | */ |
| 87 | public String getDN() { |
| 88 | return dn; |
| 89 | } |
| 90 | |
| 91 | void setDN(String name) { |
| 92 | dn = name; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * In an entry, returns the single attribute that exactly matches the |
| 97 | * specified attribute name. |
| 98 | * @param attrName name of attribute to return |
| 99 | * For example: |
| 100 | *<PRE> |
| 101 | * "cn" // Only a non-subtyped version of cn |
| 102 | * "cn;lang-ja" // Only a Japanese version of cn, will not |
| 103 | * // return "cn;lang-ja-JP-kanji", for example |
| 104 | *</PRE> |
| 105 | * @return attribute in the current entry that has exactly the same name, |
| 106 | * or null (if no attribute in the entry matches the specified name). |
| 107 | * @see org.ietf.ldap.LDAPAttribute |
| 108 | */ |
| 109 | public LDAPAttribute getAttribute(String attrName) { |
| 110 | return attrSet.getAttribute(attrName); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns the subtype that matches "attrName" and that best matches |
| 115 | * a language specification "lang". If there are subtypes other than |
| 116 | * "lang" subtypes included in attrName, e.g. "cn;binary", only |
| 117 | * attributes with all of those subtypes are returned. If lang is |
| 118 | * null or empty, the method behaves as getAttribute(attrName). If |
| 119 | * there are no matching attributes, null is returned. |
| 120 | * |
| 121 | * Example:<PRE> |
| 122 | * Assume the entry contains only the following attributes: |
| 123 | * <CODE>cn;lang-en</CODE> |
| 124 | * <CODE>cn;lang-ja-JP-kanji</CODE> |
| 125 | * <CODE>sn</CODE> |
| 126 | * getAttribute( "cn" ) returns <CODE>null</CODE>. |
| 127 | * getAttribute( "sn" ) returns the "<CODE>sn</CODE>" attribute. |
| 128 | * getAttribute( "cn", "lang-en-us" ) returns the "<CODE>cn;lang-en</CODE>" attribute. |
| 129 | * getAttribute( "cn", "lang-en" ) returns the "<CODE>cn;lang-en</CODE>" attribute. |
| 130 | * getAttribute( "cn", "lang-ja" ) returns <CODE>null</CODE>. |
| 131 | * getAttribute( "sn", "lang-en" ) returns the "<CODE>sn</CODE>" attribute. |
| 132 | *</PRE> |
| 133 | * <P> |
| 134 | * @param attrName name of attribute to find in the entry |
| 135 | * @param lang a language specification (for example, <CODE>lang-en</CODE>) |
| 136 | * @return the attribute that matches the base name and that best |
| 137 | * matches any specified language subtype. |
| 138 | * @see org.ietf.ldap.LDAPAttribute |
| 139 | */ |
| 140 | public LDAPAttribute getAttribute( String attrName, String lang ) { |
| 141 | return attrSet.getAttribute( attrName, lang ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns the attribute set of the entry. |
| 146 | * @return set of attributes in the entry. |
| 147 | * @see org.ietf.ldap.LDAPAttributeSet |
| 148 | */ |
| 149 | public LDAPAttributeSet getAttributeSet() { |
| 150 | return attrSet; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Creates a new attribute set containing only the attributes |
| 155 | * that have the specified subtypes. |
| 156 | * <P> |
| 157 | * |
| 158 | * For example, suppose an entry contains the following attributes: |
| 159 | * <P> |
| 160 | * |
| 161 | * <PRE> |
| 162 | * cn |
| 163 | * cn;lang-ja |
| 164 | * sn;phonetic;lang-ja |
| 165 | * sn;lang-us |
| 166 | * </PRE> |
| 167 | * |
| 168 | * If you call the <CODE>getAttributeSet</CODE> method and pass |
| 169 | * <CODE>lang-ja</CODE> as the argument, the method returns |
| 170 | * an attribute set containing the following attributes: |
| 171 | * <P> |
| 172 | * |
| 173 | * <PRE> |
| 174 | * cn;lang-ja |
| 175 | * sn;phonetic;lang-ja |
| 176 | * </PRE> |
| 177 | * |
| 178 | * @param subtype semi-colon delimited list of subtypes |
| 179 | * that you want to find in attribute names. |
| 180 | *<PRE> |
| 181 | * "lang-ja" // Only Japanese language subtypes |
| 182 | * "binary" // Only binary subtypes |
| 183 | * "binary;lang-ja" // Only Japanese language subtypes |
| 184 | * which also are binary |
| 185 | *</PRE> |
| 186 | * @return attribute set containing the attributes that have |
| 187 | * the specified subtypes. |
| 188 | * @see org.ietf.ldap.LDAPAttributeSet |
| 189 | * @see org.ietf.ldap.LDAPAttributeSet#getSubset |
| 190 | */ |
| 191 | public LDAPAttributeSet getAttributeSet(String subtype) { |
| 192 | return attrSet.getSubset(subtype); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Retrieves the string representation of the entry's |
| 197 | * distinguished name (DN) and its attributes. |
| 198 | * For example: |
| 199 | * |
| 200 | * <PRE> |
| 201 | * LDAPEntry: uid=bjensen, ou=People, o=airius.com; LDAPAttributeSet: |
| 202 | * LDAPAttribute {type='cn', values='Barbara Jensen,Babs Jensen'} |
| 203 | * LDAPAttribute {type='sn', values='Jensen'}LDAPAttribute {type='givenname', |
| 204 | * values='Barbara'}LDAPAttribute {type='objectclass', values='top,person, |
| 205 | * organizationalPerson,inetOrgPerson'}LDAPAttribute {type='ou', |
| 206 | * values='Product Development,People'} |
| 207 | * </PRE> |
| 208 | * |
| 209 | * @return string representation of the entry's DN and its attributes. |
| 210 | */ |
| 211 | public String toString() { |
| 212 | StringBuffer sb = new StringBuffer("LDAPEntry: "); |
| 213 | if ( dn != null ) { |
| 214 | sb.append(dn); |
| 215 | sb.append("; "); |
| 216 | } |
| 217 | if ( attrSet != null ) { |
| 218 | sb.append(attrSet.toString()); |
| 219 | } |
| 220 | return sb.toString(); |
| 221 | } |
| 222 | } |