aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/ldap/LdapDirectoryImpl.java
blob: d0c89a7a2a28269e1b378e77526be6c181c1273d (plain)
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.ldap;

import java.util.*;

import javax.naming.*;
import javax.naming.directory.*;

import net.java.sip.communicator.service.ldap.*;
import net.java.sip.communicator.service.ldap.event.*;
import net.java.sip.communicator.util.*;

/**
 * An LdapDirectory stores settings for one directory server
 * and performs ldap operations (search)
 *
 * @author Sebastien Mazy
 */
public class LdapDirectoryImpl
    extends DefaultLdapEventManager
    implements LdapDirectory,
               LdapListener,
               LdapConstants
{
    /**
     * the logger for this class
     */
    private final static Logger logger = Logger
        .getLogger(LdapDirectoryImpl.class);

    static
    {
        logger.setLevelTrace();
    }

    /**
     * The settings for this directory
     */
    private LdapDirectorySettings settings;

    /**
     * Stores the pending searches
     *
     * @see LdapPendingSearch
     */
    private HashMap<LdapQuery, LdapPendingSearch> pendingSearches =
        new HashMap<LdapQuery, LdapPendingSearch>();

    /**
     * Name of avatar attribute.
     */
    private static final String PHOTO_ATTRIBUTE = "jpegPhoto";

    /**
     * data structure used to store the LDAP attributes that
     * could contain our search string
     * e.g. cn, sn, givenname, uid
     */
    private static final Set<String> searchableAttributes
        = new HashSet<String>();

    /**
     * Map that contains list of attributes that match a certain type of
     * attributes (i.e. all attributes that match a mail, a home phone, a
     * mobile phones, ...).
     */
    private Map<String, List<String> > attributesMap = new
        HashMap<String, List<String> >();

    /**
     * List of searchables attributes.
     */
    private List<String> searchableAttrs = new ArrayList<String>();

    static
    {
        searchableAttributes.add("displayName");
        searchableAttributes.add("cn");
        searchableAttributes.add("commonname");
        searchableAttributes.add("sn");
        searchableAttributes.add("surname");
        searchableAttributes.add("gn");
        searchableAttributes.add("givenname");
        //searchableAttributes.add("mail");
        searchableAttributes.add("uid");
    }

    /**
     * data structure used to store the attribute whose values
     * could be a contact address for the person found
     * e.g. mail, telephoneNumber
     */
    private static final Set<String> retrievableAttributes
        = new HashSet<String>();

    static
    {
        retrievableAttributes.add("displayName");
        retrievableAttributes.add("cn");
        retrievableAttributes.add("commonname");
        retrievableAttributes.add("sn");
        retrievableAttributes.add("surname");
        retrievableAttributes.add("givenName");
        retrievableAttributes.add("givenname");
        retrievableAttributes.add("gn");
        retrievableAttributes.add("o");
        retrievableAttributes.add("organizationName");
        retrievableAttributes.add("company");
        retrievableAttributes.add("ou");
        retrievableAttributes.add("orgunit");
        retrievableAttributes.add("organizationalUnitName");
        retrievableAttributes.add("department");
        retrievableAttributes.add("departmentNumber");
    }

    /**
     * the env HashTable stores the settings used to create
     * an InitialDirContext (i.e. connect to the LDAP directory)
     */
    private final Hashtable<String, String> env =
        new Hashtable<String, String>();

    /**
     * The contructor for this class.
     * Since this element is immutable (otherwise it would be a real pain
     * to use with a Set), it takes all the settings we could need to store
     * This constructor will not modify the <tt>settings</tt>
     * or save a reference to it, but may save a clone.
     *
     * @param settings settings for this new server
     *
     * @see LdapDirectorySettings
     */
    public LdapDirectoryImpl(LdapDirectorySettings settings)
    {
        String portText;

        /* settings checks */
        if(!textHasContent(settings.getName()))
            throw new IllegalArgumentException("name has no content.");
        if(!textHasContent(settings.getHostname()))
            throw new IllegalArgumentException("Hostname has no content.");
        if(settings.getAuth() != Auth.NONE && !textHasContent(
                settings.getBindDN()))
            throw new IllegalArgumentException("Bind DN has no content.");
        if(settings.getAuth() != Auth.NONE && settings.getPassword() == null)
            throw new IllegalArgumentException("password is null.");
        if(settings.getPort() < 0 || settings.getPort() > 65535)
            throw new IllegalArgumentException("Illegal port number.");
        if(settings.getBaseDN() == null)
            throw new IllegalArgumentException("Base DN has no content.");

        this.settings = settings.clone();

        if(this.settings.getPort() == 0)
            portText = ":" + this.settings.getEncryption().defaultPort();
        portText = ":" + this.settings.getPort();

        /* fills environment for InitialDirContext */
        this.env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.ldap.LdapCtxFactory");
        this.env.put("com.sun.jndi.ldap.connect.timeout", LDAP_CONNECT_TIMEOUT);
        this.env.put("com.sun.jndi.ldap.read.timeout", LDAP_READ_TIMEOUT);
        this.env.put(Context.PROVIDER_URL, settings.getEncryption().
                protocolString() + settings.getHostname() + portText +"/");
        //connection pooling
        this.env.put("com.sun.jndi.ldap.connect.pool", "true");

        /* TODO STARTTLS */
        switch(this.settings.getEncryption())
        {
            case CLEAR:
                break;
            case SSL:
                this.env.put(Context.SECURITY_PROTOCOL, "ssl");
                this.env.put("java.naming.ldap.factory.socket",
                    LdapSSLSocketFactoryDelegate.class.getName());
                break;
        }

        /* TODO SASL */
        switch(this.settings.getAuth())
        {
            case NONE:
                this.env.put(Context.SECURITY_AUTHENTICATION, "none");
                break;
            case SIMPLE:
                this.env.put(Context.SECURITY_AUTHENTICATION, "simple");
                this.env.put(Context.SECURITY_PRINCIPAL,
                        this.settings.getBindDN());
                this.env.put(Context.SECURITY_CREDENTIALS,
                        this.settings.getPassword());
                break;
        }

        attributesMap.put("mail", settings.getMailSearchFields());
        attributesMap.put("workPhone", settings.getWorkPhoneSearchFields());
        attributesMap.put("mobilePhone", settings.getMobilePhoneSearchFields());
        attributesMap.put("homePhone", settings.getHomePhoneSearchFields());

        this.searchableAttrs.addAll(searchableAttributes);
        for(String s : settings.getMailSearchFields())
        {
            searchableAttrs.add(s);
        }
    }

    /**
     * Returns the state of the enabled marker.
     * Required by LdapDirectory interface.
     *
     * @return the state of the enabled marker
     *
     * @see LdapDirectory#isEnabled
     */
    public boolean isEnabled()
    {
        return this.settings.isEnabled();
    }

    /**
     * Sets the state of the enabled marker
     * Required by LdapDirectory interface.
     *
     * @param enabled whether the server is marked as enabled
     *
     * @see LdapDirectory#setEnabled
     */
    public void setEnabled(boolean enabled)
    {
        this.settings.setEnabled(enabled);
    }

    /**
     * Returns an LdapDirectorySettings object containing
     * a copy of the settings of this server
     *
     * @return a copy of this server settings
     *
     * @see LdapDirectorySettings
     * @see LdapDirectory#getSettings
     */
    public LdapDirectorySettings getSettings()
    {
        return this.settings.clone();
    }

    /**
     * Connects to the remote directory
     */
    private InitialDirContext connect()
        throws NamingException
    {
        logger.trace("connecting to directory \"" + this + "\"");
        long time0 = System.currentTimeMillis();
        InitialDirContext dirContext =
            new InitialDirContext(this.env);
        long time1 = System.currentTimeMillis();
        logger.trace("connection to directory \"" + this + "\" took " +
                (time1-time0)  + " ms");
        return dirContext;
    }

    /**
     * closes the ldap connection
     */
    private void disconnect(InitialDirContext dirContext)
    {
        if(dirContext == null)
            throw new NullPointerException("dirContext is null");

        try
        {
            dirContext.close();
        }
        catch(NamingException e)
        {
            logger.trace("disconnection from directory \"" + this +
                    "\" failed!");
        }

        logger.trace("disconnection achieved!");
    }

    /**
     * Searches a person in the directory, based on a search string.
     * Since that method might take time to process, it should be
     * implemented asynchronously and send the results (LdapPersonFound)
     * with an LdapEvent to its listeners
     *
     * @param query assumed name (can be partial) of the person searched
     * e.g. "john", "doe", "john doe"
     * @param caller the LdapListener which called the method and will
     * receive results.
     * @param searchSettings custom settings for this search, null if you
     * want to stick with the defaults
     *
     * @see LdapDirectory#searchPerson
     * @see LdapPersonFound
     * @see LdapEvent
     */
    public void searchPerson(final LdapQuery query, final LdapListener caller,
            LdapSearchSettings searchSettings)
    {
        if(query == null)
            throw new NullPointerException("query shouldn't be null!");
        if(caller == null)
            throw new NullPointerException("caller shouldn't be null!");
        if(searchSettings == null)
            searchSettings = new LdapSearchSettingsImpl();

        // if the initial query string was "john d",
        // the intermediate query strings could be:
        // "*john d*" and "d*john"
        final String[] intermediateQueryStrings
            = buildIntermediateQueryStrings(query.toString());

        // the servers list contains this directory as many times as
        // the number of intermediate query strings
        List<LdapDirectory> serversList = new ArrayList<LdapDirectory>();
        //for(String queryString : intermediateQueryStrings)
        for(int i = 0 ; i < intermediateQueryStrings.length ; i++)
            serversList.add(this);

        // when the pendingSearches element will be empty,
        // all intermediate query strings will have been searched
        // and the search will be finished
        this.pendingSearches.put(query, new LdapPendingSearch(serversList,
                caller));

        // really performs the search
        for(String queryString : intermediateQueryStrings)
            this.performSearch(query, queryString, searchSettings, this);
    }

    private void performSearch(final LdapQuery query,
            final String realQueryString,
            final LdapSearchSettings searchSettings,
            final LdapListener caller)
    {
        Thread searchThread = new Thread()
        {
            int cancelState = 0;

            @Override
            public void run()
            {
                logger.trace("starting search for " + realQueryString +
                        " (initial query: \"" + query.toString() +
                        "\") on directory \"" + LdapDirectoryImpl.this + "\"");

                SearchControls searchControls =
                    buildSearchControls(searchSettings);

                LdapEvent endEvent = null;
                InitialDirContext dirContext = null;

                try
                {
                    if(searchSettings.isDelaySet())
                        Thread.sleep(searchSettings.getDelay());

                    checkCancel();
                    dirContext = connect();
                    checkCancel();

                    long time0 = System.currentTimeMillis();

                    NamingEnumeration<?> results = dirContext.search(
                            LdapDirectoryImpl.this.settings.getBaseDN(),
                            buildSearchFilter(realQueryString),
                            searchControls
                            );

                    checkCancel();

                    while (results.hasMore())
                    {
                        checkCancel();

                        SearchResult searchResult =
                            (SearchResult) results.next();
                        Map<String, Set<String>> retrievedAttributes =
                            retrieveAttributes(searchResult);
                        LdapPersonFound person =
                            buildPerson(
                                query,
                                searchResult.getName(),
                                retrievedAttributes
                                );
                        LdapEvent resultEvent =
                            new LdapEvent(LdapDirectoryImpl.this,
                                    LdapEvent.LdapEventCause.NEW_SEARCH_RESULT,
                                    person);
                        fireLdapEvent(resultEvent, caller);
                    }

                    long time1 = System.currentTimeMillis();
                    logger.trace("search for real query \"" + realQueryString +
                            "\" (initial query: \"" + query.toString() +
                            "\") on directory \"" + LdapDirectoryImpl.this +
                            "\" took " + (time1-time0) + "ms");

                    endEvent = new LdapEvent(LdapDirectoryImpl.this,
                            LdapEvent.LdapEventCause.SEARCH_ACHIEVED, query);
                }
                catch(OperationNotSupportedException e)
                {
                    logger.trace(
                            "use bind DN without password during search" +
                            " for real query \"" +
                            realQueryString + "\" (initial query: \"" +
                            query.toString() + "\") on directory \"" +
                            LdapDirectoryImpl.this + "\": " + e);
                    endEvent = new LdapEvent(
                            LdapDirectoryImpl.this,
                            LdapEvent.LdapEventCause.SEARCH_AUTH_ERROR,
                            query
                            );
                }
                catch(AuthenticationException e)
                {
                    logger.trace(
                            "authentication failed during search" +
                            " for real query \"" +
                            realQueryString + "\" (initial query: \"" +
                            query.toString() + "\") on directory \"" +
                            LdapDirectoryImpl.this + "\": " + e);
                    endEvent = new LdapEvent(
                            LdapDirectoryImpl.this,
                            LdapEvent.LdapEventCause.SEARCH_AUTH_ERROR,
                            query
                            );
                }
                catch(NamingException e)
                {
                    logger.trace(
                            "an external exception was thrown during search" +
                            " for real query \"" +
                            realQueryString + "\" (initial query: \"" +
                            query.toString() + "\") on directory \"" +
                            LdapDirectoryImpl.this + "\": " + e);
                    endEvent = new LdapEvent(
                            LdapDirectoryImpl.this,
                            LdapEvent.LdapEventCause.SEARCH_ERROR,
                            query
                            );
                }
                catch(LdapQueryCancelledException e)
                {
                    logger.trace("search for real query \"" + realQueryString +
                            "\" (initial query: \"" + query.toString() +
                            "\") on " + LdapDirectoryImpl.this +
                            " cancelled at state " + cancelState);
                    endEvent = new LdapEvent(
                            LdapDirectoryImpl.this,
                            LdapEvent.LdapEventCause.SEARCH_CANCELLED,
                            query
                            );

                }
                catch(InterruptedException e)
                {
                    // whether sleep was interrupted
                    // is not that important
                }
                finally
                {
                    fireLdapEvent(endEvent, caller);
                    if(dirContext != null)
                        disconnect(dirContext);
                }
            }

            /**
             * Checks if the query that triggered this search has
             * been marked as cancelled. If that's the case, the
             * search thread should be stopped and this method will
             * send a search cancelled event to the search initiator.
             * This method should be called by the search thread as
             * often as possible to quickly interrupt when needed.
             */
            private void checkCancel()
                throws LdapQueryCancelledException
            {
                if(query.getState() == LdapQuery.State.CANCELLED)
                {
                    throw new LdapQueryCancelledException();
                }
                this.cancelState++;
            }
        };

        // setting the classloader is necessary so that the BundleContext can be
        // accessed from classes instantiated from JNDI (specifically from our
        // custom SocketFactory)
        searchThread.setContextClassLoader(getClass().getClassLoader());
        searchThread.setDaemon(true);
        searchThread.start();
    }

    private static String[]
        buildIntermediateQueryStrings(String initialQueryString)
    {
        // search for "doe john" as well "as john doe"
        String[] words = initialQueryString.split(" ");
        String[] intermediateQueryStrings;

        if(words.length == 2)
        {
            intermediateQueryStrings = new String[2];
            intermediateQueryStrings[0] = "*" + words[0] + " " + words[1] + "*";
            intermediateQueryStrings[1] = words[1] + "*" + words[0];
        }
        else
        {
            // one word or too many combinations
            intermediateQueryStrings = new String[1];
            intermediateQueryStrings[0] = "*" + initialQueryString + "*";
        }

        return intermediateQueryStrings;
    }

    /**
     * Fills the retrievedAttributes map with
     * "retrievable" string attributes (name, telephone number, ...)
     *
     * @param searchResult the results to browse for attributes
     * @return the attributes in a Map
     */
    private Map<String, Set<String>>
        retrieveAttributes(SearchResult searchResult)
        throws NamingException
    {
        Attributes attributes =
            searchResult.getAttributes();
        Map<String, Set<String>> retrievedAttributes =
            new HashMap<String, Set<String>>();
        NamingEnumeration<String> ids = attributes.getIDs();
        while(ids.hasMore())
        {
            String id = ids.next();
            if(retrievableAttributes.contains(id) || containsAttribute(id))
            {
                Set<String> valuesSet = new HashSet<String>();
                retrievedAttributes.put(id, valuesSet);
                Attribute attribute = attributes.get(id);
                NamingEnumeration<?> values = attribute.getAll();
                while(values.hasMore())
                {
                    String value = (String) values.next();
                    valuesSet.add(value);
                }
            }
        }
        return retrievedAttributes;
    }

    /**
     * Builds an LdapPersonFound with the retrieved attributes
     *
     * @param query the initial query issued
     * @param dn the distinguished name of the person in the directory
     * @return the LdapPersonFoulnd built
     */
    private LdapPersonFound
        buildPerson(
                LdapQuery query,
                String dn,
                Map<String,
                Set<String>> retrievedAttributes
                )
        {
            LdapPersonFound person =
                new LdapPersonFoundImpl(LdapDirectoryImpl.this, dn, query);

            /* first name */
            if(retrievedAttributes.get("givenname") != null)
            {
                String firstName =
                    retrievedAttributes.get("givenname").iterator().next();
                person.setFirstName(firstName);
            }
            else if(retrievedAttributes.get("givenName") != null)
            {
                String firstName =
                    retrievedAttributes.get("givenName").iterator().next();
                person.setFirstName(firstName);
            }
            else if(retrievedAttributes.get("gn") != null)
            {
                String firstName =
                    retrievedAttributes.get("gn").iterator().next();
                person.setFirstName(firstName);
            }

            /* surname */
            if(retrievedAttributes.get("sn") != null)
            {
                String surname =
                    retrievedAttributes.get("sn").iterator().next();
                person.setSurname(surname);
            }
            else if(retrievedAttributes.get("surname") != null)
            {
                String surname =
                    retrievedAttributes.get("surname").iterator().next();
                person.setSurname(surname);
            }

            /* displayed name */
            if(retrievedAttributes.get("displayName") != null)
            {
                String displayName =
                    retrievedAttributes.get("displayName").iterator().next();
                person.setDisplayName(displayName);
            }
            else if(retrievedAttributes.get("cn") != null)
            {
                String displayName =
                    retrievedAttributes.get("cn").iterator().next();
                person.setDisplayName(displayName);
            }
            else if(retrievedAttributes.get("commonname") != null)
            {
                String displayName =
                    retrievedAttributes.get("commonname").iterator().next();
                person.setDisplayName(displayName);
            }
            if(person.getDisplayName() == null)
            {
                person.setDisplayName("" + person.getFirstName() +
                        person.getSurname());
            }
            //should never happen
            if(person.getDisplayName() == null)
            {
                throw new RuntimeException("display name is null!");
            }

            /* organization */
            if(retrievedAttributes.get("o") != null)
            {
                String organization =
                    retrievedAttributes.get("o").iterator().next();
                person.setOrganization(organization);
            }
            else if(retrievedAttributes.get("organizationName") != null)
            {
                String organization =
                    retrievedAttributes.get("organizationName").iterator()
                    .next();
                person.setOrganization(organization);
            }
            else if(retrievedAttributes.get("company") != null)
            {
                String organization =
                    retrievedAttributes.get("company").iterator().next();
                person.setOrganization(organization);
            }

            /* department */
            if(retrievedAttributes.get("company") != null)
            {
                String department =
                    retrievedAttributes.get("company").iterator().next();
                person.setDepartment(department);
            }
            else if(retrievedAttributes.get("ou") != null)
            {
                String department =
                    retrievedAttributes.get("ou").iterator().next();
                person.setDepartment(department);
            }
            else if(retrievedAttributes.get("orgunit") != null)
            {
                String department =
                    retrievedAttributes.get("orgunit").iterator().next();
                person.setDepartment(department);
            }
            else if(retrievedAttributes.get("organizationalUnitName") != null)
            {
                String department =
                    retrievedAttributes.get("organizationalUnitName").
                        iterator().next();
                person.setDepartment(department);
            }
            else if(retrievedAttributes.get("department") != null)
            {
                String department =
                    retrievedAttributes.get("department").iterator().next();
                person.setDepartment(department);
            }
            else if(retrievedAttributes.get("departmentNumber") != null)
            {
                String department =
                    retrievedAttributes.get("departmentNumber").iterator().
                    next();
                person.setDepartment(department);
            }

            // mail
            List<String> attrs = attributesMap.get("mail");

            for(String attr : attrs)
            {
                if(retrievedAttributes.get(attr) != null)
                {
                    for(String mail : retrievedAttributes.get(attr))
                    {
                        if(!mail.contains("@"))
                        {
                            if(settings.getMailSuffix() != null)
                            {
                                mail += settings.getMailSuffix();
                            }
                            else
                                continue;
                        }
                        person.addMail(mail);
                    }
                }
            }

            // work phone
            attrs = attributesMap.get("workPhone");

            for(String attr : attrs)
            {
                if(retrievedAttributes.get(attr) != null)
                {
                    String phone =
                        retrievedAttributes.get(attr).iterator().next();
                    person.addWorkPhone(phone);
                }
            }

            // mobile phone
            attrs = attributesMap.get("mobilePhone");

            for(String attr : attrs)
            {
                if(retrievedAttributes.get(attr) != null)
                {
                    for(String phone : retrievedAttributes.get(attr))
                    {
                        person.addMobilePhone(phone);
                    }
                }
            }

            // home phone
            attrs = attributesMap.get("homePhone");

            for(String attr : attrs)
            {
                if(retrievedAttributes.get(attr) != null)
                {
                    for(String phone : retrievedAttributes.get(attr))
                    {
                        person.addHomePhone(phone);
                    }
                }
            }

            return person;
        }

    /**
     * Turns LdapDirectoryImpl into a printable object
     * Used for debugging purposes
     *
     * @return a printable string
     *
     */
    @Override
    public String toString()
    {
        return this.settings.getName();
    }

    /**
     * An LdapDirectory is comparable in order to display LdapDirectory(s)
     * in alphabetic order in the UI.
     *
     * @see java.lang.Comparable
     */
    public int compareTo(LdapDirectory server)
    {
        return this.settings.getName().compareTo(server.getSettings().
                getName());
    }

    /**
     * Two LdapDirectory(s) with the same displayed name
     * should not exist in the same LdapDirectorySet,
     * thus this function
     */
    @Override
    public boolean equals(Object anObject)
    {
        if(anObject instanceof LdapDirectory)
        {
            LdapDirectory anLdapDirectory = (LdapDirectory) anObject;
            return this.settings.getName().equals(anLdapDirectory.getSettings().
                    getName());
        }
        return false;
    }

    /**
     * We override the equals method so we also do for
     * hashCode to keep consistent behavior
     */
    @Override
    public int hashCode()
    {
        return this.settings.getName().hashCode();
    }

    /**
     * Used to check method input parameters
     *
     * @return wether the text is not empty
     */
    private boolean textHasContent(String aText)
    {
        String EMPTY_STRING = "";
        return (aText != null) && (!aText.trim().equals(EMPTY_STRING));
    }

    /**
     * Builds an LDAP search filter, base on the query string entered
     * e.g. (&(|(mail=*)(telephoneNumber=*))(|(cn=*query*)(sn=*query*)(givenname=*query*)))
     *
     * @return an LDAP search filter
     */
    private String buildSearchFilter(String query)
    {
        StringBuffer searchFilter = new StringBuffer();

        /*
        searchFilter.append("(&(|");

        for(String attribute : retrievableAttributes)
        {
            searchFilter.append("(");
            searchFilter.append(attribute);
            searchFilter.append("=*)");
        }

        searchFilter.append(")(|");
        */
        searchFilter.append("(|");

        /* cn=*query* OR sn=*query* OR ... */
        for(String attribute : searchableAttrs)
        {
            searchFilter.append("(");
            searchFilter.append(attribute);
            searchFilter.append("=");
            searchFilter.append(query);
            searchFilter.append(")");
        }

        //searchFilter.append("))");
        searchFilter.append(")");
        return searchFilter.toString();
    }

    /**
     * search the children nodes of the given dn
     *
     * @param dn the distinguished name of the node to search for children
     *
     * @see net.java.sip.communicator.service.ldap.LdapDirectory#searchChildren
     */
    public Collection<String> searchChildren(final String dn)
    {
        final Vector<String> nodes = new Vector<String>();
        InitialDirContext dirContext = null;

        if(dn.equals(""))
        {
            String[] returningAttributes = { "namingContexts" };
            /* use our custom search control */
            final SearchControls searchCtl = new SearchControls();
            searchCtl.setSearchScope(SearchControls.OBJECT_SCOPE);
            searchCtl.setReturningAttributes(returningAttributes);
            searchCtl.setTimeLimit(5000);

            logger.trace("starting search...");
            try
            {
                dirContext = connect();
                NamingEnumeration<?> result = dirContext.search(
                        dn, "(objectClass=*)", searchCtl);
                while (result.hasMore())
                {
                    SearchResult searchResult = (SearchResult) result.next();
                    Attributes attributes = searchResult.getAttributes();
                    Attribute attribute = attributes.get("namingContexts");
                    NamingEnumeration<?> values = attribute.getAll();
                    while (values.hasMore())
                    {
                        nodes.add((String) values.next());
                    }
                }
            }
            catch (NamingException e)
            {
                logger.trace("error when performing ldap search query" + e);
            }
            finally
            {
                if(dirContext != null)
                    disconnect(dirContext);
            }
        }
        else
        {
            /* use our custom search control */
            final SearchControls searchCtl = new SearchControls();
            searchCtl.setSearchScope(SearchControls.ONELEVEL_SCOPE);

            logger.trace("starting search...");
            try
            {
                dirContext = connect();
                NamingEnumeration<?> result = dirContext.search(
                        dn, "(objectClass=*)", searchCtl);
                while (result.hasMore())
                {
                    SearchResult sr = (SearchResult) result.next();
                    nodes.add(sr.getName());
                    logger.trace(sr.getName());
                }
            }
            catch (NamingException e)
            {
                logger.trace("error when performing ldap search query" + e);
                e.printStackTrace();
            }
            finally
            {
                if(dirContext != null)
                    disconnect(dirContext);
            }
        }

        return nodes;
    }

    /**
     * Tries to fetch the photo of the person with
     * the given distinguished name in the directory
     *
     * @param dn distinguished name of the person to fetch the photo
     * @return the bytes of the photo
     */
    byte[] fetchPhotoForPerson(String dn)
    {
        byte[] photo = null;
        InitialDirContext dirContext = null;

        /* use our custom search control */

        final SearchControls searchCtl = new SearchControls();
        searchCtl.setSearchScope(SearchControls.OBJECT_SCOPE);
        String[] returningAttributes = { PHOTO_ATTRIBUTE };
        searchCtl.setReturningAttributes(returningAttributes);

        logger.trace("starting photo retrieval...");
        try
        {
            dirContext = connect();
            String newBaseDN;
            if(settings.getBaseDN().equals(""))
                newBaseDN = dn;
            else
                newBaseDN = dn + "," + this.settings.getBaseDN();
            NamingEnumeration<?> result = dirContext.search(
                    newBaseDN, "(objectClass=*)", searchCtl);
            if(result.hasMore())
            {
                SearchResult searchResult = (SearchResult) result.next();
                Attributes attributes = searchResult.getAttributes();
                Attribute attribute = attributes.get(PHOTO_ATTRIBUTE);
                if(attribute != null)
                {
                    NamingEnumeration<?> values = attribute.getAll();
                    if(values.hasMore())
                    {
                        photo = (byte[]) values.next();
                    }
                }
            }
        }
        catch (NamingException e)
        {
            logger.trace("error when performing photo retrieval" + e);
            e.printStackTrace();
        }
        finally
        {
            if(dirContext != null)
                disconnect(dirContext);
        }

        return photo;
    }

    private SearchControls buildSearchControls(LdapSearchSettings
            searchSettings)
    {
        SearchControls searchControls = new SearchControls();

        if(searchSettings.isMaxResultsSet())
        {
            // take value from searchSettings
            searchControls.setCountLimit(searchSettings.getMaxResults());
        }
        else
        {
            // take default from SearchControls: no limit
        }

        if(searchSettings.isScopeSet())
        {
            // take value from searchSettings
            searchControls.setSearchScope(
                    searchSettings.getScope().getConstant()
                    );
        }
        else
        {
            //take default from directory
            searchControls.setSearchScope(
                    this.getSettings().getScope().getConstant()
                    );
        }

        List<String> retrievableAttrs = new ArrayList<String>();

        retrievableAttrs.addAll(retrievableAttributes);
        for(String key : attributesMap.keySet())
        {
            List<String> attrs = attributesMap.get(key);
            for(String attr : attrs)
            {
                retrievableAttrs.add(attr);
            }
        }

        searchControls.setReturningAttributes(retrievableAttrs.toArray(
                new String[0]));

        return searchControls;
    }

    /**
     * Required by LdapListener.
     *
     * Dispatches event received from LdapDirectory-s to
     * real search initiators (the search dialog for example)
     *
     * @param event An LdapEvent probably sent by an LdapDirectory
     */
    public synchronized void ldapEventReceived(LdapEvent event)
    {
        LdapQuery query;
        switch(event.getCause())
        {
        case NEW_SEARCH_RESULT:
            LdapPersonFound result = (LdapPersonFound) event.getContent();
            query = result.getQuery();
            if(this.pendingSearches.get(query) != null)
            {
                this.fireLdapEvent(event,
                        pendingSearches.get(query).getCaller());
                logger.trace("result event for query \"" +
                        result.getQuery().toString() + "\" forwaded");
            }
            break;
        case SEARCH_ERROR:
        case SEARCH_AUTH_ERROR:
        case SEARCH_CANCELLED:
        case SEARCH_ACHIEVED:
            query = (LdapQuery) event.getContent();
            if(this.pendingSearches.get(query) != null)
            {
                this.pendingSearches.get(query).getPendingServers().
                remove(event.getSource());
                int sizeLeft = pendingSearches.get(query).
                    getPendingServers().size();
                logger.trace("end event received for initial query \"" +
                        query.toString() + "\" on directory \"" +
                        event.getSource() + "\"\nthere is " + sizeLeft +
                        " search pending for this initial query on directory \"" +
                        event.getSource() + "\"");
                if(sizeLeft == 0)
                {
                    fireLdapEvent(event, pendingSearches.get(query).
                            getCaller());
                    event = new LdapEvent(this,
                            LdapEvent.LdapEventCause.SEARCH_ACHIEVED,
                            query);
                    pendingSearches.remove(query);
                }
            }
            break;
        }
    }

    /**
     * Returns true if Map contains <tt>attribute</tt>.
     *
     * @param attribute attribute to search
     * @return true if Map contains <tt>attribute</tt>
     */
    private boolean containsAttribute(String attribute)
    {
        for(String key : attributesMap.keySet())
        {
            List<String> attrs = attributesMap.get(key);

            if(attrs.contains(attribute))
            {
                return true;
            }
        }
        return false;
    }

    /**
     * Overrides attributes name for searching for a specific type (i.e mail,
     * homePhone, ...).
     *
     * @param attribute name
     * @param names list of attributes name
     */
    public void overrideAttributesSearch(String attribute, List<String> names)
    {
        attributesMap.put(attribute, names);
    }

    /**
     * A custom exception used internally by LdapDirectoryImpl
     * to indicate that a query was cancelled
     *
     * @author Sebastien Mazy
     */
    public class LdapQueryCancelledException extends Exception
    {
        /**
         * Serial version UID.
         */
        private final static long serialVersionUID = 0L;
    }
}