001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    https://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.api.ldap.codec.actions.request.modify;
021
022
023import org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
025import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
026import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
027import org.apache.directory.api.asn1.ber.tlv.TLV;
028import org.apache.directory.api.i18n.I18n;
029import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
030import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
031import org.apache.directory.api.ldap.model.entry.DefaultModification;
032import org.apache.directory.api.ldap.model.entry.Modification;
033import org.apache.directory.api.ldap.model.entry.ModificationOperation;
034import org.apache.directory.api.ldap.model.message.ModifyRequest;
035import org.apache.directory.api.util.Strings;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039
040/**
041 * The action used to store the ModificationRequest operation type
042 * <pre>
043 * ModifyRequest ::= [APPLICATION 6] SEQUENCE {
044 *     ...
045 *     modification SEQUENCE OF SEQUENCE {
046 *         operation  ENUMERATED {
047 *             ...
048 * </pre>
049 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
050 */
051public class StoreOperationType extends GrammarAction<LdapMessageContainer<ModifyRequest>>
052{
053    /** The logger */
054    private static final Logger LOG = LoggerFactory.getLogger( StoreOperationType.class );
055
056    /**
057     * Instantiates a new action.
058     */
059    public StoreOperationType()
060    {
061        super( "Store Modify request operation type" );
062    }
063
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    public void action( LdapMessageContainer<ModifyRequest> container ) throws DecoderException
070    {
071        ModifyRequest modifyRequest = container.getMessage();
072        TLV tlv = container.getCurrentTLV();
073
074        // Decode the operation type
075        int operation = 0;
076
077        try
078        {
079            // Store the current operation.
080            operation = IntegerDecoder.parse( tlv.getValue(), ModificationOperation.ADD_ATTRIBUTE.getValue(), 
081                ModificationOperation.INCREMENT_ATTRIBUTE.getValue() );
082            Modification modification = new DefaultModification();
083            modification.setOperation( operation );
084            modifyRequest.addModification( modification );
085            container.setCurrentModification( modification );
086        }
087        catch ( IntegerDecoderException ide )
088        {
089            String msg = I18n.err( I18n.ERR_05124_INVALID_OPERATION, Strings.dumpBytes( tlv.getValue().getData() ) );
090            LOG.error( msg );
091
092            // This will generate a PROTOCOL_ERROR
093            throw new DecoderException( msg, ide );
094        }
095
096        if ( LOG.isDebugEnabled() )
097        {
098            switch ( operation )
099            {
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}