1 20 21 package org.apache.directory.ldapstudio.browser.core.internal.model; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Set ; 31 32 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 33 import org.apache.directory.ldapstudio.browser.core.jobs.ExtendedProgressMonitor; 34 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy; 35 import org.apache.directory.ldapstudio.browser.core.model.Control; 36 import org.apache.directory.ldapstudio.browser.core.model.DN; 37 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 38 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 39 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 40 import org.apache.directory.ldapstudio.browser.core.model.IRootDSE; 41 import org.apache.directory.ldapstudio.browser.core.model.ISearch; 42 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult; 43 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 44 import org.apache.directory.ldapstudio.browser.core.model.SearchParameter; 45 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifEnumeration; 46 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer; 47 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord; 48 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine; 49 import org.apache.directory.ldapstudio.browser.core.model.schema.ObjectClassDescription; 50 import org.apache.directory.ldapstudio.browser.core.utils.Utils; 51 52 53 public class ConnectionSearchHandler 54 { 55 56 private Connection connection; 57 58 59 ConnectionSearchHandler( Connection connection ) 60 { 61 this.connection = connection; 62 } 63 64 65 void connectionClosed() 66 { 67 } 68 69 70 void connectionOpened() 71 { 72 } 73 74 75 boolean existsEntry( DN dn, ExtendedProgressMonitor monitor ) 76 { 77 SearchParameter sp = new SearchParameter(); 78 sp.setSearchBase( dn ); 79 sp.setFilter( ISearch.FILTER_TRUE ); 80 sp.setScope( ISearch.SCOPE_OBJECT ); 81 sp.setReturningAttributes( ISearch.NO_ATTRIBUTES ); 82 83 try 84 { 85 LdifEnumeration le = connection.connectionProvider.search( sp, monitor ); 86 return le.hasNext( monitor ); 87 } 88 catch ( ConnectionException e ) 89 { 90 monitor.reportError( e ); 91 return false; 92 } 93 } 94 95 96 IEntry getEntry( DN dn, ExtendedProgressMonitor monitor ) 97 { 98 try 99 { 100 IEntry entry = connection.getEntryFromCache( dn ); 102 if ( entry != null ) 103 { 104 return entry; 105 } 106 107 ISearch search = new Search( null, connection, dn, null, ISearch.NO_ATTRIBUTES, ISearch.SCOPE_OBJECT, 1, 0, 109 IConnection.DEREFERENCE_ALIASES_NEVER, IConnection.HANDLE_REFERRALS_IGNORE, true, true, null ); 110 this.search( search, monitor ); 111 ISearchResult[] srs = search.getSearchResults(); 112 if ( srs.length > 0 ) 113 { 114 return srs[0].getEntry(); 115 } 116 else 117 { 118 monitor.reportError( BrowserCoreMessages.bind( BrowserCoreMessages.model__no_such_entry, dn ) ); 119 return null; 120 } 121 } 122 catch ( Exception e ) 123 { 124 monitor.reportError( e.getMessage(), e ); 125 return null; 126 } 127 } 128 129 130 void search( ISearch search, ExtendedProgressMonitor monitor ) 131 { 132 133 try 134 { 135 136 if ( !monitor.isCanceled() ) 137 { 138 139 SearchParameter searchParameter = getSearchParameter( search ); 141 ArrayList searchResultList = new ArrayList (); 142 143 try 144 { 145 146 LdifEnumeration enumeration = connection.connectionProvider.search( searchParameter, monitor ); 148 149 while ( !monitor.isCanceled() && enumeration.hasNext( monitor ) ) 151 { 152 LdifContainer container = enumeration.next( monitor ); 153 if ( container instanceof LdifContentRecord ) 154 { 155 156 LdifContentRecord record = ( LdifContentRecord ) container; 157 DN dn = new DN( record.getDnLine().getValueAsString() ); 158 159 IEntry entry = connection.getEntryFromCache( dn ); 161 if ( entry == null ) 162 { 163 entry = createAndCacheEntry( dn ); 164 } 165 166 initFlags( entry, record, searchParameter ); 168 169 fillAttributes( entry, record, searchParameter ); 171 172 searchResultList 173 .add( new org.apache.directory.ldapstudio.browser.core.internal.model.SearchResult( 174 entry, search ) ); 175 176 monitor 177 .reportProgress( searchResultList.size() == 1 ? BrowserCoreMessages.model__retrieved_1_entry 178 : BrowserCoreMessages.bind( BrowserCoreMessages.model__retrieved_n_entries, 179 new String [] 180 { Integer.toString( searchResultList.size() ) } ) ); 181 } 182 } 183 } 184 catch ( ConnectionException ce ) 185 { 186 187 if ( ce.getLdapStatusCode() == 3 || ce.getLdapStatusCode() == 4 || ce.getLdapStatusCode() == 11 ) 188 { 189 search.setCountLimitExceeded( true ); 190 } 191 else if ( ce instanceof ReferralException ) 192 { 193 194 if ( search.getReferralsHandlingMethod() == IConnection.HANDLE_REFERRALS_FOLLOW ) 195 { 196 197 ReferralException re = ( ReferralException ) ce; 198 ISearch[] referralSearches = re.getReferralSearches(); 199 for ( int i = 0; i < referralSearches.length; i++ ) 200 { 201 ISearch referralSearch = referralSearches[i]; 202 203 if ( !referralSearch.getConnection().isOpened() ) 205 { 206 referralSearch.getConnection().open( monitor ); 207 } 208 209 referralSearch.getConnection().search( referralSearch, monitor ); 210 211 ISearchResult[] referralSearchResults = referralSearch.getSearchResults(); 212 for ( int j = 0; referralSearchResults != null && j < referralSearchResults.length; j++ ) 213 { 214 ISearchResult referralSearchResult = referralSearchResults[j]; 215 referralSearchResult.setSearch( search ); 216 searchResultList.add( referralSearchResult ); 217 } 218 } 219 } 220 } 221 else 222 { 223 monitor.reportError( ce ); 224 } 225 } 226 227 monitor.reportProgress( searchResultList.size() == 1 ? BrowserCoreMessages.model__retrieved_1_entry 228 : BrowserCoreMessages.bind( BrowserCoreMessages.model__retrieved_n_entries, new String [] 229 { Integer.toString( searchResultList.size() ) } ) ); 230 monitor.worked( 1 ); 231 232 search.setSearchResults( ( ISearchResult[] ) searchResultList 233 .toArray( new ISearchResult[searchResultList.size()] ) ); 234 } 235 } 236 catch ( Exception e ) 237 { 238 if ( search != null ) 239 { 240 search.setSearchResults( new ISearchResult[0] ); 241 } 242 monitor.reportError( e ); 243 } 244 } 245 246 247 private IEntry createAndCacheEntry( DN dn ) throws ModelModificationException 248 { 249 IEntry entry = null; 250 251 LinkedList parentDnList = new LinkedList (); 253 DN parentDN = dn; 254 while ( parentDN != null && connection.getEntryFromCache( parentDN ) == null ) 255 { 256 parentDnList.addFirst( parentDN ); 257 parentDN = parentDN.getParentDn(); 258 } 259 for ( Iterator it = parentDnList.iterator(); it.hasNext(); ) 260 { 261 DN aDN = ( DN ) it.next(); 262 if ( aDN.getParentDn() == null ) 263 { 264 entry = new BaseDNEntry( aDN, connection ); 265 connection.cacheEntry( entry ); 266 } 267 else if ( connection.getEntryFromCache( aDN.getParentDn() ) != null ) 268 { 269 IEntry parentEntry = connection.getEntryFromCache( aDN.getParentDn() ); 270 entry = new Entry( parentEntry, aDN.getRdn() ); 271 entry.setDirectoryEntry( true ); 272 273 parentEntry.addChild( entry ); 274 276 parentEntry.setChildrenInitialized( true ); 277 parentEntry.setHasMoreChildren( true ); 278 parentEntry.setHasChildrenHint( true ); 279 280 connection.cacheEntry( entry ); 281 } 282 else 283 { 284 } 286 } 287 288 return entry; 289 } 290 291 292 private void initFlags( IEntry entry, LdifContentRecord record, SearchParameter search ) 293 { 294 295 LdifAttrValLine[] lines = record.getAttrVals(); 296 for ( int i = 0; i < lines.length; i++ ) 297 { 298 299 String attributeName = lines[i].getUnfoldedAttributeDescription(); 300 301 if ( search.isInitHasChildrenFlag() ) 302 { 303 if ( IAttribute.OPERATIONAL_ATTRIBUTE_HAS_SUBORDINATES.equalsIgnoreCase( attributeName ) ) 305 { 306 if ( "FALSE".equalsIgnoreCase( lines[i].getValueAsString() ) ) { entry.setHasChildrenHint( false ); 308 } 309 } 310 if ( IAttribute.OPERATIONAL_ATTRIBUTE_NUM_SUBORDINATES.equalsIgnoreCase( attributeName ) ) 311 { 312 if ( "0".equalsIgnoreCase( lines[i].getValueAsString() ) ) { entry.setHasChildrenHint( false ); 314 } 315 } 316 if ( IAttribute.OPERATIONAL_ATTRIBUTE_SUBORDINATE_COUNT.equalsIgnoreCase( attributeName ) ) 317 { 318 if ( "0".equalsIgnoreCase( lines[i].getValueAsString() ) ) { entry.setHasChildrenHint( false ); 320 } 321 } 322 } 323 324 if ( search.isInitAliasAndReferralFlag() ) 325 { 326 if ( IAttribute.OBJECTCLASS_ATTRIBUTE.equalsIgnoreCase( attributeName ) ) 327 { 328 if ( ObjectClassDescription.OC_ALIAS.equalsIgnoreCase( lines[i].getValueAsString() ) ) 329 { 330 entry.setAlias( true ); 331 } 332 if ( ObjectClassDescription.OC_REFERRAL.equalsIgnoreCase( lines[i].getValueAsString() ) ) 333 { 334 entry.setReferral( true ); 335 } 336 } 337 } 338 339 } 340 341 if ( search.getControls() != null 342 && Arrays.asList( search.getControls() ).contains( Control.SUBENTRIES_CONTROL ) ) 343 { 344 entry.setSubentry( true ); 345 entry.setHasChildrenHint( false ); 346 } 347 348 } 349 350 351 private SearchParameter getSearchParameter( ISearch search ) 352 { 353 SearchParameter searchParameter = ( SearchParameter ) search.getSearchParameter().clone(); 354 355 if ( search.isInitHasChildrenFlag() ) 357 { 358 if ( search.getConnection().getSchema().hasAttributeTypeDescription( 359 IAttribute.OPERATIONAL_ATTRIBUTE_HAS_SUBORDINATES ) 360 && !Utils.containsIgnoreCase( Arrays.asList( searchParameter.getReturningAttributes() ), 361 IAttribute.OPERATIONAL_ATTRIBUTE_HAS_SUBORDINATES ) ) 362 { 363 String [] returningAttributes = new String [searchParameter.getReturningAttributes().length + 1]; 364 System.arraycopy( searchParameter.getReturningAttributes(), 0, returningAttributes, 0, searchParameter 365 .getReturningAttributes().length ); 366 returningAttributes[returningAttributes.length - 1] = IAttribute.OPERATIONAL_ATTRIBUTE_HAS_SUBORDINATES; 367 searchParameter.setReturningAttributes( returningAttributes ); 368 } 369 else if ( search.getConnection().getSchema().hasAttributeTypeDescription( 370 IAttribute.OPERATIONAL_ATTRIBUTE_NUM_SUBORDINATES ) 371 && !Utils.containsIgnoreCase( Arrays.asList( searchParameter.getReturningAttributes() ), 372 IAttribute.OPERATIONAL_ATTRIBUTE_NUM_SUBORDINATES ) ) 373 { 374 String [] returningAttributes = new String [searchParameter.getReturningAttributes().length + 1]; 375 System.arraycopy( searchParameter.getReturningAttributes(), 0, returningAttributes, 0, searchParameter 376 .getReturningAttributes().length ); 377 returningAttributes[returningAttributes.length - 1] = IAttribute.OPERATIONAL_ATTRIBUTE_NUM_SUBORDINATES; 378 searchParameter.setReturningAttributes( returningAttributes ); 379 } 380 else if ( search.getConnection().getSchema().hasAttributeTypeDescription( 381 IAttribute.OPERATIONAL_ATTRIBUTE_SUBORDINATE_COUNT ) 382 && !Utils.containsIgnoreCase( Arrays.asList( searchParameter.getReturningAttributes() ), 383 IAttribute.OPERATIONAL_ATTRIBUTE_SUBORDINATE_COUNT ) ) 384 { 385 String [] returningAttributes = new String [searchParameter.getReturningAttributes().length + 1]; 386 System.arraycopy( searchParameter.getReturningAttributes(), 0, returningAttributes, 0, searchParameter 387 .getReturningAttributes().length ); 388 returningAttributes[returningAttributes.length - 1] = IAttribute.OPERATIONAL_ATTRIBUTE_SUBORDINATE_COUNT; 389 searchParameter.setReturningAttributes( returningAttributes ); 390 } 391 } 392 393 if ( search.isInitAliasAndReferralFlag() ) 395 { 396 if ( !Utils.containsIgnoreCase( Arrays.asList( searchParameter.getReturningAttributes() ), 397 IAttribute.OBJECTCLASS_ATTRIBUTE ) ) 398 { 399 String [] returningAttributes = new String [searchParameter.getReturningAttributes().length + 1]; 400 System.arraycopy( searchParameter.getReturningAttributes(), 0, returningAttributes, 0, searchParameter 401 .getReturningAttributes().length ); 402 returningAttributes[returningAttributes.length - 1] = IAttribute.OBJECTCLASS_ATTRIBUTE; 403 searchParameter.setReturningAttributes( returningAttributes ); 404 } 405 } 406 407 if ( search.getReturningAttributes() == null || search.getReturningAttributes().length > 0 ) 410 { 411 if ( !Utils.containsIgnoreCase( Arrays.asList( searchParameter.getReturningAttributes() ), 412 IAttribute.OBJECTCLASS_ATTRIBUTE ) ) 413 { 414 String [] returningAttributes = new String [searchParameter.getReturningAttributes().length + 1]; 415 System.arraycopy( searchParameter.getReturningAttributes(), 0, returningAttributes, 0, searchParameter 416 .getReturningAttributes().length ); 417 returningAttributes[returningAttributes.length - 1] = IAttribute.OBJECTCLASS_ATTRIBUTE; 418 searchParameter.setReturningAttributes( returningAttributes ); 419 } 420 } 421 422 if ( searchParameter.getControls() != null ) 424 { 425 426 Set suppportedConrolSet = new HashSet (); 427 if ( connection.getRootDSE() != null 428 && connection.getRootDSE().getAttribute( IRootDSE.ROOTDSE_ATTRIBUTE_SUPPORTEDCONTROL ) != null ) 429 { 430 IAttribute scAttribute = connection.getRootDSE().getAttribute( 431 IRootDSE.ROOTDSE_ATTRIBUTE_SUPPORTEDCONTROL ); 432 String [] supportedControls = scAttribute.getStringValues(); 433 for ( int i = 0; i < supportedControls.length; i++ ) 434 { 435 suppportedConrolSet.add( supportedControls[i].toLowerCase() ); 436 } 437 } 438 439 Control[] controls = searchParameter.getControls(); 440 List controlList = new ArrayList (); 441 for ( int i = 0; i < controls.length; i++ ) 442 { 443 if ( suppportedConrolSet.contains( controls[i].getOid().toLowerCase() ) ) 444 { 445 controlList.add( controls[i] ); 446 } 447 } 448 searchParameter.setControls( ( Control[] ) controlList.toArray( new Control[controlList.size()] ) ); 449 } 450 451 return searchParameter; 452 } 453 454 455 private void fillAttributes( IEntry entry, LdifContentRecord record, SearchParameter search ) 456 throws ModelModificationException 457 { 458 459 if ( search.getReturningAttributes() == null || search.getReturningAttributes().length > 0 ) 460 { 461 LdifAttrValLine[] lines = record.getAttrVals(); 462 463 if ( search.getReturningAttributes() != null ) 465 { 466 String [] ras = search.getReturningAttributes(); 467 468 if( Arrays.asList( ras ).contains( ISearch.ALL_USER_ATTRIBUTES ) ) 470 { 471 IAttribute[] oldAttributes = entry.getAttributes(); 473 for ( int i = 0; oldAttributes != null && i < oldAttributes.length; i++ ) 474 { 475 if( !oldAttributes[i].isOperationalAttribute() ) 476 { 477 try 478 { 479 entry.deleteAttribute( oldAttributes[i] ); 480 } 481 catch ( ModelModificationException e ) 482 { 483 } 484 } 485 } 486 } 487 488 if( Arrays.asList( ras ).contains( ISearch.ALL_OPERATIONAL_ATTRIBUTES ) ) 490 { 491 IAttribute[] oldAttributes = entry.getAttributes(); 493 for ( int i = 0; oldAttributes != null && i < oldAttributes.length; i++ ) 494 { 495 if( oldAttributes[i].isOperationalAttribute() ) 496 { 497 try 498 { 499 entry.deleteAttribute( oldAttributes[i] ); 500 } 501 catch ( ModelModificationException e ) 502 { 503 } 504 } 505 } 506 } 507 508 509 for ( int r = 0; r < ras.length; r++ ) 510 { 511 AttributeHierarchy ah = entry.getAttributeWithSubtypes( ras[r] ); 513 if ( ah != null ) 514 { 515 for ( Iterator it = ah.iterator(); it.hasNext(); ) 516 { 517 IAttribute attribute = ( IAttribute ) it.next(); 518 try 519 { 520 entry.deleteAttribute( attribute ); 521 } 522 catch ( ModelModificationException e ) 523 { 524 } 525 } 526 } 527 } 528 } 529 else 530 { 531 IAttribute[] oldAttributes = entry.getAttributes(); 533 for ( int i = 0; oldAttributes != null && i < oldAttributes.length; i++ ) 534 { 535 try 536 { 537 entry.deleteAttribute( oldAttributes[i] ); 538 } 539 catch ( ModelModificationException e ) 540 { 541 } 542 } 543 } 544 545 546 for ( int i = 0; i < lines.length; i++ ) 549 { 550 String attributeDesc = lines[i].getUnfoldedAttributeDescription(); 551 IAttribute oldAttribute = entry.getAttribute( attributeDesc ); 552 if ( oldAttribute != null ) 553 { 554 try 555 { 556 entry.deleteAttribute( oldAttribute ); 557 } 558 catch ( ModelModificationException mme ) 559 { 560 } 561 } 562 } 563 564 for ( int i = 0; i < lines.length; i++ ) 566 { 567 568 IAttribute attribute = null; 569 String attributeName = lines[i].getUnfoldedAttributeDescription(); 570 if ( entry.getAttribute( attributeName ) == null ) 572 { 573 attribute = new Attribute( entry, attributeName ); 574 entry.addAttribute( attribute ); 575 } 576 else 577 { 578 attribute = entry.getAttribute( attributeName ); 579 } 580 581 attribute.addValue( new Value( attribute, lines[i].getValueAsObject() ) ); 582 } 583 } 584 } 585 586 } 587 | Popular Tags |