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.codec.decorators;
021
022
023import java.nio.BufferOverflowException;
024import java.nio.ByteBuffer;
025
026import org.apache.directory.api.asn1.EncoderException;
027import org.apache.directory.api.asn1.ber.tlv.BerValue;
028import org.apache.directory.api.asn1.ber.tlv.TLV;
029import org.apache.directory.api.i18n.I18n;
030import org.apache.directory.api.ldap.codec.api.LdapApiService;
031import org.apache.directory.api.ldap.codec.api.LdapConstants;
032import org.apache.directory.api.ldap.codec.api.LdapEncoder;
033import org.apache.directory.api.ldap.codec.api.MessageDecorator;
034import org.apache.directory.api.ldap.model.message.Referral;
035import org.apache.directory.api.ldap.model.message.SearchResultReference;
036
037
038/**
039 * A decorator for the SearchResultReference message
040 *
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public class SearchResultReferenceDecorator extends MessageDecorator<SearchResultReference>
044    implements SearchResultReference
045{
046    /** The length of the referral */
047    private int referralLength;
048
049    /** The search result reference length */
050    private int searchResultReferenceLength;
051
052
053    /**
054     * Makes a SearchResultReference encodable.
055     *
056     * @param decoratedMessage the decorated SearchResultReference
057     */
058    public SearchResultReferenceDecorator( LdapApiService codec, SearchResultReference decoratedMessage )
059    {
060        super( codec, decoratedMessage );
061    }
062
063
064    /**
065     * @return The encoded Referral's length
066     */
067    public int getReferralLength()
068    {
069        return referralLength;
070    }
071
072
073    /**
074     * Stores the encoded length for the Referrals
075     * @param referralLength The encoded length
076     */
077    public void setReferralLength( int referralLength )
078    {
079        this.referralLength = referralLength;
080    }
081
082
083    /**
084     * @return The encoded SearchResultReference's length
085     */
086    public int getSearchResultReferenceLength()
087    {
088        return searchResultReferenceLength;
089    }
090
091
092    /**
093     * Stores the encoded length for the SearchResultReference's
094     * @param searchResultReferenceLength The encoded length
095     */
096    public void setSearchResultReferenceLength( int searchResultReferenceLength )
097    {
098        this.searchResultReferenceLength = searchResultReferenceLength;
099    }
100
101
102    //-------------------------------------------------------------------------
103    // The SearchResultReference methods
104    //-------------------------------------------------------------------------
105
106    /**
107     * {@inheritDoc}
108     */
109    public Referral getReferral()
110    {
111        return getDecorated().getReferral();
112    }
113
114
115    /**
116     * {@inheritDoc}
117     */
118    public void setReferral( Referral referral )
119    {
120        getDecorated().setReferral( referral );
121    }
122
123
124    //-------------------------------------------------------------------------
125    // The Decorator methods
126    //-------------------------------------------------------------------------
127
128    /**
129     * Compute the SearchResultReference length
130     * 
131     * SearchResultReference :
132     * <pre>
133     * 0x73 L1
134     *  |
135     *  +--> 0x04 L2 reference
136     *  +--> 0x04 L3 reference
137     *  +--> ...
138     *  +--> 0x04 Li reference
139     *  +--> ...
140     *  +--> 0x04 Ln reference
141     * 
142     * L1 = n*Length(0x04) + sum(Length(Li)) + sum(Length(reference[i]))
143     * 
144     * Length(SearchResultReference) = Length(0x73 + Length(L1) + L1
145     * </pre>
146     */
147    public int computeLength()
148    {
149        int searchResultReferenceLength = 0;
150
151        // We may have more than one reference.
152        Referral referral = getReferral();
153
154        int referralLength = LdapEncoder.computeReferralLength( referral );
155
156        if ( referralLength != 0 )
157        {
158            setReferral( referral );
159
160            searchResultReferenceLength = referralLength;
161        }
162
163        // Store the length of the response 
164        setSearchResultReferenceLength( searchResultReferenceLength );
165
166        return 1 + TLV.getNbBytes( searchResultReferenceLength ) + searchResultReferenceLength;
167    }
168
169
170    /**
171     * Encode the SearchResultReference message to a PDU.
172     * 
173     * SearchResultReference :
174     * <pre>
175     * 0x73 LL
176     *   0x04 LL reference
177     *   [0x04 LL reference]*
178     * </pre>
179     * @param buffer The buffer where to put the PDU
180     * @param searchResultReferenceDecorator The SearchResultReference decorator
181     * @return The PDU.
182     */
183    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
184    {
185        SearchResultReference searchResultReference = getDecorated();
186        try
187        {
188            // The SearchResultReference Tag
189            buffer.put( LdapConstants.SEARCH_RESULT_REFERENCE_TAG );
190            buffer.put( TLV.getBytes( getSearchResultReferenceLength() ) );
191
192            // The referrals, if any
193            Referral referral = searchResultReference.getReferral();
194
195            if ( referral != null )
196            {
197                // Each referral
198                for ( byte[] ldapUrlBytes : referral.getLdapUrlsBytes() )
199                {
200                    // Encode the current referral
201                    BerValue.encode( buffer, ldapUrlBytes );
202                }
203            }
204        }
205        catch ( BufferOverflowException boe )
206        {
207            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
208        }
209
210        return buffer;
211    }
212}