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 org.apache.directory.shared.dsmlv2.DsmlDecorator;
024    import org.apache.directory.shared.dsmlv2.ParserUtils;
025    import org.apache.directory.shared.ldap.codec.LdapMessageCodec;
026    import org.dom4j.Element;
027    
028    
029    public abstract class AbstractRequestDsml extends LdapRequestDecorator implements DsmlDecorator
030    {
031        /**
032         * Creates a new instance of AbstractRequestDsml.
033         *
034         * @param ldapMessage
035         *      the message to decorate
036         */
037        public AbstractRequestDsml( LdapMessageCodec ldapMessage )
038        {
039            super( ldapMessage );
040        }
041    
042    
043        /**
044         * Creates the Request Element and adds RequestID and Controls.
045         *
046         * @param root
047         *      the root element
048         * @return
049         *      the Request Element of the given name containing
050         */
051        public Element toDsml( Element root )
052        {
053            Element element = root.addElement( getRequestName() );
054            
055            // Request ID
056            int requestID = instance.getMessageId();
057            if ( requestID != 0 )
058            {
059                element.addAttribute( "requestID", "" + requestID );
060            }
061    
062            // Controls
063            ParserUtils.addControls( element, instance.getControls() );
064            
065            return element;
066        }
067        
068        /**
069         * Gets the name of the request according to the type of the decorated element.
070         *
071         * @return
072         *      the name of the request according to the type of the decorated element.
073         */
074        private String getRequestName()
075        {
076            switch ( instance.getMessageType() )
077            {
078                case ABANDON_REQUEST:
079                    return "abandonRequest";
080                    
081                case ADD_REQUEST:
082                    return "addRequest";
083                    
084                case BIND_REQUEST:
085                    return "authRequest";
086                    
087                case COMPARE_REQUEST:
088                    return "compareRequest";
089                    
090                case DEL_REQUEST:
091                    return "delRequest";
092                    
093                case EXTENDED_REQUEST:
094                    return "extendedRequest";
095                    
096                case MODIFYDN_REQUEST:
097                    return "modDNRequest";
098                    
099                case MODIFY_REQUEST:
100                    return "modifyRequest";
101                    
102                case SEARCH_REQUEST:
103                    return "searchRequest";
104                    
105                default:
106                    return "error";
107            }
108        }
109    }