KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > ldap > LDAPURL


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.services.ldap;
18
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URLDecoder JavaDoc;
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 JavaDoc host;
31     private int port;
32     private String JavaDoc dn;
33     private String JavaDoc base;
34
35     public LDAPURL()
36     {
37         host = dn = base = null;
38         port = 389;
39     }
40
41     public LDAPURL(String JavaDoc url)
42         throws MalformedURLException JavaDoc
43     {
44         try
45         {
46             // this is the correct approach for 1.4, unfortunately its unsupported in 1.3
47
// uncomment the line below if using 1.4
48
// url = URLDecoder.decode(url,"UTF-8");
49
url = URLDecoder.decode(url); // deprecated in 1.4
50
}
51         catch(Exception JavaDoc e)
52         {
53             throw new MalformedURLException JavaDoc(e.getMessage());
54         }
55         int p1 = url.indexOf("://");
56         if(p1 == -1)
57             throw new MalformedURLException JavaDoc("Missing '[protocol]://'");
58         String JavaDoc protocol = url.substring(0, p1);
59         p1 += 3;
60         int p2 = url.indexOf(47, p1);
61         String JavaDoc 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 JavaDoc host, int port, String JavaDoc dn)
80     {
81         this.host = host;
82         this.port = port;
83         this.dn = dn;
84     }
85
86     public static String JavaDoc encode(String JavaDoc toEncode)
87     {
88         StringBuffer JavaDoc encoded = new StringBuffer JavaDoc(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 JavaDoc getBase()
107     {
108         if(base == null)
109             base = "ldap://" + host + ":" + port;
110         return base;
111     }
112
113     public String JavaDoc getDN()
114     {
115         return dn;
116     }
117
118     public String JavaDoc getEncodedUrl()
119     {
120         return getBase() + "/" + encode(dn);
121     }
122
123     public String JavaDoc getHost()
124     {
125         return host;
126     }
127
128     public int getPort()
129     {
130         return port;
131     }
132
133     public String JavaDoc 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 JavaDoc str)
149         throws MalformedURLException JavaDoc
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 JavaDoc pp = str.substring(p1 + 1);
160             try
161             {
162                 port = Integer.parseInt(pp);
163             }
164             catch(NumberFormatException JavaDoc _ex)
165             {
166                 throw new MalformedURLException JavaDoc("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 JavaDoc dn)
177     {
178         this.dn = dn;
179     }
180
181     public void setHost(String JavaDoc 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 JavaDoc toUrl(String JavaDoc host, int port, String JavaDoc dn, boolean ssl)
194     {
195         StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
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 JavaDoc toString()
209     {
210         return "LDAPURL: base = " + base + ", url = " + toUrl(host, port, dn, false);
211     }
212
213 }
214
Popular Tags