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.StringConstants;
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<MessageDecorator<? extends Message>>>
51  {
52      /** The logger */
53      private static final Logger LOG = LoggerFactory.getLogger( StoreControlValue.class );
54  
55      /** Speedup for logs */
56      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
57  
58  
59      /**
60       * Instantiates a new StoreControlValue action.
61       */
62      public StoreControlValue()
63      {
64          super( "Store the control value" );
65      }
66  
67  
68      /**
69       * {@inheritDoc}
70       */
71      public void action( LdapMessageContainer<MessageDecorator<? extends Message>> container ) throws DecoderException
72      {
73          TLV tlv = container.getCurrentTLV();
74  
75          MessageDecorator<?> message = container.getMessage();
76          CodecControl<? extends Control> control = message.getCurrentControl();
77  
78          // Get the current control
79          BerValue value = tlv.getValue();
80  
81          // Store the value - have to handle the special case of a 0 length value
82          if ( tlv.getLength() == 0 )
83          {
84              control.setValue( StringConstants.EMPTY_BYTES );
85          }
86          else
87          {
88              control.setValue( value.getData() );
89  
90              if ( control != null )
91              {
92                  control.decode( value.getData() );
93              }
94          }
95  
96          // We can have an END transition
97          container.setGrammarEndAllowed( true );
98  
99          if ( IS_DEBUG )
100         {
101             LOG.debug( "Control value : " + Strings.dumpBytes( control.getValue() ) );
102         }
103     }
104 }