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.api.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, MessageTypeEnum.SEARCH_RESULT_REFERENCE );
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, MessageTypeEnum.SEARCH_RESULT_REFERENCE );
055    }
056
057
058    // ------------------------------------------------------------------------
059    // SearchResponseReference Interface Method Implementations
060    // ------------------------------------------------------------------------
061
062    /**
063     * Gets the sequence of LdapUrls as a Referral instance.
064     * 
065     * @return the sequence of LdapUrls
066     */
067    public Referral getReferral()
068    {
069        return this.referral;
070    }
071
072
073    /**
074     * Sets the sequence of LdapUrls as a Referral instance.
075     * 
076     * @param referral the sequence of LdapUrls
077     */
078    public void setReferral( Referral referral )
079    {
080        this.referral = referral;
081    }
082
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public int hashCode()
089    {
090        int hash = 37;
091        if ( this.referral != null )
092        {
093            hash = hash * 17 + this.referral.hashCode();
094        }
095        hash = hash * 17 + super.hashCode();
096
097        return hash;
098    }
099
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}