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.dsml;
18  
19  import com.btmatthews.maven.plugins.ldap.FormatWriter;
20  import com.unboundid.ldap.sdk.Attribute;
21  import com.unboundid.ldap.sdk.Entry;
22  import com.unboundid.util.ByteStringBuffer;
23  
24  import java.io.IOException;
25  import java.io.OutputStream;
26  import java.io.PrintWriter;
27  
28  /**
29   * A {@link FormatWriter} that outputs LDAP directory entries to DSML formatted files.
30   *
31   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
32   * @since 1.2.0
33   */
34  public final class DSMLFormatWriter implements FormatWriter {
35  
36      /**
37       * The underlying output stream.
38       */
39      private final OutputStream ldifOutputStream;
40      /**
41       * The end of line marker.
42       */
43      private final String eol;
44  
45      /**
46       * Create {@link PrintWriter} that will be used to write the directory entries to the output stream and write the
47       * opening &lt;dsml:dsml;&gt; and &lt;dsml:directory-entry;&gt; tags.
48       *
49       * @param outputStream The file output stream.
50       * @throws IOException If there was a problem writing the openng tags.
51       */
52      public DSMLFormatWriter(final OutputStream outputStream) throws IOException {
53          ldifOutputStream = outputStream;
54          eol = System.getProperty("line.separator", "\n");
55          final ByteStringBuffer buffer = new ByteStringBuffer();
56          buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
57          buffer.append(eol);
58          buffer.append("<dsml:dsml xmlns:dsml=\"http://www.dsml.org/DSML\">");
59          buffer.append(eol);
60          buffer.append("\t<dsml:directory-entries>");
61          buffer.append(eol);
62          ldifOutputStream.write(buffer.toByteArray());
63      }
64  
65      /**
66       * Writes an individual LDAP directory entry to the DSML file.
67       *
68       * @param entry The directory entry.
69       * @throws IOException If there was a problem writing to the underlying output stream.
70       */
71      public void printEntry(final Entry entry) throws IOException {
72          final ByteStringBuffer buffer = new ByteStringBuffer();
73          buffer.append("\t\t<dsml:entry dn=\"");
74          buffer.append(entry.getDN());
75          buffer.append("\">");
76          buffer.append(eol);
77          final String[] values = entry.getAttributeValues("objectclass");
78          if (values != null) {
79              buffer.append("\t\t\t<dsml:objectclass>");
80              buffer.append(eol);
81              for (final String value : values) {
82                  buffer.append("\t\t\t\t<dsml:oc-value>");
83                  buffer.append(value);
84                  buffer.append("</dsml:oc-value>");
85                  buffer.append(eol);
86              }
87              buffer.append("\t\t\t</dsml:objectclass>");
88              buffer.append(eol);
89          }
90          for (final Attribute attribute : entry.getAttributes()) {
91              final String name = attribute.getName();
92              if (!name.equals("objectclass")) {
93                  buffer.append("\t\t\t<dsml:attr name=\"");
94                  buffer.append(name);
95                  buffer.append("\">");
96                  buffer.append(eol);
97                  for (final String value : attribute.getValues()) {
98                      buffer.append("\t\t\t\t<dsml:value>");
99                      buffer.append(value);
100                     buffer.append("</dsml:value>");
101                     buffer.append(eol);
102                 }
103                 buffer.append("\t\t\t</dsml:attr>");
104                 buffer.append(eol);
105             }
106         }
107         buffer.append("\t\t</dsml:entry>");
108         buffer.append(eol);
109         ldifOutputStream.write(buffer.toByteArray());
110     }
111 
112     /**
113      * Close the {@link FormatWriter} writing the closing tags.
114      *
115      * @throws IOException If there was a problem writing to the underlying output stream.
116      */
117     public void close() throws IOException {
118         final ByteStringBuffer buffer = new ByteStringBuffer();
119         buffer.append("\t</dsml:directory-entries>\n");
120         buffer.append(eol);
121         buffer.append("</dsml:dsml>");
122         buffer.append(eol);
123         ldifOutputStream.write(buffer.toByteArray());
124     }
125 }