PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/ldapbrowser.core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ExportLdifRunnable.java

https://bitbucket.org/mirror/apache-directory-studio
Java | 345 lines | 235 code | 52 blank | 58 comment | 29 complexity | a986e2dbf0e3a8164529dea0f4ae75a6 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, EPL-1.0
  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. package org.apache.directory.studio.ldapbrowser.core.jobs;
  21. import java.io.BufferedWriter;
  22. import java.io.FileWriter;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.List;
  27. import javax.naming.NamingEnumeration;
  28. import javax.naming.NamingException;
  29. import javax.naming.directory.Attribute;
  30. import javax.naming.directory.SearchResult;
  31. import javax.naming.ldap.Control;
  32. import javax.naming.ldap.PagedResultsResponseControl;
  33. import org.apache.commons.codec.digest.DigestUtils;
  34. import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
  35. import org.apache.directory.api.ldap.model.name.Dn;
  36. import org.apache.directory.studio.common.core.jobs.StudioProgressMonitor;
  37. import org.apache.directory.studio.connection.core.Connection;
  38. import org.apache.directory.studio.connection.core.StudioControl;
  39. import org.apache.directory.studio.connection.core.StudioPagedResultsControl;
  40. import org.apache.directory.studio.connection.core.io.StudioNamingEnumeration;
  41. import org.apache.directory.studio.connection.core.jobs.StudioConnectionRunnableWithProgress;
  42. import org.apache.directory.studio.ldapbrowser.core.BrowserCoreConstants;
  43. import org.apache.directory.studio.ldapbrowser.core.BrowserCoreMessages;
  44. import org.apache.directory.studio.ldapbrowser.core.BrowserCorePlugin;
  45. import org.apache.directory.studio.ldapbrowser.core.model.IBrowserConnection;
  46. import org.apache.directory.studio.ldapbrowser.core.model.SearchParameter;
  47. import org.apache.directory.studio.ldapbrowser.core.model.impl.DummyEntry;
  48. import org.apache.directory.studio.ldapbrowser.core.utils.AttributeComparator;
  49. import org.apache.directory.studio.ldapbrowser.core.utils.JNDIUtils;
  50. import org.apache.directory.studio.ldapbrowser.core.utils.ModelConverter;
  51. import org.apache.directory.studio.ldapbrowser.core.utils.Utils;
  52. import org.apache.directory.studio.ldifparser.LdifFormatParameters;
  53. import org.apache.directory.studio.ldifparser.model.LdifEnumeration;
  54. import org.apache.directory.studio.ldifparser.model.container.LdifContainer;
  55. import org.apache.directory.studio.ldifparser.model.container.LdifContentRecord;
  56. import org.apache.directory.studio.ldifparser.model.lines.LdifAttrValLine;
  57. import org.apache.directory.studio.ldifparser.model.lines.LdifDnLine;
  58. import org.apache.directory.studio.ldifparser.model.lines.LdifSepLine;
  59. import org.apache.directory.studio.ldifparser.model.lines.LdifVersionLine;
  60. /**
  61. * Runnable to export directory content to an LDIF file.
  62. *
  63. * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  64. */
  65. public class ExportLdifRunnable implements StudioConnectionRunnableWithProgress
  66. {
  67. /** The filename of the LDIF file. */
  68. private String exportLdifFilename;
  69. /** The browser connection. */
  70. private IBrowserConnection browserConnection;
  71. /** The search parameter. */
  72. private SearchParameter searchParameter;
  73. /**
  74. * Creates a new instance of ExportLdifRunnable.
  75. *
  76. * @param exportLdifFilename the filename of the LDIF file
  77. * @param browserConnection the browser connection
  78. * @param searchParameter the search parameter
  79. */
  80. public ExportLdifRunnable( String exportLdifFilename, IBrowserConnection browserConnection,
  81. SearchParameter searchParameter )
  82. {
  83. this.exportLdifFilename = exportLdifFilename;
  84. this.browserConnection = browserConnection;
  85. this.searchParameter = searchParameter;
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public Connection[] getConnections()
  91. {
  92. return new Connection[]
  93. { browserConnection.getConnection() };
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. public String getName()
  99. {
  100. return BrowserCoreMessages.jobs__export_ldif_name;
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. public Object[] getLockedObjects()
  106. {
  107. List<Object> l = new ArrayList<Object>();
  108. l.add( browserConnection.getUrl() + "_" + DigestUtils.shaHex( exportLdifFilename ) ); //$NON-NLS-1$
  109. return l.toArray();
  110. }
  111. /**
  112. * {@inheritDoc}
  113. */
  114. public String getErrorMessage()
  115. {
  116. return BrowserCoreMessages.jobs__export_ldif_error;
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public void run( StudioProgressMonitor monitor )
  122. {
  123. monitor.beginTask( BrowserCoreMessages.jobs__export_ldif_task, 2 );
  124. monitor.reportProgress( " " ); //$NON-NLS-1$
  125. monitor.worked( 1 );
  126. try
  127. {
  128. // open file
  129. FileWriter fileWriter = new FileWriter( exportLdifFilename );
  130. BufferedWriter bufferedWriter = new BufferedWriter( fileWriter );
  131. // export
  132. int count = 0;
  133. export( browserConnection, searchParameter, bufferedWriter, count, monitor );
  134. // close file
  135. bufferedWriter.close();
  136. fileWriter.close();
  137. }
  138. catch ( Exception e )
  139. {
  140. monitor.reportError( e );
  141. }
  142. }
  143. private static void export( IBrowserConnection browserConnection, SearchParameter searchParameter,
  144. BufferedWriter bufferedWriter, int count, StudioProgressMonitor monitor ) throws IOException
  145. {
  146. try
  147. {
  148. JndiLdifEnumeration enumeration = search( browserConnection, searchParameter, monitor );
  149. LdifFormatParameters ldifFormatParameters = Utils.getLdifFormatParameters();
  150. // add version spec
  151. if ( BrowserCorePlugin.getDefault().getPluginPreferences()
  152. .getBoolean( BrowserCoreConstants.PREFERENCE_LDIF_INCLUDE_VERSION_LINE ) )
  153. {
  154. LdifVersionLine ldifVersionLine = LdifVersionLine.create();
  155. String ldifVersionLineString = ldifVersionLine.toFormattedString( ldifFormatParameters );
  156. bufferedWriter.write( ldifVersionLineString );
  157. LdifSepLine ldifSepLine = LdifSepLine.create();
  158. String ldifSepLineString = ldifSepLine.toFormattedString( ldifFormatParameters );
  159. bufferedWriter.write( ldifSepLineString );
  160. }
  161. // add the records
  162. while ( !monitor.isCanceled() && !monitor.errorsReported() && enumeration.hasNext() )
  163. {
  164. LdifContainer container = enumeration.next();
  165. if ( container instanceof LdifContentRecord )
  166. {
  167. LdifContentRecord record = ( LdifContentRecord ) container;
  168. LdifDnLine dnLine = record.getDnLine();
  169. LdifAttrValLine[] attrValLines = record.getAttrVals();
  170. LdifSepLine sepLine = record.getSepLine();
  171. // sort and format
  172. DummyEntry entry = ModelConverter.ldifContentRecordToEntry( record, browserConnection );
  173. AttributeComparator comparator = new AttributeComparator( entry );
  174. Arrays.sort( attrValLines, comparator );
  175. LdifContentRecord newRecord = new LdifContentRecord( dnLine );
  176. for ( int i = 0; i < attrValLines.length; i++ )
  177. {
  178. newRecord.addAttrVal( attrValLines[i] );
  179. }
  180. newRecord.finish( sepLine );
  181. String s = newRecord.toFormattedString( ldifFormatParameters );
  182. // String s = record.toFormattedString();
  183. bufferedWriter.write( s );
  184. count++;
  185. monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__export_progress,
  186. new String[]
  187. { Integer.toString( count ) } ) );
  188. }
  189. }
  190. }
  191. catch ( NamingException ne )
  192. {
  193. int ldapStatusCode = JNDIUtils.getLdapStatusCode( ne );
  194. if ( ldapStatusCode == 3 || ldapStatusCode == 4 || ldapStatusCode == 11 )
  195. {
  196. // ignore
  197. }
  198. else
  199. {
  200. monitor.reportError( ne );
  201. }
  202. }
  203. catch ( LdapInvalidDnException e )
  204. {
  205. monitor.reportError( e );
  206. }
  207. }
  208. static JndiLdifEnumeration search( IBrowserConnection browserConnection, SearchParameter parameter,
  209. StudioProgressMonitor monitor )
  210. {
  211. StudioNamingEnumeration result = SearchRunnable.search( browserConnection, parameter, monitor );
  212. return new JndiLdifEnumeration( result, browserConnection, parameter, monitor );
  213. }
  214. static class JndiLdifEnumeration implements LdifEnumeration
  215. {
  216. private StudioNamingEnumeration enumeration;
  217. private IBrowserConnection browserConnection;
  218. private SearchParameter parameter;
  219. private StudioProgressMonitor monitor;
  220. public JndiLdifEnumeration( StudioNamingEnumeration enumeration, IBrowserConnection browserConnection,
  221. SearchParameter parameter, StudioProgressMonitor monitor )
  222. {
  223. this.enumeration = enumeration;
  224. this.browserConnection = browserConnection;
  225. this.parameter = parameter;
  226. this.monitor = monitor;
  227. }
  228. public boolean hasNext() throws NamingException
  229. {
  230. if ( enumeration != null )
  231. {
  232. if ( enumeration.hasMore() )
  233. {
  234. return true;
  235. }
  236. Control[] jndiControls = enumeration.getResponseControls();
  237. if ( jndiControls != null )
  238. {
  239. for ( Control jndiControl : jndiControls )
  240. {
  241. if ( jndiControl instanceof PagedResultsResponseControl )
  242. {
  243. PagedResultsResponseControl prrc = ( PagedResultsResponseControl ) jndiControl;
  244. byte[] cookie = prrc.getCookie();
  245. if ( cookie != null )
  246. {
  247. // search again: pass the response cookie to the request control
  248. for ( StudioControl studioControl : parameter.getControls() )
  249. {
  250. if ( studioControl instanceof StudioPagedResultsControl )
  251. {
  252. StudioPagedResultsControl sprc = ( StudioPagedResultsControl ) studioControl;
  253. sprc.setCookie( cookie );
  254. }
  255. }
  256. enumeration = SearchRunnable.search( browserConnection, parameter, monitor );
  257. return enumeration != null && enumeration.hasMore();
  258. }
  259. }
  260. }
  261. }
  262. }
  263. return false;
  264. }
  265. public LdifContainer next() throws NamingException, LdapInvalidDnException
  266. {
  267. SearchResult sr = enumeration.next();
  268. Dn dn = JNDIUtils.getDn( sr );
  269. LdifContentRecord record = LdifContentRecord.create( dn.getName() );
  270. NamingEnumeration<? extends Attribute> attributeEnumeration = sr.getAttributes().getAll();
  271. while ( attributeEnumeration.hasMore() )
  272. {
  273. Attribute attribute = attributeEnumeration.next();
  274. String attributeName = attribute.getID();
  275. NamingEnumeration<?> valueEnumeration = attribute.getAll();
  276. while ( valueEnumeration.hasMore() )
  277. {
  278. Object o = valueEnumeration.next();
  279. if ( o instanceof String )
  280. {
  281. record.addAttrVal( LdifAttrValLine.create( attributeName, ( String ) o ) );
  282. }
  283. if ( o instanceof byte[] )
  284. {
  285. record.addAttrVal( LdifAttrValLine.create( attributeName, ( byte[] ) o ) );
  286. }
  287. }
  288. }
  289. record.finish( LdifSepLine.create() );
  290. return record;
  291. }
  292. }
  293. }