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