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 * https://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.controls.sort; 021 022 023import java.util.Iterator; 024import java.util.List; 025 026import org.apache.directory.api.asn1.DecoderException; 027import org.apache.directory.api.asn1.ber.tlv.BerValue; 028import org.apache.directory.api.asn1.util.Asn1Buffer; 029import org.apache.directory.api.ldap.codec.api.AbstractControlFactory; 030import org.apache.directory.api.ldap.codec.api.ControlFactory; 031import org.apache.directory.api.ldap.codec.api.LdapApiService; 032import org.apache.directory.api.ldap.model.message.Control; 033import org.apache.directory.api.ldap.model.message.controls.SortKey; 034import org.apache.directory.api.ldap.model.message.controls.SortRequest; 035import org.apache.directory.api.ldap.model.message.controls.SortRequestImpl; 036import org.apache.directory.api.util.Strings; 037 038 039/** 040 * A {@link ControlFactory} for SortRequestControl. 041 * 042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 043 * @version $Rev$, $Date$ 044 */ 045public class SortRequestFactory extends AbstractControlFactory<SortRequest> 046{ 047 /** ASN.1 BER tag for the forward ordering rule */ 048 public static final int ORDERING_RULE_TAG = 0x80; 049 050 /** ASN.1 BER tag for the backward ordering rule */ 051 public static final int REVERSE_ORDER_TAG = 0x81; 052 053 /** 054 * Creates a new instance of SortRequestFactory. 055 * 056 * @param codec The LDAP codec. 057 */ 058 public SortRequestFactory( LdapApiService codec ) 059 { 060 super( codec, SortRequest.OID ); 061 } 062 063 064 /** 065 * {@inheritDoc} 066 */ 067 @Override 068 public SortRequest newControl() 069 { 070 return new SortRequestImpl(); 071 } 072 073 074 /** 075 * 076 * Encode the SortKeys recursively 077 * 078 * @param buffer the buffer that will contain the encoded value 079 * @param sortKeys The Sortkeys to encode 080 */ 081 private void encodeSortKeys( Asn1Buffer buffer, Iterator<SortKey> sortKeys ) 082 { 083 if ( sortKeys.hasNext() ) 084 { 085 SortKey sortKey = sortKeys.next(); 086 087 // Recurse 088 encodeSortKeys( buffer, sortKeys ); 089 090 int start = buffer.getPos(); 091 092 // The reverseOrder flag 093 if ( sortKey.isReverseOrder() ) 094 { 095 BerValue.encodeBoolean( buffer, ( byte ) REVERSE_ORDER_TAG, true ); 096 } 097 098 // The matchingRule ID, if any 099 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}