View Javadoc
1   /*
2    * Copyright 2013-2016 Brian Thomas Matthews
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.btmatthews.maven.plugins.ldap.ldif;
18  
19  import com.btmatthews.maven.plugins.ldap.FormatWriter;
20  import com.unboundid.ldap.sdk.Entry;
21  import com.unboundid.util.ByteStringBuffer;
22  
23  import java.io.IOException;
24  import java.io.OutputStream;
25  
26  /**
27   * This {@link FormatWriter} writes LDAP entries to an underlying output stream.
28   *
29   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
30   * @since 1.2.0
31   */
32  public final class LDIFFormatWriter implements FormatWriter {
33  
34      /**
35       * The underlying output stream.
36       */
37      private final OutputStream ldifOutputStream;
38      /**
39       * The system dependent end of line marker.
40       */
41      private final byte[] eol;
42      /**
43       * Used by the {@link #printEntry(com.unboundid.ldap.sdk.Entry)} method to determine if the
44       * first entry has already been output.
45       */
46      private boolean first = true;
47  
48      /**
49       * Initialize the {@link LDIFFormatWriter} by keeping a reference to the underlying output stream.
50       *
51       * @param outputStream The underlying output stream.
52       */
53      public LDIFFormatWriter(final OutputStream outputStream) {
54          ldifOutputStream = outputStream;
55          final String eolString = System.getProperty("line.separator", "\n");
56          eol = eolString.getBytes();
57      }
58  
59      /**
60       * Write the LDAP entry to the underlying output stream in LDIF format.
61       *
62       * @param entry The directory entry.
63       * @throws IOException If there was a problem writing to the underlying output stream.
64       */
65      public void printEntry(final Entry entry) throws IOException {
66          if (entry != null) {
67              final ByteStringBuffer buffer = new ByteStringBuffer();
68              entry.toLDIF(buffer, 77);
69              if (!first) {
70                  ldifOutputStream.write(eol);
71              } else {
72                  first = false;
73              }
74              ldifOutputStream.write(buffer.toByteArray());
75          }
76      }
77  
78      /**
79       * Close the {@link LDIFFormatWriter} which does not require any processing.
80       */
81      public void close() {
82      }
83  }