| 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) 2001 |
| 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 org.ietf.ldap.LDAPControl; |
| 41 | import org.ietf.ldap.ber.stream.BERInteger; |
| 42 | import org.ietf.ldap.ber.stream.BEROctetString; |
| 43 | import org.ietf.ldap.ber.stream.BERSequence; |
| 44 | import org.ietf.ldap.ber.stream.BERTag; |
| 45 | |
| 46 | /** |
| 47 | * Represents control data for returning paged results from a search. |
| 48 | * |
| 49 | * Example of usage, with JFC: |
| 50 | *<PRE><CODE> |
| 51 | * // Call this to initialize the list box, whenever the search |
| 52 | * // conditions change. |
| 53 | * // "filter" may be "objectclass=person", for example |
| 54 | * void initListBox( String host, int port, String base, String filter ) { |
| 55 | * |
| 56 | * // Create list box if not already done |
| 57 | * if ( _dataList == null ) { |
| 58 | * _dataList = new JList(); |
| 59 | * JScrollPane scrollPane = new JScrollPane(_dataList); |
| 60 | * add( scrollPane ); |
| 61 | * } |
| 62 | * |
| 63 | * // Create a virtual data model |
| 64 | * vlistModel model = new vlistModel( host, port, base, filter ); |
| 65 | * // Keep a buffer of one page before and one after |
| 66 | * model.setPageSize( getScrollVisibleSize() ); |
| 67 | * _dataList.setModel( model ); |
| 68 | * } |
| 69 | *<P> |
| 70 | * // Data model to supply buffer list data |
| 71 | *class vlistModel extends AbstractListModel { |
| 72 | * vlistModel( String host, int port, String base, String filter ) { |
| 73 | * _base = base; |
| 74 | * _filter = filter; |
| 75 | * |
| 76 | * // Connect to the server |
| 77 | * try { |
| 78 | * _ldc = new LDAPConnection(); |
| 79 | * System.out.println( "Connecting to " + host + |
| 80 | * ":" + port ); |
| 81 | * _ldc.connect( host, port ); |
| 82 | * } catch ( LDAPException e ) { |
| 83 | * System.out.println( e ); |
| 84 | * _ldc = null; |
| 85 | * } |
| 86 | * } |
| 87 | * |
| 88 | * // Called by JList to get virtual list size |
| 89 | * public int getSize() { |
| 90 | * if ( !_initialized ) { |
| 91 | * _initialized = true; |
| 92 | * _pageControls = new LDAPControl[2]; |
| 93 | * // Paged results also require a sort control |
| 94 | * _pageControls[0] = |
| 95 | * new LDAPSortControl( new LDAPSortKey("cn"), |
| 96 | * true ); |
| 97 | * // Do an initial search to get the virtual list size |
| 98 | * // Keep one page before and one page after the start |
| 99 | * _beforeCount = _pageSize; |
| 100 | * _afterCount = _pageSize; |
| 101 | * // Create the initial paged results control |
| 102 | * LDAPVirtualListControl cont = |
| 103 | * new LDAPVirtualListControl( "A", |
| 104 | * _beforeCount, |
| 105 | * _afterCount ); |
| 106 | * _pageControls[1] = cont; |
| 107 | * _vlc = (LDAPVirtualListControl)_pageControls[1]; |
| 108 | * getPage( 0 ); |
| 109 | * } |
| 110 | * return _size; |
| 111 | * } |
| 112 | * |
| 113 | * // Get a page starting at first (although we may also fetch |
| 114 | * // some preceding entries) |
| 115 | * boolean getPage( int first ) { |
| 116 | * _vlc.setRange( first, _beforeCount, _afterCount ); |
| 117 | * return getPage(); |
| 118 | * } |
| 119 | * |
| 120 | * boolean getEntries() { |
| 121 | * // Specify necessary controls for vlv |
| 122 | * if ( _pageControls != null ) { |
| 123 | * try { |
| 124 | * _ldc.setOption( _ldc.SERVERCONTROLS, _pageControls ); |
| 125 | * } catch ( LDAPException e ) { |
| 126 | * System.out.println( e + ", setting vlv control" ); |
| 127 | * } |
| 128 | * } |
| 129 | * // Empty the buffer |
| 130 | * _entries.removeAllElements(); |
| 131 | * // Do a search |
| 132 | * try { |
| 133 | * String[] attrs = { "cn" }; |
| 134 | * LDAPSearchResults result = |
| 135 | * _ldc.search( _base, |
| 136 | * LDAPConnection.SCOPE_SUB, |
| 137 | * _filter, |
| 138 | * attrs, false ); |
| 139 | * while ( result.hasMoreElements() ) { |
| 140 | * LDAPEntry entry = (LDAPEntry)result.nextElement(); |
| 141 | * LDAPAttribute attr = entry.getAttribute( attrs[0] ); |
| 142 | * if ( attr != null ) { |
| 143 | * Enumeration en = attr.getStringValues(); |
| 144 | * while( en.hasMoreElements() ) { |
| 145 | * String name = (String)en.nextElement(); |
| 146 | * _entries.addElement( name ); |
| 147 | * } |
| 148 | * } |
| 149 | * } |
| 150 | * } catch ( LDAPException e ) { |
| 151 | * System.out.println( e + ", searching" ); |
| 152 | * return false; |
| 153 | * } |
| 154 | * return true; |
| 155 | * } |
| 156 | * |
| 157 | * // Fetch a buffer |
| 158 | * boolean getPage() { |
| 159 | * // Get the actual entries |
| 160 | * if ( !getEntries() ) |
| 161 | * return false; |
| 162 | * |
| 163 | * // Check if we have a control returned |
| 164 | * LDAPControl[] c = _ldc.getResponseControls(); |
| 165 | * LDAPVirtualListResponse nextCont = null; |
| 166 | * for ( int i = 0; i < c.length; i++ ) { |
| 167 | * if ( c[i] instanceof LDAPVirtualListResponse ) { |
| 168 | * nextCont = (LDAPVirtualListResponse)c[i]; |
| 169 | * break; |
| 170 | * } |
| 171 | * } |
| 172 | * if ( nextCont != null ) { |
| 173 | * _selectedIndex = nextCont.getFirstPosition() - 1; |
| 174 | * _top = Math.max( 0, _selectedIndex - _beforeCount ); |
| 175 | * // Now we know the total size of the virtual list box |
| 176 | * _size = nextCont.getContentCount(); |
| 177 | * _vlc.setListSize( _size ); |
| 178 | * } else { |
| 179 | * System.out.println( "Null response control" ); |
| 180 | * } |
| 181 | * return true; |
| 182 | * } |
| 183 | * |
| 184 | * // Called by JList to fetch data to paint a single list item |
| 185 | * public Object getElementAt(int index) { |
| 186 | * if ( (index < _top) || (index >= _top + _entries.size()) ) { |
| 187 | * getPage( index ); |
| 188 | * } |
| 189 | * int offset = index - _top; |
| 190 | * if ( (offset < 0) || (offset >= _entries.size()) ) |
| 191 | * return new String( "No entry at " + index ); |
| 192 | * else |
| 193 | * return _entries.elementAt( offset ); |
| 194 | * } |
| 195 | * |
| 196 | * // Called by application to find out the virutal selected index |
| 197 | * public int getSelectedIndex() { |
| 198 | * return _selectedIndex; |
| 199 | * } |
| 200 | * |
| 201 | * // Called by application to find out the top of the buffer |
| 202 | * public int getFirstIndex() { |
| 203 | * return _top; |
| 204 | * } |
| 205 | * |
| 206 | * public void setPageSize( int size ) { |
| 207 | * _pageSize = size; |
| 208 | * } |
| 209 | * |
| 210 | * Vector _entries = new Vector(); |
| 211 | * protected boolean _initialized = false; |
| 212 | * private int _top = 0; |
| 213 | * protected int _beforeCount; |
| 214 | * protected int _afterCount; |
| 215 | * private int _pageSize = 10; |
| 216 | * private int _selectedIndex = 0; |
| 217 | * protected LDAPControl[] _pageControls = null; |
| 218 | * protected LDAPVirtualListControl _vlc = null; |
| 219 | * protected int _size = -1; |
| 220 | * private String _base; |
| 221 | * private String _filter; |
| 222 | * private LDAPConnection _ldc; |
| 223 | *} |
| 224 | *</CODE></PRE> |
| 225 | * <PRE> |
| 226 | * VirtualListViewRequest ::= SEQUENCE { |
| 227 | * beforeCount INTEGER (0 .. maxInt), |
| 228 | * afterCount INTEGER (0 .. maxInt), |
| 229 | * CHOICE { |
| 230 | * byIndex [0] SEQUENCE { |
| 231 | * index INTEGER, |
| 232 | * contentCount INTEGER |
| 233 | * } |
| 234 | * byFilter [1] jumpTo Substring |
| 235 | * }, |
| 236 | * contextID OCTET STRING OPTIONAL |
| 237 | * } |
| 238 | * </PRE> |
| 239 | * |
| 240 | * @version 1.0 |
| 241 | */ |
| 242 | |
| 243 | public class LDAPVirtualListControl extends LDAPControl { |
| 244 | public final static String VIRTUALLIST = "2.16.840.1.113730.3.4.9"; |
| 245 | |
| 246 | /** |
| 247 | * Blank constructor for internal use in <CODE>LDAPVirtualListControl</CODE>. |
| 248 | * @see org.ietf.ldap.LDAPControl |
| 249 | */ |
| 250 | LDAPVirtualListControl() { |
| 251 | super( VIRTUALLIST, true, null ); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Constructs a new <CODE>LDAPVirtualListControl</CODE> object. Use this |
| 256 | * constructor on an initial search operation, specifying the first |
| 257 | * entry to be matched, or the initial part of it. |
| 258 | * @param jumpTo an LDAP search expression defining the result set |
| 259 | * @param beforeCount the number of results before the top/center to |
| 260 | * return per page |
| 261 | * @param afterCount the number of results after the top/center to |
| 262 | * return per page |
| 263 | * @see org.ietf.ldap.LDAPControl |
| 264 | */ |
| 265 | public LDAPVirtualListControl( String jumpTo, int beforeCount, |
| 266 | int afterCount ) { |
| 267 | super( VIRTUALLIST, true, null ); |
| 268 | setRange( jumpTo, beforeCount, afterCount ); |
| 269 | } |
| 270 | |
| 271 | public LDAPVirtualListControl( String jumpTo, int beforeCount, |
| 272 | int afterCount, String context ) { |
| 273 | this( jumpTo, beforeCount, afterCount ); |
| 274 | _context = context; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Constructs a new <CODE>LDAPVirtualListControl</CODE> object. Use this |
| 279 | * constructor on a subsquent search operation, after we know the |
| 280 | * size of the virtual list, to fetch a subset. |
| 281 | * @param startIndex the index into the virtual list of an entry to |
| 282 | * return |
| 283 | * @param beforeCount the number of results before the top/center to |
| 284 | * return per page |
| 285 | * @param afterCount the number of results after the top/center to |
| 286 | * return per page |
| 287 | * @see org.ietf.ldap.LDAPControl |
| 288 | */ |
| 289 | public LDAPVirtualListControl( int startIndex, int beforeCount, |
| 290 | int afterCount, int contentCount ) { |
| 291 | super( VIRTUALLIST, true, null ); |
| 292 | _listSize = contentCount; |
| 293 | setRange( startIndex, beforeCount, afterCount ); |
| 294 | } |
| 295 | |
| 296 | public LDAPVirtualListControl( int startIndex, int beforeCount, |
| 297 | int afterCount, int contentCount, |
| 298 | String context ) { |
| 299 | this( startIndex, beforeCount, afterCount, contentCount ); |
| 300 | _context = context; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Sets the starting index, and the number of entries before and after |
| 305 | * to return. Apply this method to a control returned from a previous |
| 306 | * search, to specify what result range to return on the next search. |
| 307 | * @param startIndex the index into the virtual list of an entry to |
| 308 | * return |
| 309 | * @param beforeCount the number of results before startIndex to |
| 310 | * return per page |
| 311 | * @param afterCount the number of results after startIndex to |
| 312 | * return per page |
| 313 | * @see org.ietf.ldap.LDAPControl |
| 314 | */ |
| 315 | public void setRange( int startIndex, int beforeCount, int afterCount ) { |
| 316 | _beforeCount = beforeCount; |
| 317 | _afterCount = afterCount; |
| 318 | _listIndex = startIndex; |
| 319 | _value = createPageSpecification( _listIndex, _listSize, |
| 320 | _beforeCount, _afterCount ); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Sets the search expression, and the number of entries before and after |
| 325 | * to return. |
| 326 | * @param jumpTo an LDAP search expression defining the result set |
| 327 | * return. |
| 328 | * @param beforeCount the number of results before startIndex to |
| 329 | * return per page |
| 330 | * @param afterCount the number of results after startIndex to |
| 331 | * return per page |
| 332 | * @see org.ietf.ldap.LDAPControl |
| 333 | */ |
| 334 | public void setRange( String jumpTo, int beforeCount, int afterCount ) { |
| 335 | _beforeCount = beforeCount; |
| 336 | _afterCount = afterCount; |
| 337 | _value = createPageSpecification( jumpTo, _beforeCount, _afterCount ); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Gets the size of the virtual result set. |
| 342 | * @return the size of the virtual result set, or -1 if not known. |
| 343 | */ |
| 344 | public int getIndex() { |
| 345 | return _listIndex; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Gets the size of the virtual result set. |
| 350 | * @return the size of the virtual result set, or -1 if not known. |
| 351 | */ |
| 352 | public int getListSize() { |
| 353 | return _listSize; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Sets the size of the virtual result set. |
| 358 | * @param listSize the virtual result set size |
| 359 | */ |
| 360 | public void setListSize( int listSize ) { |
| 361 | _listSize = listSize; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Gets the number of results before the top/center to return per page. |
| 366 | * @return the number of results before the top/center to return per page. |
| 367 | */ |
| 368 | public int getBeforeCount() { |
| 369 | return _beforeCount; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Gets the number of results after the top/center to return per page. |
| 374 | * @return the number of results after the top/center to return per page. |
| 375 | */ |
| 376 | public int getAfterCount() { |
| 377 | return _afterCount; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Gets the optional context cookie. |
| 382 | * @return the optional context cookie. |
| 383 | */ |
| 384 | public String getContext() { |
| 385 | return _context; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Sets the optional context cookie. |
| 390 | * @param context the optional context cookie |
| 391 | */ |
| 392 | public void setContext( String context ) { |
| 393 | _context = context; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Creates a "flattened" BER encoding of the requested page |
| 398 | * specifications and return it as a byte array. |
| 399 | * @param subFilter filter expression for generating the results |
| 400 | * @param beforeCount number of entries before first match to return |
| 401 | * @param afterCount number of entries after first match to return |
| 402 | * @return the byte array of encoded data. |
| 403 | */ |
| 404 | private byte[] createPageSpecification( String subFilter, |
| 405 | int beforeCount, |
| 406 | int afterCount ) { |
| 407 | /* A sequence */ |
| 408 | BERSequence seq = new BERSequence(); |
| 409 | seq.addElement( new BERInteger( beforeCount ) ); |
| 410 | seq.addElement( new BERInteger( afterCount ) ); |
| 411 | seq.addElement( new BERTag(BERTag.CONTEXT|TAG_BYFILTER, |
| 412 | new BEROctetString(subFilter), true) ); |
| 413 | if ( _context != null ) { |
| 414 | seq.addElement( new BEROctetString(_context) ); |
| 415 | } |
| 416 | /* Suck out the data and return it */ |
| 417 | return flattenBER( seq ); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Creates a "flattened" BER encoding of the requested page |
| 422 | * specifications and return it as a byte array. |
| 423 | * @param listIndex the center or starting entry to return |
| 424 | * @param listSize the virtual list size |
| 425 | * @param beforeCount number of entries before the first match to return |
| 426 | * @param afterCount number of entries after the first match to return |
| 427 | * @return the byte array of encoded data. |
| 428 | */ |
| 429 | private byte[] createPageSpecification( int listIndex, |
| 430 | int listSize, |
| 431 | int beforeCount, |
| 432 | int afterCount ) { |
| 433 | /* A sequence */ |
| 434 | BERSequence seq = new BERSequence(); |
| 435 | seq.addElement( new BERInteger( beforeCount ) ); |
| 436 | seq.addElement( new BERInteger( afterCount ) ); |
| 437 | /* A sequence of list index and list size */ |
| 438 | BERSequence indexSeq = new BERSequence(); |
| 439 | indexSeq.addElement( new BERInteger(listIndex) ); |
| 440 | indexSeq.addElement( new BERInteger(listSize) ); |
| 441 | seq.addElement( |
| 442 | new BERTag(BERTag.CONTEXT|BERTag.CONSTRUCTED|TAG_BYINDEX, |
| 443 | indexSeq,true) ); |
| 444 | if ( _context != null ) { |
| 445 | seq.addElement( new BEROctetString(_context) ); |
| 446 | } |
| 447 | |
| 448 | /* Suck out the data and return it */ |
| 449 | return flattenBER( seq ); |
| 450 | } |
| 451 | |
| 452 | public String toString() { |
| 453 | StringBuffer sb = new StringBuffer("{VirtListCtrl:"); |
| 454 | |
| 455 | sb.append(" isCritical="); |
| 456 | sb.append(isCritical()); |
| 457 | |
| 458 | sb.append(" beforeCount="); |
| 459 | sb.append(_beforeCount); |
| 460 | |
| 461 | sb.append(" afterCount="); |
| 462 | sb.append(_afterCount); |
| 463 | |
| 464 | sb.append(" listIndex="); |
| 465 | sb.append(_listIndex); |
| 466 | |
| 467 | sb.append(" listSize="); |
| 468 | sb.append(_listSize); |
| 469 | |
| 470 | if (_context != null) { |
| 471 | sb.append(" conext="); |
| 472 | sb.append(_context); |
| 473 | } |
| 474 | |
| 475 | sb.append("}"); |
| 476 | |
| 477 | return sb.toString(); |
| 478 | } |
| 479 | |
| 480 | private final static int TAG_BYINDEX = 0; |
| 481 | private final static int TAG_BYFILTER = 1; |
| 482 | private int _beforeCount = 0; |
| 483 | private int _afterCount = 0; |
| 484 | private int _listIndex = -1; |
| 485 | private int _listSize = 0; |
| 486 | private String _context = null; |
| 487 | } |