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