| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 1999 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap.client.opers; |
| 39 | |
| 40 | import org.ietf.ldap.ber.stream.BERBoolean; |
| 41 | import org.ietf.ldap.ber.stream.BERElement; |
| 42 | import org.ietf.ldap.ber.stream.BEREnumerated; |
| 43 | import org.ietf.ldap.ber.stream.BERInteger; |
| 44 | import org.ietf.ldap.ber.stream.BEROctetString; |
| 45 | import org.ietf.ldap.ber.stream.BERSequence; |
| 46 | import org.ietf.ldap.ber.stream.BERTag; |
| 47 | import org.ietf.ldap.client.JDAPFilter; |
| 48 | import org.ietf.ldap.client.JDAPFilterOpers; |
| 49 | |
| 50 | /** |
| 51 | * This class implements the search request. This object is |
| 52 | * sent to the ldap server. |
| 53 | * <pre> |
| 54 | * SearchRequest ::= [APPLICATION 3] SEQUENCE { |
| 55 | * baseObject LDAPDN, |
| 56 | * scope ENUMERATED { |
| 57 | * baseObject (0), |
| 58 | * singleLevel (1), |
| 59 | * wholeSubtree (2) |
| 60 | * }, |
| 61 | * derefAliases ENUMERATED { |
| 62 | * neverDerefAliases (0), |
| 63 | * derefInSearching (1), |
| 64 | * DerefFindingBaseObj (2), |
| 65 | * DerefAlways (3) |
| 66 | * }, |
| 67 | * sizeLimit INTEGER(0..maxInt), |
| 68 | * timeLimit INTEGER(0..maxInt), |
| 69 | * attrsOnly BOOLEAN, |
| 70 | * filter Filter, |
| 71 | * attributes SEQUENCE OF AttributeType |
| 72 | * } |
| 73 | * </pre> |
| 74 | * |
| 75 | * @version 1.0 |
| 76 | */ |
| 77 | public class JDAPSearchRequest extends JDAPBaseDNRequest |
| 78 | implements JDAPProtocolOp { |
| 79 | /** |
| 80 | * search scope |
| 81 | */ |
| 82 | public final static int BASE_OBJECT = 0; |
| 83 | public final static int SINGLE_LEVEL = 1; |
| 84 | public final static int WHOLE_SUBTREE = 2; |
| 85 | /** |
| 86 | * alias dereference |
| 87 | */ |
| 88 | public final static int NEVER_DEREF_ALIASES = 0; |
| 89 | public final static int DEREF_IN_SEARCHING = 1; |
| 90 | public final static int DEREF_FINDING_BASE_OBJ = 2; |
| 91 | public final static int DEREF_ALWAYS = 3; |
| 92 | |
| 93 | public final static String DEFAULT_FILTER = "(objectclass=*)"; |
| 94 | /** |
| 95 | * Private variables |
| 96 | */ |
| 97 | protected String m_base_dn = null; |
| 98 | protected int m_scope; |
| 99 | protected int m_deref; |
| 100 | protected int m_size_limit; |
| 101 | protected int m_time_limit; |
| 102 | protected boolean m_attrs_only; |
| 103 | protected String m_filter = null; |
| 104 | protected JDAPFilter m_parsedFilter = null; |
| 105 | protected String m_attrs[] = null; |
| 106 | |
| 107 | /** |
| 108 | * Constructs search request. |
| 109 | * @param base_dn base object entry relative to the search |
| 110 | * @param scope scope of the search |
| 111 | * @param deref how alias objects should be handled |
| 112 | * @param size_limit maximum number of entries |
| 113 | * @param time_limit maximum time (in time) allowed |
| 114 | * @param attrs_only should return type only |
| 115 | * @param filter string filter based on RFC1558 |
| 116 | * @param attrs list of attribute types |
| 117 | * @exception IllegalArgumentException if the filter has bad syntax |
| 118 | * |
| 119 | */ |
| 120 | public JDAPSearchRequest(String base_dn, int scope, int deref, |
| 121 | int size_limit, int time_limit, boolean attrs_only, |
| 122 | String filter, String attrs[]) throws IllegalArgumentException { |
| 123 | m_base_dn = base_dn; |
| 124 | m_scope = scope; |
| 125 | m_deref = deref; |
| 126 | m_size_limit = size_limit; |
| 127 | m_time_limit = time_limit; |
| 128 | m_attrs_only = attrs_only; |
| 129 | m_filter = (filter == null) ? DEFAULT_FILTER : filter; |
| 130 | m_parsedFilter = JDAPFilter.getFilter( |
| 131 | JDAPFilterOpers.convertLDAPv2Escape(m_filter)); |
| 132 | if (m_parsedFilter == null){ |
| 133 | throw new IllegalArgumentException("Bad search filter"); |
| 134 | } |
| 135 | m_attrs = attrs; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Retrieves the protocol operation type. |
| 140 | * @return operation type |
| 141 | */ |
| 142 | public int getType() { |
| 143 | return JDAPProtocolOp.SEARCH_REQUEST; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Sets the base dn component. |
| 148 | * @param basedn base dn |
| 149 | */ |
| 150 | public void setBaseDN(String basedn) { |
| 151 | m_base_dn = basedn; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Gets the base dn component. |
| 156 | * @return base dn |
| 157 | */ |
| 158 | public String getBaseDN() { |
| 159 | return m_base_dn; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Gets the ber representation of search request. |
| 164 | * @return ber representation of request. |
| 165 | */ |
| 166 | public BERElement getBERElement() { |
| 167 | /* Assumed that searching with the following criteria: |
| 168 | * base object = "c=ca" |
| 169 | * filter = FilterPresent(objectClass) |
| 170 | * [*] zoomit server v1.0 (search on c=ca) |
| 171 | * 0x63 0x81 0x8d ([APPLICATION 3]) |
| 172 | * 0x30 0x81 0x8a 0x04 0x00 (seq of) |
| 173 | * 0x0a 0x01 0x02 |
| 174 | * 0x0a 0x01 0x00 |
| 175 | * 0x02 0x01 0x65 |
| 176 | * 0x02 0x01 0x1e |
| 177 | * 0x01 0x01 0x00 |
| 178 | * 0xa0 0x3b |
| 179 | * 0x30 0x39 0xa5 0x0d |
| 180 | * 0x30 0x0b 0x04 0x07 P A G E _ D N |
| 181 | * 0x04 0x00 |
| 182 | * 0xa3 0x0e |
| 183 | * 0x30 0x0c 0x04 0x08 P A G E _ K E Y |
| 184 | * 0x04 0x00 0xa3 0x18 |
| 185 | * 0x30 0x16 0x04 0x0e z c A n y A t t r i b u t e |
| 186 | * 0x04 0x04 c = c a |
| 187 | * 0x30 0x3a 0x04 0x0b O b j e c t C l a s s |
| 188 | * 0x04 0x11 a l i a s e d O b j e c t N a m e |
| 189 | * 0x04 0x07 d s e T y p e |
| 190 | * 0x04 0x0f t e l e p h o n e N u m b e r |
| 191 | * [*] umich-ldap-v3.3: |
| 192 | * 0x63 0x24 ([APPLICATION 3]) |
| 193 | * 0x04 0x04 c = c a (base object - OctetString) |
| 194 | * 0x0a 0x01 0x00 (scope - Enumerated) |
| 195 | * 0x0a 0x01 0x00 (derefAlias - Enumerated) |
| 196 | * 0x02 0x01 0x00 (size limit - Integer) |
| 197 | * 0x02 0x01 0x00 (time limit - Integer) |
| 198 | * 0x01 0x01 0x00 (attr only - Boolean) |
| 199 | * 0x87 0x0b o b j e c t C l a s s (filter) |
| 200 | * 0x30 0x00 (attrs - Sequence of OctetString) |
| 201 | */ |
| 202 | BERSequence seq = new BERSequence(); |
| 203 | seq.addElement(new BEROctetString(m_base_dn)); |
| 204 | seq.addElement(new BEREnumerated(m_scope)); |
| 205 | seq.addElement(new BEREnumerated(m_deref)); |
| 206 | seq.addElement(new BERInteger(m_size_limit)); |
| 207 | seq.addElement(new BERInteger(m_time_limit)); |
| 208 | seq.addElement(new BERBoolean(m_attrs_only)); |
| 209 | seq.addElement(m_parsedFilter.getBERElement()); |
| 210 | BERSequence attr_type_list = new BERSequence(); |
| 211 | if (m_attrs != null) { |
| 212 | for (int i = 0; i < m_attrs.length; i++) { |
| 213 | attr_type_list.addElement(new BEROctetString(m_attrs[i])); |
| 214 | } |
| 215 | } |
| 216 | seq.addElement(attr_type_list); |
| 217 | BERTag element = new BERTag(BERTag.APPLICATION|BERTag.CONSTRUCTED|3, |
| 218 | seq, true); |
| 219 | return element; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Retrieves the string representation of the request. |
| 224 | * @return string representation |
| 225 | */ |
| 226 | public String toString() { |
| 227 | String s = null; |
| 228 | if (m_attrs != null) { |
| 229 | s = ""; |
| 230 | for (int i = 0; i < m_attrs.length; i++) { |
| 231 | if (i != 0) |
| 232 | s = s + "+"; |
| 233 | s = s + m_attrs[i]; |
| 234 | } |
| 235 | } |
| 236 | return "SearchRequest {baseObject=" + m_base_dn + ", scope=" + |
| 237 | m_scope + ", derefAliases=" + m_deref + ",sizeLimit=" + |
| 238 | m_size_limit + ", timeLimit=" + m_time_limit + ", attrsOnly=" + |
| 239 | m_attrs_only + ", filter=" + m_filter + ", attributes=" + |
| 240 | s + "}"; |
| 241 | } |
| 242 | } |