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