1 /******************************************************************************
3 * Copyright (c) 2003 Novell Inc. www.novell.com
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
22 *******************************************************************************/
24 // Novell.Directory.Ldap.LdapModifyRequest.cs
27 // Sunil Kumar (Sunilk@novell.com)
29 // (C) 2003 Novell, Inc (http://www.novell.com)
33 using Novell
.Directory
.Ldap
.Asn1
;
34 using Novell
.Directory
.Ldap
.Rfc2251
;
36 namespace Novell
.Directory
.Ldap
39 /// <summary> Modification Request.
42 /// <seealso cref="LdapConnection.SendRequest">
45 * ModifyRequest ::= [APPLICATION 6] SEQUENCE {
47 * modification SEQUENCE OF SEQUENCE {
48 * operation ENUMERATED {
52 * modification AttributeTypeAndValues } }
54 public class LdapModifyRequest
:LdapMessage
56 /// <summary> Returns of the dn of the entry to modify in the directory
59 /// <returns> the dn of the entry to modify
61 virtual public System
.String DN
65 return Asn1Object
.RequestDN
;
69 /// <summary> Constructs the modifications associated with this request
72 /// <returns> an array of LdapModification objects
74 virtual public LdapModification
[] Modifications
78 // Get the RFC request object for this request
79 RfcModifyRequest req
= (RfcModifyRequest
) Asn1Object
.getRequest();
80 // get beginning sequenceOf modifications
81 Asn1SequenceOf seqof
= req
.Modifications
;
82 Asn1Object
[] mods
= seqof
.toArray();
83 LdapModification
[] modifications
= new LdapModification
[mods
.Length
];
84 // Process each modification
85 for (int m
= 0; m
< mods
.Length
; m
++)
87 // Each modification consists of a mod type and a sequence
88 // containing the attr name and a set of values
89 Asn1Sequence opSeq
= (Asn1Sequence
) mods
[m
];
90 if (opSeq
.size() != 2)
92 throw new System
.SystemException("LdapModifyRequest: modification " + m
+ " is wrong size: " + opSeq
.size());
94 // Contains operation and sequence for the attribute
95 Asn1Object
[] opArray
= opSeq
.toArray();
96 Asn1Enumerated asn1op
= (Asn1Enumerated
) opArray
[0];
98 int op
= asn1op
.intValue();
99 Asn1Sequence attrSeq
= (Asn1Sequence
) opArray
[1];
100 Asn1Object
[] attrArray
= attrSeq
.toArray();
101 RfcAttributeDescription aname
= (RfcAttributeDescription
) attrArray
[0];
102 System
.String name
= aname
.stringValue();
103 Asn1SetOf avalue
= (Asn1SetOf
) attrArray
[1];
104 Asn1Object
[] valueArray
= avalue
.toArray();
105 LdapAttribute attr
= new LdapAttribute(name
);
107 for (int v
= 0; v
< valueArray
.Length
; v
++)
109 RfcAttributeValue rfcV
= (RfcAttributeValue
) valueArray
[v
];
110 attr
.addValue(rfcV
.byteValue());
113 modifications
[m
] = new LdapModification(op
, attr
);
115 return modifications
;
119 /// <summary> Constructs an Ldap Modify request.
122 /// <param name="dn"> The distinguished name of the entry to modify.
125 /// <param name="mods"> The changes to be made to the entry.
128 /// <param name="cont"> Any controls that apply to the modify request,
131 public LdapModifyRequest(System
.String dn
, LdapModification
[] mods
, LdapControl
[] cont
):base(LdapMessage
.MODIFY_REQUEST
, new RfcModifyRequest(new RfcLdapDN(dn
), encodeModifications(mods
)), cont
)
136 /// <summary> Encode an array of LdapModifications to ASN.1.
139 /// <param name="mods">an array of LdapModification objects
142 /// <returns> an Asn1SequenceOf object containing the modifications.
144 static private Asn1SequenceOf
encodeModifications(LdapModification
[] mods
)
146 // Convert Java-API LdapModification[] to RFC2251 SEQUENCE OF SEQUENCE
147 Asn1SequenceOf rfcMods
= new Asn1SequenceOf(mods
.Length
);
148 for (int i
= 0; i
< mods
.Length
; i
++)
150 LdapAttribute attr
= mods
[i
].Attribute
;
152 // place modification attribute values in Asn1SetOf
153 Asn1SetOf vals
= new Asn1SetOf(attr
.size());
156 System
.Collections
.IEnumerator attrEnum
= attr
.ByteValues
;
157 while (attrEnum
.MoveNext())
159 vals
.add(new RfcAttributeValue((sbyte[]) attrEnum
.Current
));
163 // create SEQUENCE containing mod operation and attr type and vals
164 Asn1Sequence rfcMod
= new Asn1Sequence(2);
165 rfcMod
.add(new Asn1Enumerated(mods
[i
].Op
));
166 rfcMod
.add(new RfcAttributeTypeAndValues(new RfcAttributeDescription(attr
.Name
), vals
));
168 // place SEQUENCE into SEQUENCE OF
174 /// <summary> Return an Asn1 representation of this modify request
176 /// #return an Asn1 representation of this object
178 public override System
.String
ToString()
180 return Asn1Object
.ToString();