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.add;
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.TLV;
026import org.apache.directory.api.i18n.I18n;
027import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
028import org.apache.directory.api.ldap.codec.api.ResponseCarryingException;
029import org.apache.directory.api.ldap.model.entry.Attribute;
030import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
031import org.apache.directory.api.ldap.model.exception.LdapException;
032import org.apache.directory.api.ldap.model.message.AddRequest;
033import org.apache.directory.api.ldap.model.message.AddResponseImpl;
034import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
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 AddRequest AttributeDescription
042 * <pre>
043 * AttributeList ::= SEQUENCE OF SEQUENCE {
044 *     type    AttributeDescription,
045 *     ...
046 * </pre>
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 */
049public class AddAddRequestAttributeType extends GrammarAction<LdapMessageContainer<AddRequest>>
050{
051    /** The logger */
052    private static final Logger LOG = LoggerFactory.getLogger( AddAddRequestAttributeType.class );
053
054    /**
055     * Instantiates a new action.
056     */
057    public AddAddRequestAttributeType()
058    {
059        super( "Store attribute type" );
060    }
061
062
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public void action( LdapMessageContainer<AddRequest> container ) throws DecoderException
068    {
069        AddRequest addRequest = container.getMessage();
070
071        TLV tlv = container.getCurrentTLV();
072
073        // Store the type. It can't be null.
074        if ( tlv.getLength() == 0 )
075        {
076            String msg = I18n.err( I18n.ERR_05111_NULL_OR_EMPTY_TYPE_NOT_ALLOWED );
077            LOG.error( msg );
078
079            AddResponseImpl response = new AddResponseImpl( addRequest.getMessageId() );
080
081            throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX,
082                addRequest.getEntry().getDn(), null );
083        }
084
085        String type = Strings.utf8ToString( tlv.getValue().getData() );
086
087        try
088        {
089            Attribute attribute = addRequest.getEntry().get( type );
090            
091            if ( attribute == null )
092            {
093                attribute = new DefaultAttribute( type );
094                addRequest.getEntry().add( attribute );
095            }
096            
097            container.setCurrentAttribute( attribute );
098        }
099        catch ( LdapException ne )
100        {
101            String msg = I18n.err( I18n.ERR_05112_ERROR_WITH_ATTRIBUTE_TYPE );
102            LOG.error( msg );
103
104            AddResponseImpl response = new AddResponseImpl( addRequest.getMessageId() );
105            throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX,
106                addRequest.getEntry().getDn(), ne );
107        }
108
109        if ( LOG.isDebugEnabled() )
110        {
111            LOG.debug( I18n.msg( I18n.MSG_05111_ADDING_TYPE, type ) );
112        }
113    }
114}