1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.services.ldap;
18
19 import java.net.MalformedURLException;
20 import java.net.URLDecoder;
21
22 /***
23 *
24 * @author <a href="mailto:ender@kilicoglu.nom.tr">Ender KILICOGLU</a>
25 * @version $Id: LDAPURL.java,v 1.6 2004/02/23 03:28:31 jford Exp $
26 *
27 */
28 public class LDAPURL
29 {
30 private String host;
31 private int port;
32 private String dn;
33 private String base;
34
35 public LDAPURL()
36 {
37 host = dn = base = null;
38 port = 389;
39 }
40
41 public LDAPURL(String url)
42 throws MalformedURLException
43 {
44 try
45 {
46
47
48
49 url = URLDecoder.decode(url);
50 }
51 catch(Exception e)
52 {
53 throw new MalformedURLException(e.getMessage());
54 }
55 int p1 = url.indexOf("://");
56 if(p1 == -1)
57 throw new MalformedURLException("Missing '[protocol]://'");
58 String protocol = url.substring(0, p1);
59 p1 += 3;
60 int p2 = url.indexOf(47, p1);
61 String base = null;
62 if(p2 == -1)
63 {
64 base = url.substring(p1);
65 parseHostPort(base);
66 dn = "";
67 } else
68 {
69 base = url.substring(p1, p2);
70 p2++;
71 dn = url.substring(p2);
72 int p3 = dn.indexOf(63);
73 if(p3 != -1)
74 dn = dn.substring(0, p3);
75 parseHostPort(base);
76 }
77 }
78
79 public LDAPURL(String host, int port, String dn)
80 {
81 this.host = host;
82 this.port = port;
83 this.dn = dn;
84 }
85
86 public static String encode(String toEncode)
87 {
88 StringBuffer encoded = new StringBuffer(toEncode.length() + 10);
89 for(int currPos = 0; currPos < toEncode.length(); currPos++)
90 {
91 char currChar = toEncode.charAt(currPos);
92 if(currChar >= 'a' && currChar <= 'z' || currChar >= 'A' && currChar <= 'Z' || currChar >= '0' && currChar <= '9' || "$-_.+!*'(),".indexOf(currChar) > 0)
93 {
94 encoded.append(currChar);
95 } else
96 {
97 encoded.append("%");
98 encoded.append(hexChar((currChar & 0xf0) >> 4));
99 encoded.append(hexChar(currChar & 0xf));
100 }
101 }
102
103 return encoded.toString();
104 }
105
106 public String getBase()
107 {
108 if(base == null)
109 base = "ldap://" + host + ":" + port;
110 return base;
111 }
112
113 public String getDN()
114 {
115 return dn;
116 }
117
118 public String getEncodedUrl()
119 {
120 return getBase() + "/" + encode(dn);
121 }
122
123 public String getHost()
124 {
125 return host;
126 }
127
128 public int getPort()
129 {
130 return port;
131 }
132
133 public String getUrl()
134 {
135 return getBase() + "/" + dn;
136 }
137
138 private static char hexChar(int hexValue)
139 {
140 if(hexValue < 0 || hexValue > 15)
141 return 'x';
142 if(hexValue < 10)
143 return (char)(hexValue + 48);
144 else
145 return (char)((hexValue - 10) + 97);
146 }
147
148 private void parseHostPort(String str)
149 throws MalformedURLException
150 {
151 int p1 = str.indexOf(58);
152 if(p1 == -1)
153 {
154 host = str;
155 port = 389;
156 } else
157 {
158 host = str.substring(0, p1);
159 String pp = str.substring(p1 + 1);
160 try
161 {
162 port = Integer.parseInt(pp);
163 }
164 catch(NumberFormatException _ex)
165 {
166 throw new MalformedURLException("Invalid port number: " + pp);
167 }
168 }
169 }
170
171 public boolean sameHosts(LDAPURL url)
172 {
173 return getHost().equalsIgnoreCase(url.getHost()) && getPort() == url.getPort();
174 }
175
176 public void setDN(String dn)
177 {
178 this.dn = dn;
179 }
180
181 public void setHost(String host)
182 {
183 this.host = host;
184 base = null;
185 }
186
187 public void setPort(int port)
188 {
189 this.port = port;
190 base = null;
191 }
192
193 public static String toUrl(String host, int port, String dn, boolean ssl)
194 {
195 StringBuffer msg = new StringBuffer();
196 msg.append(ssl ? "ldaps://" : "ldap://");
197 msg.append(host);
198 if(ssl && port != 636 || !ssl && port != 389)
199 {
200 msg.append(":");
201 msg.append(String.valueOf(port));
202 }
203 msg.append("/");
204 msg.append(dn);
205 return msg.toString();
206 }
207
208 public String toString()
209 {
210 return "LDAPURL: base = " + base + ", url = " + toUrl(host, port, dn, false);
211 }
212
213 }