001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.shared.dsmlv2.request;
021
022
023import org.apache.directory.shared.dsmlv2.ParserUtils;
024import org.apache.directory.shared.ldap.codec.api.LdapApiService;
025import org.apache.directory.shared.ldap.model.entry.Attribute;
026import org.apache.directory.shared.ldap.model.entry.DefaultAttribute;
027import org.apache.directory.shared.ldap.model.entry.Entry;
028import org.apache.directory.shared.ldap.model.entry.Value;
029import org.apache.directory.shared.ldap.model.exception.LdapException;
030import org.apache.directory.shared.ldap.model.exception.MessageException;
031import org.apache.directory.shared.ldap.model.message.AddRequest;
032import org.apache.directory.shared.ldap.model.message.AddRequestImpl;
033import org.apache.directory.shared.ldap.model.message.AddResponse;
034import org.apache.directory.shared.ldap.model.message.Control;
035import org.apache.directory.shared.ldap.model.message.MessageTypeEnum;
036import org.apache.directory.shared.ldap.model.name.Dn;
037import org.dom4j.Element;
038import org.dom4j.Namespace;
039import org.dom4j.QName;
040
041
042/**
043 * DSML Decorator for AddRequest
044 *
045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046 */
047public class AddRequestDsml 
048    extends AbstractResultResponseRequestDsml<AddRequest, AddResponse> 
049    implements AddRequest
050{
051
052    /** The current attribute being decoded */
053    private Attribute currentAttribute;
054    
055    
056    /**
057     * Creates a new getDecoratedMessage() of AddRequestDsml.
058     */
059    public AddRequestDsml( LdapApiService codec )
060    {
061        super( codec, new AddRequestImpl() );
062    }
063
064
065    /**
066     * Creates a new getDecoratedMessage() of AddRequestDsml.
067    *
068    * @param ldapMessage
069    *      the message to decorate
070    */
071    public AddRequestDsml( LdapApiService codec, AddRequest ldapMessage )
072    {
073        super( codec, ldapMessage );
074    }
075
076    
077
078    
079    /**
080     * Create a new attributeValue
081     * 
082     * @param type The attribute's name (called 'type' in the grammar)
083     */
084    public void addAttributeType( String type ) throws LdapException
085    {
086        // do not create a new attribute if we have seen this attributeType before
087        if ( getDecorated().getEntry().get( type ) != null )
088        {
089            currentAttribute = getDecorated().getEntry().get( type );
090            return;
091        }
092
093        // fix this to use AttributeImpl(type.getString().toLowerCase())
094        currentAttribute = new DefaultAttribute( type );
095        getDecorated().getEntry().put( currentAttribute );
096    }
097
098
099    /**
100     * @return Returns the currentAttribute type.
101     */
102    public String getCurrentAttributeType()
103    {
104        return currentAttribute.getId();
105    }
106
107
108    /**
109     * Add a new value to the current attribute
110     * 
111     * @param value The value to add
112     */
113    public void addAttributeValue( String value ) throws LdapException
114    {
115        currentAttribute.add( value );
116    }
117
118
119    /**
120     * Add a new value to the current attribute
121     * 
122     * @param value The value to add
123     */
124    public void addAttributeValue( Value<?> value ) throws LdapException
125    {
126        currentAttribute.add( value );
127    }
128
129
130    /**
131     * Add a new value to the current attribute
132     * 
133     * @param value The value to add
134     */
135    public void addAttributeValue( byte[] value ) throws LdapException
136    {
137        currentAttribute.add( value );
138    }
139    
140    
141    /**
142     * {@inheritDoc}
143     */
144    public MessageTypeEnum getType()
145    {
146        return getDecorated().getType();
147    }
148
149
150    /**
151     * {@inheritDoc}
152     */
153    public Element toDsml( Element root )
154    {
155        Element element = super.toDsml( root );
156
157        // Dn
158        if ( getDecorated().getEntry() != null )
159        {
160            element.addAttribute( "dn", getDecorated().getEntry().getDn().getName() );
161        }
162
163        // Attributes
164        Entry entry = getDecorated().getEntry();
165        if ( entry != null )
166        {
167            for ( Attribute attribute : entry )
168            {
169                Element attributeElement = element.addElement( "attr" );
170                attributeElement.addAttribute( "name", attribute.getId() );
171                // Looping on Values
172                for ( Value<?> value : attribute )
173                {
174                    if ( ParserUtils.needsBase64Encoding( value.getValue() ) )
175                    {
176                        Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI );
177                        Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI );
178                        attributeElement.getDocument().getRootElement().add( xsdNamespace );
179                        attributeElement.getDocument().getRootElement().add( xsiNamespace );
180
181                        Element valueElement = attributeElement.addElement( "value" ).addText(
182                            ParserUtils.base64Encode( value.getValue() ) );
183                        valueElement
184                            .addAttribute( new QName( "type", xsiNamespace ), "xsd:" + ParserUtils.BASE64BINARY );
185                    }
186                    else
187                    {
188                        attributeElement.addElement( "value" ).addText( value.getString() );
189                    }
190                }
191            }
192        }
193
194        return element;
195    }
196
197
198    /**
199     * Initialize the Entry.
200     */
201    public void initEntry()
202    {
203        //( ( AddRequest ) getDecoratedMessage() ).initEntry();
204    }
205
206
207    /**
208     * Get the entry with its attributes.
209     * 
210     * @return Returns the entry.
211     */
212    public Entry getEntry()
213    {
214        return getDecorated().getEntry();
215    }
216
217
218    /**
219     * Add a new value to the current attribute
220     * 
221     * @param value The value to be added
222     */
223    public void addAttributeValue( Object value ) throws LdapException
224    {
225        if ( value instanceof Value<?> )
226        {
227            ( ( AddRequestDsml ) getDecorated() ).addAttributeValue( (Value<?>) value );
228        }
229        else if ( value instanceof String )
230        {
231            ( ( AddRequestDsml ) getDecorated() ).addAttributeValue( ( String ) value );
232        }
233        else if ( value instanceof byte[] )
234        {
235            ( ( AddRequestDsml ) getDecorated() ).addAttributeValue( ( byte[] ) value );
236        }
237    }
238
239
240    /**
241     * Get the added Dn
242     * 
243     * @return Returns the entry Dn.
244     */
245    public Dn getEntryDn()
246    {
247        return getDecorated().getEntryDn();
248    }
249
250
251    /**
252     * {@inheritDoc}
253     */
254    public AddRequest setEntryDn( Dn entryDn )
255    {
256        getDecorated().setEntryDn( entryDn );
257        
258        return this;
259    }
260
261
262    /**
263     * {@inheritDoc}
264     */
265    public AddRequest setEntry( Entry entry )
266    {
267        getDecorated().setEntry( entry );
268        
269        return this;
270    }
271    /**
272     * {@inheritDoc}
273     */
274    public AddRequest setMessageId( int messageId )
275    {
276        super.setMessageId( messageId );
277        
278        return this;
279    }
280
281    
282    /**
283     * {@inheritDoc}
284     */
285    public AddRequest addControl( Control control ) throws MessageException
286    {
287        return (AddRequest)super.addControl( control );
288    }
289    
290    
291    /**
292     * {@inheritDoc}
293     */
294    public AddRequest addAllControls( Control[] controls ) throws MessageException
295    {
296        return (AddRequest)super.addAllControls( controls );
297    }
298    
299    
300    /**
301     * {@inheritDoc}
302     */
303    public AddRequest removeControl( Control control ) throws MessageException
304    {
305        return (AddRequest)super.removeControl( control );
306    }
307
308
309    /**
310     * {@inheritDoc}
311     */
312    public MessageTypeEnum getResponseType()
313    {
314        return getDecorated().getResponseType();
315    }
316}