1 20 21 package org.apache.directory.ldapstudio.browser.core.jobs; 22 23 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.PrintStream ; 27 import java.text.ParseException ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 31 import org.apache.commons.codec.digest.DigestUtils; 32 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 33 import org.apache.directory.ldapstudio.browser.core.model.Control; 34 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 35 import org.apache.directory.ldapstudio.browser.core.model.ISearch; 36 import org.apache.directory.ldapstudio.browser.core.model.SearchParameter; 37 import org.apache.directory.ldapstudio.dsmlv2.engine.Dsmlv2Engine; 38 import org.apache.directory.ldapstudio.dsmlv2.request.SearchRequestDsml; 39 import org.apache.directory.shared.asn1.codec.DecoderException; 40 import org.apache.directory.shared.ldap.codec.AttributeValueAssertion; 41 import org.apache.directory.shared.ldap.codec.LdapConstants; 42 import org.apache.directory.shared.ldap.codec.search.AndFilter; 43 import org.apache.directory.shared.ldap.codec.search.AttributeValueAssertionFilter; 44 import org.apache.directory.shared.ldap.codec.search.ExtensibleMatchFilter; 45 import org.apache.directory.shared.ldap.codec.search.Filter; 46 import org.apache.directory.shared.ldap.codec.search.NotFilter; 47 import org.apache.directory.shared.ldap.codec.search.OrFilter; 48 import org.apache.directory.shared.ldap.codec.search.PresentFilter; 49 import org.apache.directory.shared.ldap.codec.search.SearchRequest; 50 import org.apache.directory.shared.ldap.codec.search.SubstringFilter; 51 import org.apache.directory.shared.ldap.filter.BranchNode; 52 import org.apache.directory.shared.ldap.filter.ExprNode; 53 import org.apache.directory.shared.ldap.filter.ExtensibleNode; 54 import org.apache.directory.shared.ldap.filter.FilterParser; 55 import org.apache.directory.shared.ldap.filter.FilterParserImpl; 56 import org.apache.directory.shared.ldap.filter.PresenceNode; 57 import org.apache.directory.shared.ldap.filter.SimpleNode; 58 import org.apache.directory.shared.ldap.filter.SubstringNode; 59 import org.apache.directory.shared.ldap.message.ScopeEnum; 60 import org.apache.directory.shared.ldap.name.LdapDN; 61 import org.dom4j.Document; 62 import org.dom4j.DocumentHelper; 63 import org.dom4j.Element; 64 65 66 72 public class ExportDsmlJob extends AbstractEclipseJob 73 { 74 75 private String exportDsmlFilename; 76 77 78 private IConnection connection; 79 80 81 private SearchParameter searchParameter; 82 83 84 94 public ExportDsmlJob( String exportDsmlFilename, IConnection connection, SearchParameter searchParameter ) 95 { 96 this.exportDsmlFilename = exportDsmlFilename; 97 this.connection = connection; 98 this.searchParameter = searchParameter; 99 100 setName( BrowserCoreMessages.jobs__export_dsml_name ); 101 } 102 103 104 107 protected IConnection[] getConnections() 108 { 109 return new IConnection[] 110 { connection }; 111 } 112 113 114 117 protected Object [] getLockedObjects() 118 { 119 List <String > l = new ArrayList <String >(); 120 l.add( connection.getUrl() + "_" + DigestUtils.shaHex( exportDsmlFilename ) ); 121 return l.toArray(); 122 } 123 124 125 128 protected void executeAsyncJob( ExtendedProgressMonitor monitor ) 129 { 130 monitor.beginTask( BrowserCoreMessages.jobs__export_dsml_task, 2 ); 131 monitor.reportProgress( " " ); monitor.worked( 1 ); 133 134 try 135 { 136 SearchRequest searchRequest = new SearchRequest(); 137 searchRequest.setProtocolOP( searchRequest ); 138 139 searchRequest.setBaseObject( new LdapDN( searchParameter.getSearchBase().toString() ) ); 141 142 int scope = searchParameter.getScope(); 144 if ( scope == ISearch.SCOPE_OBJECT ) 145 { 146 searchRequest.setScope( ScopeEnum.BASE_OBJECT ); 147 } 148 else if ( scope == ISearch.SCOPE_ONELEVEL ) 149 { 150 searchRequest.setScope( ScopeEnum.SINGLE_LEVEL ); 151 } 152 else if ( scope == ISearch.SCOPE_SUBTREE ) 153 { 154 searchRequest.setScope( ScopeEnum.WHOLE_SUBTREE ); 155 } 156 157 int derefAliases = searchParameter.getAliasesDereferencingMethod(); 159 if ( derefAliases == IConnection.DEREFERENCE_ALIASES_ALWAYS ) 160 { 161 searchRequest.setDerefAliases( LdapConstants.DEREF_ALWAYS ); 162 } 163 else if ( derefAliases == IConnection.DEREFERENCE_ALIASES_FINDING ) 164 { 165 searchRequest.setDerefAliases( LdapConstants.DEREF_FINDING_BASE_OBJ ); 166 } 167 else if ( derefAliases == IConnection.DEREFERENCE_ALIASES_NEVER ) 168 { 169 searchRequest.setDerefAliases( LdapConstants.NEVER_DEREF_ALIASES ); 170 } 171 else if ( derefAliases == IConnection.DEREFERENCE_ALIASES_SEARCH ) 172 { 173 searchRequest.setDerefAliases( LdapConstants.DEREF_IN_SEARCHING ); 174 } 175 176 int timeLimit = searchParameter.getTimeLimit(); 178 if ( timeLimit != 0 ) 179 { 180 searchRequest.setTimeLimit( timeLimit ); 181 } 182 183 int countLimit = searchParameter.getCountLimit(); 185 if ( countLimit != 0 ) 186 { 187 searchRequest.setSizeLimit( countLimit ); 188 } 189 190 searchRequest.setFilter( convertToSharedLdapFilter( searchParameter.getFilter() ) ); 192 193 String [] returningAttributes = searchParameter.getReturningAttributes(); 195 for ( int i = 0; i < returningAttributes.length; i++ ) 196 { 197 searchRequest.addAttribute( returningAttributes[i] ); 198 } 199 200 List <org.apache.directory.shared.ldap.codec.Control> sharedLdapControls = convertToSharedLdapControls( searchParameter 202 .getControls() ); 203 for ( int i = 0; i < sharedLdapControls.size(); i++ ) 204 { 205 searchRequest.addControl( sharedLdapControls.get( i ) ); 206 } 207 208 Document xmlRequest = DocumentHelper.createDocument(); 210 Element rootElement = xmlRequest.addElement( "batchRequest" ); 211 SearchRequestDsml searchRequestDsml = new SearchRequestDsml( searchRequest ); 212 searchRequestDsml.toDsml( rootElement ); 213 Dsmlv2Engine engine = new Dsmlv2Engine( connection.getHost(), connection.getPort(), connection 214 .getBindPrincipal(), connection.getBindPassword() ); 215 String response = engine.processDSML( xmlRequest.asXML() ); 216 217 FileOutputStream fout = new FileOutputStream ( exportDsmlFilename ); 219 new PrintStream ( fout ).println( response ); 220 fout.close(); 221 } 222 catch ( Exception e ) 223 { 224 monitor.reportError( e ); 225 } 226 } 227 228 229 232 protected String getErrorMessage() 233 { 234 return BrowserCoreMessages.jobs__export_dsml_error; 235 } 236 237 238 249 public static Filter convertToSharedLdapFilter( String filter ) throws IOException , ParseException , 250 DecoderException 251 { 252 FilterParser filterParser = new FilterParserImpl(); 253 254 ExprNode exprNode = filterParser.parse( filter ); 255 256 return convertToSharedLdapFilter( exprNode ); 257 } 258 259 260 269 public static Filter convertToSharedLdapFilter( ExprNode exprNode ) throws DecoderException 270 { 271 Filter sharedLdapFilter = null; 272 273 if ( exprNode instanceof BranchNode ) 274 { 275 BranchNode branchNode = ( BranchNode ) exprNode; 276 277 switch ( branchNode.getOperator() ) 278 { 279 case AND: 280 AndFilter andFilter = new AndFilter(); 281 sharedLdapFilter = andFilter; 282 283 List <Filter> andFilters = iterateOnFilters( branchNode.getChildren() ); 284 for ( int i = 0; i < andFilters.size(); i++ ) 285 { 286 andFilter.addFilter( andFilters.get( i ) ); 287 } 288 break; 289 290 case OR: 291 OrFilter orFilter = new OrFilter(); 292 sharedLdapFilter = orFilter; 293 294 List <Filter> orFilters = iterateOnFilters( branchNode.getChildren() ); 295 for ( int i = 0; i < orFilters.size(); i++ ) 296 { 297 orFilter.addFilter( orFilters.get( i ) ); 298 } 299 break; 300 case NOT: 301 NotFilter notFilter = new NotFilter(); 302 sharedLdapFilter = notFilter; 303 304 List <Filter> notFilters = iterateOnFilters( branchNode.getChildren() ); 305 notFilter.setNotFilter( notFilters.get( 0 ) ); 306 break; 307 } 308 } 309 else if ( exprNode instanceof PresenceNode ) 310 { 311 PresenceNode presenceNode = ( PresenceNode ) exprNode; 312 313 PresentFilter presentFilter = new PresentFilter(); 314 sharedLdapFilter = presentFilter; 315 316 presentFilter.setAttributeDescription( presenceNode.getAttribute() ); 317 } 318 else if ( exprNode instanceof SimpleNode ) 319 { 320 SimpleNode simpleNode = ( SimpleNode ) exprNode; 321 322 switch ( simpleNode.getAssertionType() ) 323 { 324 case APPROXIMATE: 325 AttributeValueAssertionFilter approxMatchFilter = createAttributeValueAssertionFilter( simpleNode, 326 LdapConstants.APPROX_MATCH_FILTER ); 327 sharedLdapFilter = approxMatchFilter; 328 break; 329 330 case EQUALITY: 331 AttributeValueAssertionFilter equalityMatchFilter = createAttributeValueAssertionFilter( 332 simpleNode, LdapConstants.EQUALITY_MATCH_FILTER ); 333 sharedLdapFilter = equalityMatchFilter; 334 break; 335 336 case GREATEREQ: 337 AttributeValueAssertionFilter greaterOrEqualFilter = createAttributeValueAssertionFilter( 338 simpleNode, LdapConstants.GREATER_OR_EQUAL_FILTER ); 339 sharedLdapFilter = greaterOrEqualFilter; 340 break; 341 342 case LESSEQ: 343 AttributeValueAssertionFilter lessOrEqualFilter = createAttributeValueAssertionFilter( simpleNode, 344 LdapConstants.LESS_OR_EQUAL_FILTER ); 345 sharedLdapFilter = lessOrEqualFilter; 346 break; 347 } 348 } 349 else if ( exprNode instanceof ExtensibleNode ) 350 { 351 ExtensibleNode extensibleNode = ( ExtensibleNode ) exprNode; 352 353 ExtensibleMatchFilter extensibleMatchFilter = new ExtensibleMatchFilter(); 354 sharedLdapFilter = extensibleMatchFilter; 355 356 extensibleMatchFilter.setDnAttributes( extensibleNode.dnAttributes() ); 357 extensibleMatchFilter.setMatchingRule( extensibleNode.getMatchingRuleId() ); 358 extensibleMatchFilter.setMatchValue( extensibleNode.getValue() ); 359 extensibleMatchFilter.setType( extensibleNode.getAttribute() ); 360 } 361 else if ( exprNode instanceof SubstringNode ) 362 { 363 SubstringNode substringNode = ( SubstringNode ) exprNode; 364 365 SubstringFilter substringFilter = new SubstringFilter(); 366 sharedLdapFilter = substringFilter; 367 368 substringFilter.setType( substringNode.getAttribute() ); 369 substringFilter.setInitialSubstrings( substringNode.getInitial() ); 370 substringFilter.setFinalSubstrings( substringNode.getFinal() ); 371 List anys = substringNode.getAny(); 372 for ( int i = 0; i < anys.size(); i++ ) 373 { 374 substringFilter.addAnySubstrings( ( String ) anys.get( i ) ); 375 } 376 } 377 378 return sharedLdapFilter; 379 } 380 381 382 391 private static List <Filter> iterateOnFilters( List <ExprNode> filters ) throws DecoderException 392 { 393 List <Filter> filtersList = new ArrayList <Filter>(); 394 395 for ( int c = 0; c < filters.size(); c++ ) 396 { 397 filtersList.add( convertToSharedLdapFilter( filters.get( c ) ) ); 398 } 399 400 return filtersList; 401 } 402 403 404 414 private static AttributeValueAssertionFilter createAttributeValueAssertionFilter( SimpleNode node, int type ) 415 { 416 AttributeValueAssertionFilter avaFilter = new AttributeValueAssertionFilter( type ); 417 418 AttributeValueAssertion assertion = new AttributeValueAssertion(); 419 avaFilter.setAssertion( assertion ); 420 assertion.setAttributeDesc( node.getAttribute() ); 421 assertion.setAssertionValue( node.getValue() ); 422 423 return avaFilter; 424 } 425 426 427 435 private List <org.apache.directory.shared.ldap.codec.Control> convertToSharedLdapControls( Control[] controls ) 436 { 437 List <org.apache.directory.shared.ldap.codec.Control> returnList = new ArrayList <org.apache.directory.shared.ldap.codec.Control>(); 438 439 if ( controls != null) 440 { 441 for ( int i = 0; i < controls.length; i++ ) 442 { 443 returnList.add( convertToSharedLDAP( controls[i] ) ); 444 } 445 } 446 447 return returnList; 448 } 449 450 451 459 private static org.apache.directory.shared.ldap.codec.Control convertToSharedLDAP( Control control ) 460 { 461 org.apache.directory.shared.ldap.codec.Control sharedLdapControl = new org.apache.directory.shared.ldap.codec.Control(); 462 463 sharedLdapControl.setControlType( control.getOid() ); 464 sharedLdapControl.setControlValue( control.getControlValue() ); 465 466 return sharedLdapControl; 467 } 468 } 469 | Popular Tags |