KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > model > URL


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

20
21 package org.apache.directory.ldapstudio.browser.core.model;
22
23
24 import java.io.UnsupportedEncodingException JavaDoc;
25 import java.net.URLDecoder JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
28 import org.apache.directory.ldapstudio.browser.core.utils.Utils;
29
30
31 /**
32  * An URL represents a LDAP URL.
33  *
34  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
35  * @version $Rev$, $Date$
36  */

37 public class URL
38 {
39
40     // ldap://host:port/dn?attributes?scope?filter?extensions
41
// ldap://localhost:389/ou=Testdata100,dc=seelmann,dc=muc
42

43     // ldapurl = scheme "://" [hostport] ["/" [dn ["?" [attributes] ["?"
44
// [scope] ["?" [filter] ["?" extensions]]]]]]
45
// scheme = "ldap"
46
// attributes = attrdesc *("," attrdesc)
47
// scope = "base" / "one" / "sub"
48
// dn = distinguishedName from Section 3 of [1]
49
// hostport = hostport from Section 5 of RFC 1738 [5]
50
// attrdesc = AttributeDescription from Section 4.1.5 of [2]
51
// filter = filter from Section 4 of [4]
52
// extensions = extension *("," extension)
53
// extension = ["!"] extype ["=" exvalue]
54
// extype = token / xtoken
55
// exvalue = LDAPString from section 4.1.2 of [2]
56
// token = oid from section 4.1 of [3]
57
// xtoken = ("X-" / "x-") token
58

59     /** The protocoll, ldap or ldaps */
60     private String JavaDoc protocol = null;
61
62     /** The host */
63     private String JavaDoc host = null;
64
65     /** The port */
66     private String JavaDoc port = null;
67
68     /** The dn */
69     private String JavaDoc dn = null;
70
71     /** The attributes */
72     private String JavaDoc attributes = null;
73
74     /** The scope */
75     private String JavaDoc scope = null;
76
77     /** The filter */
78     private String JavaDoc filter = null;
79
80     /** The extensions */
81     private String JavaDoc extensions = null;
82
83
84     /**
85      * Creates a new instance of URL. The given string is
86      * parsed to an URL.
87      *
88      * @param url the URL
89      */

90     public URL( String JavaDoc url )
91     {
92         if ( url == null )
93         {
94             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_url );
95         }
96
97         this.parseUrl( url );
98     }
99
100
101     /**
102      * Creates a new instance of URL, based on the given connection and DN.
103      * Only the fields protocol, host, port and dn exists when using this constructor.
104      *
105      * @param connection the connection
106      * @param dn the DN
107      */

108     public URL( IConnection connection, DN dn )
109     {
110         this( connection );
111
112         if ( dn == null )
113         {
114             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_url );
115         }
116
117         this.dn = dn.toString();
118     }
119
120
121     /**
122      * Creates a new instance of URL, based on the given connection. Only
123      * the fields protocol, host and port exists when using this constructor.
124      *
125      * @param connection the connection
126      */

127     public URL( IConnection connection )
128     {
129         if ( connection == null )
130         {
131             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_url );
132         }
133
134         if ( connection.getEncryptionMethod() == IConnection.ENCYRPTION_LDAPS )
135         {
136             this.protocol = "ldaps";; //$NON-NLS-1$
137
}
138         else
139         {
140             this.protocol = "ldap"; //$NON-NLS-1$
141
}
142         this.host = connection.getHost();
143         this.port = Integer.toString( connection.getPort() );
144     }
145
146
147     /**
148      * Creates a new instance of URL, based on the given search. Initializes
149      * the fields protocol, host, port, dn, attributes, scope and filter.
150      *
151      * @param search the search
152      */

153     public URL( ISearch search )
154     {
155         this( search.getConnection(), search.getSearchBase() );
156
157         if ( search == null )
158         {
159             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_url );
160         }
161
162         this.attributes = Utils.arrayToString( search.getReturningAttributes() );
163         this.scope = search.getScope() == ISearch.SCOPE_SUBTREE ? "sub" : //$NON-NLS-1$
164
search.getScope() == ISearch.SCOPE_ONELEVEL ? "one" : //$NON-NLS-1$
165
"base"; //$NON-NLS-1$
166
this.filter = search.getFilter();
167     }
168
169
170     /**
171      * {@inheritDoc}
172      */

173     public boolean equals( Object JavaDoc o ) throws ClassCastException JavaDoc
174     {
175         if ( o instanceof URL )
176         {
177             return this.toString().equals( ( ( URL ) o ).toString() );
178         }
179         return false;
180     }
181
182
183     /**
184      * {@inheritDoc}
185      */

186     public int hashCode()
187     {
188         return this.toString().hashCode();
189     }
190
191
192     /**
193      * Returns the string representation of this LDAP URL.
194      */

195     public String JavaDoc toString()
196     {
197         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
198
199         if ( hasProtocol() )
200             sb.append( protocol );
201
202         sb.append( "://" ); //$NON-NLS-1$
203

204         if ( hasHost() )
205             sb.append( host );
206         if ( hasPort() )
207             sb.append( ":" ).append( port ); //$NON-NLS-1$
208

209         if ( hasDn() || hasAttributes() || hasScope() || hasFilter() || hasExtensions() )
210             sb.append( "/" ); //$NON-NLS-1$
211
if ( hasDn() )
212             sb.append( dn );
213
214         if ( hasAttributes() || hasScope() || hasFilter() || hasExtensions() )
215             sb.append( "?" ); //$NON-NLS-1$
216
if ( hasAttributes() )
217             sb.append( attributes );
218
219         if ( hasScope() || hasFilter() || hasExtensions() )
220             sb.append( "?" ); //$NON-NLS-1$
221
if ( hasScope() )
222             sb.append( scope );
223
224         if ( hasFilter() || hasExtensions() )
225             sb.append( "?" ); //$NON-NLS-1$
226
if ( hasFilter() )
227             sb.append( filter );
228
229         if ( hasExtensions() )
230             sb.append( "?" ); //$NON-NLS-1$
231
if ( hasExtensions() )
232             sb.append( extensions );
233
234         return sb.toString();
235     }
236
237
238     /**
239      * Parses the given string represntation of the URL.
240      *
241      * @param url the URL
242      */

243     private void parseUrl( String JavaDoc url )
244     {
245
246         try
247         {
248             url = URLDecoder.decode( url, "UTF-8" ); //$NON-NLS-1$
249

250             // protocol
251
String JavaDoc[] protocolAndRest = url.split( "://", 2 ); //$NON-NLS-1$
252
if ( protocolAndRest.length > 0 )
253             {
254                 if ( "ldap".equals( protocolAndRest[0] ) || "ldaps".equals( protocolAndRest[0] ) ) { //$NON-NLS-1$ //$NON-NLS-2$
255
this.protocol = protocolAndRest[0];
256                 }
257             }
258             if ( protocolAndRest.length < 2 )
259             {
260                 return;
261             }
262
263             // host and port
264
String JavaDoc[] hostportAndRest = protocolAndRest[1].split( "/", 2 ); //$NON-NLS-1$
265
if ( hostportAndRest.length > 0 )
266             {
267                 String JavaDoc[] hostAndPort = hostportAndRest[0].split( ":", 2 ); //$NON-NLS-1$
268
if ( hostAndPort.length == 2 )
269                 {
270                     this.host = hostAndPort[0];
271                     this.port = hostAndPort[1];
272                 }
273                 else if ( hostAndPort.length == 1 && hostAndPort[0].length() > 0 )
274                 {
275                     this.host = hostAndPort[0];
276                     this.port = "389"; //$NON-NLS-1$
277
}
278             }
279             if ( hostportAndRest.length < 2 )
280             {
281                 return;
282             }
283
284             // dn
285
String JavaDoc[] dnAndRest = hostportAndRest[1].split( "\\?", 2 ); //$NON-NLS-1$
286
if ( dnAndRest.length > 0 && dnAndRest[0].length() > 0 )
287             {
288                 this.dn = dnAndRest[0];
289             }
290             if ( dnAndRest.length < 2 )
291             {
292                 return;
293             }
294
295             // attributes
296
String JavaDoc[] attributesAndRest = dnAndRest[1].split( "\\?", 2 ); //$NON-NLS-1$
297
if ( attributesAndRest.length > 0 && attributesAndRest[0].length() > 0 )
298             {
299                 this.attributes = attributesAndRest[0];
300             }
301             if ( attributesAndRest.length < 2 )
302             {
303                 return;
304             }
305
306             // scope
307
String JavaDoc[] scopeAndRest = attributesAndRest[1].split( "\\?", 2 ); //$NON-NLS-1$
308
if ( scopeAndRest.length > 0 && scopeAndRest[0].length() > 0 )
309             {
310                 this.scope = scopeAndRest[0];
311             }
312             if ( scopeAndRest.length < 2 )
313             {
314                 return;
315             }
316
317             // filter
318
String JavaDoc[] filterAndRest = scopeAndRest[1].split( "\\?", 2 ); //$NON-NLS-1$
319
if ( filterAndRest.length > 0 && filterAndRest[0].length() > 0 )
320             {
321                 this.filter = filterAndRest[0];
322             }
323             if ( filterAndRest.length < 2 )
324             {
325                 return;
326             }
327
328             if ( filterAndRest[1].length() > 0 )
329             {
330                 this.extensions = filterAndRest[0];
331             }
332
333         }
334         catch ( UnsupportedEncodingException JavaDoc e1 )
335         {
336         }
337
338     }
339
340
341     /**
342      * Checks for protocol.
343      *
344      * @return true, if has protocol
345      */

346     public boolean hasProtocol()
347     {
348         try
349         {
350             getProtocol();
351             return true;
352         }
353         catch ( NoSuchFieldException JavaDoc e )
354         {
355             return false;
356         }
357     }
358
359
360     /**
361      * Gets the protocol.
362      *
363      * @return the protocol
364      * @throws NoSuchFieldException if not has protocol
365      */

366     public String JavaDoc getProtocol() throws NoSuchFieldException JavaDoc
367     {
368         if ( protocol == null )
369         {
370             throw new NoSuchFieldException JavaDoc( BrowserCoreMessages.model__url_no_protocol );
371         }
372
373         return protocol;
374     }
375
376
377     /**
378      * Checks for host.
379      *
380      * @return true, if has host
381      */

382     public boolean hasHost()
383     {
384         try
385         {
386             getHost();
387             return true;
388         }
389         catch ( NoSuchFieldException JavaDoc e )
390         {
391             return false;
392         }
393     }
394
395
396     /**
397      * Gets the host.
398      *
399      * @return the host
400      * @throws NoSuchFieldException if not has host
401      */

402     public String JavaDoc getHost() throws NoSuchFieldException JavaDoc
403     {
404         if ( host == null )
405         {
406             throw new NoSuchFieldException JavaDoc( BrowserCoreMessages.model__url_no_host );
407         }
408
409         return host;
410     }
411
412
413     /**
414      * Checks for port.
415      *
416      * @return true, if has port
417      */

418     public boolean hasPort()
419     {
420         try
421         {
422             getPort();
423             return true;
424         }
425         catch ( NoSuchFieldException JavaDoc e )
426         {
427             return false;
428         }
429     }
430
431
432     /**
433      * Gets the port.
434      *
435      * @return the port
436      * @throws NoSuchFieldException if not has port
437      */

438     public String JavaDoc getPort() throws NoSuchFieldException JavaDoc
439     {
440         try
441         {
442             int p = Integer.parseInt( port );
443             if ( p > 0 && p <= 65536 )
444             {
445                 return port;
446             }
447             else
448             {
449                 throw new NoSuchFieldException JavaDoc( BrowserCoreMessages.model__url_no_port );
450             }
451         }
452         catch ( NumberFormatException JavaDoc e )
453         {
454             throw new NoSuchFieldException JavaDoc( BrowserCoreMessages.model__url_no_port );
455         }
456     }
457
458
459     /**
460      * Checks for dn.
461      *
462      * @return true, if has dn
463      */

464     public boolean hasDn()
465     {
466         try
467         {
468             getDn();
469             return true;
470         }
471         catch ( NoSuchFieldException JavaDoc e )
472         {
473             return false;
474         }
475     }
476
477
478     /**
479      * Gets the dn.
480      *
481      * @return the dn
482      * @throws NoSuchFieldException if not has dn
483      */

484     public DN getDn() throws NoSuchFieldException JavaDoc
485     {
486         if ( dn == null )
487         {
488             throw new NoSuchFieldException JavaDoc( BrowserCoreMessages.model__url_no_dn );
489         }
490
491         try
492         {
493             return new DN( dn );
494         }
495         catch ( NameException e )
496         {
497             throw new NoSuchFieldException JavaDoc( BrowserCoreMessages.model__url_no_dn );
498         }
499     }
500
501
502     /**
503      * Checks for attributes.
504      *
505      * @return true, if has attributes
506      */

507     public boolean hasAttributes()
508     {
509         try
510         {
511             getAttributes();
512             return true;
513         }
514         catch ( NoSuchFieldException JavaDoc e )
515         {
516             return false;
517         }
518     }
519
520
521     /**
522      * Gets the attributes.
523      *
524      * @return the attributes
525      * @throws NoSuchFieldException if not has attributes
526      */

527     public String JavaDoc[] getAttributes() throws NoSuchFieldException JavaDoc
528     {
529         if ( attributes == null )
530         {
531             throw new NoSuchFieldException JavaDoc( BrowserCoreMessages.model__url_no_attributes );
532         }
533
534         return Utils.stringToArray( attributes );
535         // return attributes.split(",");
536
}
537
538
539     /**
540      * Checks for scope.
541      *
542      * @return true, if has scope
543      */

544     public boolean hasScope()
545     {
546         try
547         {
548             getScope();
549             return true;
550         }
551         catch ( NoSuchFieldException JavaDoc e )
552         {
553             return false;
554         }
555     }
556
557
558     /**
559      * Gets the scope.
560      *
561      * @return the scope
562      * @throws NoSuchFieldException if not has scope
563      */

564     public int getScope() throws NoSuchFieldException JavaDoc
565     {
566         if ( scope == null )
567         {
568             throw new NoSuchFieldException JavaDoc( BrowserCoreMessages.model__url_no_scope );
569         }
570
571         if ( "base".equals( scope ) ) { //$NON-NLS-1$
572
return ISearch.SCOPE_OBJECT;
573         }
574         else if ( "one".equals( scope ) ) { //$NON-NLS-1$
575
return ISearch.SCOPE_ONELEVEL;
576         }
577         else if ( "sub".equals( scope ) ) { //$NON-NLS-1$
578
return ISearch.SCOPE_SUBTREE;
579         }
580         else
581         {
582             throw new NoSuchFieldException JavaDoc( BrowserCoreMessages.model__url_no_scope );
583         }
584     }
585
586
587     /**
588      * Checks for filter.
589      *
590      * @return true, if has filter
591      */

592     public boolean hasFilter()
593     {
594         try
595         {
596             getFilter();
597             return true;
598         }
599         catch ( NoSuchFieldException JavaDoc e )
600         {
601             return false;
602         }
603     }
604
605
606     /**
607      * Gets the filter.
608      *
609      * @return the filter
610      * @throws NoSuchFieldException if not has filter
611      */

612     public String JavaDoc getFilter() throws NoSuchFieldException JavaDoc
613     {
614         if ( filter == null )
615         {
616             throw new NoSuchFieldException JavaDoc( BrowserCoreMessages.model__url_no_filter );
617         }
618
619         return filter;
620     }
621
622
623     /**
624      * Checks for extensions.
625      *
626      * @return true, if has extensions
627      */

628     public boolean hasExtensions()
629     {
630         try
631         {
632             getExtensions();
633             return true;
634         }
635         catch ( NoSuchFieldException JavaDoc e )
636         {
637             return false;
638         }
639     }
640
641
642     /**
643      * Gets the extensions.
644      *
645      * @return the extensions
646      * @throws NoSuchFieldException if not has extensions
647      */

648     public String JavaDoc getExtensions() throws NoSuchFieldException JavaDoc
649     {
650         if ( extensions == null )
651         {
652             throw new NoSuchFieldException JavaDoc( BrowserCoreMessages.model__url_no_extensions );
653         }
654
655         return extensions;
656     }
657
658 }
659
Popular Tags