1 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 package org.acegisecurity.ldap;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import org.springframework.util.Assert;
22
23 import java.io.UnsupportedEncodingException;
24
25 import javax.naming.Context;
26 import javax.naming.NamingException;
27
28
29 /**
30 * LDAP Utility methods.
31 *
32 * @author Luke Taylor
33 * @version $Id: LdapUtils.java 1784 2007-02-24 21:00:24Z luke_t $
34 */
35 public final class LdapUtils {
36 //~ Static fields/initializers =====================================================================================
37
38 private static final Log logger = LogFactory.getLog(LdapUtils.class);
39
40 //~ Constructors ===================================================================================================
41
42 private LdapUtils() {
43 }
44
45 //~ Methods ========================================================================================================
46
47 public static void closeContext(Context ctx) {
48 try {
49 if (ctx != null) {
50 ctx.close();
51 }
52 } catch (NamingException e) {
53 logger.error("Failed to close context.", e);
54 }
55 }
56
57 /**
58 * Obtains the part of a DN relative to a supplied base context.<p>If the DN is
59 * "cn=bob,ou=people,dc=acegisecurity,dc=org" and the base context name is "ou=people,dc=acegisecurity,dc=org" it
60 * would return "cn=bob".</p>
61 *
62 * @param fullDn the DN
63 * @param baseCtx the context to work out the name relative to.
64 *
65 * @return the
66 *
67 * @throws NamingException any exceptions thrown by the context are propagated.
68 */
69 public static String getRelativeName(String fullDn, Context baseCtx)
70 throws NamingException {
71 String baseDn = baseCtx.getNameInNamespace();
72
73 if (baseDn.length() == 0) {
74 return fullDn;
75 }
76
77 if (baseDn.equals(fullDn)) {
78 return "";
79 }
80
81 int index = fullDn.lastIndexOf(baseDn);
82
83 Assert.isTrue(index > 0, "Context base DN is not contained in the full DN");
84
85 // remove the base name and preceding comma.
86 return fullDn.substring(0, index - 1);
87 }
88
89 public static byte[] getUtf8Bytes(String s) {
90 try {
91 return s.getBytes("UTF-8");
92 } catch (UnsupportedEncodingException e) {
93 // Should be impossible since UTF-8 is required by all implementations
94 throw new IllegalStateException("Failed to convert string to UTF-8 bytes. Shouldn't be possible");
95 }
96 }
97
98 /**
99 * Works out the root DN for an LDAP URL.<p>For example, the URL
100 * <tt>ldap://monkeymachine:11389/dc=acegisecurity,dc=org</tt> has the root DN "dc=acegisecurity,dc=org".</p>
101 *
102 * @param url the LDAP URL
103 *
104 * @return the root DN
105 */
106 public static String parseRootDnFromUrl(String url) {
107 Assert.hasLength(url);
108
109 String urlRootDn = "";
110
111 if (url.startsWith("ldap:") || url.startsWith("ldaps:")) {
112 // URI uri = parseLdapUrl(url);
113
114 // urlRootDn = uri.getPath();
115 // skip past the "://"
116 int colon = url.indexOf(':');
117
118 url = url.substring(colon + 3);
119
120 // Match the slash at the end of the address (if there)
121 int slash = url.indexOf('/');
122
123 if (slash >= 0) {
124 urlRootDn = url.substring(slash);
125 }
126 } else {
127 // Assume it's an embedded server
128 urlRootDn = url;
129 }
130
131 if (urlRootDn.startsWith("/")) {
132 urlRootDn = urlRootDn.substring(1);
133 }
134
135 return urlRootDn;
136 }
137
138 // removed for 1.3 compatibility
139 /**
140 * Parses the supplied LDAP URL.
141 * @param url the URL (e.g. <tt>ldap://monkeymachine:11389/dc=acegisecurity,dc=org</tt>).
142 * @return the URI object created from the URL
143 * @throws IllegalArgumentException if the URL is null, empty or the URI syntax is invalid.
144 */
145
146 // private static URI parseLdapUrl(String url) {
147 // Assert.hasLength(url);
148 //
149 // try {
150 // return new URI(url);
151 // } catch (URISyntaxException e) {
152 // IllegalArgumentException iae = new IllegalArgumentException("Unable to parse url: " + url);
153 // iae.initCause(e);
154 // throw iae;
155 // }
156 // }
157 }