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.ldap.model.message;
021
022
023/**
024 * ExtendedRequest implementation.
025 * 
026 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
027 */
028public abstract class AbstractExtendedRequest extends AbstractRequest implements ExtendedRequest
029{
030    static final long serialVersionUID = 7916990159044177480L;
031
032    /** Extended request's Object Identifier or <b>requestName</b> */
033    private String oid;
034
035    /** The associated response */
036    protected ExtendedResponse response;
037
038
039    /**
040     * Creates an ExtendedRequest implementing object used to perform
041     * extended protocol operation on the server.
042     */
043    public AbstractExtendedRequest()
044    {
045        super( -1, MessageTypeEnum.EXTENDED_REQUEST, true );
046    }
047
048
049    /**
050     * Creates an ExtendedRequest implementing object used to perform
051     * extended protocol operation on the server.
052     * 
053     * @param id the sequential message identifier
054     */
055    public AbstractExtendedRequest( final int id )
056    {
057        super( id, MessageTypeEnum.EXTENDED_REQUEST, true );
058    }
059
060
061    // -----------------------------------------------------------------------
062    // ExtendedRequest Interface Method Implementations
063    // -----------------------------------------------------------------------
064
065    /**
066     * Gets the Object Identifier corresponding to the extended request type.
067     * This is the <b>requestName</b> portion of the ext. req. PDU.
068     * 
069     * @return the dotted-decimal representation as a String of the OID
070     */
071    public String getRequestName()
072    {
073        return oid;
074    }
075
076
077    /**
078     * Sets the Object Identifier corresponding to the extended request type.
079     * 
080     * @param newOid the dotted-decimal representation as a String of the OID
081     */
082    public ExtendedRequest setRequestName( String newOid )
083    {
084        this.oid = newOid;
085
086        return this;
087    }
088
089
090    /**
091     * {@inheritDoc}
092     */
093    public ExtendedRequest setMessageId( int messageId )
094    {
095        super.setMessageId( messageId );
096
097        return this;
098    }
099
100
101    /**
102     * {@inheritDoc}
103     */
104    public ExtendedRequest addControl( Control control )
105    {
106        return ( ExtendedRequest ) super.addControl( control );
107    }
108
109
110    /**
111     * {@inheritDoc}
112     */
113    public ExtendedRequest addAllControls( Control[] controls )
114    {
115        return ( ExtendedRequest ) super.addAllControls( controls );
116    }
117
118
119    /**
120     * {@inheritDoc}
121     */
122    public ExtendedRequest removeControl( Control control )
123    {
124        return ( ExtendedRequest ) super.removeControl( control );
125    }
126
127
128    // ------------------------------------------------------------------------
129    // SingleReplyRequest Interface Method Implementations
130    // ------------------------------------------------------------------------
131
132    /**
133     * Gets the protocol response message type for this request which produces
134     * at least one response.
135     * 
136     * @return the message type of the response.
137     */
138    public MessageTypeEnum getResponseType()
139    {
140        return MessageTypeEnum.EXTENDED_RESPONSE;
141    }
142
143
144    /**
145     * The result containing response for this request.
146     * 
147     * @return the result containing response for this request
148     */
149    public abstract ExtendedResponse getResultResponse();
150
151
152    /**
153     * {@inheritDoc}
154     */
155    @Override
156    public int hashCode()
157    {
158        int hash = 37;
159        if ( oid != null )
160        {
161            hash = hash * 17 + oid.hashCode();
162        }
163        hash = hash * 17 + super.hashCode();
164
165        return hash;
166    }
167
168
169    /**
170     * Checks to see if an object equals this ExtendedRequest.
171     * 
172     * @param obj the object to be checked for equality
173     * @return true if the obj equals this ExtendedRequest, false otherwise
174     */
175    public boolean equals( Object obj )
176    {
177        if ( obj == this )
178        {
179            return true;
180        }
181
182        if ( !super.equals( obj ) )
183        {
184            return false;
185        }
186
187        if ( !( obj instanceof ExtendedRequest ) )
188        {
189            return false;
190        }
191
192        ExtendedRequest req = ( ExtendedRequest ) obj;
193
194        if ( ( oid != null ) && ( req.getRequestName() == null ) )
195        {
196            return false;
197        }
198
199        if ( ( oid == null ) && ( req.getRequestName() != null ) )
200        {
201            return false;
202        }
203
204        if ( ( oid != null ) && ( req.getRequestName() != null ) && !oid.equals( req.getRequestName() ) )
205        {
206            return false;
207        }
208
209        return true;
210    }
211
212
213    /**
214     * Get a String representation of an Extended Request
215     * 
216     * @return an Extended Request String
217     */
218    public String toString()
219    {
220        StringBuffer sb = new StringBuffer();
221
222        sb.append( "    Extended request\n" );
223        sb.append( "        Request name : '" ).append( oid ).append( "'\n" );
224
225        // The controls
226        sb.append( super.toString() );
227
228        return super.toString( sb.toString() );
229    }
230}