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.api.dsmlv2.request;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.api.asn1.EncoderException;
026import org.apache.directory.api.dsmlv2.AbstractDsmlMessageDecorator;
027import org.apache.directory.api.dsmlv2.ParserUtils;
028import org.apache.directory.api.ldap.codec.api.LdapApiService;
029import org.apache.directory.api.ldap.model.message.Request;
030import org.dom4j.Element;
031
032
033/**
034 * Abstract class for DSML requests.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public abstract class AbstractRequestDsml<E extends Request>
039    extends AbstractDsmlMessageDecorator<E>
040    implements Request
041{
042    /**
043     * Creates a new instance of AbstractRequestDsml.
044     *
045     * @param codec The LDAP Service to use
046     * @param ldapMessage the message to decorate
047     */
048    public AbstractRequestDsml( LdapApiService codec, E ldapMessage )
049    {
050        super( codec, ldapMessage );
051    }
052
053
054    /**
055     * Creates the Request Element and adds RequestID and Controls.
056     *
057     * @param root the root element
058     * @return the Request Element of the given name containing
059     */
060    public Element toDsml( Element root )
061    {
062        Element element = root.addElement( getRequestName() );
063
064        // Request ID
065        int requestID = getDecorated().getMessageId();
066        if ( requestID > 0 )
067        {
068            element.addAttribute( "requestID", "" + requestID );
069        }
070
071        // Controls
072        ParserUtils.addControls( getCodecService(), element, getDecorated().getControls().values() );
073
074        return element;
075    }
076
077
078    /**
079     * Gets the name of the request according to the type of the decorated element.
080     *
081     * @return
082     *      the name of the request according to the type of the decorated element.
083     */
084    private String getRequestName()
085    {
086        switch ( getDecorated().getType() )
087        {
088            case ABANDON_REQUEST:
089                return "abandonRequest";
090
091            case ADD_REQUEST:
092                return "addRequest";
093
094            case BIND_REQUEST:
095                return "authRequest";
096
097            case COMPARE_REQUEST:
098                return "compareRequest";
099
100            case DEL_REQUEST:
101                return "delRequest";
102
103            case EXTENDED_REQUEST:
104                return "extendedRequest";
105
106            case MODIFYDN_REQUEST:
107                return "modDNRequest";
108
109            case MODIFY_REQUEST:
110                return "modifyRequest";
111
112            case SEARCH_REQUEST:
113                return "searchRequest";
114
115            default:
116                return "error";
117        }
118    }
119
120
121    public int computeLength()
122    {
123        return 0;
124    }
125
126
127    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
128    {
129        return null;
130    }
131
132
133    /**
134     * {@inheritDoc}
135     */
136    public boolean hasResponse()
137    {
138        return getDecorated().hasResponse();
139    }
140}