KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal.model;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
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             // first check cache
101
IEntry entry = connection.getEntryFromCache( dn );
102             if ( entry != null )
103             {
104                 return entry;
105             }
106
107             // search in directory
108
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 JavaDoc 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                 // add returning attributes for children and alias detection
140
SearchParameter searchParameter = getSearchParameter( search );
141                 ArrayList JavaDoc searchResultList = new ArrayList JavaDoc();
142
143                 try
144                 {
145
146                     // search
147
LdifEnumeration enumeration = connection.connectionProvider.search( searchParameter, monitor );
148
149                     // iterate through the search result
150
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                             // get entry from cache or create it
160
IEntry entry = connection.getEntryFromCache( dn );
161                             if ( entry == null )
162                             {
163                                 entry = createAndCacheEntry( dn );
164                             }
165
166                             // initialize special flags
167
initFlags( entry, record, searchParameter );
168
169                             // fill the attributes
170
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 JavaDoc[]
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                                 // open connection
204
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 JavaDoc[]
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 JavaDoc 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         // build tree to parent
252
LinkedList JavaDoc parentDnList = new LinkedList JavaDoc();
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 JavaDoc 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                 // parentEntry.setAttributesInitialized(false, this);
275

276                 parentEntry.setChildrenInitialized( true );
277                 parentEntry.setHasMoreChildren( true );
278                 parentEntry.setHasChildrenHint( true );
279
280                 connection.cacheEntry( entry );
281             }
282             else
283             {
284                 // ??
285
}
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 JavaDoc attributeName = lines[i].getUnfoldedAttributeDescription();
300
301             if ( search.isInitHasChildrenFlag() )
302             {
303                 // hasChildren flag
304
if ( IAttribute.OPERATIONAL_ATTRIBUTE_HAS_SUBORDINATES.equalsIgnoreCase( attributeName ) )
305                 {
306                     if ( "FALSE".equalsIgnoreCase( lines[i].getValueAsString() ) ) { //$NON-NLS-1$
307
entry.setHasChildrenHint( false );
308                     }
309                 }
310                 if ( IAttribute.OPERATIONAL_ATTRIBUTE_NUM_SUBORDINATES.equalsIgnoreCase( attributeName ) )
311                 {
312                     if ( "0".equalsIgnoreCase( lines[i].getValueAsString() ) ) { //$NON-NLS-1$
313
entry.setHasChildrenHint( false );
314                     }
315                 }
316                 if ( IAttribute.OPERATIONAL_ATTRIBUTE_SUBORDINATE_COUNT.equalsIgnoreCase( attributeName ) )
317                 {
318                     if ( "0".equalsIgnoreCase( lines[i].getValueAsString() ) ) { //$NON-NLS-1$
319
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         // add children detetion attributes
356
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 JavaDoc[] returningAttributes = new String JavaDoc[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 JavaDoc[] returningAttributes = new String JavaDoc[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 JavaDoc[] returningAttributes = new String JavaDoc[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         // to init the alias/referral flag we need the objectClass
394
if ( search.isInitAliasAndReferralFlag() )
395         {
396             if ( !Utils.containsIgnoreCase( Arrays.asList( searchParameter.getReturningAttributes() ),
397                 IAttribute.OBJECTCLASS_ATTRIBUTE ) )
398             {
399                 String JavaDoc[] returningAttributes = new String JavaDoc[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 returning attributes are requested but objectClass isn't included
408
// then add it
409
if ( search.getReturningAttributes() == null || search.getReturningAttributes().length > 0 )
410         {
411             if ( !Utils.containsIgnoreCase( Arrays.asList( searchParameter.getReturningAttributes() ),
412                 IAttribute.OBJECTCLASS_ATTRIBUTE ) )
413             {
414                 String JavaDoc[] returningAttributes = new String JavaDoc[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         // filter controls if not supported by server
423
if ( searchParameter.getControls() != null )
424         {
425
426             Set JavaDoc suppportedConrolSet = new HashSet JavaDoc();
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 JavaDoc[] 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 JavaDoc controlList = new ArrayList JavaDoc();
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             // clear old attributes defined as returing attributes or all
464
if ( search.getReturningAttributes() != null )
465             {
466                 String JavaDoc[] ras = search.getReturningAttributes();
467                 
468                 // special case *
469
if( Arrays.asList( ras ).contains( ISearch.ALL_USER_ATTRIBUTES ) )
470                 {
471                     // clear all user attributes
472
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                 // special case +
489
if( Arrays.asList( ras ).contains( ISearch.ALL_OPERATIONAL_ATTRIBUTES ) )
490                 {
491                     // clear all operational attributes
492
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                     // clear attributes requested from server, also include subtypes
512
AttributeHierarchy ah = entry.getAttributeWithSubtypes( ras[r] );
513                     if ( ah != null )
514                     {
515                         for ( Iterator JavaDoc 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                 // clear all
532
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             // additional clear old attributes if the record contains the
547
// attribute
548
for ( int i = 0; i < lines.length; i++ )
549             {
550                 String JavaDoc 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             // set new attributes and values
565
for ( int i = 0; i < lines.length; i++ )
566             {
567
568                 IAttribute attribute = null;
569                 String JavaDoc attributeName = lines[i].getUnfoldedAttributeDescription();
570                 // attributeName = attributeName.replaceAll(";binary", "");
571
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