View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
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   * DSML Decorator for AddRequest
45   *
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   */
48  public class AddRequestDsml
49      extends AbstractResultResponseRequestDsml<AddRequest, AddResponse>
50      implements AddRequest
51  {
52  
53      /** The current attribute being decoded */
54      private Attribute currentAttribute;
55  
56  
57      /**
58       * Creates a new getDecoratedMessage() of AddRequestDsml.
59       * 
60       * @param codec The LDAP Service to use
61       */
62      public AddRequestDsml( LdapApiService codec )
63      {
64          super( codec, new AddRequestImpl() );
65      }
66  
67  
68      /**
69       * Creates a new getDecoratedMessage() of AddRequestDsml.
70      *
71       * @param codec The LDAP Service to use
72      * @param ldapMessage the message to decorate
73      */
74      public AddRequestDsml( LdapApiService codec, AddRequest ldapMessage )
75      {
76          super( codec, ldapMessage );
77      }
78  
79  
80      /**
81       * Create a new attributeValue
82       * 
83       * @param type The attribute's name (called 'type' in the grammar)
84       * @throws LdapException If we can't add the type
85       */
86      public void addAttributeType( String type ) throws LdapException
87      {
88          // do not create a new attribute if we have seen this attributeType before
89          if ( getDecorated().getEntry().get( type ) != null )
90          {
91              currentAttribute = getDecorated().getEntry().get( type );
92              return;
93          }
94  
95          // fix this to use AttributeImpl(type.getString().toLowerCase())
96          currentAttribute = new DefaultAttribute( type );
97          getDecorated().getEntry().put( currentAttribute );
98      }
99  
100 
101     /**
102      * @return Returns the currentAttribute type.
103      */
104     public String getCurrentAttributeType()
105     {
106         return currentAttribute.getId();
107     }
108 
109 
110     /**
111      * Add a new value to the current attribute
112      * 
113      * @param value The value to add
114      * @throws LdapException If we can't add a new value
115      */
116     public void addAttributeValue( String value ) throws LdapException
117     {
118         currentAttribute.add( value );
119     }
120 
121 
122     /**
123      * Add a new value to the current attribute
124      * 
125      * @param value The value to add
126      * @throws LdapException If we can't add a new value
127      */
128     public void addAttributeValue( Value value ) throws LdapException
129     {
130         currentAttribute.add( value );
131     }
132 
133 
134     /**
135      * Add a new value to the current attribute
136      * 
137      * @param value The value to add
138      * @throws LdapException If we can't add a new value
139      */
140     public void addAttributeValue( byte[] value ) throws LdapException
141     {
142         currentAttribute.add( value );
143     }
144 
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public MessageTypeEnum getType()
151     {
152         return getDecorated().getType();
153     }
154 
155 
156     /**
157      * {@inheritDoc}
158      */
159     @Override
160     public Element toDsml( Element root )
161     {
162         Element element = super.toDsml( root );
163 
164         // Dn
165         if ( getDecorated().getEntry() != null )
166         {
167             element.addAttribute( DsmlLiterals.DN, getDecorated().getEntry().getDn().getName() );
168         }
169 
170         // Attributes
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                 // Looping on Values
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      * Initialize the Entry.
208      */
209     public void initEntry()
210     {
211     }
212 
213 
214     /**
215      * Get the entry with its attributes.
216      * 
217      * @return Returns the entry.
218      */
219     @Override
220     public Entry getEntry()
221     {
222         return getDecorated().getEntry();
223     }
224 
225 
226     /**
227      * Add a new value to the current attribute
228      * 
229      * @param value The value to be added
230      * @throws LdapException If we can't add a new value
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      * Get the added Dn
251      * 
252      * @return Returns the entry Dn.
253      */
254     @Override
255     public Dn getEntryDn()
256     {
257         return getDecorated().getEntryDn();
258     }
259 
260 
261     /**
262      * {@inheritDoc}
263      */
264     @Override
265     public AddRequest setEntryDn( Dn entryDn )
266     {
267         getDecorated().setEntryDn( entryDn );
268 
269         return this;
270     }
271 
272 
273     /**
274      * {@inheritDoc}
275      */
276     @Override
277     public AddRequest setEntry( Entry entry )
278     {
279         getDecorated().setEntry( entry );
280 
281         return this;
282     }
283 
284 
285     /**
286      * {@inheritDoc}
287      */
288     @Override
289     public AddRequest setMessageId( int messageId )
290     {
291         super.setMessageId( messageId );
292 
293         return this;
294     }
295 
296 
297     /**
298      * {@inheritDoc}
299      */
300     @Override
301     public AddRequest addControl( Control control )
302     {
303         return ( AddRequest ) super.addControl( control );
304     }
305 
306 
307     /**
308      * {@inheritDoc}
309      */
310     @Override
311     public AddRequest addAllControls( Control[] controls )
312     {
313         return ( AddRequest ) super.addAllControls( controls );
314     }
315 
316 
317     /**
318      * {@inheritDoc}
319      */
320     @Override
321     public AddRequest removeControl( Control control )
322     {
323         return ( AddRequest ) super.removeControl( control );
324     }
325 
326 
327     /**
328      * {@inheritDoc}
329      */
330     @Override
331     public MessageTypeEnum getResponseType()
332     {
333         return getDecorated().getResponseType();
334     }
335 }