1 20 21 package org.apache.directory.ldapstudio.browser.core.model; 22 23 24 import java.io.UnsupportedEncodingException ; 25 import java.net.URLDecoder ; 26 27 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 28 import org.apache.directory.ldapstudio.browser.core.utils.Utils; 29 30 31 37 public class URL 38 { 39 40 43 59 60 private String protocol = null; 61 62 63 private String host = null; 64 65 66 private String port = null; 67 68 69 private String dn = null; 70 71 72 private String attributes = null; 73 74 75 private String scope = null; 76 77 78 private String filter = null; 79 80 81 private String extensions = null; 82 83 84 90 public URL( String url ) 91 { 92 if ( url == null ) 93 { 94 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_url ); 95 } 96 97 this.parseUrl( url ); 98 } 99 100 101 108 public URL( IConnection connection, DN dn ) 109 { 110 this( connection ); 111 112 if ( dn == null ) 113 { 114 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_url ); 115 } 116 117 this.dn = dn.toString(); 118 } 119 120 121 127 public URL( IConnection connection ) 128 { 129 if ( connection == null ) 130 { 131 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_url ); 132 } 133 134 if ( connection.getEncryptionMethod() == IConnection.ENCYRPTION_LDAPS ) 135 { 136 this.protocol = "ldaps";; } 138 else 139 { 140 this.protocol = "ldap"; } 142 this.host = connection.getHost(); 143 this.port = Integer.toString( connection.getPort() ); 144 } 145 146 147 153 public URL( ISearch search ) 154 { 155 this( search.getConnection(), search.getSearchBase() ); 156 157 if ( search == null ) 158 { 159 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_url ); 160 } 161 162 this.attributes = Utils.arrayToString( search.getReturningAttributes() ); 163 this.scope = search.getScope() == ISearch.SCOPE_SUBTREE ? "sub" : search.getScope() == ISearch.SCOPE_ONELEVEL ? "one" : "base"; this.filter = search.getFilter(); 167 } 168 169 170 173 public boolean equals( Object o ) throws ClassCastException  174 { 175 if ( o instanceof URL ) 176 { 177 return this.toString().equals( ( ( URL ) o ).toString() ); 178 } 179 return false; 180 } 181 182 183 186 public int hashCode() 187 { 188 return this.toString().hashCode(); 189 } 190 191 192 195 public String toString() 196 { 197 StringBuffer sb = new StringBuffer (); 198 199 if ( hasProtocol() ) 200 sb.append( protocol ); 201 202 sb.append( "://" ); 204 if ( hasHost() ) 205 sb.append( host ); 206 if ( hasPort() ) 207 sb.append( ":" ).append( port ); 209 if ( hasDn() || hasAttributes() || hasScope() || hasFilter() || hasExtensions() ) 210 sb.append( "/" ); if ( hasDn() ) 212 sb.append( dn ); 213 214 if ( hasAttributes() || hasScope() || hasFilter() || hasExtensions() ) 215 sb.append( "?" ); if ( hasAttributes() ) 217 sb.append( attributes ); 218 219 if ( hasScope() || hasFilter() || hasExtensions() ) 220 sb.append( "?" ); if ( hasScope() ) 222 sb.append( scope ); 223 224 if ( hasFilter() || hasExtensions() ) 225 sb.append( "?" ); if ( hasFilter() ) 227 sb.append( filter ); 228 229 if ( hasExtensions() ) 230 sb.append( "?" ); if ( hasExtensions() ) 232 sb.append( extensions ); 233 234 return sb.toString(); 235 } 236 237 238 243 private void parseUrl( String url ) 244 { 245 246 try 247 { 248 url = URLDecoder.decode( url, "UTF-8" ); 250 String [] protocolAndRest = url.split( "://", 2 ); if ( protocolAndRest.length > 0 ) 253 { 254 if ( "ldap".equals( protocolAndRest[0] ) || "ldaps".equals( protocolAndRest[0] ) ) { this.protocol = protocolAndRest[0]; 256 } 257 } 258 if ( protocolAndRest.length < 2 ) 259 { 260 return; 261 } 262 263 String [] hostportAndRest = protocolAndRest[1].split( "/", 2 ); if ( hostportAndRest.length > 0 ) 266 { 267 String [] hostAndPort = hostportAndRest[0].split( ":", 2 ); 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"; } 278 } 279 if ( hostportAndRest.length < 2 ) 280 { 281 return; 282 } 283 284 String [] dnAndRest = hostportAndRest[1].split( "\\?", 2 ); 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 String [] attributesAndRest = dnAndRest[1].split( "\\?", 2 ); 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 String [] scopeAndRest = attributesAndRest[1].split( "\\?", 2 ); 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 String [] filterAndRest = scopeAndRest[1].split( "\\?", 2 ); 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 e1 ) 335 { 336 } 337 338 } 339 340 341 346 public boolean hasProtocol() 347 { 348 try 349 { 350 getProtocol(); 351 return true; 352 } 353 catch ( NoSuchFieldException e ) 354 { 355 return false; 356 } 357 } 358 359 360 366 public String getProtocol() throws NoSuchFieldException  367 { 368 if ( protocol == null ) 369 { 370 throw new NoSuchFieldException ( BrowserCoreMessages.model__url_no_protocol ); 371 } 372 373 return protocol; 374 } 375 376 377 382 public boolean hasHost() 383 { 384 try 385 { 386 getHost(); 387 return true; 388 } 389 catch ( NoSuchFieldException e ) 390 { 391 return false; 392 } 393 } 394 395 396 402 public String getHost() throws NoSuchFieldException  403 { 404 if ( host == null ) 405 { 406 throw new NoSuchFieldException ( BrowserCoreMessages.model__url_no_host ); 407 } 408 409 return host; 410 } 411 412 413 418 public boolean hasPort() 419 { 420 try 421 { 422 getPort(); 423 return true; 424 } 425 catch ( NoSuchFieldException e ) 426 { 427 return false; 428 } 429 } 430 431 432 438 public String getPort() throws NoSuchFieldException  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 ( BrowserCoreMessages.model__url_no_port ); 450 } 451 } 452 catch ( NumberFormatException e ) 453 { 454 throw new NoSuchFieldException ( BrowserCoreMessages.model__url_no_port ); 455 } 456 } 457 458 459 464 public boolean hasDn() 465 { 466 try 467 { 468 getDn(); 469 return true; 470 } 471 catch ( NoSuchFieldException e ) 472 { 473 return false; 474 } 475 } 476 477 478 484 public DN getDn() throws NoSuchFieldException  485 { 486 if ( dn == null ) 487 { 488 throw new NoSuchFieldException ( 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 ( BrowserCoreMessages.model__url_no_dn ); 498 } 499 } 500 501 502 507 public boolean hasAttributes() 508 { 509 try 510 { 511 getAttributes(); 512 return true; 513 } 514 catch ( NoSuchFieldException e ) 515 { 516 return false; 517 } 518 } 519 520 521 527 public String [] getAttributes() throws NoSuchFieldException  528 { 529 if ( attributes == null ) 530 { 531 throw new NoSuchFieldException ( BrowserCoreMessages.model__url_no_attributes ); 532 } 533 534 return Utils.stringToArray( attributes ); 535 } 537 538 539 544 public boolean hasScope() 545 { 546 try 547 { 548 getScope(); 549 return true; 550 } 551 catch ( NoSuchFieldException e ) 552 { 553 return false; 554 } 555 } 556 557 558 564 public int getScope() throws NoSuchFieldException  565 { 566 if ( scope == null ) 567 { 568 throw new NoSuchFieldException ( BrowserCoreMessages.model__url_no_scope ); 569 } 570 571 if ( "base".equals( scope ) ) { return ISearch.SCOPE_OBJECT; 573 } 574 else if ( "one".equals( scope ) ) { return ISearch.SCOPE_ONELEVEL; 576 } 577 else if ( "sub".equals( scope ) ) { return ISearch.SCOPE_SUBTREE; 579 } 580 else 581 { 582 throw new NoSuchFieldException ( BrowserCoreMessages.model__url_no_scope ); 583 } 584 } 585 586 587 592 public boolean hasFilter() 593 { 594 try 595 { 596 getFilter(); 597 return true; 598 } 599 catch ( NoSuchFieldException e ) 600 { 601 return false; 602 } 603 } 604 605 606 612 public String getFilter() throws NoSuchFieldException  613 { 614 if ( filter == null ) 615 { 616 throw new NoSuchFieldException ( BrowserCoreMessages.model__url_no_filter ); 617 } 618 619 return filter; 620 } 621 622 623 628 public boolean hasExtensions() 629 { 630 try 631 { 632 getExtensions(); 633 return true; 634 } 635 catch ( NoSuchFieldException e ) 636 { 637 return false; 638 } 639 } 640 641 642 648 public String getExtensions() throws NoSuchFieldException  649 { 650 if ( extensions == null ) 651 { 652 throw new NoSuchFieldException ( BrowserCoreMessages.model__url_no_extensions ); 653 } 654 655 return extensions; 656 } 657 658 } 659 | Popular Tags |