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.addRequest;
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.AddRequestDecorator;
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 AddRequest
035 * <pre>
036 * AttributeList ::= SEQUENCE OF SEQUENCE {
037 *     ...
038 *     vals SET OF AttributeValue }
039 *
040 * AttributeValue OCTET STRING
041 * </pre>
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 */
044public class AddAttributeValue extends GrammarAction<LdapMessageContainer<AddRequestDecorator>>
045{
046    /** The logger */
047    private static final Logger LOG = LoggerFactory.getLogger( AddAttributeValue.class );
048
049    /** Speedup for logs */
050    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
051
052
053    /**
054     * Instantiates a new value action.
055     */
056    public AddAttributeValue()
057    {
058        super( "Store a value" );
059    }
060
061
062    /**
063     * {@inheritDoc}
064     */
065    public void action( LdapMessageContainer<AddRequestDecorator> container )
066    {
067        AddRequestDecorator addRequest = container.getMessage();
068
069        TLV tlv = container.getCurrentTLV();
070
071        // Store the value. It can't be null
072        Object value = null;
073
074        try
075        {
076            if ( tlv.getLength() == 0 )
077            {
078                addRequest.addAttributeValue( "" );
079            }
080            else
081            {
082                if ( container.isBinary( addRequest.getCurrentAttributeType() ) )
083                {
084                    value = tlv.getValue().getData();
085
086                    if ( IS_DEBUG )
087                    {
088                        LOG.debug( "Adding value {}", Strings.dumpBytes( ( byte[] ) value ) );
089                    }
090
091                    addRequest.addAttributeValue( ( byte[] ) value );
092                }
093                else
094                {
095                    value = Strings.utf8ToString( tlv.getValue().getData() );
096
097                    if ( IS_DEBUG )
098                    {
099                        LOG.debug( "Adding value {}" + value );
100                    }
101
102                    addRequest.addAttributeValue( ( String ) value );
103                }
104            }
105        }
106        catch ( LdapException le )
107        {
108            // Just swallow the exception, it can't occur here
109        }
110
111        // We can have an END transition
112        container.setGrammarEndAllowed( true );
113    }
114}