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.request.modify;
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.LdapCodecConstants;
30  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
31  import org.apache.directory.api.ldap.model.entry.DefaultModification;
32  import org.apache.directory.api.ldap.model.entry.Modification;
33  import org.apache.directory.api.ldap.model.entry.ModificationOperation;
34  import org.apache.directory.api.ldap.model.message.ModifyRequest;
35  import org.apache.directory.api.util.Strings;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  
40  /**
41   * The action used to store the ModificationRequest operation type
42   * <pre>
43   * ModifyRequest ::= [APPLICATION 6] SEQUENCE {
44   *     ...
45   *     modification SEQUENCE OF SEQUENCE {
46   *         operation  ENUMERATED {
47   *             ...
48   * </pre>
49   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
50   */
51  public class StoreOperationType extends GrammarAction<LdapMessageContainer<ModifyRequest>>
52  {
53      /** The logger */
54      private static final Logger LOG = LoggerFactory.getLogger( StoreOperationType.class );
55  
56      /**
57       * Instantiates a new action.
58       */
59      public StoreOperationType()
60      {
61          super( "Store Modify request operation type" );
62      }
63  
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public void action( LdapMessageContainer<ModifyRequest> container ) throws DecoderException
70      {
71          ModifyRequest modifyRequest = container.getMessage();
72          TLV tlv = container.getCurrentTLV();
73  
74          // Decode the operation type
75          int operation = 0;
76  
77          try
78          {
79              // Store the current operation.
80              operation = IntegerDecoder.parse( tlv.getValue(), ModificationOperation.ADD_ATTRIBUTE.getValue(), 
81                  ModificationOperation.INCREMENT_ATTRIBUTE.getValue() );
82              Modification modification = new DefaultModification();
83              modification.setOperation( operation );
84              modifyRequest.addModification( modification );
85              container.setCurrentModification( modification );
86          }
87          catch ( IntegerDecoderException ide )
88          {
89              String msg = I18n.err( I18n.ERR_05124_INVALID_OPERATION, Strings.dumpBytes( tlv.getValue().getData() ) );
90              LOG.error( msg );
91  
92              // This will generate a PROTOCOL_ERROR
93              throw new DecoderException( msg, ide );
94          }
95  
96          if ( LOG.isDebugEnabled() )
97          {
98              switch ( operation )
99              {
100                 case LdapCodecConstants.OPERATION_ADD:
101                     LOG.debug( I18n.msg( I18n.MSG_05133_MODIFY_OPERATION, "ADD" ) );
102                     break;
103 
104                 case LdapCodecConstants.OPERATION_DELETE:
105                     LOG.debug( I18n.msg( I18n.MSG_05133_MODIFY_OPERATION, "DELETE" ) );
106                     break;
107 
108                 case LdapCodecConstants.OPERATION_REPLACE:
109                     LOG.debug( I18n.msg( I18n.MSG_05133_MODIFY_OPERATION, "REPLACE" ) );
110                     break;
111 
112                 default:
113                     LOG.debug( I18n.msg( I18n.MSG_05133_MODIFY_OPERATION, "UNKNOWN" ) );
114             }
115         }
116     }
117 }