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