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.modifyRequest;
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.IntegerDecoder;
26  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
27  import org.apache.directory.api.asn1.ber.tlv.TLV;
28  import org.apache.directory.api.i18n.I18n;
29  import org.apache.directory.api.ldap.codec.api.LdapConstants;
30  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
31  import org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator;
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 store the ModificationRequest operation type
39   * <pre>
40   * ModifyRequest ::= [APPLICATION 6] SEQUENCE {
41   *     ...
42   *     modification SEQUENCE OF SEQUENCE {
43   *         operation  ENUMERATED {
44   *             ...
45   * </pre>
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   */
48  public class StoreOperationType extends GrammarAction<LdapMessageContainer<ModifyRequestDecorator>>
49  {
50      /** The logger */
51      private static final Logger LOG = LoggerFactory.getLogger( StoreOperationType.class );
52  
53      /** Speedup for logs */
54      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
55  
56  
57      /**
58       * Instantiates a new action.
59       */
60      public StoreOperationType()
61      {
62          super( "Store Modify request operation type" );
63      }
64  
65  
66      /**
67       * {@inheritDoc}
68       */
69      public void action( LdapMessageContainer<ModifyRequestDecorator> container ) throws DecoderException
70      {
71          ModifyRequestDecorator modifyRequestDecorator = container.getMessage();
72  
73          TLV tlv = container.getCurrentTLV();
74  
75          // Decode the operation type
76          int operation = 0;
77  
78          try
79          {
80              operation = IntegerDecoder.parse( tlv.getValue(), 0, 2 );
81          }
82          catch ( IntegerDecoderException ide )
83          {
84              String msg = I18n.err( I18n.ERR_04082, Strings.dumpBytes( tlv.getValue().getData() ) );
85              LOG.error( msg );
86  
87              // This will generate a PROTOCOL_ERROR
88              throw new DecoderException( msg );
89          }
90  
91          // Store the current operation.
92          modifyRequestDecorator.setCurrentOperation( operation );
93  
94          if ( IS_DEBUG )
95          {
96              switch ( operation )
97              {
98                  case LdapConstants.OPERATION_ADD:
99                      LOG.debug( "Modification operation : ADD" );
100                     break;
101 
102                 case LdapConstants.OPERATION_DELETE:
103                     LOG.debug( "Modification operation : DELETE" );
104                     break;
105 
106                 case LdapConstants.OPERATION_REPLACE:
107                     LOG.debug( "Modification operation : REPLACE" );
108                     break;
109             }
110         }
111     }
112 }