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     */
020    package org.apache.directory.shared.dsmlv2.request;
021    
022    
023    import java.util.Iterator;
024    import java.util.List;
025    
026    import org.apache.directory.shared.dsmlv2.ParserUtils;
027    import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
028    import org.apache.directory.shared.ldap.codec.modify.ModifyRequestCodec;
029    import org.apache.directory.shared.ldap.entry.Modification;
030    import org.apache.directory.shared.ldap.entry.ModificationOperation;
031    import org.apache.directory.shared.ldap.entry.Value;
032    import org.dom4j.Element;
033    import org.dom4j.Namespace;
034    import org.dom4j.QName;
035    
036    
037    /**
038     * DSML Decorator for ModifyRequest
039     *
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev$, $Date$
042     */
043    public class ModifyRequestDsml extends AbstractRequestDsml
044    {
045        /**
046         * Creates a new instance of ModifyRequestDsml.
047         */
048        public ModifyRequestDsml()
049        {
050            super( new ModifyRequestCodec() );
051        }
052    
053    
054        /**
055         * Creates a new instance of ModifyRequestDsml.
056         *
057         * @param ldapMessage
058         *      the message to decorate
059         */
060        public ModifyRequestDsml( ModifyRequestCodec ldapMessage )
061        {
062            super( ldapMessage );
063        }
064    
065    
066        /**
067         * {@inheritDoc}
068         */
069        public MessageTypeEnum getMessageType()
070        {
071            return instance.getMessageType();
072        }
073    
074    
075        /**
076         * {@inheritDoc}
077         */
078        public Element toDsml( Element root )
079        {
080            Element element = super.toDsml( root );
081    
082            ModifyRequestCodec request = ( ModifyRequestCodec ) instance;
083    
084            // DN
085            if ( request.getObject() != null )
086            {
087                element.addAttribute( "dn", request.getObject().getName() );
088            }
089    
090            // Modifications
091            List<Modification> modifications = request.getModifications();
092    
093            for ( int i = 0; i < modifications.size(); i++ )
094            {
095                Modification modificationItem = modifications.get( i );
096    
097                Element modElement = element.addElement( "modification" );
098                if ( modificationItem.getAttribute() != null )
099                {
100                    modElement.addAttribute( "name", modificationItem.getAttribute().getId() );
101    
102                    Iterator<Value<?>> iterator = modificationItem.getAttribute().getAll();
103                    while ( iterator.hasNext() )
104                    {
105                        Value<?> value = iterator.next();
106    
107                        if ( value.get() != null )
108                        {
109                            if ( ParserUtils.needsBase64Encoding( value.get() ) )
110                            {
111                                Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI );
112                                Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI );
113                                element.getDocument().getRootElement().add( xsdNamespace );
114                                element.getDocument().getRootElement().add( xsiNamespace );
115    
116                                Element valueElement = modElement.addElement( "value" ).addText(
117                                    ParserUtils.base64Encode( value.get() ) );
118                                valueElement.addAttribute( new QName( "type", xsiNamespace ), "xsd:"
119                                    + ParserUtils.BASE64BINARY );
120                            }
121                            else
122                            {
123                                modElement.addElement( "value" ).setText( value.getString() );
124                            }
125                        }
126                    }
127                }
128    
129                ModificationOperation operation = modificationItem.getOperation();
130                if ( operation == ModificationOperation.ADD_ATTRIBUTE )
131                {
132                    modElement.addAttribute( "operation", "add" );
133                }
134                else if ( operation == ModificationOperation.REPLACE_ATTRIBUTE )
135                {
136                    modElement.addAttribute( "operation", "replace" );
137                }
138                else if ( operation == ModificationOperation.REMOVE_ATTRIBUTE )
139                {
140                    modElement.addAttribute( "operation", "delete" );
141                }
142            }
143    
144            return element;
145        }
146    }