View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.codec.decorators;
21  
22  
23  import java.nio.BufferOverflowException;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.api.asn1.EncoderException;
27  import org.apache.directory.api.asn1.ber.tlv.BerValue;
28  import org.apache.directory.api.asn1.ber.tlv.TLV;
29  import org.apache.directory.api.i18n.I18n;
30  import org.apache.directory.api.ldap.codec.api.LdapApiService;
31  import org.apache.directory.api.ldap.codec.api.LdapConstants;
32  import org.apache.directory.api.ldap.codec.api.LdapEncoder;
33  import org.apache.directory.api.ldap.codec.api.MessageDecorator;
34  import org.apache.directory.api.ldap.model.message.Referral;
35  import org.apache.directory.api.ldap.model.message.SearchResultReference;
36  
37  
38  /**
39   * A decorator for the SearchResultReference message
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public class SearchResultReferenceDecorator extends MessageDecorator<SearchResultReference>
44      implements SearchResultReference
45  {
46      /** The length of the referral */
47      private int referralLength;
48  
49      /** The search result reference length */
50      private int searchResultReferenceLength;
51  
52  
53      /**
54       * Makes a SearchResultReference encodable.
55       *
56       * @param decoratedMessage the decorated SearchResultReference
57       */
58      public SearchResultReferenceDecorator( LdapApiService codec, SearchResultReference decoratedMessage )
59      {
60          super( codec, decoratedMessage );
61      }
62  
63  
64      /**
65       * @return The encoded Referral's length
66       */
67      public int getReferralLength()
68      {
69          return referralLength;
70      }
71  
72  
73      /**
74       * Stores the encoded length for the Referrals
75       * @param referralLength The encoded length
76       */
77      public void setReferralLength( int referralLength )
78      {
79          this.referralLength = referralLength;
80      }
81  
82  
83      /**
84       * @return The encoded SearchResultReference's length
85       */
86      public int getSearchResultReferenceLength()
87      {
88          return searchResultReferenceLength;
89      }
90  
91  
92      /**
93       * Stores the encoded length for the SearchResultReference's
94       * @param searchResultReferenceLength The encoded length
95       */
96      public void setSearchResultReferenceLength( int searchResultReferenceLength )
97      {
98          this.searchResultReferenceLength = searchResultReferenceLength;
99      }
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 }