KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > activedirectory > PagedResultTemplate


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.activedirectory;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Collection JavaDoc;
24
25 import javax.naming.NamingEnumeration JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27 import javax.naming.PartialResultException JavaDoc;
28 import javax.naming.directory.SearchControls JavaDoc;
29 import javax.naming.directory.SearchResult JavaDoc;
30 import javax.naming.ldap.Control JavaDoc;
31 import javax.naming.ldap.InitialLdapContext JavaDoc;
32 import javax.naming.ldap.PagedResultsControl JavaDoc;
33 import javax.naming.ldap.PagedResultsResponseControl JavaDoc;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 final class PagedResultTemplate {
39     private static final Log logger = LogFactory.getLog(PagedResultTemplate.class);
40     private final Collection JavaDoc<String JavaDoc> includedOuBasesList;
41     private final Collection JavaDoc<String JavaDoc> excludedOuBasesList;
42     private final Collection JavaDoc<String JavaDoc> ouSearchBase;
43     private final int pageSize;
44
45     PagedResultTemplate(Collection JavaDoc<String JavaDoc> includedOuBasesList, Collection JavaDoc<String JavaDoc> excludedOuBasesList, Collection JavaDoc<String JavaDoc> ouSearchBase, int pageSize) {
46         this.includedOuBasesList = includedOuBasesList;
47         this.excludedOuBasesList = excludedOuBasesList;
48         this.ouSearchBase = ouSearchBase;
49         this.pageSize = pageSize;
50     }
51
52     /**
53      * @param context
54      * @param filter
55      * @param attributes
56      * @param mapper
57      * @throws Exception
58      */

59     void search(InitialLdapContext JavaDoc context, String JavaDoc filter, String JavaDoc[] attributes, PagedResultMapper mapper) throws Exception JavaDoc {
60         if (pageSize == 0) {
61             doSearch(context, filter, attributes, mapper);
62         } else {
63             doPagedSearch(context, filter, attributes, mapper);
64         }
65     }
66
67     private void doSearch(InitialLdapContext JavaDoc context, String JavaDoc filter, String JavaDoc[] attributes, PagedResultMapper mapper)
68                     throws NamingException JavaDoc, Exception JavaDoc {
69         SearchControls JavaDoc constraints = new SearchControls JavaDoc();
70         constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
71
72         for (String JavaDoc searchBase : ouSearchBase) {
73             if (logger.isDebugEnabled()) {
74                 logger.debug("Looking for items starting at " + searchBase + " (filter = " + filter + ")");
75             }
76
77             try {
78                 constraints.setReturningAttributes(attributes);
79                 NamingEnumeration JavaDoc results = context.search(searchBase, filter, constraints);
80                 mapResults(mapper, results);
81             } catch (PartialResultException JavaDoc e) {
82                 // ignore
83
} catch (NamingException JavaDoc e) {
84                 mapper.processException(e);
85                 logger.error("Possible configuration error! Did you enter your OUs correctly? [" + searchBase + "]", e);
86             }
87         }
88     }
89
90     private void doPagedSearch(InitialLdapContext JavaDoc context, String JavaDoc filter, String JavaDoc[] attributes, PagedResultMapper mapper)
91                     throws NamingException JavaDoc, Exception JavaDoc {
92         SearchControls JavaDoc constraints = new SearchControls JavaDoc();
93         constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
94         applyControls(context, pageSize);
95
96         for (String JavaDoc searchBase : ouSearchBase) {
97             if (logger.isDebugEnabled()) {
98                 logger.debug("Looking for items starting at " + searchBase + " (filter = " + filter + ")");
99             }
100
101             try {
102                 int currentPage = 1;
103                 int startPosition = 0;
104                 int endPosition = pageSize - 1;
105                 byte[] cookie = null;
106         
107                 do {
108                     String JavaDoc range = startPosition + "-" + endPosition;
109
110                     if (logger.isDebugEnabled()) {
111                         logger.debug("Starting search on page " + currentPage + " " + range);
112                     }
113
114                     constraints.setReturningAttributes(attributes);
115                     NamingEnumeration JavaDoc results = context.search(searchBase, filter, constraints);
116
117                     try {
118                         mapResults(mapper, results);
119                     } catch (PartialResultException JavaDoc pre) {
120                         // We're paging so we dont care and don't log anymore
121
}
122
123                     // Examine the paged results control response
124
Control JavaDoc[] controls = context.getResponseControls();
125                     if (controls != null) {
126                         for (int index = 0; index < controls.length; index++) {
127                             if (controls[index] instanceof PagedResultsResponseControl JavaDoc) {
128                                 PagedResultsResponseControl JavaDoc prrc = (PagedResultsResponseControl JavaDoc) controls[index];
129                                 cookie = prrc.getCookie();
130                             }
131                         }
132                     }
133
134                     applyControls(context, pageSize, cookie);
135                     startPosition = startPosition + pageSize;
136                     endPosition = endPosition + pageSize;
137                     currentPage++;
138                 } while ((cookie != null) && (cookie.length != 0));
139             } catch (NamingException JavaDoc e) {
140                 mapper.processException(e);
141                 logger.error("Possible configuration error! Did you enter your OUs correctly? [" + searchBase + "]", e);
142             }
143         }
144     }
145
146     private void mapResults(PagedResultMapper mapper, NamingEnumeration JavaDoc results) throws NamingException JavaDoc {
147         while (results != null && results.hasMore()) {
148             SearchResult JavaDoc searchResult = (SearchResult JavaDoc) results.next();
149             String JavaDoc dn = searchResult.getNameInNamespace();
150             
151             try {
152                 if (isDnValid(dn)) {
153                     if (logger.isDebugEnabled()) {
154                         logger.debug("Included result " + dn);
155                     }
156                     mapper.mapSearchResult(searchResult);
157                 } else {
158                     if (logger.isDebugEnabled()) {
159                         logger.debug("Excluding result " + dn);
160                     }
161                 }
162             } catch (Exception JavaDoc e) {
163                 mapper.processSearchResultException(searchResult, e);
164             }
165         }
166     }
167
168     boolean isDnValid(String JavaDoc dn) {
169         boolean included = isInOuList(includedOuBasesList, dn);
170         boolean notExcluded = !isInOuList(excludedOuBasesList, dn);
171         return included && notExcluded;
172     }
173
174     private static boolean isInOuList(Collection JavaDoc<String JavaDoc> basesList, String JavaDoc dn) {
175         for (String JavaDoc dnToCheck : basesList) {
176             if (dn.toLowerCase().endsWith(dnToCheck.toLowerCase())) {
177                 return true;
178             }
179         }
180         return false;
181     }
182     
183     private void applyControls(InitialLdapContext JavaDoc context, int pageSize) throws NamingException JavaDoc {
184         try {
185             Control JavaDoc[] control = new Control JavaDoc[] { new PagedResultsControl JavaDoc(pageSize, Control.CRITICAL) };
186             context.setRequestControls(control);
187         } catch (IOException JavaDoc e) {
188             logger.warn("Tried to configure paged search but got error", e);
189         }
190     }
191
192     private void applyControls(InitialLdapContext JavaDoc context, int pageSize, byte[] cookie) throws NamingException JavaDoc {
193         try {
194             context.setRequestControls(new Control JavaDoc[] { new PagedResultsControl JavaDoc(pageSize, cookie, Control.CRITICAL) });
195         } catch (IOException JavaDoc ex) {
196             logger.warn("Tried to reconfigure paged result controls with error", ex);
197         }
198     }
199 }
Free Books   Free Magazines  
Popular Tags