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 ldapMessage
046     *      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
058     *      the root element
059     * @return
060     *      the Request Element of the given name containing
061     */
062    public Element toDsml( Element root )
063    {
064        Element element = root.addElement( getRequestName() );
065
066        // Request ID
067        int requestID = getDecorated().getMessageId();
068        if ( requestID > 0 )
069        {
070            element.addAttribute( "requestID", "" + requestID );
071        }
072
073        // Controls
074        ParserUtils.addControls( getCodecService(), element, getDecorated().getControls().values() );
075
076        return element;
077    }
078
079
080    /**
081     * Gets the name of the request according to the type of the decorated element.
082     *
083     * @return
084     *      the name of the request according to the type of the decorated element.
085     */
086    private String getRequestName()
087    {
088        switch ( getDecorated().getType() )
089        {
090            case ABANDON_REQUEST:
091                return "abandonRequest";
092
093            case ADD_REQUEST:
094                return "addRequest";
095
096            case BIND_REQUEST:
097                return "authRequest";
098
099            case COMPARE_REQUEST:
100                return "compareRequest";
101
102            case DEL_REQUEST:
103                return "delRequest";
104
105            case EXTENDED_REQUEST:
106                return "extendedRequest";
107
108            case MODIFYDN_REQUEST:
109                return "modDNRequest";
110
111            case MODIFY_REQUEST:
112                return "modifyRequest";
113
114            case SEARCH_REQUEST:
115                return "searchRequest";
116
117            default:
118                return "error";
119        }
120    }
121
122
123    public int computeLength()
124    {
125        return 0;
126    }
127
128
129    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
130    {
131        return null;
132    }
133
134
135    /**
136     * {@inheritDoc}
137     */
138    public boolean hasResponse()
139    {
140        return getDecorated().hasResponse();
141    }
142}