| 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.controls; |
| 39 | |
| 40 | import java.io.ByteArrayInputStream; |
| 41 | import java.io.IOException; |
| 42 | import java.io.UnsupportedEncodingException; |
| 43 | |
| 44 | import org.ietf.ldap.LDAPControl; |
| 45 | import org.ietf.ldap.LDAPException; |
| 46 | import org.ietf.ldap.LDAPSortKey; |
| 47 | import org.ietf.ldap.ber.stream.BERElement; |
| 48 | import org.ietf.ldap.ber.stream.BEREnumerated; |
| 49 | import org.ietf.ldap.ber.stream.BEROctetString; |
| 50 | import org.ietf.ldap.ber.stream.BERSequence; |
| 51 | import org.ietf.ldap.ber.stream.BERTag; |
| 52 | import org.ietf.ldap.client.JDAPBERTagDecoder; |
| 53 | |
| 54 | /** |
| 55 | * Represents an LDAP v3 server control that specifies that you want |
| 56 | * the server to return sorted search results. (The OID for this |
| 57 | * control is 1.2.840.113556.1.4.473.) |
| 58 | * <P> |
| 59 | * |
| 60 | * When constructing an <CODE>LDAPSortControl</CODE> object, you can |
| 61 | * specify the order in which you want the results sorted. |
| 62 | * You can also specify whether or not this control is critical |
| 63 | * to the search operation. |
| 64 | * <P> |
| 65 | * |
| 66 | * To specify the sort order, you construct an <CODE>LDAPSortKey</CODE> |
| 67 | * object and pass it to the <CODE>LDAPSortControl</CODE> constructor. |
| 68 | * The <CODE>LDAPSortKey</CODE> object represents a list of the attribute |
| 69 | * types used for sorting (a "sort key list"). |
| 70 | * <P> |
| 71 | * |
| 72 | * You can include the control in a search request by constructing |
| 73 | * an <CODE>LDAPSearchConstraints</CODE> object and calling the |
| 74 | * <CODE>setServerControls</CODE> method. You can then pass this |
| 75 | * <CODE>LDAPSearchConstraints</CODE> object to the <CODE>search</CODE> |
| 76 | * method of an <CODE>LDAPConnection</CODE> object. |
| 77 | * <P> |
| 78 | * |
| 79 | * For example: |
| 80 | * <PRE> |
| 81 | * ... |
| 82 | * LDAPConnection ld = new LDAPConnection(); |
| 83 | * try { |
| 84 | * // Connect to server. |
| 85 | * ld.connect( 3, hostname, portnumber, "", "" ); |
| 86 | * |
| 87 | * // Create a sort key that specifies the sort order. |
| 88 | * LDAPSortKey sortOrder = new LDAPSortKey( attrname ); |
| 89 | * |
| 90 | * // Create a "critical" server control using that sort key. |
| 91 | * LDAPSortControl sortCtrl = new LDAPSortControl(sortOrder,true); |
| 92 | * |
| 93 | * // Create search constraints to use that control. |
| 94 | * LDAPSearchConstraints cons = new LDAPSearchConstraints(); |
| 95 | * cons.setServerControls( sortCtrl ); |
| 96 | * |
| 97 | * // Send the search request. |
| 98 | * LDAPSearchResults res = ld.search( "o=Airius.com", |
| 99 | * LDAPConnection.SCOPE_SUB, "(cn=Barbara*)", null, false, cons ); |
| 100 | * |
| 101 | * ... |
| 102 | * |
| 103 | * </PRE> |
| 104 | * |
| 105 | * The LDAP server sends back a sort response control to indicate |
| 106 | * the result of the sorting operation. (The OID for this control |
| 107 | * is 1.2.840.113556.1.4.474.) |
| 108 | * <P> |
| 109 | * |
| 110 | * This control contains: |
| 111 | * <P> |
| 112 | * |
| 113 | * <UL> |
| 114 | * <LI>the result code from the sorting operation |
| 115 | * <LI>optionally, the first attribute type in the sort key list |
| 116 | * that resulted in an error (for example, if the attribute does |
| 117 | * not exist) |
| 118 | * </UL> |
| 119 | * <P> |
| 120 | * |
| 121 | * To retrieve the data from this control, use the <CODE>getResultCode</CODE> |
| 122 | * and <CODE>getFailedAttribute</CODE> methods. |
| 123 | * <P> |
| 124 | * |
| 125 | * The following table lists what kinds of results to expect from the |
| 126 | * LDAP server under different situations: |
| 127 | * <P> |
| 128 | * |
| 129 | * <TABLE BORDER=1 COLS=4> |
| 130 | * <TR VALIGN=BASELINE> |
| 131 | * <TH>Does the Server Support the Sorting Control?</TH> |
| 132 | * <TH>Is the Sorting Control Marked As Critical?</TH> |
| 133 | * <TH>Other Conditions</TH> |
| 134 | * <TH>Results from LDAP Server</TH> |
| 135 | * </TR> |
| 136 | * <TR VALIGN=BASELINE> |
| 137 | * <TD ROWSPAN=2> |
| 138 | * No |
| 139 | * </TD> |
| 140 | * <TD> |
| 141 | * Yes |
| 142 | * </TD> |
| 143 | * <TD> |
| 144 | * None |
| 145 | * </TD> |
| 146 | * <TD> |
| 147 | * <UL> |
| 148 | * <LI>The server does not send back any entries. |
| 149 | * <LI>An LDAPException.UNAVAILABLE_CRITICAL_EXTENSION |
| 150 | * exception is thrown. |
| 151 | * </UL> |
| 152 | * </TD> |
| 153 | * </TR> |
| 154 | * <TR VALIGN=BASELINE> |
| 155 | * <TD> |
| 156 | * No |
| 157 | * </TD> |
| 158 | * <TD> |
| 159 | * None |
| 160 | * </TD> |
| 161 | * <TD> |
| 162 | * <UL> |
| 163 | * <LI>The server ignores the sorting control and |
| 164 | * returns the entries unsorted. |
| 165 | * </UL> |
| 166 | * <P> |
| 167 | * </TD> |
| 168 | * </TR> |
| 169 | * <TR VALIGN=BASELINE> |
| 170 | * <TD ROWSPAN=4> |
| 171 | * Yes |
| 172 | * </TD> |
| 173 | * <TD> |
| 174 | * Yes |
| 175 | * </TD> |
| 176 | * <TD ROWSPAN=2> |
| 177 | * The server cannot sort the results using the specified |
| 178 | * sort key list. |
| 179 | * </TD> |
| 180 | * <TD> |
| 181 | * <UL> |
| 182 | * <LI>The server does not send back any entries. |
| 183 | * <LI>An LDAPException.UNAVAILABLE_CRITICAL_EXTENSION |
| 184 | * exception is thrown. |
| 185 | * <LI>The server sends back the sorting response control, which |
| 186 | * specifies the result code of the sort attempt and (optionally) |
| 187 | * the attribute type that caused the error. |
| 188 | * </UL> |
| 189 | * </TD> |
| 190 | * </TR> |
| 191 | * <TR VALIGN=BASELINE> |
| 192 | * <TD> |
| 193 | * No |
| 194 | * </TD> |
| 195 | * <TD> |
| 196 | * <UL> |
| 197 | * <LI>The server returns the entries unsorted. |
| 198 | * <LI>The server sends back the sorting response control, which |
| 199 | * specifies the result code of the sort attempt and (optionally) |
| 200 | * the attribute type that caused the error. |
| 201 | * </UL> |
| 202 | * </TD> |
| 203 | * </TR> |
| 204 | * <TR VALIGN=BASELINE> |
| 205 | * <TD ROWSPAN=2> |
| 206 | * N/A (could either be marked as critical or not) |
| 207 | * </TD> |
| 208 | * <TD> |
| 209 | * The server successfully sorted the entries. |
| 210 | * </TD> |
| 211 | * <TD> |
| 212 | * <UL> |
| 213 | * <LI>The server sends back the sorted entries. |
| 214 | * <LI>The server sends back the sorting response control, which |
| 215 | * specifies the result code of the sort attempt |
| 216 | * (LDAPException.SUCCESS). |
| 217 | * </UL> |
| 218 | * </TD> |
| 219 | * </TR> |
| 220 | * <TR VALIGN=BASELINE> |
| 221 | * <TD> |
| 222 | * The search itself failed (for any reason). |
| 223 | * </TD> |
| 224 | * <TD> |
| 225 | * <UL> |
| 226 | * <LI>The server sends back a result code for the search |
| 227 | * operation. |
| 228 | * <LI>The server does not send back the sorting response control. |
| 229 | * </UL> |
| 230 | * </TD> |
| 231 | * </TR> |
| 232 | * </TABLE> |
| 233 | * <P> |
| 234 | * |
| 235 | * @version 1.0 |
| 236 | * @see org.ietf.ldap.LDAPSortKey |
| 237 | * @see org.ietf.ldap.LDAPControl |
| 238 | * @see org.ietf.ldap.LDAPConstraints |
| 239 | * @see org.ietf.ldap.LDAPSearchConstraints |
| 240 | * @see org.ietf.ldap.LDAPConstraints#setControls(LDAPControl) |
| 241 | */ |
| 242 | public class LDAPSortControl extends LDAPControl { |
| 243 | public final static String SORTREQUEST = "1.2.840.113556.1.4.473"; |
| 244 | public final static String SORTRESPONSE = "1.2.840.113556.1.4.474"; |
| 245 | |
| 246 | // Response varibales |
| 247 | private String _failedAttribute = null; |
| 248 | private int _resultCode = 0; |
| 249 | |
| 250 | // Request variables |
| 251 | private LDAPSortKey[] _keys; |
| 252 | |
| 253 | /** |
| 254 | * Constructs a sort response <CODE>LDAPSortControl</CODE> object. |
| 255 | * This constructor is used by <CODE>LDAPControl.register</CODE> to |
| 256 | * instantiate sort response controls. |
| 257 | * <P> |
| 258 | * To retrieve the result code of the sort operation, call |
| 259 | * <CODE>getResultCode</CODE>. |
| 260 | * <P> |
| 261 | * To retrieve the attribute that caused the sort operation to fail, call |
| 262 | * <CODE>getFailedAttribute</CODE>. |
| 263 | * @param oid the oid for this control. This must be |
| 264 | * <CODE>LDAPSortControl.SORTRESPONSE</CODE> or an <CODE>LDAPException</CODE> |
| 265 | * is thrown. |
| 266 | * @param critical <code>true</code> if this control is critical to the operation |
| 267 | * @param value the value associated with this control |
| 268 | * @exception org.ietf.ldap.LDAPException If oid is not |
| 269 | * <CODE>LDAPSortControl.SORTRESPONSE</CODE>. |
| 270 | * @exception java.io.IOException If value contains an invalid BER sequence. |
| 271 | * @see org.ietf.ldap.LDAPControl#register |
| 272 | */ |
| 273 | public LDAPSortControl( String oid, boolean critical, byte[] value ) |
| 274 | throws LDAPException, IOException { |
| 275 | super( oid, critical, value ); |
| 276 | |
| 277 | if ( !oid.equals( SORTRESPONSE )) { |
| 278 | throw new LDAPException( "oid must be LDAPSortControl.SORTRESPONSE", |
| 279 | LDAPException.PARAM_ERROR); |
| 280 | } |
| 281 | |
| 282 | ByteArrayInputStream inStream = new ByteArrayInputStream( value ); |
| 283 | BERSequence ber = new BERSequence(); |
| 284 | JDAPBERTagDecoder decoder = new JDAPBERTagDecoder(); |
| 285 | int[] nRead = { 0 }; |
| 286 | |
| 287 | /* A sequence */ |
| 288 | BERSequence seq = (BERSequence)BERElement.getElement(decoder, inStream, |
| 289 | nRead ); |
| 290 | /* First is result code */ |
| 291 | _resultCode = ((BEREnumerated)seq.elementAt( 0 )).getValue(); |
| 292 | /* Then, possibly an attribute that failed sorting */ |
| 293 | if(seq.size() == 1) { |
| 294 | return; |
| 295 | } |
| 296 | /* If this is not an octet string, let there be an exception */ |
| 297 | BEROctetString t = (BEROctetString)seq.elementAt(1); |
| 298 | try { |
| 299 | _failedAttribute = new String(t.getValue(), "UTF8"); |
| 300 | } catch (UnsupportedEncodingException e) { |
| 301 | } |
| 302 | |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * @return the attribute that caused the sort operation to fail. |
| 307 | */ |
| 308 | public String getFailedAttribute() { |
| 309 | return _failedAttribute; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * @return the result code from the search operation. |
| 314 | */ |
| 315 | public int getResultCode() { |
| 316 | return _resultCode; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Constructs an <CODE>LDAPSortControl</CODE> object with a single |
| 321 | * sorting key. |
| 322 | * @param key a single attribute by which to sort |
| 323 | * @param critical <CODE>true</CODE> if the LDAP operation should be |
| 324 | * discarded when the server does not support this control (in other |
| 325 | * words, this control is critical to the LDAP operation) |
| 326 | * @see org.ietf.ldap.LDAPControl |
| 327 | * @see org.ietf.ldap.LDAPSortKey |
| 328 | */ |
| 329 | public LDAPSortControl(LDAPSortKey key, |
| 330 | boolean critical) { |
| 331 | super( SORTREQUEST, critical, null ); |
| 332 | LDAPSortKey[] keys = new LDAPSortKey[1]; |
| 333 | keys[0] = key; |
| 334 | _value = createSortSpecification( _keys = keys ); |
| 335 | |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Constructs an <CODE>LDAPSortControl</CODE> object with an array of |
| 340 | * sorting keys. |
| 341 | * @param keys the attributes by which to sort |
| 342 | * @param critical <CODE>true</CODE> if the LDAP operation should be |
| 343 | * discarded when the server does not support this control (in other |
| 344 | * words, this control is critical to the LDAP operation) |
| 345 | * @see org.ietf.ldap.LDAPControl |
| 346 | * @see org.ietf.ldap.LDAPSortKey |
| 347 | */ |
| 348 | public LDAPSortControl(LDAPSortKey[] keys, |
| 349 | boolean critical) { |
| 350 | super( SORTREQUEST, critical, null ); |
| 351 | _value = createSortSpecification( _keys = keys ); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Parses the sorting response control sent by the server and |
| 356 | * retrieves the result code from the sorting operation and |
| 357 | * the attribute that caused sorting to fail (if specified by |
| 358 | * the server). |
| 359 | * <P> |
| 360 | * |
| 361 | * You can get the controls returned by the server by using the |
| 362 | * <CODE>getResponseControls</CODE> method of the |
| 363 | * <CODE>LDAPConnection</CODE> class. |
| 364 | * <P> |
| 365 | * |
| 366 | * This method returns the attribute that caused the sort operation |
| 367 | * to fail (or null, if the server did not specify any attribute). |
| 368 | * The result code of the sorting operation is returned in the |
| 369 | * <CODE>results</CODE> argument. This argument is an array of |
| 370 | * integers; the result code is specified in the first element of |
| 371 | * this array. |
| 372 | * <P> |
| 373 | * |
| 374 | * For example: |
| 375 | * <PRE> |
| 376 | * ... |
| 377 | * LDAPConnection ld = new LDAPConnection(); |
| 378 | * try { |
| 379 | * // Connect to the server, set up the sorting control, |
| 380 | * // and set up the search constraints. |
| 381 | * ... |
| 382 | * |
| 383 | * // Search the directory. |
| 384 | * LDAPSearchResults res = ld.search( "o=Airius.com", |
| 385 | * LDAPConnection.SCOPE_SUB, "(cn=Barbara*)", attrs, false, cons ); |
| 386 | * |
| 387 | * // Determine if the server sent a control back to you. |
| 388 | * LDAPControl[] returnedControls = ld.getResponseControls(); |
| 389 | * if ( returnedControls != null ) { |
| 390 | * int[] resultCodes = new int[1]; |
| 391 | * String failedAttr = LDAPSortControl.parseResponse( |
| 392 | * returnedControls, resultCodes ); |
| 393 | * |
| 394 | * // Check if the result code indicated an error occurred. |
| 395 | * if ( resultCodes[0] != 0 ) { |
| 396 | * System.out.println( "Result code: " + resultCodes[0] ); |
| 397 | * if ( failedAttr != null ) { |
| 398 | * System.out.println( "Sorting operation failed on " + |
| 399 | * failedAttr ); |
| 400 | * } else { |
| 401 | * System.out.println( "Server did not indicate which " + |
| 402 | * "attribute caused sorting to fail." ); |
| 403 | * } |
| 404 | * } |
| 405 | * } |
| 406 | * ... |
| 407 | * } |
| 408 | * ... |
| 409 | * |
| 410 | * </PRE> |
| 411 | * |
| 412 | * The following table lists some of the possible result codes |
| 413 | * for the sorting operation. |
| 414 | * <P> |
| 415 | * |
| 416 | * <TABLE BORDER=1 COLS=2> |
| 417 | * <TR VALIGN=BASELINE> |
| 418 | * <TD> |
| 419 | * <CODE>LDAPException.SUCCESS</CODE> |
| 420 | * </TD> |
| 421 | * <TD> |
| 422 | * The server successfully sorted the results. |
| 423 | * </TD> |
| 424 | * </TR> |
| 425 | * <TR VALIGN=BASELINE> |
| 426 | * <TD> |
| 427 | * <CODE>LDAPException.OPERATION_ERROR</CODE> |
| 428 | * </TD> |
| 429 | * <TD> |
| 430 | * An internal server error occurred. |
| 431 | * </TD> |
| 432 | * </TR> |
| 433 | * <TR VALIGN=BASELINE> |
| 434 | * <TD> |
| 435 | * <CODE>LDAPException.TIME_LIMIT_EXCEEDED</CODE> |
| 436 | * </TD> |
| 437 | * <TD> |
| 438 | * The maximum time allowed for a search was exceeded |
| 439 | * before the server finished sorting the results. |
| 440 | * </TD> |
| 441 | * </TR> |
| 442 | * <TR VALIGN=BASELINE> |
| 443 | * <TD> |
| 444 | * <CODE>LDAPException.STRONG_AUTH_REQUIRED</CODE> |
| 445 | * </TD> |
| 446 | * <TD> |
| 447 | * The server refused to send back the sorted search |
| 448 | * results because it requires you to use a stronger |
| 449 | * authentication method. |
| 450 | * </TD> |
| 451 | * </TR> |
| 452 | * <TR VALIGN=BASELINE> |
| 453 | * <TD> |
| 454 | * <CODE>LDAPException.ADMIN_LIMIT_EXCEEDED</CODE> |
| 455 | * </TD> |
| 456 | * <TD> |
| 457 | * There are too many entries for the server to sort. |
| 458 | * </TD> |
| 459 | * </TR> |
| 460 | * <TR VALIGN=BASELINE> |
| 461 | * <TD> |
| 462 | * <CODE>LDAPException.NO_SUCH_ATTRIBUTE</CODE> |
| 463 | * </TD> |
| 464 | * <TD> |
| 465 | * The sort key list specifies an attribute that does not exist. |
| 466 | * </TD> |
| 467 | * </TR> |
| 468 | * <TR VALIGN=BASELINE> |
| 469 | * <TD> |
| 470 | * <CODE>LDAPException.INAPPROPRIATE_MATCHING</CODE> |
| 471 | * </TD> |
| 472 | * <TD> |
| 473 | * The sort key list specifies a matching rule that is |
| 474 | * not recognized or appropriate |
| 475 | * </TD> |
| 476 | * </TR> |
| 477 | * <TR VALIGN=BASELINE> |
| 478 | * <TD> |
| 479 | * <CODE>LDAPException.INSUFFICIENT_ACCESS_RIGHTS</CODE> |
| 480 | * </TD> |
| 481 | * <TD> |
| 482 | * The server did not send the sorted results because the |
| 483 | * client has insufficient access rights. |
| 484 | * </TD> |
| 485 | * </TR> |
| 486 | * <TR VALIGN=BASELINE> |
| 487 | * <TD> |
| 488 | * <CODE>LDAPException.BUSY</CODE> |
| 489 | * </TD> |
| 490 | * <TD> |
| 491 | * The server is too busy to sort the results. |
| 492 | * </TD> |
| 493 | * </TR> |
| 494 | * <TR VALIGN=BASELINE> |
| 495 | * <TD> |
| 496 | * <CODE>LDAPException.UNWILLING_TO_PERFORM</CODE> |
| 497 | * </TD> |
| 498 | * <TD> |
| 499 | * The server is unable to sort the results. |
| 500 | * </TD> |
| 501 | * </TR> |
| 502 | * <TR VALIGN=BASELINE> |
| 503 | * <TD> |
| 504 | * <CODE>LDAPException.OTHER</CODE> |
| 505 | * </TD> |
| 506 | * <TD> |
| 507 | * This general result code indicates that the server |
| 508 | * failed to sort the results for a reason other than |
| 509 | * the ones listed above. |
| 510 | * </TD> |
| 511 | * </TR> |
| 512 | * </TABLE> |
| 513 | * <P> |
| 514 | * |
| 515 | * @param controls an array of <CODE>LDAPControl</CODE> objects, |
| 516 | * representing the controls returned by the server |
| 517 | * after a search. To get these controls, use the |
| 518 | * <CODE>getResponseControls</CODE> method of the |
| 519 | * <CODE>LDAPConnection</CODE> class. |
| 520 | * @param results an array of integers. The first element of this array |
| 521 | * specifies the result code of the sorting operation. |
| 522 | * @return the attribute that caused the error, or null if the server did |
| 523 | * not specify which attribute caused the error. |
| 524 | * @see org.ietf.ldap.LDAPConnection#getResponseControls |
| 525 | * @deprecated LDAPSortControl response controls are now automatically |
| 526 | * instantiated. |
| 527 | */ |
| 528 | public static String parseResponse( LDAPControl[] controls, int[] results ) { |
| 529 | String attr = null; |
| 530 | LDAPControl sort = null; |
| 531 | /* See if there is a sort control in the array */ |
| 532 | for( int i = 0; (controls != null) && (i < controls.length); i++ ) { |
| 533 | if ( controls[i].getID().equals( SORTRESPONSE ) ) { |
| 534 | sort = controls[i]; |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | if ( sort != null ) { |
| 539 | /* Suck out the data and return it */ |
| 540 | ByteArrayInputStream inStream = |
| 541 | new ByteArrayInputStream( sort.getValue() ); |
| 542 | BERSequence ber = new BERSequence(); |
| 543 | JDAPBERTagDecoder decoder = new JDAPBERTagDecoder(); |
| 544 | int[] nRead = new int[1]; |
| 545 | nRead[0] = 0; |
| 546 | try { |
| 547 | /* A sequence */ |
| 548 | BERSequence seq = (BERSequence)BERElement.getElement( |
| 549 | decoder, inStream, |
| 550 | nRead ); |
| 551 | /* First is result code */ |
| 552 | int result = ((BEREnumerated)seq.elementAt( 0 )).getValue(); |
| 553 | if ( (results != null) && (results.length > 0) ) |
| 554 | results[0] = result; |
| 555 | /* Then, possibly an attribute that failed sorting */ |
| 556 | /* If this is not an octet string, let there be an exception */ |
| 557 | BEROctetString t = (BEROctetString)seq.elementAt(1); |
| 558 | attr = new String(t.getValue(), "UTF8"); |
| 559 | } catch(Exception x) { |
| 560 | } |
| 561 | } |
| 562 | return attr; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Create a "flattened" BER encoding of the requested sort keys, |
| 567 | * and return it as a byte array. |
| 568 | * @param keys the attributes by which to sort |
| 569 | * @return the byte array of encoded data. |
| 570 | */ |
| 571 | private byte[] createSortSpecification( LDAPSortKey[] keys ) { |
| 572 | /* A sequence of sequences */ |
| 573 | BERSequence ber = new BERSequence(); |
| 574 | /* Add each sort key as a sequence */ |
| 575 | for( int i = 0; i < keys.length; i++ ) { |
| 576 | BERSequence seq = new BERSequence(); |
| 577 | /* The attribute name */ |
| 578 | seq.addElement(new BEROctetString(keys[i].getKey())); |
| 579 | /* Optional matching rule */ |
| 580 | if ( keys[i].getMatchRule() != null ) |
| 581 | seq.addElement(new BERTag( |
| 582 | BERTag.CONTEXT|0, |
| 583 | new BEROctetString(keys[i].getMatchRule()), |
| 584 | true)); |
| 585 | /* Optional reverse-order sort */ |
| 586 | if ( keys[i].getReverse() ) |
| 587 | seq.addElement(new BERTag( |
| 588 | BERTag.CONTEXT|1, |
| 589 | new BEREnumerated(LDAPSortKey.REVERSE), |
| 590 | true)); |
| 591 | ber.addElement( seq ); |
| 592 | } |
| 593 | /* Suck out the data and return it */ |
| 594 | return flattenBER( ber ); |
| 595 | } |
| 596 | |
| 597 | public String toString() { |
| 598 | return (getID() == SORTREQUEST) ? reqToString() : rspToString(); |
| 599 | } |
| 600 | |
| 601 | String reqToString() { |
| 602 | |
| 603 | StringBuffer sb = new StringBuffer("{SortCtrl:"); |
| 604 | |
| 605 | sb.append(" isCritical="); |
| 606 | sb.append(isCritical()); |
| 607 | |
| 608 | sb.append(" "); |
| 609 | for (int i=0; i < _keys.length; i++) { |
| 610 | sb.append(_keys[i]); |
| 611 | } |
| 612 | |
| 613 | sb.append("}"); |
| 614 | |
| 615 | return sb.toString(); |
| 616 | } |
| 617 | |
| 618 | String rspToString() { |
| 619 | |
| 620 | StringBuffer sb = new StringBuffer("{SortResponseCtrl:"); |
| 621 | |
| 622 | sb.append(" isCritical="); |
| 623 | sb.append(isCritical()); |
| 624 | |
| 625 | if (_failedAttribute != null) { |
| 626 | sb.append(" failedAttr="); |
| 627 | sb.append(_failedAttribute); |
| 628 | } |
| 629 | |
| 630 | sb.append(" resultCode="); |
| 631 | sb.append(_resultCode); |
| 632 | |
| 633 | sb.append("}"); |
| 634 | |
| 635 | return sb.toString(); |
| 636 | } |
| 637 | } |