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.ldap.model.message;
021
022
023/**
024 * An Abstract ExtendedResponse implementation.
025 * 
026 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
027 */
028public abstract class AbstractExtendedResponse extends AbstractResultResponse implements ExtendedResponse
029{
030    static final long serialVersionUID = -6646752766410531060L;
031
032    /** Object identifier for the extended response */
033    protected String responseName;
034    
035    /** Extended response message type enumeration value */
036    private static final MessageTypeEnum TYPE = MessageTypeEnum.EXTENDED_RESPONSE;
037
038    /**
039     * Creates an ExtendedResponse as a reply to an ExtendedRequest.
040     * 
041     * @param responseName the ExtendedResponse's name
042     */
043    public AbstractExtendedResponse( String responseName )
044    {
045        super( -1, TYPE );
046        this.responseName = responseName;
047    }
048
049
050    /**
051     * Creates an ExtendedResponse as a reply to an ExtendedRequest.
052     * 
053     * @param id the session unique message id
054     * @param responseName the ExtendedResponse's name
055     */
056    public AbstractExtendedResponse( final int id, String responseName )
057    {
058        super( id, TYPE );
059        this.responseName = responseName;
060    }
061
062
063    /**
064     * Creates an ExtendedResponse as a reply to an ExtendedRequest.
065     * 
066     * @param id the session unique message id
067     */
068    public AbstractExtendedResponse( int id )
069    {
070        super( id, TYPE );
071    }
072
073
074    // ------------------------------------------------------------------------
075    // ExtendedResponse Interface Method Implementations
076    // ------------------------------------------------------------------------
077
078    /**
079     * Gets the OID uniquely identifying this extended response (a.k.a. its
080     * name).
081     * 
082     * @return the responseName of the extended response
083     */
084    @Override
085    public String getResponseName()
086    {
087        return ( responseName == null ) ? "" : responseName;
088    }
089
090
091    /**
092     * Sets the OID uniquely identifying this extended response (a.k.a. its
093     * name).
094     * 
095     * @param responseName the OID of the extended response type.
096     */
097    @Override
098    public void setResponseName( String responseName )
099    {
100        this.responseName = responseName;
101    }
102
103
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public int hashCode()
109    {
110        int hash = 37;
111
112        if ( responseName != null )
113        {
114            hash = hash * 17 + responseName.hashCode();
115        }
116
117        hash = hash * 17 + super.hashCode();
118
119        return hash;
120    }
121
122
123    /**
124     * Checks to see if an object equals this ExtendedRequest.
125     * 
126     * @param obj
127     *            the object to be checked for equality
128     * @return true if the obj equals this ExtendedRequest, false otherwise
129     */
130    @Override
131    public boolean equals( Object obj )
132    {
133        if ( obj == this )
134        {
135            return true;
136        }
137
138        if ( !super.equals( obj ) )
139        {
140            return false;
141        }
142
143        if ( !( obj instanceof ExtendedResponse ) )
144        {
145            return false;
146        }
147
148        ExtendedResponse resp = ( ExtendedResponse ) obj;
149
150        if ( ( responseName != null ) && ( resp.getResponseName() == null ) )
151        {
152            return false;
153        }
154
155        if ( ( responseName == null ) && ( resp.getResponseName() != null ) )
156        {
157            return false;
158        }
159
160        if ( ( responseName != null ) && ( resp.getResponseName() != null )
161            && !responseName.equals( resp.getResponseName() ) )
162        {
163            return false;
164        }
165
166        return true;
167    }
168
169
170    /**
171     * Get a String representation of an ExtendedResponse
172     * 
173     * @return An ExtendedResponse String
174     */
175    @Override
176    public String toString()
177    {
178        StringBuilder sb = new StringBuilder();
179
180        sb.append( "    Extended Response\n" );
181
182        if ( responseName != null )
183        {
184            sb.append( "        ResponseName :'" ).append( responseName ).append( "'\n" );
185        }
186
187        sb.append( super.toString() );
188
189        return super.toString( sb.toString() );
190    }
191}