New test.
[mono.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap / LdapModifyRequest.cs
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.LdapModifyRequest.cs
25 //
26 // Author:
27 //   Sunil Kumar (Sunilk@novell.com)
28 //
29 // (C) 2003 Novell, Inc (http://www.novell.com)
30 //
31
32 using System;
33 using Novell.Directory.Ldap.Asn1;
34 using Novell.Directory.Ldap.Rfc2251;
35
36 namespace Novell.Directory.Ldap
37 {
38         
39         /// <summary> Modification Request.
40         /// 
41         /// </summary>
42         /// <seealso cref="LdapConnection.SendRequest">
43         /// </seealso>
44    /*
45         *       ModifyRequest ::= [APPLICATION 6] SEQUENCE {
46         *               object          LdapDN,
47         *               modification    SEQUENCE OF SEQUENCE {
48         *                       operation       ENUMERATED {
49         *                                               add     (0),
50         *                                               delete  (1),
51         *                                               replace (2) },
52         *                       modification    AttributeTypeAndValues } }
53         */
54         public class LdapModifyRequest:LdapMessage
55         {
56                 /// <summary> Returns of the dn of the entry to modify in the directory
57                 /// 
58                 /// </summary>
59                 /// <returns> the dn of the entry to modify
60                 /// </returns>
61                 virtual public System.String DN
62                 {
63                         get
64                         {
65                                 return Asn1Object.RequestDN;
66                         }
67                         
68                 }
69                 /// <summary> Constructs the modifications associated with this request
70                 /// 
71                 /// </summary>
72                 /// <returns> an array of LdapModification objects
73                 /// </returns>
74                 virtual public LdapModification[] Modifications
75                 {
76                         get
77                         {
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++)
86                                 {
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)
91                                         {
92                                                 throw new System.SystemException("LdapModifyRequest: modification " + m + " is wrong size: " + opSeq.size());
93                                         }
94                                         // Contains operation and sequence for the attribute
95                                         Asn1Object[] opArray = opSeq.toArray();
96                                         Asn1Enumerated asn1op = (Asn1Enumerated) opArray[0];
97                                         // get the operation
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);
106                                         
107                                         for (int v = 0; v < valueArray.Length; v++)
108                                         {
109                                                 RfcAttributeValue rfcV = (RfcAttributeValue) valueArray[v];
110                                                 attr.addValue(rfcV.byteValue());
111                                         }
112                                         
113                                         modifications[m] = new LdapModification(op, attr);
114                                 }
115                                 return modifications;
116                         }
117                         
118                 }
119                 /// <summary> Constructs an Ldap Modify request.
120                 /// 
121                 /// </summary>
122                 /// <param name="dn">        The distinguished name of the entry to modify.
123                 /// 
124                 /// </param>
125                 /// <param name="mods">      The changes to be made to the entry.
126                 /// 
127                 /// </param>
128                 /// <param name="cont">       Any controls that apply to the modify request,
129                 /// or null if none.
130                 /// </param>
131                 public LdapModifyRequest(System.String dn, LdapModification[] mods, LdapControl[] cont):base(LdapMessage.MODIFY_REQUEST, new RfcModifyRequest(new RfcLdapDN(dn), encodeModifications(mods)), cont)
132                 {
133                         return ;
134                 }
135                 
136                 /// <summary> Encode an array of LdapModifications to ASN.1.
137                 /// 
138                 /// </summary>
139                 /// <param name="mods">an array of LdapModification objects
140                 /// 
141                 /// </param>
142                 /// <returns> an Asn1SequenceOf object containing the modifications.
143                 /// </returns>
144                 static private Asn1SequenceOf encodeModifications(LdapModification[] mods)
145                 {
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++)
149                         {
150                                 LdapAttribute attr = mods[i].Attribute;
151                                 
152                                 // place modification attribute values in Asn1SetOf
153                                 Asn1SetOf vals = new Asn1SetOf(attr.size());
154                                 if (attr.size() > 0)
155                                 {
156                                         System.Collections.IEnumerator attrEnum = attr.ByteValues;
157                                         while (attrEnum.MoveNext())
158                                         {
159                                                 vals.add(new RfcAttributeValue((sbyte[]) attrEnum.Current));
160                                         }
161                                 }
162                                 
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));
167                                 
168                                 // place SEQUENCE into SEQUENCE OF
169                                 rfcMods.add(rfcMod);
170                         }
171                         return rfcMods;
172                 }
173                 
174                 /// <summary> Return an Asn1 representation of this modify request
175                 /// 
176                 /// #return an Asn1 representation of this object
177                 /// </summary>
178                 public override System.String ToString()
179                 {
180                         return Asn1Object.ToString();
181                 }
182         }
183 }