summaryrefslogtreecommitdiff
path: root/src/com/memberwebs/ldapxml/LXSpecs.java
blob: b9bc68d8a82e730b5932fbdb50ec5f1a0ed47819 (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
/*
 * Copyright (c) 2004, Nate Nielsen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above
 *       copyright notice, this list of conditions and the
 *       following disclaimer.
 *     * Redistributions in binary form must reproduce the
 *       above copyright notice, this list of conditions and
 *       the following disclaimer in the documentation and/or
 *       other materials provided with the distribution.
 *     * The names of contributors to this software may not be
 *       used to endorse or promote products derived from this
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 *
 * CONTRIBUTORS
 *  Nate Nielsen <nielsen@memberwebs.com>
 *
 */

package com.memberwebs.ldapxml;

import java.util.Map;

import com.memberwebs.ldapxml.map.*;

/**
 * Additional specifications for retrieving data from an
 * LDAP directory.
 *
 * @author nielsen@memberwebs.com
 * @version 0.5
 */
public class LXSpecs
{
    /**
     * Don't retrieve any sub entries.
     */
    public static final int DEPTH_BASE = 0;

    /**
     * Retrive only direct children of the referenced entry.
     */
    public static final int DEPTH_SINGLE = 1;

    /**
     * Retrieve all sub entries.
     */
    public static final int DEPTH_INFINITE = 0xFFFFFFFF;


    private static final String LANG = "lang-";
	private static final String FILTER_ALL = "(objectClass=*)";

    /**
     * Construct a new LXSpecs object.
     */
    public LXSpecs()
    {
        m_filter = FILTER_ALL;
        m_depth = DEPTH_BASE;
        m_sort = null;
        m_data = null;
        m_language = null;
        m_start = 0;
        m_limit = Integer.MAX_VALUE;
    }

    public final int getStart()
    {
        return m_start;
    }

    public final int getLimit()
    {
        return m_limit;
    }

    public final void setStart(int start)
    {
        m_start = start;
    }

    public final void setLimit(int limit)
    {
        m_limit = limit;
    }

    /**
     * Get preferred language.
     *
     * @return The language or null if none set.
     */
    public final String getLanguage()
    {
        return m_language;
    }

    /**
     * Set preferred language for LDAP retrieval.
     *
     * @param language The language or null for no preference.
     */
    public final void setLanguage(String language)
    {
        m_language = language;

        if(m_language != null && !m_language.startsWith(LANG))
            m_language = LANG + m_language;
    }

    /**
     * Returns the depth to which entries are recursively
     * retrieved from the LDAP directory.
     *
     * @return The depth.
     */
    public final int getDepth()
    {
        return m_depth;
    }

    /**
     * Sets the depth to which entries are recursively
     * retrieved from the LDAP directory.
     *
     * @return The depth.
     */
    public final void setDepth(int depth)
    {
        m_depth = depth;
    }

    /**
     * Gets the additional filter when searching for entries.
     *
     * @return The filter.
     */
    public final String getFilter()
    {
        return m_filter == null ? FILTER_ALL : m_filter;
    }

    /**
     * Gets the filter when searching for entries, combining it
     * with another filter.
     *
     * @param prevFilter The other filter.
     * @return The combined filter.
     */
    public final String getFilter(String prevFilter)
    {
        String filter = combineFilters(m_filter, prevFilter);
        return filter == null ? FILTER_ALL : filter;
    }

    /**
     * Set an additional filter used when searching for entries.
     *
     * @param filter The filter.
     */
    public final void setFilter(String filter)
    {
        m_filter = combineFilters(filter, null);
    }

    /**
     * Get the requested sort order.
     *
     * @return The sort order, or null if none present.
     */
    public final String[] getSort()
    {
        if(m_sort != null && m_sort.length > 0)
            return m_sort;
        else
            return null;
    }

    /**
     * Get the sort directions (ascending, descending) for the
     * sort order.
     *
     * @return An array of sort directions.
     */
    public final boolean[] getSortDirection()
    {
        if(m_direc != null && m_direc.length > 0)
            return m_direc;
        else
            return null;
    }

    /**
     * Set the sort order. Attribute names prefixed with '-' are
     * treated as descending.
     *
     * @param sort The sort order.
     */
    public final void setSort(String[] sort)
    {
        m_mappedSort = null;

        m_sort = new String[sort.length];
        m_direc = new boolean[sort.length];

        for(int i = 0; i < sort.length; i++)
        {
            boolean desc = sort[i].startsWith("-");
            if(desc)
                m_sort[i] = sort[i].substring(1);
            else
                m_sort[i] = sort[i];

            m_direc[i] = !desc;
        }
    }

    public final Object getData()
    {
        return m_data;
    }

    public final void setData(Object data)
    {
        m_data = data;
    }

    /**
     * Translate the sort order with mapped LDAP attribute
     * names retrieved from the map.
     *
     * @param map The LX Map to get the attribute names from.
     * @return The translated sort order.
     */
    protected final String[] getMappedSort(LXMap map)
    {
        if(m_mappedSort == null)
        {
            if(m_sort != null && m_sort.length > 0)
            {
                m_mappedSort = new String[m_sort.length];

                Map nameMap = map.getNameMap();
                for(int i = 0; i < m_sort.length; i++)
                {
                    String name = (String)nameMap.get(m_sort[i]);
                    m_mappedSort[i] = name == null ? m_sort[i] : name;
                }
            }
        }
        return m_mappedSort;
    }

	/**
	 * Combine two LDAP filters in an AND operation.
	 *
	 * @param filter1 The first filter to combine.
	 * @param filter2 The second filter.
	 * @return The combined filter.
	 */
	protected static String combineFilters(String filter1, String filter2)
	{
		return combineFilters(filter1, filter2, '&');
	}

	/**
	 * Combine two LDAP filters in a boolean operation.
	 *
	 * @param filter1 The first filter to combine.
	 * @param filter2 The second filter.
	 * @param op The boolean operation (ie: '&' or '|' etc...)
	 * @return The combined filter.
	 */
	protected static String combineFilters(String filter1, String filter2, char op)
	{
		String filter = null;

		filter1 = cleanFilter(filter1);
		filter2 = cleanFilter(filter2);

		if(filter1 != null)
			filter = filter1;

		if(filter2 != null)
		{
			if(filter == null)
				filter = filter2;
			else
				filter = "("  + op + filter + filter2 + ")";
		}

		return filter;
	}

	/**
	 * Clean up an LDAP filter, substituting null for an empty,
	 * or all encompasing filter. Also wraps filters in parens
	 * as need.
	 *
	 * @param filter The filter to check.
	 * @return The new filter or null.
	 */
	protected static String cleanFilter(String filter)
	{
		if(filter != null)
		{
			if(!filter.startsWith("("))
				filter = "(" + filter + ")";

			if(filter.equals(""))
				filter = null;
			else if(filter.equals(FILTER_ALL))
				filter = null;
		}

		return filter;
	}

    // The additional filter
    private String m_filter;

    // The depth to which to recursively retrieve sub entries
    private int m_depth;

    // The starting point to retrieve entries
    private int m_start;
    // The number of entries to retrieve
    private int m_limit;

    // The sort order and direction
    private String[] m_sort;
    private String[] m_mappedSort;
    private boolean[] m_direc;

    // The preferred language
    private String m_language;

    // Data for hooks
    private Object m_data;
}