| 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 org.ietf.ldap.util.DN; |
| 41 | import org.ietf.ldap.util.RDN; |
| 42 | |
| 43 | /** |
| 44 | * Represents a distinguished name in LDAP. |
| 45 | * <P> |
| 46 | * |
| 47 | * You can use objects of this class to split a distinguished name |
| 48 | * (DN) into its individual components. You can also escape the |
| 49 | * characters in a DN. |
| 50 | * <P> |
| 51 | * |
| 52 | * @version 1.0 |
| 53 | */ |
| 54 | public class LDAPDN { |
| 55 | |
| 56 | /** |
| 57 | * Returns the individual components of a relative distinguished name (RDN). |
| 58 | * @param rdn relative distinguished name of which you want to get the components. |
| 59 | * @param noTypes if <CODE>true</CODE>, returns only the values of the |
| 60 | * components and not the names (such as 'cn=') |
| 61 | * @return an array of strings representing the components of the RDN. |
| 62 | * @see org.ietf.ldap.LDAPDN#explodeDN(java.lang.String, boolean) |
| 63 | */ |
| 64 | public static String[] explodeRDN (String rdn, boolean noTypes) { |
| 65 | RDN name = new RDN(rdn); |
| 66 | if ( noTypes ) { |
| 67 | return name.getValues(); |
| 68 | } else { |
| 69 | String[] str = new String[1]; |
| 70 | str[0] = name.toString(); |
| 71 | return str; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Compares two dn's for equality. |
| 77 | * @param dn1 the first dn to compare |
| 78 | * @param dn2 the second dn to compare |
| 79 | * @return true if the two dn's are equal |
| 80 | */ |
| 81 | public static boolean equals(String dn1, String dn2) { |
| 82 | return normalize(dn1).equals(normalize(dn2)); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the RDN after escaping the characters specified |
| 87 | * by <CODE>org.ietf.ldap.util.DN.ESCAPED_CHAR</CODE>. |
| 88 | * <P> |
| 89 | * |
| 90 | * @param rdn the RDN to escape |
| 91 | * @return the RDN with the characters escaped. |
| 92 | * @see org.ietf.ldap.util.DN#ESCAPED_CHAR |
| 93 | * @see org.ietf.ldap.LDAPDN#unEscapeRDN(java.lang.String) |
| 94 | */ |
| 95 | public static String escapeRDN(String rdn) { |
| 96 | |
| 97 | RDN name = new RDN(rdn); |
| 98 | String[] val = name.getValues(); |
| 99 | if (val == null) |
| 100 | return rdn; |
| 101 | |
| 102 | StringBuffer[] buffer = new StringBuffer[val.length]; |
| 103 | StringBuffer retbuf = new StringBuffer(); |
| 104 | String[] types = name.getTypes(); |
| 105 | |
| 106 | for (int j = 0; j < val.length; j++ ) { |
| 107 | buffer[j] = new StringBuffer(val[j]); |
| 108 | |
| 109 | int i=0; |
| 110 | while (i<buffer[j].length()) { |
| 111 | if (isEscape(buffer[j].charAt(i))) { |
| 112 | buffer[j].insert(i, '\\'); |
| 113 | i++; |
| 114 | } |
| 115 | |
| 116 | i++; |
| 117 | } |
| 118 | |
| 119 | retbuf.append( ((retbuf.length() > 0) ? " + " : "") + types[j] + "=" + |
| 120 | ( new String( buffer[j] ) ) ); |
| 121 | } |
| 122 | |
| 123 | return new String( retbuf ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns the individual components of a distinguished name (DN). |
| 128 | * @param dn distinguished name of which you want to get the components. |
| 129 | * @param noTypes if <CODE>true</CODE>, returns only the values of the |
| 130 | * components and not the names (such as 'cn=') |
| 131 | * @return an array of strings representing the components of the DN. |
| 132 | * @see org.ietf.ldap.LDAPDN#explodeRDN(java.lang.String, boolean) |
| 133 | */ |
| 134 | public static String[] explodeDN (String dn, boolean noTypes) { |
| 135 | DN name = new DN(dn); |
| 136 | return name.explodeDN(noTypes); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns true if the string conforms to distinguished name syntax; |
| 141 | * NOT IMPLEMENTED YET |
| 142 | * |
| 143 | * @param dn string to evaluate for distinguished name syntax |
| 144 | * @return true if the string conforms to distinguished name syntax |
| 145 | */ |
| 146 | public boolean isValid( String dn ) { |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Normalizes the dn. |
| 152 | * @param dn the DN to normalize |
| 153 | * @return the normalized DN |
| 154 | */ |
| 155 | public static String normalize(String dn) { |
| 156 | return (new DN(dn)).toString(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns the RDN after unescaping any escaped characters. |
| 161 | * For a list of characters that are typically escaped in a |
| 162 | * DN, see <CODE>org.ietf.ldap.LDAPDN.ESCAPED_CHAR</CODE>. |
| 163 | * <P> |
| 164 | * |
| 165 | * @param rdn the RDN to unescape |
| 166 | * @return the unescaped RDN. |
| 167 | * @see org.ietf.ldap.util.DN#ESCAPED_CHAR |
| 168 | * @see org.ietf.ldap.LDAPDN#escapeRDN(java.lang.String) |
| 169 | */ |
| 170 | public static String unEscapeRDN(String rdn) { |
| 171 | RDN name = new RDN(rdn); |
| 172 | String[] vals = name.getValues(); |
| 173 | if ( (vals == null) || (vals.length < 1) ) |
| 174 | return rdn; |
| 175 | |
| 176 | StringBuffer buffer = new StringBuffer(vals[0]); |
| 177 | StringBuffer copy = new StringBuffer(); |
| 178 | int i=0; |
| 179 | while (i<buffer.length()) { |
| 180 | char c = buffer.charAt(i++); |
| 181 | if (c != '\\') { |
| 182 | copy.append(c); |
| 183 | } |
| 184 | else { // copy the escaped char following the back slash |
| 185 | if (i<buffer.length()) { |
| 186 | copy.append(buffer.charAt(i++)); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return name.getTypes()[0]+"="+(new String(copy)); |
| 192 | } |
| 193 | |
| 194 | private static boolean isEscape(char c) { |
| 195 | for (int i=0; i<DN.ESCAPED_CHAR.length; i++) |
| 196 | if (c == DN.ESCAPED_CHAR[i]) |
| 197 | return true; |
| 198 | return false; |
| 199 | } |
| 200 | } |