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 */
020 package org.apache.directory.shared.ldap.codec.search.controls.subEntry;
021
022
023 import org.apache.directory.shared.asn1.ber.IAsn1Container;
024 import org.apache.directory.shared.asn1.ber.grammar.AbstractGrammar;
025 import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
026 import org.apache.directory.shared.asn1.ber.grammar.GrammarTransition;
027 import org.apache.directory.shared.asn1.ber.grammar.IGrammar;
028 import org.apache.directory.shared.asn1.ber.tlv.TLV;
029 import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
030 import org.apache.directory.shared.asn1.ber.tlv.Value;
031 import org.apache.directory.shared.asn1.codec.DecoderException;
032 import org.apache.directory.shared.asn1.util.BooleanDecoder;
033 import org.apache.directory.shared.asn1.util.BooleanDecoderException;
034 import org.apache.directory.shared.ldap.util.StringTools;
035 import org.slf4j.Logger;
036 import org.slf4j.LoggerFactory;
037
038
039 /**
040 * This class implements the SubEntryControl. All the actions are declared in
041 * this class. As it is a singleton, these declaration are only done once.
042 *
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 * @version $Rev: 764131 $, $Date: 2009-04-11 03:03:00 +0200 (Sam, 11 avr 2009) $,
045 */
046 public class SubEntryControlGrammar extends AbstractGrammar
047 {
048 /** The logger */
049 static final Logger log = LoggerFactory.getLogger( SubEntryControlGrammar.class );
050
051 /** The instance of grammar. SubEntryControlGrammar is a singleton */
052 private static IGrammar instance = new SubEntryControlGrammar();
053
054
055 /**
056 * Creates a new SubEntryGrammar object.
057 */
058 private SubEntryControlGrammar()
059 {
060 name = SubEntryControlGrammar.class.getName();
061 statesEnum = SubEntryControlStatesEnum.getInstance();
062
063 // Create the transitions table
064 super.transitions = new GrammarTransition[SubEntryControlStatesEnum.LAST_SUB_ENTRY_STATE][256];
065
066 super.transitions[SubEntryControlStatesEnum.START_STATE][UniversalTag.BOOLEAN_TAG] =
067 new GrammarTransition( SubEntryControlStatesEnum.START_STATE,
068 SubEntryControlStatesEnum.SUB_ENTRY_VISIBILITY_STATE, UniversalTag.BOOLEAN_TAG,
069 new GrammarAction( "SubEntryControl visibility" )
070 {
071 public void action( IAsn1Container container ) throws DecoderException
072 {
073 SubEntryControlContainer subEntryContainer = ( SubEntryControlContainer ) container;
074 SubEntryControlCodec control = new SubEntryControlCodec();
075 subEntryContainer.setSubEntryControl( control );
076
077 TLV tlv = subEntryContainer.getCurrentTLV();
078
079 // We get the value. If it's a 0, it's a FALSE. If it's
080 // a FF, it's a TRUE. Any other value should be an error,
081 // but we could relax this constraint. So if we have
082 // something
083 // which is not 0, it will be interpreted as TRUE, but we
084 // will generate a warning.
085 Value value = tlv.getValue();
086
087 try
088 {
089 control.setVisibility( BooleanDecoder.parse( value ) );
090
091 // We can have an END transition
092 container.grammarEndAllowed( true );
093 }
094 catch ( BooleanDecoderException bde )
095 {
096 log.error( "The visibility flag {} is invalid : {}. It should be 0 or 255", StringTools
097 .dumpBytes( value.getData() ), bde.getMessage() );
098
099 // This will generate a PROTOCOL_ERROR
100 throw new DecoderException( bde.getMessage() );
101 }
102 }
103 } );
104 }
105
106
107 /**
108 * This class is a singleton.
109 *
110 * @return An instance on this grammar
111 */
112 public static IGrammar getInstance()
113 {
114 return instance;
115 }
116 }