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.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 the root element
063     * @return the Request Element of the given name containing
064     */
065    @Override
066    public Element toDsml( Element root )
067    {
068        Element element = root.addElement( getRequestName() );
069
070        // Request ID
071        int requestID = getDecorated().getMessageId();
072        if ( requestID > 0 )
073        {
074            element.addAttribute( "requestID", Integer.toString( requestID ) );
075        }
076
077        // Controls
078        ParserUtils.addControls( getCodecService(), element, getDecorated().getControls().values(), false );
079
080        return element;
081    }
082
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public int computeLength()
089    {
090        return 0;
091    }
092
093
094    /**
095     * {@inheritDoc}
096     */
097    @Override
098    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
099    {
100        return null;
101    }
102
103
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public ResultResponse getResultResponse()
109    {
110        return getDecorated().getResultResponse();
111    }
112
113
114    /**
115     * {@inheritDoc}
116     */
117    @Override
118    public void abandon()
119    {
120        ( ( AbandonableRequest ) getDecorated() ).abandon();
121    }
122
123
124    /**
125     * {@inheritDoc}
126     */
127    @Override
128    public boolean isAbandoned()
129    {
130        return ( ( AbandonableRequest ) getDecorated() ).isAbandoned();
131    }
132
133
134    /**
135     * {@inheritDoc}
136     */
137    @Override
138    public AbandonableRequest addAbandonListener( AbandonListener listener )
139    {
140        ( ( AbandonableRequest ) getDecorated() ).addAbandonListener( listener );
141
142        return this;
143    }
144}