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