1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.dsmlv2.request;
21
22
23 import org.apache.directory.api.dsmlv2.ParserUtils;
24 import org.apache.directory.api.ldap.codec.api.LdapApiService;
25 import org.apache.directory.api.ldap.model.entry.Attribute;
26 import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
27 import org.apache.directory.api.ldap.model.entry.Entry;
28 import org.apache.directory.api.ldap.model.entry.Value;
29 import org.apache.directory.api.ldap.model.exception.LdapException;
30 import org.apache.directory.api.ldap.model.message.AddRequest;
31 import org.apache.directory.api.ldap.model.message.AddRequestImpl;
32 import org.apache.directory.api.ldap.model.message.AddResponse;
33 import org.apache.directory.api.ldap.model.message.Control;
34 import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
35 import org.apache.directory.api.ldap.model.name.Dn;
36 import org.dom4j.Element;
37 import org.dom4j.Namespace;
38 import org.dom4j.QName;
39
40
41
42
43
44
45
46 public class AddRequestDsml
47 extends AbstractResultResponseRequestDsml<AddRequest, AddResponse>
48 implements AddRequest
49 {
50
51
52 private Attribute currentAttribute;
53
54
55
56
57
58
59
60 public AddRequestDsml( LdapApiService codec )
61 {
62 super( codec, new AddRequestImpl() );
63 }
64
65
66
67
68
69
70
71
72 public AddRequestDsml( LdapApiService codec, AddRequest ldapMessage )
73 {
74 super( codec, ldapMessage );
75 }
76
77
78
79
80
81
82
83
84 public void addAttributeType( String type ) throws LdapException
85 {
86
87 if ( getDecorated().getEntry().get( type ) != null )
88 {
89 currentAttribute = getDecorated().getEntry().get( type );
90 return;
91 }
92
93
94 currentAttribute = new DefaultAttribute( type );
95 getDecorated().getEntry().put( currentAttribute );
96 }
97
98
99
100
101
102 public String getCurrentAttributeType()
103 {
104 return currentAttribute.getId();
105 }
106
107
108
109
110
111
112
113
114 public void addAttributeValue( String value ) throws LdapException
115 {
116 currentAttribute.add( value );
117 }
118
119
120
121
122
123
124
125
126 public void addAttributeValue( Value<?> value ) throws LdapException
127 {
128 currentAttribute.add( value );
129 }
130
131
132
133
134
135
136
137
138 public void addAttributeValue( byte[] value ) throws LdapException
139 {
140 currentAttribute.add( value );
141 }
142
143
144
145
146
147 public MessageTypeEnum getType()
148 {
149 return getDecorated().getType();
150 }
151
152
153
154
155
156 public Element toDsml( Element root )
157 {
158 Element element = super.toDsml( root );
159
160
161 if ( getDecorated().getEntry() != null )
162 {
163 element.addAttribute( "dn", getDecorated().getEntry().getDn().getName() );
164 }
165
166
167 Entry entry = getDecorated().getEntry();
168 if ( entry != null )
169 {
170 for ( Attribute attribute : entry )
171 {
172 Element attributeElement = element.addElement( "attr" );
173 attributeElement.addAttribute( "name", attribute.getId() );
174
175 for ( Value<?> value : attribute )
176 {
177 if ( ParserUtils.needsBase64Encoding( value.getValue() ) )
178 {
179 Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI );
180 Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI );
181 attributeElement.getDocument().getRootElement().add( xsdNamespace );
182 attributeElement.getDocument().getRootElement().add( xsiNamespace );
183
184 Element valueElement = attributeElement.addElement( "value" ).addText(
185 ParserUtils.base64Encode( value.getValue() ) );
186 valueElement
187 .addAttribute( new QName( "type", xsiNamespace ), "xsd:" + ParserUtils.BASE64BINARY );
188 }
189 else
190 {
191 attributeElement.addElement( "value" ).addText( value.getString() );
192 }
193 }
194 }
195 }
196
197 return element;
198 }
199
200
201
202
203
204 public void initEntry()
205 {
206 }
207
208
209
210
211
212
213
214 public Entry getEntry()
215 {
216 return getDecorated().getEntry();
217 }
218
219
220
221
222
223
224
225
226 public void addAttributeValue( Object value ) throws LdapException
227 {
228 if ( value instanceof Value<?> )
229 {
230 ( ( AddRequestDsml ) getDecorated() ).addAttributeValue( ( Value<?> ) value );
231 }
232 else if ( value instanceof String )
233 {
234 ( ( AddRequestDsml ) getDecorated() ).addAttributeValue( ( String ) value );
235 }
236 else if ( value instanceof byte[] )
237 {
238 ( ( AddRequestDsml ) getDecorated() ).addAttributeValue( ( byte[] ) value );
239 }
240 }
241
242
243
244
245
246
247
248 public Dn getEntryDn()
249 {
250 return getDecorated().getEntryDn();
251 }
252
253
254
255
256
257 public AddRequest setEntryDn( Dn entryDn )
258 {
259 getDecorated().setEntryDn( entryDn );
260
261 return this;
262 }
263
264
265
266
267
268 public AddRequest setEntry( Entry entry )
269 {
270 getDecorated().setEntry( entry );
271
272 return this;
273 }
274
275
276
277
278
279 public AddRequest setMessageId( int messageId )
280 {
281 super.setMessageId( messageId );
282
283 return this;
284 }
285
286
287
288
289
290 public AddRequest addControl( Control control )
291 {
292 return ( AddRequest ) super.addControl( control );
293 }
294
295
296
297
298
299 public AddRequest addAllControls( Control[] controls )
300 {
301 return ( AddRequest ) super.addAllControls( controls );
302 }
303
304
305
306
307
308 public AddRequest removeControl( Control control )
309 {
310 return ( AddRequest ) super.removeControl( control );
311 }
312
313
314
315
316
317 public MessageTypeEnum getResponseType()
318 {
319 return getDecorated().getResponseType();
320 }
321 }