View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.message;
21  
22  
23  /**
24   * An Abstract ExtendedResponse implementation.
25   * 
26   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
27   */
28  public abstract class AbstractExtendedResponse extends AbstractResultResponse implements ExtendedResponse
29  {
30      static final long serialVersionUID = -6646752766410531060L;
31  
32      /** Object identifier for the extended response */
33      protected String responseName;
34      
35      /** Extended response message type enumeration value */
36      private static final MessageTypeEnum TYPE = MessageTypeEnum.EXTENDED_RESPONSE;
37  
38      /**
39       * Creates an ExtendedResponse as a reply to an ExtendedRequest.
40       * 
41       * @param responseName the ExtendedResponse's name
42       */
43      public AbstractExtendedResponse( String responseName )
44      {
45          super( -1, TYPE );
46          this.responseName = responseName;
47      }
48  
49  
50      /**
51       * Creates an ExtendedResponse as a reply to an ExtendedRequest.
52       * 
53       * @param id the session unique message id
54       * @param responseName the ExtendedResponse's name
55       */
56      public AbstractExtendedResponse( final int id, String responseName )
57      {
58          super( id, TYPE );
59          this.responseName = responseName;
60      }
61  
62  
63      /**
64       * Creates an ExtendedResponse as a reply to an ExtendedRequest.
65       * 
66       * @param id the session unique message id
67       */
68      public AbstractExtendedResponse( int id )
69      {
70          super( id, TYPE );
71      }
72  
73  
74      // ------------------------------------------------------------------------
75      // ExtendedResponse Interface Method Implementations
76      // ------------------------------------------------------------------------
77  
78      /**
79       * Gets the OID uniquely identifying this extended response (a.k.a. its
80       * name).
81       * 
82       * @return the responseName of the extended response
83       */
84      @Override
85      public String getResponseName()
86      {
87          return ( responseName == null ) ? "" : responseName;
88      }
89  
90  
91      /**
92       * Sets the OID uniquely identifying this extended response (a.k.a. its
93       * name).
94       * 
95       * @param responseName the OID of the extended response type.
96       */
97      @Override
98      public void setResponseName( String responseName )
99      {
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 }