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.codec.actions.controls;
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.codec.api.LdapMessageContainer;
30  import org.apache.directory.api.ldap.model.message.Control;
31  import org.apache.directory.api.ldap.model.message.Message;
32  import org.apache.directory.api.ldap.model.message.controls.OpaqueControl;
33  import org.apache.directory.api.util.Strings;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  
38  /**
39   * The action used to set the value of a control. This is an extension point
40   * where different controls can be plugged in (at least eventually). For now we
41   * hard code controls.
42   * <pre>
43   * Control ::= SEQUENCE {
44   *     ...
45   *     controlValue OCTET STRING OPTIONAL }
46   * </pre>
47   *
48   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
49   */
50  public class StoreControlValue extends GrammarAction<LdapMessageContainer<Message>>
51  {
52      /** The logger */
53      private static final Logger LOG = LoggerFactory.getLogger( StoreControlValue.class );
54  
55  
56      /**
57       * Instantiates a new StoreControlValue action.
58       */
59      public StoreControlValue()
60      {
61          super( "Store the control value" );
62      }
63  
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public void action( LdapMessageContainer<Message> container ) throws DecoderException
70      {
71          TLV tlv = container.getCurrentTLV();
72  
73          Control control = container.getCurrentControl();
74  
75          // Get the current control
76          BerValue value = tlv.getValue();
77  
78          // Store the value - have to handle the special case of a 0 length value
79          if ( tlv.getLength() >= 0 )
80          {
81              ControlFactory<?> factory = container.getControlFactory();
82             
83              if ( factory == null )
84              {
85                  // We don't know about this control, so it's an opaque control 
86                  ( ( OpaqueControl ) control ).setEncodedValue( value.getData() );
87              }
88              else
89              {
90                  factory.decodeValue( control, value.getData() );
91              }
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_08203_CONTROL_VALUE, Strings.dumpBytes( value.getData() ) ) );
100         }
101     }
102 }