File: basic.xml

package info (click to toggle)
libspring-ldap-java 1.3.1.RELEASE-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,872 kB
  • sloc: java: 12,509; xml: 4,106; jsp: 36; makefile: 33; sh: 13
file content (385 lines) | stat: -rw-r--r-- 13,462 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?xml version="1.0" encoding="UTF-8"?>
<chapter id="basic">
  <title>Basic Operations</title>

  <sect1 id="basic-searches">
    <title>Search and Lookup Using AttributesMapper</title>

    <para>In this example we will use an <literal>AttributesMapper</literal>
    to easily build a List of all common names of all person objects.</para>

    <example>
      <title>AttributesMapper that returns a single attribute</title>

      <programlisting>package com.example.dao;

public class PersonDaoImpl implements PersonDao {
   private LdapTemplate ldapTemplate;

   public void setLdapTemplate(LdapTemplate ldapTemplate) {
      this.ldapTemplate = ldapTemplate;
   }

   public List getAllPersonNames() {
      return ldapTemplate.search(
         "", "(objectclass=person)",
<emphasis role="bold">         new AttributesMapper() {
            public Object mapFromAttributes(Attributes attrs)
               throws NamingException {
               return attrs.get("cn").get();
            }
         }</emphasis>);
   }
}</programlisting>
    </example>

    <para>The inline implementation of <literal>AttributesMapper</literal>
    just gets the desired attribute value from the
    <literal>Attributes</literal> and returns it. Internally,
    <literal>LdapTemplate</literal> iterates over all entries found, calling
    the given <literal>AttributesMapper</literal> for each entry, and collects
    the results in a list. The list is then returned by the
    <literal>search</literal> method.</para>

    <para>Note that the <literal>AttributesMapper</literal> implementation
    could easily be modified to return a full <literal>Person</literal>
    object:</para>

    <example>
      <title>AttributesMapper that returns a Person object</title>

      <programlisting>package com.example.dao;

public class PersonDaoImpl implements PersonDao {
   private LdapTemplate ldapTemplate;
   ...
<emphasis role="bold">   private class PersonAttributesMapper implements AttributesMapper {
      public Object mapFromAttributes(Attributes attrs) throws NamingException {
         Person person = new Person();
         person.setFullName((String)attrs.get("cn").get());
         person.setLastName((String)attrs.get("sn").get());
         person.setDescription((String)attrs.get("description").get());
         return person;
      }
   }
</emphasis>
   public List getAllPersons() {
      return ldapTemplate.search("", "(objectclass=person)", <emphasis
          role="bold">new PersonAttributesMapper()</emphasis>);
   }
}</programlisting>
    </example>

    <para>If you have the distinguished name (<literal>dn</literal>) that
    identifies an entry, you can retrieve the entry directly, without
    searching for it. This is called a <emphasis>lookup</emphasis> in Java
    LDAP. The following example shows how a lookup results in a Person
    object:</para>

    <example>
      <title>A lookup resulting in a Person object</title>

      <programlisting>package com.example.dao;

public class PersonDaoImpl implements PersonDao {
   private LdapTemplate ldapTemplate;
   ...
   public Person findPerson(String dn) {
      return (Person) ldapTemplate.lookup(dn, new PersonAttributesMapper());
   }
}</programlisting>
    </example>

    <para>This will look up the specified <literal>dn</literal> and pass the
    found attributes to the supplied <literal>AttributesMapper</literal>, in
    this case resulting in a <literal>Person</literal> object.</para>
  </sect1>

  <sect1 id="basic-filters">
    <title>Building Dynamic Filters</title>

    <para>We can build dynamic filters to use in searches, using the classes
    from the <literal>org.springframework.ldap.filter</literal>
    package. Let's say that we want the following filter:
    <literal>(&amp;(objectclass=person)(sn=?))</literal>, where we want the
    <literal>?</literal> to be replaced with the value of the parameter
    <literal>lastName</literal>. This is how we do it using the filter support
    classes:</para>

    <example>
      <title>Building a search filter dynamically</title>

      <programlisting>package com.example.dao;

public class PersonDaoImpl implements PersonDao {
   private LdapTemplate ldapTemplate;
   ...
   public List getPersonNamesByLastName(String lastName) {
<emphasis role="bold">      AndFilter filter = new AndFilter();
      filter.and(new EqualsFilter("objectclass", "person"));
      filter.and(new EqualsFilter("sn", lastName));
</emphasis>      return ldapTemplate.search(
         "", <emphasis role="bold">filter.encode()</emphasis>,
         new AttributesMapper() {
            public Object mapFromAttributes(Attributes attrs)
               throws NamingException {
               return attrs.get("cn").get();
            }
         });
   }
}</programlisting>
    </example>

    <para>To perform a wildcard search, it's possible to use the
    <literal>WhitespaceWildcardsFilter</literal>:</para>

    <example>
      <title>Building a wildcard search filter</title>

      <programlisting>AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "person"));
filter.and(new WhitespaceWildcardsFilter("cn", cn));</programlisting>
    </example>

    <para>
    	<note>
    		In addition to simplifying building of complex search filters,
    		the <literal>Filter</literal> classes also provide proper escaping
    		of any unsafe characters. This prevents &quot;ldap injection&quot;,
    		where a user might use such characters to inject unwanted operations
    		into your LDAP operations.
    	</note>
    </para>
  </sect1>

  <sect1>
    <title>Building Dynamic Distinguished Names</title>

    <para>The standard <ulink
    url="http://download.oracle.com/javase/1.5.0/docs/api/javax/naming/Name.html">Name</ulink>
    interface represents a generic name, which is basically an ordered
    sequence of components. The <literal>Name</literal> interface also
    provides operations on that sequence; e.g., <literal>add</literal> or
    <literal>remove</literal>. LdapTemplate provides an implementation of the
    <literal>Name</literal> interface: <literal>DistinguishedName</literal>.
    Using this class will greatly simplify building distinguished names,
    especially considering the sometimes complex rules regarding escapings and
    encodings. As with the <literal>Filter</literal> classes this helps preventing
    potentially malicious data being injected into your LDAP operations.
    </para>
    <para>
    The following example illustrates how
    <literal>DistinguishedName</literal> can be used to dynamically construct
    a distinguished name:</para>

    <example>
      <title>Building a distinguished name dynamically</title>

      <programlisting>package com.example.dao;

import org.springframework.ldap.core.support.DistinguishedName;
import javax.naming.Name;

public class PersonDaoImpl implements PersonDao {
   public static final String BASE_DN = "dc=example,dc=com";
   ...
   protected Name buildDn(Person p) {
<emphasis role="bold">      DistinguishedName dn = new DistinguishedName(BASE_DN);
      dn.add("c", p.getCountry());
      dn.add("ou", p.getCompany());
      dn.add("cn", p.getFullname());
</emphasis>      return dn;
   }
}</programlisting>
    </example>

    <para>Assuming that a Person has the following attributes:</para>

    <informaltable>
      <tgroup cols="2">
        <tbody>
          <row>
            <entry><literal>country</literal></entry>

            <entry>Sweden</entry>
          </row>

          <row>
            <entry><literal>company</literal></entry>

            <entry>Some Company</entry>
          </row>

          <row>
            <entry><literal>fullname</literal></entry>

            <entry>Some Person</entry>
          </row>
        </tbody>
      </tgroup>
    </informaltable>

    <para>The code above would then result in the following distinguished
    name:</para>

    <para><programlisting>cn=Some Person, ou=Some Company, c=Sweden, dc=example, dc=com</programlisting></para>

    <para>In Java 5, there is an implementation of the Name interface: <ulink
    url="http://download.oracle.com/javase/1.5.0/docs/api/javax/naming/ldap/LdapName.html">LdapName</ulink>.
    If you are in the Java 5 world, you might as well use
    <literal>LdapName</literal>. However, you may still use
    <literal>DistinguishedName</literal> if you so wish.</para>
  </sect1>

  <sect1 id="basic-binding-unbinding">
    <title>Binding and Unbinding</title>

    <sect2 id="basic-binding-data">
      <title>Binding Data</title>

      <para>Inserting data in Java LDAP is called binding. In order to do
      that, a distinguished name that uniquely identifies the new entry is
      required. The following example shows how data is bound using
      LdapTemplate:</para>

      <example>
        <title>Binding data using Attributes</title>

        <programlisting>package com.example.dao;

public class PersonDaoImpl implements PersonDao {
   private LdapTemplate ldapTemplate;
   ...
   public void create(Person p) {
      Name dn = buildDn(p);
<emphasis role="bold">      ldapTemplate.bind(dn, null, buildAttributes(p));
</emphasis>   }

   private Attributes buildAttributes(Person p) {
      Attributes attrs = new BasicAttributes();
      BasicAttribute ocattr = new BasicAttribute("objectclass");
      ocattr.add("top");
      ocattr.add("person");
      attrs.put(ocattr);
      attrs.put("cn", "Some Person");
      attrs.put("sn", "Person");
      return attrs;
   }
}</programlisting>
      </example>

      <para>The Attributes building is--while dull and verbose--sufficient for
      many purposes. It is, however, possible to simplify the binding
      operation further, which will be described in <xref
      linkend="dirobjectfactory" />.</para>
    </sect2>

    <sect2 id="basic-unbinding-data">
      <title>Unbinding Data</title>

      <para>Removing data in Java LDAP is called unbinding. A distinguished
      name (dn) is required to identify the entry, just as in the binding
      operation. The following example shows how data is unbound using
      LdapTemplate:</para>

      <example>
        <title>Unbinding data</title>

        <programlisting>package com.example.dao;

public class PersonDaoImpl implements PersonDao {
   private LdapTemplate ldapTemplate;
   ...
   public void delete(Person p) {
      Name dn = buildDn(p);
<emphasis role="bold">      ldapTemplate.unbind(dn);
</emphasis>   }
}</programlisting>
      </example>
    </sect2>
  </sect1>

  <sect1 id="basic-modifying">
    <title>Modifying</title>

    <para>In Java LDAP, data can be modified in two ways: either using
    <emphasis>rebind</emphasis> or
    <emphasis>modifyAttributes</emphasis>.</para>

    <sect2>
      <title>Modifying using <literal>rebind</literal></title>

      <para>A <literal>rebind</literal> is a very crude way to modify data.
      It's basically an <literal>unbind</literal> followed by a
      <literal>bind</literal>. It looks like this:</para>

      <example>
        <title>Modifying using rebind</title>

        <programlisting>package com.example.dao;

public class PersonDaoImpl implements PersonDao {
   private LdapTemplate ldapTemplate;
   ...
   public void update(Person p) {
      Name dn = buildDn(p);
<emphasis role="bold">      ldapTemplate.rebind(dn, null, buildAttributes(p));
</emphasis>   }
}</programlisting>
      </example>
    </sect2>

    <sect2 id="modify-modifyAttributes">
      <title>Modifying using <literal>modifyAttributes</literal></title>

      <para>If only the modified attributes should be replaced, there is a
      method called <literal>modifyAttributes</literal> that takes an array of
      modifications:</para>

      <example>
        <title>Modifying using modifyAttributes</title>

        <programlisting>package com.example.dao;

public class PersonDaoImpl implements PersonDao {
   private LdapTemplate ldapTemplate;
   ...
   public void updateDescription(Person p) {
      Name dn = buildDn(p);
      Attribute attr = new BasicAttribute("description", p.getDescription())
      ModificationItem item = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
<emphasis role="bold">      ldapTemplate.modifyAttributes(dn, new ModificationItem[] {item});
</emphasis>   }
}</programlisting>
      </example>

      <para>Building <literal>Attributes</literal> and
      <literal>ModificationItem</literal> arrays is a lot of work, but as you
      will see in <xref linkend="dirobjectfactory" />, the update operations
      can be simplified.</para>
    </sect2>
  </sect1>

  <sect1 id="samples">
    <title>Sample applications</title>

    <para>It is recommended that you review the Spring LDAP sample
    applications included in the release distribution for best-practice
    illustrations of the features of this library. A description of each
    sample is provided below:</para>

    <para><orderedlist>
        <listitem>
          <para>spring-ldap-person - the sample demonstrating most
          features.</para>
        </listitem>

        <listitem>
          <para>spring-ldap-article - the sample application that was written
          to accompany a <ulink
          url="http://today.java.net/pub/a/today/2006/04/18/ldaptemplate-java-ldap-made-simple.html">java.net
          article</ulink> about Spring LDAP.</para>
        </listitem>
      </orderedlist></para>
  </sect1>
</chapter>