**** Merged from MCS ****
[mono-project.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap / LdapAddRequest.cs
blobaf0775a9e2a15d208f7eba89334f59532fafb1e5
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:
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
21 * SOFTWARE.
22 *******************************************************************************/
24 // Novell.Directory.Ldap.LdapAddRequest.cs
26 // Author:
27 // Sunil Kumar (Sunilk@novell.com)
29 // (C) 2003 Novell, Inc (http://www.novell.com)
32 using System;
33 using Novell.Directory.Ldap.Asn1;
34 using Novell.Directory.Ldap.Rfc2251;
35 namespace Novell.Directory.Ldap
38 /// <summary> Represents an Ldap Add Request.
39 ///
40 /// </summary>
41 /// <seealso cref="LdapConnection.SendRequest">
42 /// </seealso>
44 * AddRequest ::= [APPLICATION 8] SEQUENCE {
45 * entry LdapDN,
46 * attributes AttributeList }
48 public class LdapAddRequest:LdapMessage
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
57 get
59 RfcAddRequest addreq = (RfcAddRequest) Asn1Object.getRequest();
61 LdapAttributeSet attrs = new LdapAttributeSet();
63 // Build the list of attributes
64 Asn1Object[] seqArray = addreq.Attributes.toArray();
65 for (int i = 0; i < seqArray.Length; i++)
67 RfcAttributeTypeAndValues seq = (RfcAttributeTypeAndValues) seqArray[i];
68 LdapAttribute attr = new LdapAttribute(((Asn1OctetString) seq.get_Renamed(0)).stringValue());
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++)
75 attr.addValue(((Asn1OctetString) setArray[j]).byteValue());
77 attrs.Add(attr);
80 return new LdapEntry(Asn1Object.RequestDN, attrs);
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)
95 return ;
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)
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())
111 LdapAttribute attr = (LdapAttribute) itr.Current;
112 Asn1SetOf vals = new Asn1SetOf(attr.size());
113 System.Collections.IEnumerator attrEnum = attr.ByteValues;
114 while (attrEnum.MoveNext())
116 vals.add(new RfcAttributeValue((sbyte[]) attrEnum.Current));
118 attrList.add(new RfcAttributeTypeAndValues(new RfcAttributeDescription(attr.Name), vals));
120 return attrList;
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()
129 return Asn1Object.ToString();