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   *    http://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.ldap.codec.api.CodecControl;
28  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
29  import org.apache.directory.api.ldap.codec.api.MessageDecorator;
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.util.Strings;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  
37  /**
38   * The action used to set the value of a control. This is an extension point
39   * where different controls can be plugged in (at least eventually). For now we
40   * hard code controls.
41   * <pre>
42   * Control ::= SEQUENCE {
43   *     ...
44   *     controlValue OCTET STRING OPTIONAL }
45   * </pre>
46   *
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  public class StoreControlValue extends GrammarAction<LdapMessageContainer<MessageDecorator<? extends Message>>>
50  {
51      /** The logger */
52      private static final Logger LOG = LoggerFactory.getLogger( StoreControlValue.class );
53  
54      /** Speedup for logs */
55      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
56  
57  
58      /**
59       * Instantiates a new StoreControlValue action.
60       */
61      public StoreControlValue()
62      {
63          super( "Store the control value" );
64      }
65  
66  
67      /**
68       * {@inheritDoc}
69       */
70      public void action( LdapMessageContainer<MessageDecorator<? extends Message>> container ) throws DecoderException
71      {
72          TLV tlv = container.getCurrentTLV();
73  
74          MessageDecorator<?> message = container.getMessage();
75          CodecControl<? extends Control> control = message.getCurrentControl();
76  
77          // Get the current control
78          BerValue value = tlv.getValue();
79  
80          // Store the value - have to handle the special case of a 0 length value
81          if ( tlv.getLength() == 0 )
82          {
83              control.setValue( Strings.EMPTY_BYTES );
84          }
85          else
86          {
87              control.setValue( value.getData() );
88              control.decode( value.getData() );
89          }
90  
91          // We can have an END transition
92          container.setGrammarEndAllowed( true );
93  
94          if ( IS_DEBUG )
95          {
96              LOG.debug( "Control value : " + Strings.dumpBytes( control.getValue() ) );
97          }
98      }
99  }