001/* 002 * Copyright 2009-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2009-2020 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2009-2020 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.ldap.sdk.migrate.ldapjdk; 037 038 039 040import java.io.Serializable; 041import java.util.ArrayList; 042import java.util.Enumeration; 043 044import com.unboundid.ldap.sdk.Attribute; 045import com.unboundid.ldap.sdk.Entry; 046import com.unboundid.util.NotExtensible; 047import com.unboundid.util.NotMutable; 048import com.unboundid.util.ThreadSafety; 049import com.unboundid.util.ThreadSafetyLevel; 050 051 052 053/** 054 * This class provides a data structure that represents an LDAP entry. 055 * <BR><BR> 056 * This class is primarily intended to be used in the process of updating 057 * applications which use the Netscape Directory SDK for Java to switch to or 058 * coexist with the UnboundID LDAP SDK for Java. For applications not written 059 * using the Netscape Directory SDK for Java, the {@link Entry} class should be 060 * used instead. 061 */ 062@NotExtensible() 063@NotMutable() 064@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE) 065public class LDAPEntry 066 implements Serializable 067{ 068 /** 069 * The serial version UID for this serializable class. 070 */ 071 private static final long serialVersionUID = -6285850560316222689L; 072 073 074 075 // The DN for this entry. 076 private final String dn; 077 078 // The attribute set for this entry. 079 private final LDAPAttributeSet attributeSet; 080 081 082 083 /** 084 * Creates a new LDAP entry with a zero-length DN and no attributes. 085 */ 086 public LDAPEntry() 087 { 088 this("", new LDAPAttributeSet()); 089 } 090 091 092 093 /** 094 * Creates a new LDAP entry with the provided DN and no attributes. 095 * 096 * @param distinguishedName The DN to use for the entry. 097 */ 098 public LDAPEntry(final String distinguishedName) 099 { 100 this(distinguishedName, new LDAPAttributeSet()); 101 } 102 103 104 105 /** 106 * Creates a new LDAP entry with the provided DN and attributes. 107 * 108 * @param distinguishedName The DN to use for the entry. 109 * @param attrs The attributes to use for the entry. 110 */ 111 public LDAPEntry(final String distinguishedName, final LDAPAttributeSet attrs) 112 { 113 dn = distinguishedName; 114 115 if (attrs == null) 116 { 117 attributeSet = new LDAPAttributeSet(); 118 } 119 else 120 { 121 attributeSet = attrs; 122 } 123 } 124 125 126 127 /** 128 * Creates a new LDAP entry from the provided {@link Entry} object. 129 * 130 * @param entry The entry to use to create this LDAP entry. 131 */ 132 public LDAPEntry(final Entry entry) 133 { 134 dn = entry.getDN(); 135 136 attributeSet = new LDAPAttributeSet(); 137 for (final Attribute a : entry.getAttributes()) 138 { 139 attributeSet.add(new LDAPAttribute(a)); 140 } 141 } 142 143 144 145 /** 146 * Retrieves the distinguished name for this entry. 147 * 148 * @return The distinguished name for this entry. 149 */ 150 public String getDN() 151 { 152 return dn; 153 } 154 155 156 157 /** 158 * Retrieves the attributes for this entry. 159 * 160 * @return The attributes for this entry. 161 */ 162 public LDAPAttributeSet getAttributeSet() 163 { 164 return attributeSet; 165 } 166 167 168 169 /** 170 * Retrieves the set of attributes containing the specified subtype for this 171 * entry. 172 * 173 * @param subtype The subtype for the attributes to retrieve. 174 * 175 * @return The set of attributes containing the specified subtype. 176 */ 177 public LDAPAttributeSet getAttributeSet(final String subtype) 178 { 179 return attributeSet.getSubset(subtype); 180 } 181 182 183 184 /** 185 * Retrieves the attribute with the specified name. 186 * 187 * @param attrName The name of the attribute to retrieve. 188 * 189 * @return The requested attribute, or {@code null} if there is none. 190 */ 191 public LDAPAttribute getAttribute(final String attrName) 192 { 193 return attributeSet.getAttribute(attrName); 194 } 195 196 197 198 /** 199 * Retrieves the attribute with the specified base name and language subtype. 200 * 201 * @param attrName The base name of the attribute to retrieve. 202 * @param lang The language subtype for the attribute to retrieve. 203 * 204 * @return The requested attribute, or {@code null} if there is none. 205 */ 206 public LDAPAttribute getAttribute(final String attrName, final String lang) 207 { 208 return attributeSet.getAttribute(attrName, lang); 209 } 210 211 212 213 /** 214 * Retrieves an {@link Entry} object that is the equivalent of this LDAP 215 * entry. 216 * 217 * @return The {@code Entry} object that is the equivalent of this LDAP 218 * entry. 219 */ 220 public final Entry toEntry() 221 { 222 final ArrayList<Attribute> attrs = new ArrayList<>(attributeSet.size()); 223 final Enumeration<LDAPAttribute> attrEnum = attributeSet.getAttributes(); 224 while (attrEnum.hasMoreElements()) 225 { 226 attrs.add(attrEnum.nextElement().toAttribute()); 227 } 228 229 return new Entry(dn, attrs); 230 } 231 232 233 234 /** 235 * Retrieves a string representation of this LDAP entry. 236 * 237 * @return A string representation of this LDAP entry. 238 */ 239 @Override() 240 public String toString() 241 { 242 return toEntry().toString(); 243 } 244}