001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.ldap;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020:
021: import org.springframework.util.Assert;
022:
023: import java.io.UnsupportedEncodingException;
024:
025: import javax.naming.Context;
026: import javax.naming.NamingException;
027:
028: /**
029: * LDAP Utility methods.
030: *
031: * @author Luke Taylor
032: * @version $Id: LdapUtils.java 1784 2007-02-24 21:00:24Z luke_t $
033: */
034: public final class LdapUtils {
035: //~ Static fields/initializers =====================================================================================
036:
037: private static final Log logger = LogFactory
038: .getLog(LdapUtils.class);
039:
040: //~ Constructors ===================================================================================================
041:
042: private LdapUtils() {
043: }
044:
045: //~ Methods ========================================================================================================
046:
047: public static void closeContext(Context ctx) {
048: try {
049: if (ctx != null) {
050: ctx.close();
051: }
052: } catch (NamingException e) {
053: logger.error("Failed to close context.", e);
054: }
055: }
056:
057: /**
058: * Obtains the part of a DN relative to a supplied base context.<p>If the DN is
059: * "cn=bob,ou=people,dc=acegisecurity,dc=org" and the base context name is "ou=people,dc=acegisecurity,dc=org" it
060: * would return "cn=bob".</p>
061: *
062: * @param fullDn the DN
063: * @param baseCtx the context to work out the name relative to.
064: *
065: * @return the
066: *
067: * @throws NamingException any exceptions thrown by the context are propagated.
068: */
069: public static String getRelativeName(String fullDn, Context baseCtx)
070: throws NamingException {
071: String baseDn = baseCtx.getNameInNamespace();
072:
073: if (baseDn.length() == 0) {
074: return fullDn;
075: }
076:
077: if (baseDn.equals(fullDn)) {
078: return "";
079: }
080:
081: int index = fullDn.lastIndexOf(baseDn);
082:
083: Assert.isTrue(index > 0,
084: "Context base DN is not contained in the full DN");
085:
086: // remove the base name and preceding comma.
087: return fullDn.substring(0, index - 1);
088: }
089:
090: public static byte[] getUtf8Bytes(String s) {
091: try {
092: return s.getBytes("UTF-8");
093: } catch (UnsupportedEncodingException e) {
094: // Should be impossible since UTF-8 is required by all implementations
095: throw new IllegalStateException(
096: "Failed to convert string to UTF-8 bytes. Shouldn't be possible");
097: }
098: }
099:
100: /**
101: * Works out the root DN for an LDAP URL.<p>For example, the URL
102: * <tt>ldap://monkeymachine:11389/dc=acegisecurity,dc=org</tt> has the root DN "dc=acegisecurity,dc=org".</p>
103: *
104: * @param url the LDAP URL
105: *
106: * @return the root DN
107: */
108: public static String parseRootDnFromUrl(String url) {
109: Assert.hasLength(url);
110:
111: String urlRootDn = "";
112:
113: if (url.startsWith("ldap:") || url.startsWith("ldaps:")) {
114: // URI uri = parseLdapUrl(url);
115:
116: // urlRootDn = uri.getPath();
117: // skip past the "://"
118: int colon = url.indexOf(':');
119:
120: url = url.substring(colon + 3);
121:
122: // Match the slash at the end of the address (if there)
123: int slash = url.indexOf('/');
124:
125: if (slash >= 0) {
126: urlRootDn = url.substring(slash);
127: }
128: } else {
129: // Assume it's an embedded server
130: urlRootDn = url;
131: }
132:
133: if (urlRootDn.startsWith("/")) {
134: urlRootDn = urlRootDn.substring(1);
135: }
136:
137: return urlRootDn;
138: }
139:
140: // removed for 1.3 compatibility
141: /**
142: * Parses the supplied LDAP URL.
143: * @param url the URL (e.g. <tt>ldap://monkeymachine:11389/dc=acegisecurity,dc=org</tt>).
144: * @return the URI object created from the URL
145: * @throws IllegalArgumentException if the URL is null, empty or the URI syntax is invalid.
146: */
147:
148: // private static URI parseLdapUrl(String url) {
149: // Assert.hasLength(url);
150: //
151: // try {
152: // return new URI(url);
153: // } catch (URISyntaxException e) {
154: // IllegalArgumentException iae = new IllegalArgumentException("Unable to parse url: " + url);
155: // iae.initCause(e);
156: // throw iae;
157: // }
158: // }
159: }
|