KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > jobs > ExportDsmlJob


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.jobs;
22
23
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.PrintStream JavaDoc;
27 import java.text.ParseException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
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 /**
67  * This class implements a Job for Exporting a part of a LDAP Server into a DSML File.
68  *
69  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
70  * @version $Rev$, $Date$
71  */

72 public class ExportDsmlJob extends AbstractEclipseJob
73 {
74     /** The name of the DSML file to export to */
75     private String JavaDoc exportDsmlFilename;
76
77     /** The connection to use */
78     private IConnection connection;
79
80     /** The Search Parameter of the export*/
81     private SearchParameter searchParameter;
82
83
84     /**
85      * Creates a new instance of ExportDsmlJob.
86      *
87      * @param exportDsmlFilename
88      * the name of the DSML file to export to
89      * @param connection
90      * the connection to use
91      * @param searchParameter
92      * the Search Parameter of the export
93      */

94     public ExportDsmlJob( String JavaDoc 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     /* (non-Javadoc)
105      * @see org.apache.directory.ldapstudio.browser.core.jobs.AbstractEclipseJob#getConnections()
106      */

107     protected IConnection[] getConnections()
108     {
109         return new IConnection[]
110             { connection };
111     }
112
113
114     /* (non-Javadoc)
115      * @see org.apache.directory.ldapstudio.browser.core.jobs.AbstractEclipseJob#getLockedObjects()
116      */

117     protected Object JavaDoc[] getLockedObjects()
118     {
119         List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>();
120         l.add( connection.getUrl() + "_" + DigestUtils.shaHex( exportDsmlFilename ) );
121         return l.toArray();
122     }
123
124
125     /* (non-Javadoc)
126      * @see org.apache.directory.ldapstudio.browser.core.jobs.AbstractEclipseJob#executeAsyncJob(org.apache.directory.ldapstudio.browser.core.jobs.ExtendedProgressMonitor)
127      */

128     protected void executeAsyncJob( ExtendedProgressMonitor monitor )
129     {
130         monitor.beginTask( BrowserCoreMessages.jobs__export_dsml_task, 2 );
131         monitor.reportProgress( " " ); //$NON-NLS-1$
132
monitor.worked( 1 );
133
134         try
135         {
136             SearchRequest searchRequest = new SearchRequest();
137             searchRequest.setProtocolOP( searchRequest );
138
139             // DN
140
searchRequest.setBaseObject( new LdapDN( searchParameter.getSearchBase().toString() ) );
141
142             // Scope
143
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             // DerefAliases
158
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             // Time Limit
177
int timeLimit = searchParameter.getTimeLimit();
178             if ( timeLimit != 0 )
179             {
180                 searchRequest.setTimeLimit( timeLimit );
181             }
182
183             // Size Limit
184
int countLimit = searchParameter.getCountLimit();
185             if ( countLimit != 0 )
186             {
187                 searchRequest.setSizeLimit( countLimit );
188             }
189
190             // Filter
191
searchRequest.setFilter( convertToSharedLdapFilter( searchParameter.getFilter() ) );
192
193             // Attributes
194
String JavaDoc[] returningAttributes = searchParameter.getReturningAttributes();
195             for ( int i = 0; i < returningAttributes.length; i++ )
196             {
197                 searchRequest.addAttribute( returningAttributes[i] );
198             }
199
200             // Controls
201
List JavaDoc<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             // Executing the request
209
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 JavaDoc response = engine.processDSML( xmlRequest.asXML() );
216
217             // Saving the response
218
FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc( exportDsmlFilename );
219             new PrintStream JavaDoc( fout ).println( response );
220             fout.close();
221         }
222         catch ( Exception JavaDoc e )
223         {
224             monitor.reportError( e );
225         }
226     }
227
228
229     /* (non-Javadoc)
230      * @see org.apache.directory.ldapstudio.browser.core.jobs.AbstractEclipseJob#getErrorMessage()
231      */

232     protected String JavaDoc getErrorMessage()
233     {
234         return BrowserCoreMessages.jobs__export_dsml_error;
235     }
236
237
238     /**
239      * Converts a String filter into a Shared LDAP Filter.
240      *
241      * @param filter
242      * the filter String to convert
243      * @return
244      * the corresponding Shared LDAP Filter
245      * @throws ParseException
246      * @throws IOException
247      * @throws DecoderException
248      */

249     public static Filter convertToSharedLdapFilter( String JavaDoc filter ) throws IOException JavaDoc, ParseException JavaDoc,
250         DecoderException
251     {
252         FilterParser filterParser = new FilterParserImpl();
253
254         ExprNode exprNode = filterParser.parse( filter );
255
256         return convertToSharedLdapFilter( exprNode );
257     }
258
259
260     /**
261      * Converts a ExprNode Filter Model into a Shared LDAP Model.
262      *
263      * @param exprNode
264      * the filter
265      * @return
266      * the corresponding filter in the Shared LDAP Model
267      * @throws DecoderException
268      */

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 JavaDoc<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 JavaDoc<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 JavaDoc<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 JavaDoc anys = substringNode.getAny();
372             for ( int i = 0; i < anys.size(); i++ )
373             {
374                 substringFilter.addAnySubstrings( ( String JavaDoc ) anys.get( i ) );
375             }
376         }
377
378         return sharedLdapFilter;
379     }
380
381
382     /**
383      * Iterates the conversion on the given List of notdes.
384      *
385      * @param filters
386      * the List of nodes to convert
387      * @return
388      * an array containing the conversion for each Ldap Filter into its Shared LDAP Model
389      * @throws DecoderException
390      */

391     private static List JavaDoc<Filter> iterateOnFilters( List JavaDoc<ExprNode> filters ) throws DecoderException
392     {
393         List JavaDoc<Filter> filtersList = new ArrayList JavaDoc<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     /**
405      * Create and returns an Attribute Value Assertion Filter from the given SimpleNode ant the given type.
406      *
407      * @param node
408      * the filter to convert
409      * @param type
410      * the type of the Attribute Value Assertion Filter
411      * @return
412      * the corresponding Attribute Value Assertion Filter
413      */

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     /**
428      * Converts the given array of Controls into their corresponding representation in the Shared LDAP Model.
429      *
430      * @param controls
431      * the array of Controls to convert
432      * @return
433      * a List of Shared LDAP Control Objects corresponding to the given Controls
434      */

435     private List JavaDoc<org.apache.directory.shared.ldap.codec.Control> convertToSharedLdapControls( Control[] controls )
436     {
437         List JavaDoc<org.apache.directory.shared.ldap.codec.Control> returnList = new ArrayList JavaDoc<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     /**
452      * Converts the given Control into its corresponding representation in the Shared LDAP Model.
453      *
454      * @param control
455      * the Control to convert
456      * @return
457      * the corresponding Control in the Shared LDAP Model
458      */

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