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    @Override
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    @Override
080    public void setReferral( Referral referral )
081    {
082        this.referral = referral;
083    }
084
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    public int hashCode()
091    {
092        int hash = 37;
093        if ( this.referral != null )
094        {
095            hash = hash * 17 + this.referral.hashCode();
096        }
097        hash = hash * 17 + super.hashCode();
098
099        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}