View Javadoc
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.TLV;
27  import org.apache.directory.api.i18n.I18n;
28  import org.apache.directory.api.ldap.codec.api.ControlFactory;
29  import org.apache.directory.api.ldap.extras.extended.ads_impl.endTransaction.controls.ControlsContainer;
30  import org.apache.directory.api.ldap.model.message.Control;
31  import org.apache.directory.api.util.Strings;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  
36  /**
37   * The action used to set the value of a control. This is an extension point
38   * where different controls can be plugged in (at least eventually). For now we
39   * hard code controls.
40   * <pre>
41   * Control ::= SEQUENCE {
42   *     ...
43   *     controlValue OCTET STRING OPTIONAL }
44   * </pre>
45   *
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   */
48  public class StoreControlValue extends GrammarAction<ControlsContainer>
49  {
50      /** The logger */
51      private static final Logger LOG = LoggerFactory.getLogger( StoreControlValue.class );
52  
53      /**
54       * Instantiates a new StoreControlValue action.
55       */
56      public StoreControlValue()
57      {
58          super( "Store the control value" );
59      }
60  
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public void action( ControlsContainer container ) throws DecoderException
67      {
68          TLV tlv = container.getCurrentTLV();
69  
70          Control control = container.getCurrentControl();
71          ControlFactory<?> controlFactory = container.getFactory();
72  
73          // Get the current control
74          BerValue value = tlv.getValue();
75  
76          // Store the value - have to handle the special case of a 0 length value
77          try
78          {
79              if ( tlv.getLength() == 0 )
80              {
81                  controlFactory.decodeValue( control, Strings.EMPTY_BYTES );
82              }
83              else
84              {
85                  controlFactory.decodeValue( control, value.getData() );
86              }
87          }
88          catch ( DecoderException de )
89          {
90              String message = I18n.err( I18n.ERR_08109_BAD_CONTROL_VALUE, 
91                  Strings.dumpBytes( value.getData() ) );
92              LOG.error( message );
93  
94              // This will generate a PROTOCOL_ERROR
95              throw new DecoderException( message, de );
96              
97          }
98  
99          // We can have an END transition
100         container.setGrammarEndAllowed( true );
101 
102         if ( LOG.isDebugEnabled() )
103         {
104             LOG.debug( I18n.msg( I18n.MSG_08203_CONTROL_VALUE, Strings.dumpBytes( value.getData() ) ) );
105         }
106     }
107 }