Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap / LdapAddRequest.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.LdapAddRequest.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 namespace Novell.Directory.Ldap
36 {
37         
38         /// <summary> Represents an Ldap Add Request.
39         /// 
40         /// </summary>
41         /// <seealso cref="LdapConnection.SendRequest">
42         /// </seealso>
43    /*
44         *       AddRequest ::= [APPLICATION 8] SEQUENCE {
45         *               entry           LdapDN,
46         *               attributes      AttributeList }
47         */
48         public class LdapAddRequest:LdapMessage
49         {
50                 /// <summary> Constructs an LdapEntry that represents the add request
51                 /// 
52                 /// </summary>
53                 /// <returns> an LdapEntry that represents the add request.
54                 /// </returns>
55                 virtual public LdapEntry Entry
56                 {
57                         get
58                         {
59                                 RfcAddRequest addreq = (RfcAddRequest) Asn1Object.getRequest();
60                                 
61                                 LdapAttributeSet attrs = new LdapAttributeSet();
62                                 
63                                 // Build the list of attributes
64                                 Asn1Object[] seqArray = addreq.Attributes.toArray();
65                                 for (int i = 0; i < seqArray.Length; i++)
66                                 {
67                                         RfcAttributeTypeAndValues seq = (RfcAttributeTypeAndValues) seqArray[i];
68                                         LdapAttribute attr = new LdapAttribute(((Asn1OctetString) seq.get_Renamed(0)).stringValue());
69                                         
70                                         // Add the values to the attribute
71                                         Asn1SetOf set_Renamed = (Asn1SetOf) seq.get_Renamed(1);
72                                         System.Object[] setArray = set_Renamed.toArray();
73                                         for (int j = 0; j < setArray.Length; j++)
74                                         {
75                                                 attr.addValue(((Asn1OctetString) setArray[j]).byteValue());
76                                         }
77                                         attrs.Add(attr);
78                                 }
79                                 
80                                 return new LdapEntry(Asn1Object.RequestDN, attrs);
81                         }
82                         
83                 }
84                 /// <summary> Constructs a request to add an entry to the directory.
85                 /// 
86                 /// </summary>
87                 /// <param name="entry">The LdapEntry to add to the directory.
88                 /// 
89                 /// </param>
90                 /// <param name="cont">Any controls that apply to the add request,
91                 /// or null if none.
92                 /// </param>
93                 public LdapAddRequest(LdapEntry entry, LdapControl[] cont):base(LdapMessage.ADD_REQUEST, new RfcAddRequest(new RfcLdapDN(entry.DN), makeRfcAttrList(entry)), cont)
94                 {
95                         return ;
96                 }
97                 
98                 /// <summary> Build the attribuite list from an LdapEntry.
99                 /// 
100                 /// </summary>
101                 /// <param name="entry">The LdapEntry associated with this add request.
102                 /// </param>
103                 private static RfcAttributeList makeRfcAttrList(LdapEntry entry)
104                 {
105                         // convert Java-API LdapEntry to RFC2251 AttributeList
106                         LdapAttributeSet attrSet = entry.getAttributeSet();
107                         RfcAttributeList attrList = new RfcAttributeList(attrSet.Count);
108                         System.Collections.IEnumerator itr = attrSet.GetEnumerator();
109                         while (itr.MoveNext())
110                         {
111                                 LdapAttribute attr = (LdapAttribute) itr.Current;
112                                 Asn1SetOf vals = new Asn1SetOf(attr.size());
113                                 System.Collections.IEnumerator attrEnum = attr.ByteValues;
114                                 while (attrEnum.MoveNext())
115                                 {
116                                         vals.add(new RfcAttributeValue((sbyte[]) attrEnum.Current));
117                                 }
118                                 attrList.add(new RfcAttributeTypeAndValues(new RfcAttributeDescription(attr.Name), vals));
119                         }
120                         return attrList;
121                 }
122                 
123                 /// <summary> Return an Asn1 representation of this add request.
124                 /// 
125                 /// #return an Asn1 representation of this object.
126                 /// </summary>
127                 public override System.String ToString()
128                 {
129                         return Asn1Object.ToString();
130                 }
131         }
132 }