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 *    http://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.ber.grammar.GrammarAction;
024import org.apache.directory.api.asn1.ber.tlv.TLV;
025import org.apache.directory.api.i18n.I18n;
026import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
027import org.apache.directory.api.ldap.model.entry.Attribute;
028import org.apache.directory.api.ldap.model.exception.LdapException;
029import org.apache.directory.api.ldap.model.message.ModifyRequest;
030import org.apache.directory.api.util.Strings;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034
035/**
036 * The action used to store a Value to an modifyRequest
037 * 
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class StoreModifyRequestAttributeValue extends GrammarAction<LdapMessageContainer<ModifyRequest>>
041{
042    /** The logger */
043    private static final Logger LOG = LoggerFactory.getLogger( StoreModifyRequestAttributeValue.class );
044
045    /**
046     * Instantiates a new modify attribute value action.
047     */
048    public StoreModifyRequestAttributeValue()
049    {
050        super( "Stores AttributeValue" );
051    }
052
053
054    /**
055     * {@inheritDoc}
056     */
057    @Override
058    public void action( LdapMessageContainer<ModifyRequest> container )
059    {
060        TLV tlv = container.getCurrentTLV();
061
062        // Store the value. It can't be null
063        byte[] value = Strings.EMPTY_BYTES;
064        Attribute currentAttribute = container.getCurrentAttribute();
065
066        try
067        {
068            if ( tlv.getLength() == 0 )
069            {
070                currentAttribute.add( "" );
071            }
072            else
073            {
074                value = tlv.getValue().getData();
075
076                if ( container.isBinary( currentAttribute.getId() ) )
077                {
078                    container.getCurrentAttribute().add( value );
079                }
080                else
081                {
082                    currentAttribute.add( Strings.utf8ToString( ( byte[] ) value ) );
083                }
084            }
085        }
086        catch ( LdapException le )
087        {
088            // Just swallow the exception, it can't occur here
089        }
090
091        // We can have an END transition
092        container.setGrammarEndAllowed( true );
093
094        if ( LOG.isDebugEnabled() )
095        {
096            LOG.debug( I18n.msg( I18n.MSG_05131_VALUE_MODIFIED, value ) );
097        }
098    }
099}