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    private 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    @Override
072    public String getRequestName()
073    {
074        return oid;
075    }
076
077
078    /**
079     * Sets the Object Identifier corresponding to the extended request type.
080     * 
081     * @param newOid the dotted-decimal representation as a String of the OID
082     */
083    @Override
084    public ExtendedRequest setRequestName( String newOid )
085    {
086        this.oid = newOid;
087
088        return this;
089    }
090
091
092    /**
093     * {@inheritDoc}
094     */
095    @Override
096    public ExtendedRequest setMessageId( int messageId )
097    {
098        super.setMessageId( messageId );
099
100        return this;
101    }
102
103
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public ExtendedRequest addControl( Control control )
109    {
110        return ( ExtendedRequest ) super.addControl( control );
111    }
112
113
114    /**
115     * {@inheritDoc}
116     */
117    @Override
118    public ExtendedRequest addAllControls( Control[] controls )
119    {
120        return ( ExtendedRequest ) super.addAllControls( controls );
121    }
122
123
124    /**
125     * {@inheritDoc}
126     */
127    @Override
128    public ExtendedRequest removeControl( Control control )
129    {
130        return ( ExtendedRequest ) super.removeControl( control );
131    }
132
133
134    // ------------------------------------------------------------------------
135    // SingleReplyRequest Interface Method Implementations
136    // ------------------------------------------------------------------------
137
138    /**
139     * Gets the protocol response message type for this request which produces
140     * at least one response.
141     * 
142     * @return the message type of the response.
143     */
144    @Override
145    public MessageTypeEnum getResponseType()
146    {
147        return MessageTypeEnum.EXTENDED_RESPONSE;
148    }
149
150
151    /**
152     * The result containing response for this request.
153     * 
154     * @return the result containing response for this request
155     */
156    @Override
157    public abstract ExtendedResponse getResultResponse();
158
159
160    /**
161     * @return the response
162     */
163    public ExtendedResponse getResponse()
164    {
165        return response;
166    }
167
168
169    /**
170     * @param response the response to set
171     */
172    public void setResponse( ExtendedResponse response )
173    {
174        this.response = response;
175    }
176
177
178    /**
179     * {@inheritDoc}
180     */
181    @Override
182    public int hashCode()
183    {
184        int hash = 37;
185        if ( oid != null )
186        {
187            hash = hash * 17 + oid.hashCode();
188        }
189        hash = hash * 17 + super.hashCode();
190
191        return hash;
192    }
193
194
195    /**
196     * Checks to see if an object equals this ExtendedRequest.
197     * 
198     * @param obj the object to be checked for equality
199     * @return true if the obj equals this ExtendedRequest, false otherwise
200     */
201    @Override
202    public boolean equals( Object obj )
203    {
204        if ( obj == this )
205        {
206            return true;
207        }
208
209        if ( !super.equals( obj ) )
210        {
211            return false;
212        }
213
214        if ( !( obj instanceof ExtendedRequest ) )
215        {
216            return false;
217        }
218
219        ExtendedRequest req = ( ExtendedRequest ) obj;
220
221        if ( ( oid != null ) && ( req.getRequestName() == null ) )
222        {
223            return false;
224        }
225
226        if ( ( oid == null ) && ( req.getRequestName() != null ) )
227        {
228            return false;
229        }
230
231        if ( ( oid != null ) && ( req.getRequestName() != null ) && !oid.equals( req.getRequestName() ) )
232        {
233            return false;
234        }
235
236        return true;
237    }
238
239
240    /**
241     * Get a String representation of an Extended Request
242     * 
243     * @return an Extended Request String
244     */
245    @Override
246    public String toString()
247    {
248        StringBuilder sb = new StringBuilder();
249
250        sb.append( "    Extended request\n" );
251        sb.append( "        Request name : '" ).append( oid ).append( "'\n" );
252
253        // The controls
254        sb.append( super.toString() );
255
256        return super.toString( sb.toString() );
257    }
258}