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.extras.extended.ads_impl.endTransaction.controls.actions;
21
22
23 import org.apache.directory.api.asn1.DecoderException;
24 import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
25 import org.apache.directory.api.asn1.ber.tlv.BerValue;
26 import org.apache.directory.api.asn1.ber.tlv.BooleanDecoder;
27 import org.apache.directory.api.asn1.ber.tlv.BooleanDecoderException;
28 import org.apache.directory.api.asn1.ber.tlv.TLV;
29 import org.apache.directory.api.i18n.I18n;
30 import org.apache.directory.api.ldap.extras.extended.ads_impl.endTransaction.controls.ControlsContainer;
31 import org.apache.directory.api.ldap.model.message.Control;
32 import org.apache.directory.api.util.Strings;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37 /**
38 * The action used to set the control criticality flag
39 * <pre>
40 * Control ::= SEQUENCE {
41 * ...
42 * criticality BOOLEAN DEFAULT FALSE,
43 * ...
44 * </pre>
45 *
46 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47 */
48 public class StoreControlCriticality extends GrammarAction<ControlsContainer>
49 {
50 /** The logger */
51 private static final Logger LOG = LoggerFactory.getLogger( StoreControlCriticality.class );
52
53 /**
54 * Instantiates a new StoreControlCriticality action.
55 */
56 public StoreControlCriticality()
57 {
58 super( "Store the control criticality" );
59 }
60
61
62 /**
63 * {@inheritDoc}
64 */
65 public void action( ControlsContainer container ) throws DecoderException
66 {
67 TLV tlv = container.getCurrentTLV();
68
69 // Get the current control
70 Control control = container.getCurrentControl();
71
72 // Store the criticality
73 // We get the value. If it's a 0, it's a FALSE. If it's
74 // a FF, it's a TRUE. Any other value should be an error,
75 // but we could relax this constraint. So if we have
76 // something
77 // which is not 0, it will be interpreted as TRUE, but we
78 // will generate a warning.
79 BerValue value = tlv.getValue();
80
81 try
82 {
83 control.setCritical( BooleanDecoder.parse( value ) );
84 }
85 catch ( BooleanDecoderException bde )
86 {
87 LOG.error( I18n
88 .err( I18n.ERR_08103_BAD_CONTROL_CRITICALITY, Strings.dumpBytes( value.getData() ), bde.getMessage() ) );
89
90 // This will generate a PROTOCOL_ERROR
91 throw new DecoderException( bde.getMessage(), bde );
92 }
93
94 // We can have an END transition
95 container.setGrammarEndAllowed( true );
96
97 if ( LOG.isDebugEnabled() )
98 {
99 LOG.debug( I18n.msg( I18n.MSG_08202_CONTROL_CRITICALITY, control.isCritical() ) );
100 }
101 }
102 }