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   *     https://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.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.directory.api.asn1.DecoderException;
27  import org.apache.directory.api.asn1.ber.tlv.BerValue;
28  import org.apache.directory.api.asn1.util.Asn1Buffer;
29  import org.apache.directory.api.ldap.codec.api.AbstractControlFactory;
30  import org.apache.directory.api.ldap.codec.api.ControlFactory;
31  import org.apache.directory.api.ldap.codec.api.LdapApiService;
32  import org.apache.directory.api.ldap.model.message.Control;
33  import org.apache.directory.api.ldap.model.message.controls.SortKey;
34  import org.apache.directory.api.ldap.model.message.controls.SortRequest;
35  import org.apache.directory.api.ldap.model.message.controls.SortRequestImpl;
36  import org.apache.directory.api.util.Strings;
37  
38  
39  /**
40   * A {@link ControlFactory} for SortRequestControl.
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   * @version $Rev$, $Date$
44   */
45  public class SortRequestFactory extends AbstractControlFactory<SortRequest>
46  {
47      /** ASN.1 BER tag for the forward ordering rule */
48      public static final int ORDERING_RULE_TAG = 0x80;
49  
50      /** ASN.1 BER tag for the backward ordering rule */
51      public static final int REVERSE_ORDER_TAG = 0x81;
52  
53      /** 
54       * Creates a new instance of SortRequestFactory.
55       *
56       * @param codec The LDAP codec.
57       */
58      public SortRequestFactory( LdapApiService codec )
59      {
60          super( codec, SortRequest.OID );
61      }
62  
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public SortRequest newControl()
69      {
70          return new SortRequestImpl();
71      }
72  
73  
74      /**
75       *
76       * Encode the SortKeys recursively
77       *
78       * @param buffer the buffer that will contain the encoded value
79       * @param sortKeys The Sortkeys to encode
80       */
81      private void encodeSortKeys( Asn1Buffer buffer, Iterator<SortKey> sortKeys )
82      {
83          if ( sortKeys.hasNext() )
84          {
85              SortKey sortKey = sortKeys.next();
86  
87              // Recurse
88              encodeSortKeys( buffer, sortKeys );
89  
90              int start = buffer.getPos();
91  
92              // The reverseOrder flag
93              if ( sortKey.isReverseOrder() )
94              {
95                  BerValue.encodeBoolean( buffer, ( byte ) REVERSE_ORDER_TAG, true );
96              }
97  
98              // The matchingRule ID, if any
99              if ( sortKey.getMatchingRuleId() != null )
100             {
101                 BerValue.encodeOctetString( buffer, ( byte ) ORDERING_RULE_TAG,
102                     Strings.getBytesUtf8Ascii( sortKey.getMatchingRuleId() ) );
103             }
104 
105             // The attributeType
106             BerValue.encodeOctetString( buffer, sortKey.getAttributeTypeDesc() );
107 
108             // The sequence
109             BerValue.encodeSequence( buffer, start );
110         }
111     }
112 
113     
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public void encodeValue( Asn1Buffer buffer, Control control )
119     {
120         SortRequest sortRequest = ( SortRequest ) control;
121 
122         int start = buffer.getPos();
123 
124         // Iterate on all the sort keys
125         List<SortKey> sortKeys = sortRequest.getSortKeys();
126 
127         encodeSortKeys( buffer, sortKeys.iterator() );
128 
129         // The overall sequence
130         BerValue.encodeSequence( buffer, start );
131     }
132 
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
138     public void decodeValue( Control control, byte[] controlBytes ) throws DecoderException
139     {
140         decodeValue( new SortRequestContainer( control ), control, controlBytes );
141     }
142 }