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.LdapCodecConstants;
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 search result reference length */
047    private int searchResultReferenceLength;
048
049
050    /**
051     * Makes a SearchResultReference encodable.
052     *
053     * @param codec The LDAP service instance
054     * @param decoratedMessage the decorated SearchResultReference
055     */
056    public SearchResultReferenceDecorator( LdapApiService codec, SearchResultReference decoratedMessage )
057    {
058        super( codec, decoratedMessage );
059    }
060
061
062    //-------------------------------------------------------------------------
063    // The SearchResultReference methods
064    //-------------------------------------------------------------------------
065
066    /**
067     * {@inheritDoc}
068     */
069    public Referral getReferral()
070    {
071        return getDecorated().getReferral();
072    }
073
074
075    /**
076     * {@inheritDoc}
077     */
078    public void setReferral( Referral referral )
079    {
080        getDecorated().setReferral( referral );
081    }
082
083
084    //-------------------------------------------------------------------------
085    // The Decorator methods
086    //-------------------------------------------------------------------------
087
088    /**
089     * Compute the SearchResultReference length
090     * <br>
091     * SearchResultReference :
092     * <pre>
093     * 0x73 L1
094     *  |
095     *  +--&gt; 0x04 L2 reference
096     *  +--&gt; 0x04 L3 reference
097     *  +--&gt; ...
098     *  +--&gt; 0x04 Li reference
099     *  +--&gt; ...
100     *  +--&gt; 0x04 Ln reference
101     * 
102     * L1 = n*Length(0x04) + sum(Length(Li)) + sum(Length(reference[i]))
103     * 
104     * Length(SearchResultReference) = Length(0x73 + Length(L1) + L1
105     * </pre>
106     * 
107     * @return The encoded length
108     */
109    public int computeLength()
110    {
111        searchResultReferenceLength = 0;
112
113        // We may have more than one reference.
114        Referral referral = getReferral();
115
116        int referralLength = LdapEncoder.computeReferralLength( referral );
117
118        if ( referralLength != 0 )
119        {
120            setReferral( referral );
121
122            searchResultReferenceLength = referralLength;
123        }
124
125        return 1 + TLV.getNbBytes( searchResultReferenceLength ) + searchResultReferenceLength;
126    }
127
128
129    /**
130     * Encode the SearchResultReference message to a PDU.
131     * <br>
132     * SearchResultReference :
133     * <pre>
134     * 0x73 LL
135     *   0x04 LL reference
136     *   [0x04 LL reference]*
137     * </pre>
138     * 
139     * @param buffer The buffer where to put the PDU
140     * @return The PDU.
141     */
142    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
143    {
144        SearchResultReference searchResultReference = getDecorated();
145        try
146        {
147            // The SearchResultReference Tag
148            buffer.put( LdapCodecConstants.SEARCH_RESULT_REFERENCE_TAG );
149            buffer.put( TLV.getBytes( searchResultReferenceLength ) );
150
151            // The referrals, if any
152            Referral referral = searchResultReference.getReferral();
153
154            if ( referral != null )
155            {
156                // Each referral
157                for ( byte[] ldapUrlBytes : referral.getLdapUrlsBytes() )
158                {
159                    // Encode the current referral
160                    BerValue.encode( buffer, ldapUrlBytes );
161                }
162            }
163        }
164        catch ( BufferOverflowException boe )
165        {
166            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
167        }
168
169        return buffer;
170    }
171}