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 * http://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.shared.ldap.extras.extended.ads_impl.cancel; 021 022 023import org.apache.directory.shared.asn1.ber.Asn1Container; 024import org.apache.directory.shared.asn1.ber.grammar.AbstractGrammar; 025import org.apache.directory.shared.asn1.ber.grammar.Grammar; 026import org.apache.directory.shared.asn1.ber.grammar.GrammarAction; 027import org.apache.directory.shared.asn1.ber.grammar.GrammarTransition; 028import org.apache.directory.shared.asn1.ber.tlv.UniversalTag; 029import org.apache.directory.shared.asn1.ber.tlv.Value; 030import org.apache.directory.shared.asn1.DecoderException; 031import org.apache.directory.shared.asn1.ber.tlv.IntegerDecoder; 032import org.apache.directory.shared.asn1.ber.tlv.IntegerDecoderException; 033import org.apache.directory.shared.i18n.I18n; 034import org.apache.directory.shared.util.Strings; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037 038 039/** 040 * This class implements the Cancel operation. All the actions are declared 041 * in this class. As it is a singleton, these declaration are only done once. 042 * The grammar is : 043 * 044 * <pre> 045 * cancelRequestValue ::= SEQUENCE { 046 * cancelId MessageID 047 * -- MessageID is as defined in [RFC2251] 048 * } 049 * </pre> 050 * 051 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 052 */ 053public final class CancelGrammar extends AbstractGrammar<CancelContainer> 054{ 055 /** The logger */ 056 static final Logger LOG = LoggerFactory.getLogger( CancelGrammar.class ); 057 058 /** Speedup for logs */ 059 static final boolean IS_DEBUG = LOG.isDebugEnabled(); 060 061 /** The instance of grammar. CancelGrammar is a singleton */ 062 private static Grammar<CancelContainer> instance = new CancelGrammar(); 063 064 065 /** 066 * Creates a new GracefulDisconnectGrammar object. 067 */ 068 @SuppressWarnings({ "unchecked", "rawtypes" }) 069 private CancelGrammar() 070 { 071 setName( CancelGrammar.class.getName() ); 072 073 // Create the transitions table 074 super.transitions = new GrammarTransition[CancelStatesEnum.LAST_CANCEL_STATE.ordinal()][256]; 075 076 /** 077 * Transition from init state to cancel sequence 078 * cancelRequestValue ::= SEQUENCE { 079 * ... 080 * 081 * Creates the Cancel object 082 */ 083 super.transitions[CancelStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] = 084 new GrammarTransition<CancelContainer>( CancelStatesEnum.START_STATE, 085 CancelStatesEnum.CANCEL_SEQUENCE_STATE, 086 UniversalTag.SEQUENCE.getValue(), 087 new GrammarAction( "Init Cancel" ) 088 { 089 public void action( Asn1Container container ) 090 { 091 CancelContainer cancelContainer = ( CancelContainer ) container; 092 Cancel cancel = new Cancel(); 093 cancelContainer.setCancel( cancel ); 094 } 095 } ); 096 097 /** 098 * Transition from cancel SEQ to cancelId 099 * 100 * cancelRequestValue ::= SEQUENCE { 101 * cancelId MessageID 102 * } 103 * 104 * Set the cancelId value into the Cancel object. 105 */ 106 super.transitions[CancelStatesEnum.CANCEL_SEQUENCE_STATE.ordinal()][UniversalTag.INTEGER.getValue()] = 107 new GrammarTransition<CancelContainer>( CancelStatesEnum.CANCEL_SEQUENCE_STATE, 108 CancelStatesEnum.CANCEL_ID_STATE, 109 UniversalTag.INTEGER.getValue(), 110 new GrammarAction( "Stores CancelId" ) 111 { 112 public void action( Asn1Container container ) throws DecoderException 113 { 114 CancelContainer cancelContainer = ( CancelContainer ) container; 115 Value value = cancelContainer.getCurrentTLV().getValue(); 116 117 try 118 { 119 int cancelId = IntegerDecoder.parse( value, 0, Integer.MAX_VALUE ); 120 121 if ( IS_DEBUG ) 122 { 123 LOG.debug( "CancelId = " + cancelId ); 124 } 125 126 cancelContainer.getCancel().setCancelId( cancelId ); 127 cancelContainer.setGrammarEndAllowed( true ); 128 } 129 catch ( IntegerDecoderException e ) 130 { 131 String msg = I18n.err( I18n.ERR_04031, Strings.dumpBytes(value.getData()) ); 132 LOG.error( msg ); 133 throw new DecoderException( msg ); 134 } 135 } 136 }); 137 } 138 139 140 /** 141 * This class is a singleton. 142 * 143 * @return An instance on this grammar 144 */ 145 public static Grammar<CancelContainer> getInstance() 146 { 147 return instance; 148 } 149}