| 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 org.ietf.ldap.LDAPControl; |
| 41 | |
| 42 | /** |
| 43 | * Represents an LDAP v3 server control that contains a string as its |
| 44 | * only value. This is to be used as a base class by real such controls. |
| 45 | */ |
| 46 | abstract class LDAPStringControl extends LDAPControl { |
| 47 | protected String m_msg = null; |
| 48 | |
| 49 | LDAPStringControl() { |
| 50 | super(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Constructs an <CODE>LDAPStringControl</CODE> object, and stores the |
| 55 | * value as a string. To retrieve this string value, use |
| 56 | * <CODE>getMsg()</CODE>. |
| 57 | * @param oid the oid of this control |
| 58 | * @param critical <code>true</code> if this control is critical to the search |
| 59 | * @param value the value associated with this control |
| 60 | * @see org.ietf.ldap.LDAPcontrol |
| 61 | */ |
| 62 | public LDAPStringControl( String oid, boolean critical, byte[] value ) { |
| 63 | super( oid, critical, value ); |
| 64 | |
| 65 | if ( value != null ) { |
| 66 | try { |
| 67 | m_msg = new String( value, "UTF8" ); |
| 68 | } catch ( java.io.IOException e ) { |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Parses a response control sent by the server and |
| 75 | * retrieves a string. |
| 76 | * <P> |
| 77 | * |
| 78 | * You can get the controls returned by the server by using the |
| 79 | * <CODE>getResponseControls</CODE> method of the |
| 80 | * <CODE>LDAPConnection</CODE> class. |
| 81 | * <P> |
| 82 | * |
| 83 | * @param controls an array of <CODE>LDAPControl</CODE> objects, |
| 84 | * representing the controls returned by the server |
| 85 | * after a search. To get these controls, use the |
| 86 | * <CODE>getResponseControls</CODE> method of the |
| 87 | * <CODE>LDAPConnection</CODE> class. |
| 88 | * @param type the OID of the control to look for |
| 89 | * @return a message string, or null if the server did |
| 90 | * not return a string. |
| 91 | * @see org.ietf.ldap.LDAPConnection#getResponseControls |
| 92 | */ |
| 93 | public static String parseResponse( LDAPControl[] controls, String type ) { |
| 94 | String msg = null; |
| 95 | LDAPControl cont = null; |
| 96 | /* See if there is a password control in the array */ |
| 97 | for( int i = 0; (controls != null) && (i < controls.length); i++ ) { |
| 98 | if ( controls[i].getID().equals( type ) ) { |
| 99 | cont = controls[i]; |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | if ( cont != null ) { |
| 104 | /* Suck out the data and return it */ |
| 105 | try { |
| 106 | msg = new String( cont.getValue(), "UTF8" ); |
| 107 | } catch ( Exception e ) { |
| 108 | } |
| 109 | } |
| 110 | return msg; |
| 111 | } |
| 112 | } |