| 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 java.io.ByteArrayInputStream; |
| 41 | |
| 42 | import org.ietf.ldap.LDAPControl; |
| 43 | import org.ietf.ldap.LDAPException; |
| 44 | import org.ietf.ldap.ber.stream.BERElement; |
| 45 | import org.ietf.ldap.ber.stream.BEREnumerated; |
| 46 | import org.ietf.ldap.ber.stream.BERInteger; |
| 47 | import org.ietf.ldap.ber.stream.BEROctetString; |
| 48 | import org.ietf.ldap.ber.stream.BERSequence; |
| 49 | import org.ietf.ldap.client.JDAPBERTagDecoder; |
| 50 | |
| 51 | /** |
| 52 | * Represents control data for returning paged results from a search. |
| 53 | * |
| 54 | * @version 1.0 |
| 55 | * |
| 56 | *<PRE> |
| 57 | * VirtualListViewResponse ::= SEQUENCE { |
| 58 | * targetPosition INTEGER (0 .. maxInt), |
| 59 | * contentCount INTEGER (0 .. maxInt), |
| 60 | * virtualListViewResult ENUMERATED { |
| 61 | * success (0), |
| 62 | * operatonsError (1), |
| 63 | * timeLimitExceeded (3), |
| 64 | * adminLimitExceeded (11), |
| 65 | * insufficientAccessRights (50), |
| 66 | * busy (51), |
| 67 | * unwillingToPerform (53), |
| 68 | * sortControlMissing (60), |
| 69 | * offsetRangeError (61), |
| 70 | * other (80) |
| 71 | * }, |
| 72 | * contextID OCTET STRING OPTIONAL |
| 73 | * } |
| 74 | *</PRE> |
| 75 | */ |
| 76 | |
| 77 | public class LDAPVirtualListResponse extends LDAPControl { |
| 78 | public final static String VIRTUALLISTRESPONSE = "2.16.840.1.113730.3.4.10"; |
| 79 | |
| 80 | /** |
| 81 | * Blank constructor for internal use in <CODE>LDAPVirtualListResponse</CODE>. |
| 82 | * @see org.ietf.ldap.LDAPControl |
| 83 | */ |
| 84 | LDAPVirtualListResponse() { |
| 85 | super( VIRTUALLISTRESPONSE, true, null ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Contructs an <CODE>LDAPVirtualListResponse</CODE> object. |
| 90 | * @param oid this parameter must be equal to |
| 91 | * <CODE>LDAPVirtualListResponse.VIRTUALLISTRESPONSE</CODE> or an |
| 92 | * <CODE>LDAPException</CODE>is thrown |
| 93 | * @param critical <code>true</code> if this control is critical |
| 94 | * @param value the value associated with this control |
| 95 | * @exception org.ietf.ldap.LDAPException If oid is not |
| 96 | * <CODE>LDAPVirtualListResponse.VIRTUALLISTRESPONSE</CODE>. |
| 97 | * @see org.ietf.ldap.LDAPControl#register |
| 98 | */ |
| 99 | public LDAPVirtualListResponse( String oid, boolean critical, |
| 100 | byte[] value ) throws LDAPException { |
| 101 | super( VIRTUALLISTRESPONSE, critical, value ); |
| 102 | if ( !oid.equals( VIRTUALLISTRESPONSE ) ) { |
| 103 | throw new LDAPException( "oid must be LDAPVirtualListResponse." + |
| 104 | "VIRTUALLISTRESPONSE", |
| 105 | LDAPException.PARAM_ERROR); |
| 106 | } |
| 107 | |
| 108 | parseResponse(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Constructs a new <CODE>LDAPVirtualListResponse</CODE> object. |
| 113 | * @param value a BER encoded byte array |
| 114 | * @see org.ietf.ldap.LDAPControl |
| 115 | */ |
| 116 | public LDAPVirtualListResponse( byte[] value ) { |
| 117 | super( VIRTUALLISTRESPONSE, true, null ); |
| 118 | _value = value; |
| 119 | parseResponse(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Gets the size of the virtual result set. |
| 124 | * @return the size of the virtual result set, or -1 if not known. |
| 125 | */ |
| 126 | public int getContentCount() { |
| 127 | return _contentCount; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Gets the index of the first entry returned. |
| 132 | * @return the index of the first entry returned. |
| 133 | */ |
| 134 | public int getFirstPosition() { |
| 135 | return _firstPosition; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Gets the result code. |
| 140 | * @return the result code. |
| 141 | */ |
| 142 | public int getResultCode() { |
| 143 | return _resultCode; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Gets the context cookie, if any. |
| 148 | * @return the result context cookie. |
| 149 | */ |
| 150 | public String getContext() { |
| 151 | return _context; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Returns a control useful for subsequent paged results searches. |
| 156 | * "this" should be a control returned on a previous paged results |
| 157 | * search, so it contains information on the virtual result set |
| 158 | * size. |
| 159 | * @return a control useful for subsequent paged results searches. |
| 160 | */ |
| 161 | private void parseResponse() { |
| 162 | /* Suck out the data and parse it */ |
| 163 | ByteArrayInputStream inStream = |
| 164 | new ByteArrayInputStream( getValue() ); |
| 165 | BERSequence ber = new BERSequence(); |
| 166 | JDAPBERTagDecoder decoder = new JDAPBERTagDecoder(); |
| 167 | int[] nRead = new int[1]; |
| 168 | nRead[0] = 0; |
| 169 | try { |
| 170 | /* A sequence */ |
| 171 | BERSequence seq = (BERSequence)BERElement.getElement( |
| 172 | decoder, inStream, |
| 173 | nRead ); |
| 174 | /* First is firstPosition */ |
| 175 | _firstPosition = ((BERInteger)seq.elementAt( 0 )).getValue(); |
| 176 | _contentCount = ((BERInteger)seq.elementAt( 1 )).getValue(); |
| 177 | _resultCode = ((BEREnumerated)seq.elementAt( 2 )).getValue(); |
| 178 | if( seq.size() > 3 ) { |
| 179 | BEROctetString str = (BEROctetString)seq.elementAt( 3 ); |
| 180 | _context = new String(str.getValue(), "UTF8"); |
| 181 | } |
| 182 | } catch(Exception x) { |
| 183 | _firstPosition = _contentCount = _resultCode = -1; |
| 184 | _context = null; } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Returns a control returned on a VLV search. |
| 189 | * @param controls an array of controls that may include a VLV |
| 190 | * results control |
| 191 | * @return the control, if any; otherwise null. |
| 192 | * @deprecated LDAPVirtualListResponse controls are now automatically |
| 193 | * instantiated. |
| 194 | */ |
| 195 | public static LDAPVirtualListResponse parseResponse( |
| 196 | LDAPControl[] controls ) { |
| 197 | LDAPVirtualListResponse con = null; |
| 198 | /* See if there is a VLV response control in the array */ |
| 199 | for( int i = 0; (controls != null) && (i < controls.length); i++ ) { |
| 200 | if ( controls[i].getID().equals( VIRTUALLISTRESPONSE ) ) { |
| 201 | con = new LDAPVirtualListResponse( controls[i].getValue() ); |
| 202 | con.parseResponse(); |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | if ( con != null ) { |
| 207 | con.parseResponse(); |
| 208 | } |
| 209 | return con; |
| 210 | } |
| 211 | |
| 212 | public String toString() { |
| 213 | StringBuffer sb = new StringBuffer("{VirtListResponseCtrl:"); |
| 214 | |
| 215 | sb.append(" isCritical="); |
| 216 | sb.append(isCritical()); |
| 217 | |
| 218 | sb.append(" firstPosition="); |
| 219 | sb.append(_firstPosition); |
| 220 | |
| 221 | sb.append(" contentCount="); |
| 222 | sb.append(_contentCount); |
| 223 | |
| 224 | sb.append(" resultCode="); |
| 225 | sb.append(_resultCode); |
| 226 | |
| 227 | if (_context != null) { |
| 228 | sb.append(" conext="); |
| 229 | sb.append(_context); |
| 230 | } |
| 231 | |
| 232 | sb.append("}"); |
| 233 | |
| 234 | return sb.toString(); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | private int _firstPosition = 0; |
| 239 | private int _contentCount = 0; |
| 240 | private int _resultCode = -1; |
| 241 | private String _context = null; |
| 242 | |
| 243 | } |