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   *    http://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   * SearchResponseReference implementation
25   * 
26   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
27   */
28  public class SearchResultReferenceImpl extends AbstractResponse implements SearchResultReference
29  {
30      static final long serialVersionUID = 7423807019951309810L;
31  
32      /** Referral holding the reference urls */
33      private Referral referral = new ReferralImpl();
34  
35  
36      /**
37       * Creates a SearchResponseReference as a reply to an SearchRequest
38       * to indicate the end of a search operation.
39       */
40      public SearchResultReferenceImpl()
41      {
42          super( -1, MessageTypeEnum.SEARCH_RESULT_REFERENCE );
43      }
44  
45  
46      /**
47       * Creates a SearchResponseReference as a reply to an SearchRequest
48       * to indicate the end of a search operation.
49       * 
50       * @param id the session unique message id
51       */
52      public SearchResultReferenceImpl( final int id )
53      {
54          super( id, MessageTypeEnum.SEARCH_RESULT_REFERENCE );
55      }
56  
57  
58      // ------------------------------------------------------------------------
59      // SearchResponseReference Interface Method Implementations
60      // ------------------------------------------------------------------------
61  
62      /**
63       * Gets the sequence of LdapUrls as a Referral instance.
64       * 
65       * @return the sequence of LdapUrls
66       */
67      @Override
68      public Referral getReferral()
69      {
70          return this.referral;
71      }
72  
73  
74      /**
75       * Sets the sequence of LdapUrls as a Referral instance.
76       * 
77       * @param referral the sequence of LdapUrls
78       */
79      @Override
80      public void setReferral( Referral referral )
81      {
82          this.referral = referral;
83      }
84  
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public int hashCode()
91      {
92          int hash = 37;
93          if ( this.referral != null )
94          {
95              hash = hash * 17 + this.referral.hashCode();
96          }
97          hash = hash * 17 + super.hashCode();
98  
99          return hash;
100     }
101 
102 
103     /**
104      * Checks to see if an object is equal to this SearchResponseReference stub.
105      * 
106      * @param obj
107      *            the object to compare to this response stub
108      * @return true if the objects are equivalent false otherwise
109      */
110     @Override
111     public boolean equals( Object obj )
112     {
113         if ( obj == this )
114         {
115             return true;
116         }
117 
118         if ( !super.equals( obj ) )
119         {
120             return false;
121         }
122 
123         SearchResultReference resp = ( SearchResultReference ) obj;
124 
125         if ( referral != null && resp.getReferral() == null )
126         {
127             return false;
128         }
129 
130         if ( referral == null && resp.getReferral() != null )
131         {
132             return false;
133         }
134 
135         return referral == null || resp.getReferral() == null || referral.equals( resp.getReferral() );
136     }
137 
138 
139     /**
140      * Returns the Search Result Reference string
141      * 
142      * @return The Search Result Reference string
143      */
144     @Override
145     public String toString()
146     {
147 
148         StringBuilder sb = new StringBuilder();
149 
150         sb.append( "    Search Result Reference\n" );
151 
152         if ( ( referral == null ) || ( referral.getLdapUrls() == null ) || referral.getLdapUrls().isEmpty() )
153         {
154             sb.append( "        No Reference\n" );
155         }
156         else
157         {
158             sb.append( "        References\n" );
159 
160             for ( String url : referral.getLdapUrls() )
161             {
162                 sb.append( "            '" ).append( url ).append( "'\n" );
163             }
164         }
165 
166         return super.toString( sb.toString() );
167     }
168 }