| 1 |
/****************************************************************************** |
| 2 |
* The MIT License |
| 3 |
* Copyright (c) 2003 Novell Inc. www.novell.com |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
* of this software and associated documentation files (the Software), to deal |
| 7 |
* in the Software without restriction, including without limitation the rights |
| 8 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the Software is |
| 10 |
* furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in |
| 13 |
* all copies or substantial portions of the Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
* SOFTWARE. |
| 22 |
*******************************************************************************/ |
| 23 |
// |
| 24 |
// Novell.Directory.Ldap.Rfc2251.RfcControl.cs |
| 25 |
// |
| 26 |
// Author: |
| 27 |
// Sunil Kumar ([email protected]) |
| 28 |
// |
| 29 |
// (C) 2003 Novell, Inc (http://www.novell.com) |
| 30 |
// |
| 31 |
|
| 32 |
using System; |
| 33 |
using Novell.Directory.Ldap.Asn1; |
| 34 |
|
| 35 |
|
| 36 |
namespace Novell.Directory.Ldap.Rfc2251 |
| 37 |
{ |
| 38 |
|
| 39 |
/// <summary> Represents an Ldap Control. |
| 40 |
/// |
| 41 |
/// <pre> |
| 42 |
/// Control ::= SEQUENCE { |
| 43 |
/// controlType LdapOID, |
| 44 |
/// criticality BOOLEAN DEFAULT FALSE, |
| 45 |
/// controlValue OCTET STRING OPTIONAL } |
| 46 |
/// </pre> |
| 47 |
/// </summary> |
| 48 |
public class RfcControl:Asn1Sequence |
| 49 |
{ |
| 50 |
/// <summary> </summary> |
| 51 |
virtual public Asn1OctetString ControlType |
| 52 |
{ |
| 53 |
get |
| 54 |
{ |
| 55 |
return (Asn1OctetString) get_Renamed(0); |
| 56 |
} |
| 57 |
|
| 58 |
} |
| 59 |
/// <summary> Returns criticality. |
| 60 |
/// |
| 61 |
/// If no value present, return the default value of FALSE. |
| 62 |
/// </summary> |
| 63 |
virtual public Asn1Boolean Criticality |
| 64 |
{ |
| 65 |
get |
| 66 |
{ |
| 67 |
if (size() > 1) |
| 68 |
{ |
| 69 |
// MAY be a criticality |
| 70 |
Asn1Object obj = get_Renamed(1); |
| 71 |
if (obj is Asn1Boolean) |
| 72 |
return (Asn1Boolean) obj; |
| 73 |
} |
| 74 |
|
| 75 |
return new Asn1Boolean(false); |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
/// <summary> Since controlValue is an OPTIONAL component, we need to check |
| 80 |
/// to see if one is available. Remember that if criticality is of default |
| 81 |
/// value, it will not be present. |
| 82 |
/// </summary> |
| 83 |
/// <summary> Called to set/replace the ControlValue. Will normally be called by |
| 84 |
/// the child classes after the parent has been instantiated. |
| 85 |
/// </summary> |
| 86 |
virtual public Asn1OctetString ControlValue |
| 87 |
{ |
| 88 |
get |
| 89 |
{ |
| 90 |
if (size() > 2) |
| 91 |
{ |
| 92 |
// MUST be a control value |
| 93 |
return (Asn1OctetString) get_Renamed(2); |
| 94 |
} |
| 95 |
else if (size() > 1) |
| 96 |
{ |
| 97 |
// MAY be a control value |
| 98 |
Asn1Object obj = get_Renamed(1); |
| 99 |
if (obj is Asn1OctetString) |
| 100 |
return (Asn1OctetString) obj; |
| 101 |
} |
| 102 |
return null; |
| 103 |
} |
| 104 |
|
| 105 |
set |
| 106 |
{ |
| 107 |
|
| 108 |
if (value == null) |
| 109 |
return ; |
| 110 |
|
| 111 |
if (size() == 3) |
| 112 |
{ |
| 113 |
// We already have a control value, replace it |
| 114 |
set_Renamed(2, value); |
| 115 |
return ; |
| 116 |
} |
| 117 |
|
| 118 |
if (size() == 2) |
| 119 |
{ |
| 120 |
|
| 121 |
// Get the second element |
| 122 |
Asn1Object obj = get_Renamed(1); |
| 123 |
|
| 124 |
// Is this a control value |
| 125 |
if (obj is Asn1OctetString) |
| 126 |
{ |
| 127 |
|
| 128 |
// replace this one |
| 129 |
set_Renamed(1, value); |
| 130 |
return ; |
| 131 |
} |
| 132 |
else |
| 133 |
{ |
| 134 |
// add a new one at the end |
| 135 |
add(value); |
| 136 |
return ; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
//************************************************************************* |
| 144 |
// Constructors for Control |
| 145 |
//************************************************************************* |
| 146 |
|
| 147 |
/// <summary> </summary> |
| 148 |
public RfcControl(RfcLdapOID controlType):this(controlType, new Asn1Boolean(false), null) |
| 149 |
{ |
| 150 |
} |
| 151 |
|
| 152 |
/// <summary> </summary> |
| 153 |
public RfcControl(RfcLdapOID controlType, Asn1Boolean criticality):this(controlType, criticality, null) |
| 154 |
{ |
| 155 |
} |
| 156 |
|
| 157 |
/// <summary> |
| 158 |
/// Note: criticality is only added if true, as per RFC 2251 sec 5.1 part |
| 159 |
/// (4): If a value of a type is its default value, it MUST be |
| 160 |
/// absent. |
| 161 |
/// </summary> |
| 162 |
public RfcControl(RfcLdapOID controlType, Asn1Boolean criticality, Asn1OctetString controlValue):base(3) |
| 163 |
{ |
| 164 |
add(controlType); |
| 165 |
if (criticality.booleanValue() == true) |
| 166 |
add(criticality); |
| 167 |
if (controlValue != null) |
| 168 |
add(controlValue); |
| 169 |
} |
| 170 |
|
| 171 |
/// <summary> Constructs a Control object by decoding it from an InputStream.</summary> |
| 172 |
[CLSCompliantAttribute(false)] |
| 173 |
public RfcControl(Asn1Decoder dec, System.IO.Stream in_Renamed, int len):base(dec, in_Renamed, len) |
| 174 |
{ |
| 175 |
} |
| 176 |
|
| 177 |
/// <summary> Constructs a Control object by decoding from an Asn1Sequence</summary> |
| 178 |
public RfcControl(Asn1Sequence seqObj):base(3) |
| 179 |
{ |
| 180 |
int len = seqObj.size(); |
| 181 |
for (int i = 0; i < len; i++) |
| 182 |
add(seqObj.get_Renamed(i)); |
| 183 |
} |
| 184 |
|
| 185 |
//************************************************************************* |
| 186 |
// Accessors |
| 187 |
//************************************************************************* |
| 188 |
} |
| 189 |
} |