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.controls.sort;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import org.apache.directory.api.asn1.Asn1Object;
26  import org.apache.directory.api.asn1.DecoderException;
27  import org.apache.directory.api.asn1.EncoderException;
28  import org.apache.directory.api.asn1.ber.Asn1Decoder;
29  import org.apache.directory.api.asn1.ber.tlv.BerValue;
30  import org.apache.directory.api.asn1.ber.tlv.TLV;
31  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
32  import org.apache.directory.api.i18n.I18n;
33  import org.apache.directory.api.ldap.codec.api.ControlDecorator;
34  import org.apache.directory.api.ldap.codec.api.LdapApiService;
35  import org.apache.directory.api.ldap.model.message.controls.SortResponse;
36  import org.apache.directory.api.ldap.model.message.controls.SortResponseControlImpl;
37  import org.apache.directory.api.ldap.model.message.controls.SortResultCode;
38  import org.apache.directory.api.util.Strings;
39  
40  
41  /**
42   * Decorator class for SortResponseControl.
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class SortResponseDecorator extends ControlDecorator<SortResponse> implements SortResponse
47  {
48      private Asn1Decoder decoder = new Asn1Decoder();
49  
50      private int sortRespLen = 0;
51  
52  
53      /**
54       * Creates a new instance of SortResponseDecorator.
55       *
56       * @param codec the LDAP codec
57       */
58      public SortResponseDecorator( LdapApiService codec )
59      {
60          super( codec, new SortResponseControlImpl() );
61      }
62  
63  
64      /**
65       * Creates a new instance of SortResponseDecorator.
66       *
67       * @param codec the LDAP codec
68       * @param control the sort response control
69       */
70      public SortResponseDecorator( LdapApiService codec, SortResponse control )
71      {
72          super( codec, control );
73      }
74  
75  
76      /**
77       * 
78       */
79      @Override
80      public int computeLength()
81      {
82          sortRespLen = 0;
83          valueLength = 0;
84  
85          // result code value
86          sortRespLen += 1 + 1 + 1;
87          
88          if( getAttributeName() != null )
89          {
90              byte[] data = Strings.getBytesUtf8( getAttributeName() );
91              sortRespLen += 1 + TLV.getNbBytes( data.length ) + data.length;
92          }
93          
94          valueLength = 1 + TLV.getNbBytes( sortRespLen ) + sortRespLen;
95  
96          return valueLength;
97      }
98  
99  
100     @Override
101     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
102     {
103         if ( buffer == null )
104         {
105             throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
106         }
107 
108         buffer.put( UniversalTag.SEQUENCE.getValue() );
109         buffer.put( TLV.getBytes( sortRespLen ) );
110         
111         BerValue.encodeEnumerated( buffer, getSortResult().getVal() );
112         
113         if( getAttributeName() != null )
114         {
115             BerValue.encode( buffer, getAttributeName() );
116         }
117         
118         return buffer;
119     }
120 
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public Asn1Object decode( byte[] controlBytes ) throws DecoderException
127     {
128         ByteBuffer buffer = ByteBuffer.wrap( controlBytes );
129         SortResponseContainer container = new SortResponseContainer( getCodecService(), this );
130         decoder.decode( buffer, container );
131         return this;
132     }
133 
134 
135     /**
136      * {@inheritDoc}
137      */
138     public byte[] getValue()
139     {
140         if ( value == null )
141         {
142             try
143             {
144                 computeLength();
145                 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
146 
147                 value = encode( buffer ).array();
148             }
149             catch ( Exception e )
150             {
151                 return null;
152             }
153         }
154 
155         return value;
156     }
157 
158 
159     @Override
160     public void setSortResult( SortResultCode result )
161     {
162         getDecorated().setSortResult( result );
163     }
164 
165 
166     @Override
167     public SortResultCode getSortResult()
168     {
169         return getDecorated().getSortResult();
170     }
171 
172 
173     @Override
174     public void setAttributeName( String attributeName )
175     {
176         getDecorated().setAttributeName( attributeName );
177     }
178 
179 
180     @Override
181     public String getAttributeName()
182     {
183         return getDecorated().getAttributeName();
184     }
185 
186 }