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.ParserUtils;
027import org.apache.directory.api.ldap.codec.api.LdapApiService;
028import org.apache.directory.api.ldap.model.message.AbandonListener;
029import org.apache.directory.api.ldap.model.message.AbandonableRequest;
030import org.apache.directory.api.ldap.model.message.ResultResponse;
031import org.apache.directory.api.ldap.model.message.ResultResponseRequest;
032import org.dom4j.Element;
033
034
035/**
036 * Abstract class for DSML requests.
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public abstract class AbstractResultResponseRequestDsml<E extends ResultResponseRequest, F extends ResultResponse>
041    extends AbstractRequestDsml<E>
042    implements ResultResponseRequest, AbandonableRequest
043{
044    /**
045     * Creates a new instance of AbstractRequestDsml.
046     *
047     * @param ldapMessage
048     *      the message to decorate
049     */
050    public AbstractResultResponseRequestDsml( LdapApiService codec, E ldapMessage )
051    {
052        super( codec, ldapMessage );
053    }
054
055
056    /**
057     * Creates the Request Element and adds RequestID and Controls.
058     *
059     * @param root
060     *      the root element
061     * @return
062     *      the Request Element of the given name containing
063     */
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( "requestID", "" + requestID );
073        }
074
075        // Controls
076        ParserUtils.addControls( getCodecService(), element, getDecorated().getControls().values() );
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
086     *      the name of the request according to the type of the decorated element.
087     */
088    private String getRequestName()
089    {
090        switch ( getDecorated().getType() )
091        {
092            case ABANDON_REQUEST:
093                return "abandonRequest";
094
095            case ADD_REQUEST:
096                return "addRequest";
097
098            case BIND_REQUEST:
099                return "authRequest";
100
101            case COMPARE_REQUEST:
102                return "compareRequest";
103
104            case DEL_REQUEST:
105                return "delRequest";
106
107            case EXTENDED_REQUEST:
108                return "extendedRequest";
109
110            case MODIFYDN_REQUEST:
111                return "modDNRequest";
112
113            case MODIFY_REQUEST:
114                return "modifyRequest";
115
116            case SEARCH_REQUEST:
117                return "searchRequest";
118
119            default:
120                return "error";
121        }
122    }
123
124
125    public int computeLength()
126    {
127        return 0;
128    }
129
130
131    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
132    {
133        return null;
134    }
135
136
137    /**
138     * {@inheritDoc}
139     */
140    public ResultResponse getResultResponse()
141    {
142        return getDecorated().getResultResponse();
143    }
144
145
146    /**
147     * {@inheritDoc}
148     */
149    public void abandon()
150    {
151        ( ( AbandonableRequest ) getDecorated() ).abandon();
152    }
153
154
155    /**
156     * {@inheritDoc}
157     */
158    public boolean isAbandoned()
159    {
160        return ( ( AbandonableRequest ) getDecorated() ).isAbandoned();
161    }
162
163
164    /**
165     * {@inheritDoc}
166     */
167    public AbandonableRequest addAbandonListener( AbandonListener listener )
168    {
169        ( ( AbandonableRequest ) getDecorated() ).addAbandonListener( listener );
170
171        return this;
172    }
173}