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      public Referral getReferral()
68      {
69          return this.referral;
70      }
71  
72  
73      /**
74       * Sets the sequence of LdapUrls as a Referral instance.
75       * 
76       * @param referral the sequence of LdapUrls
77       */
78      public void setReferral( Referral referral )
79      {
80          this.referral = referral;
81      }
82  
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public int hashCode()
89      {
90          int hash = 37;
91          if ( this.referral != null )
92          {
93              hash = hash * 17 + this.referral.hashCode();
94          }
95          hash = hash * 17 + super.hashCode();
96  
97          return hash;
98      }
99  
100 
101     /**
102      * Checks to see if an object is equal to this SearchResponseReference stub.
103      * 
104      * @param obj
105      *            the object to compare to this response stub
106      * @return true if the objects are equivalent false otherwise
107      */
108     public boolean equals( Object obj )
109     {
110         if ( obj == this )
111         {
112             return true;
113         }
114 
115         if ( !super.equals( obj ) )
116         {
117             return false;
118         }
119 
120         SearchResultReference resp = ( SearchResultReference ) obj;
121 
122         if ( this.referral != null && resp.getReferral() == null )
123         {
124             return false;
125         }
126 
127         if ( this.referral == null && resp.getReferral() != null )
128         {
129             return false;
130         }
131 
132         return ( this.referral == null || resp.getReferral() == null || this.referral.equals( resp.getReferral() ) );
133     }
134 
135 
136     /**
137      * Returns the Search Result Reference string
138      * 
139      * @return The Search Result Reference string
140      */
141     public String toString()
142     {
143 
144         StringBuilder sb = new StringBuilder();
145 
146         sb.append( "    Search Result Reference\n" );
147 
148         if ( ( referral == null ) || ( referral.getLdapUrls() == null ) || ( referral.getLdapUrls().size() == 0 ) )
149         {
150             sb.append( "        No Reference\n" );
151         }
152         else
153         {
154             sb.append( "        References\n" );
155 
156             for ( String url : referral.getLdapUrls() )
157             {
158                 sb.append( "            '" ).append( url ).append( "'\n" );
159             }
160         }
161 
162         return super.toString( sb.toString() );
163     }
164 }