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 *    https://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.DsmlLiterals;
028import org.apache.directory.api.dsmlv2.ParserUtils;
029import org.apache.directory.api.ldap.codec.api.LdapApiService;
030import org.apache.directory.api.ldap.model.message.Request;
031import org.dom4j.Element;
032
033
034/**
035 * Abstract class for DSML requests.
036 *
037 * @param <E> The request type
038 *
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041public abstract class AbstractRequestDsml<E extends Request>
042    extends AbstractDsmlMessageDecorator<E>
043    implements Request
044{
045    /**
046     * Creates a new instance of AbstractRequestDsml.
047     *
048     * @param codec The LDAP Service to use
049     * @param ldapMessage the message to decorate
050     */
051    public AbstractRequestDsml( LdapApiService codec, E ldapMessage )
052    {
053        super( codec, ldapMessage );
054    }
055
056
057    /**
058     * Creates the Request Element and adds RequestID and Controls.
059     *
060     * @param root the root element
061     * @return the Request Element of the given name containing
062     */
063    @Override
064    public Element toDsml( Element root )
065    {
066        Element element = root.addElement( getRequestName() );
067
068        // Request ID
069        int requestID = getDecorated().getMessageId();
070        if ( requestID > 0 )
071        {
072            element.addAttribute( DsmlLiterals.REQUEST_ID, Integer.toString( requestID ) );
073        }
074
075        // Controls
076        ParserUtils.addControls( getCodecService(), element, getDecorated().getControls().values(), true );
077
078        return element;
079    }
080
081
082    /**
083     * Gets the name of the request according to the type of the decorated element.
084     *
085     * @return the name of the request according to the type of the decorated element.
086     */
087    protected String getRequestName()
088    {
089        switch ( getDecorated().getType() )
090        {
091            case ABANDON_REQUEST:
092                return DsmlLiterals.ABANDON_REQUEST;
093
094            case ADD_REQUEST:
095                return DsmlLiterals.ADD_REQUEST;
096
097            case BIND_REQUEST:
098                return DsmlLiterals.AUTH_REQUEST;
099
100            case COMPARE_REQUEST:
101                return DsmlLiterals.COMPARE_REQUEST;
102
103            case DEL_REQUEST:
104                return DsmlLiterals.DEL_REQUEST;
105
106            case EXTENDED_REQUEST:
107                return DsmlLiterals.EXTENDED_REQUEST;
108
109            case MODIFYDN_REQUEST:
110                return DsmlLiterals.MOD_DN_REQUEST;
111
112            case MODIFY_REQUEST:
113                return DsmlLiterals.MODIFY_REQUEST;
114
115            case SEARCH_REQUEST:
116                return DsmlLiterals.SEARCH_REQUEST;
117
118            default:
119                return DsmlLiterals.ERROR;
120        }
121    }
122
123
124    /**
125     * @return the buffer's length (always 0)
126     */
127    public int computeLength()
128    {
129        return 0;
130    }
131
132
133    /**
134     * Encode the request. Always return an empty buffer.
135     *
136     * @param buffer The buffer to allocate
137     * @return The resulting buffer
138     * @throws EncoderException If we had an error while encoding the request
139     */
140    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
141    {
142        return null;
143    }
144
145
146    /**
147     * {@inheritDoc}
148     */
149    @Override
150    public boolean hasResponse()
151    {
152        return getDecorated().hasResponse();
153    }
154}