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 static org.apache.directory.api.ldap.codec.controls.sort.SortResponseFactory.ATTRIBUTE_TYPE_TAG; 024 025import org.apache.directory.api.asn1.DecoderException; 026import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar; 027import org.apache.directory.api.asn1.ber.grammar.Grammar; 028import org.apache.directory.api.asn1.ber.grammar.GrammarAction; 029import org.apache.directory.api.asn1.ber.grammar.GrammarTransition; 030import org.apache.directory.api.asn1.ber.tlv.BerValue; 031import org.apache.directory.api.asn1.ber.tlv.UniversalTag; 032import org.apache.directory.api.i18n.I18n; 033import org.apache.directory.api.util.Strings; 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036 037 038/** 039 * Grammar for decoding SortResponseControl. 040 * 041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 042 */ 043public final class SortResponseGrammar extends AbstractGrammar<SortResponseContainer> 044{ 045 /** The logger */ 046 static final Logger LOG = LoggerFactory.getLogger( SortRequestGrammar.class ); 047 048 /** The instance of grammar. SortResponseGrammar is a singleton */ 049 private static Grammar<SortResponseContainer> instance = new SortResponseGrammar(); 050 051 052 @SuppressWarnings("unchecked") 053 private SortResponseGrammar() 054 { 055 setName( SortResponseGrammar.class.getName() ); 056 057 // Create the transitions table 058 super.transitions = new GrammarTransition[SortResponseStates.END_STATE.ordinal()][256]; 059 060 super.transitions[SortResponseStates.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] = 061 new GrammarTransition<SortResponseContainer>( SortResponseStates.START_STATE, 062 SortResponseStates.SEQUENCE_STATE, 063 UniversalTag.SEQUENCE.getValue(), null ); 064 065 super.transitions[SortResponseStates.SEQUENCE_STATE.ordinal()][UniversalTag.ENUMERATED.getValue()] = 066 new GrammarTransition<SortResponseContainer>( SortResponseStates.SEQUENCE_STATE, 067 SortResponseStates.RESULT_CODE_STATE, 068 UniversalTag.ENUMERATED.getValue(), new StoreSortResponseResultCode<SortResponseContainer>() ); 069 070 super.transitions[SortResponseStates.RESULT_CODE_STATE.ordinal()][ATTRIBUTE_TYPE_TAG] = 071 new GrammarTransition<SortResponseContainer>( SortResponseStates.RESULT_CODE_STATE, 072 SortResponseStates.AT_DESC_STATE, 073 ATTRIBUTE_TYPE_TAG, new GrammarAction<SortResponseContainer>() 074 { 075 076 @Override 077 public void action( SortResponseContainer container ) throws DecoderException 078 { 079 BerValue value = container.getCurrentTLV().getValue(); 080 081 String atType = Strings.utf8ToString( value.getData() ); 082 083 if ( LOG.isDebugEnabled() ) 084 { 085 LOG.debug( I18n.msg( I18n.MSG_05310_ATTRIBUTE_TYPE, atType ) ); 086 } 087 088 container.getControl().setAttributeName( atType ); 089 container.setGrammarEndAllowed( true ); 090 } 091 } ); 092 093 } 094 095 096 /** 097 * This class is a singleton. 098 * 099 * @return An instance on this grammar 100 */ 101 public static Grammar<?> getInstance() 102 { 103 return instance; 104 } 105}