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