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
022
023/**
024 * SearchResponseReference implementation
025 * 
026 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
027 */
028public class SearchResultReferenceImpl extends AbstractResponse implements SearchResultReference
029{
030    static final long serialVersionUID = 7423807019951309810L;
031
032    /** Referral holding the reference urls */
033    private Referral referral = new ReferralImpl();
034
035
036    /**
037     * Creates a SearchResponseReference as a reply to an SearchRequest
038     * to indicate the end of a search operation.
039     */
040    public SearchResultReferenceImpl()
041    {
042        super( -1, TYPE );
043    }
044
045
046    /**
047     * Creates a SearchResponseReference as a reply to an SearchRequest
048     * to indicate the end of a search operation.
049     * 
050     * @param id the session unique message id
051     */
052    public SearchResultReferenceImpl( final int id )
053    {
054        super( id, TYPE );
055    }
056
057
058    // ------------------------------------------------------------------------
059    // SearchResponseReference Interface Method Implementations
060    // ------------------------------------------------------------------------
061
062
063    /**
064     * Gets the sequence of LdapUrls as a Referral instance.
065     * 
066     * @return the sequence of LdapUrls
067     */
068    public Referral getReferral()
069    {
070        return this.referral;
071    }
072
073
074    /**
075     * Sets the sequence of LdapUrls as a Referral instance.
076     * 
077     * @param referral the sequence of LdapUrls
078     */
079    public void setReferral( Referral referral )
080    {
081        this.referral = referral;
082    }
083
084
085    /**
086     * {@inheritDoc}
087     */
088    @Override
089    public int hashCode()
090    {
091        int hash = 37;
092        if ( this.referral != null )
093        {
094            hash = hash * 17 + this.referral.hashCode();
095        }
096        hash = hash * 17 + super.hashCode();
097
098        return hash;
099    }
100
101
102    /**
103     * Checks to see if an object is equal to this SearchResponseReference stub.
104     * 
105     * @param obj
106     *            the object to compare to this response stub
107     * @return true if the objects are equivalent false otherwise
108     */
109    public boolean equals( Object obj )
110    {
111        if ( obj == this )
112        {
113            return true;
114        }
115
116        if ( !super.equals( obj ) )
117        {
118            return false;
119        }
120
121        SearchResultReference resp = ( SearchResultReference ) obj;
122
123        if ( this.referral != null && resp.getReferral() == null )
124        {
125            return false;
126        }
127
128        if ( this.referral == null && resp.getReferral() != null )
129        {
130            return false;
131        }
132
133        return ( this.referral == null || resp.getReferral() == null || this.referral.equals( resp.getReferral() ) );
134    }
135
136
137    /**
138     * Returns the Search Result Reference string
139     * 
140     * @return The Search Result Reference string
141     */
142    public String toString()
143    {
144
145        StringBuilder sb = new StringBuilder();
146
147        sb.append( "    Search Result Reference\n" );
148
149        if ( ( referral == null ) || ( referral.getLdapUrls() == null ) || ( referral.getLdapUrls().size() == 0 ) )
150        {
151            sb.append( "        No Reference\n" );
152        }
153        else
154        {
155            sb.append( "        References\n" );
156
157            for ( String url : referral.getLdapUrls() )
158            {
159                sb.append( "            '" ).append( url ).append( "'\n" );
160            }
161        }
162
163        if ( ( controls != null ) && ( controls.size() != 0 ) )
164        {
165            for ( Control control : controls.values() )
166            {
167                sb.append( control );
168            }
169        }
170
171        return super.toString( sb.toString() );
172    }
173}