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.response.search.entry;
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.SearchResultEntry;
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 search result entry
037 * <pre>
038 * PartialAttributeList ::= 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 StoreSearchResultAttributeValue extends GrammarAction<LdapMessageContainer<SearchResultEntry>>
047{
048    /** The logger */
049    private static final Logger LOG = LoggerFactory.getLogger( StoreSearchResultAttributeValue.class );
050
051    /**
052     * Instantiates a new search result attribute value action.
053     */
054    public StoreSearchResultAttributeValue()
055    {
056        super( "Stores AttributeValue" );
057    }
058
059
060    /**
061     * {@inheritDoc}
062     */
063    public void action( LdapMessageContainer<SearchResultEntry> container )
064    {
065        Attribute currentAttribute = container.getCurrentAttribute();
066
067        TLV tlv = container.getCurrentTLV();
068
069        // Store the value
070        try
071        {
072            if ( tlv.getLength() == 0 )
073            {
074                currentAttribute.add( "" );
075
076                if ( LOG.isDebugEnabled() )
077                {
078                    LOG.debug( I18n.msg( I18n.MSG_05180_NULL_ATTRIBUTE_VALUE ) );
079                }
080            }
081            else
082            {
083                if ( container.isBinary( container.getCurrentAttribute().getId() ) )
084                {
085                    byte[] value = tlv.getValue().getData();
086                    currentAttribute.add( value );
087
088                    if ( LOG.isDebugEnabled() )
089                    {
090                        LOG.debug( I18n.msg( I18n.MSG_05181_ATTRIBUTE_VALUE, Strings.dumpBytes( ( byte[] ) value ) ) );
091                    }
092                }
093                else
094                {
095                    String value = Strings.utf8ToString( tlv.getValue().getData() );
096                    currentAttribute.add( value );
097
098                    if ( LOG.isDebugEnabled() )
099                    {
100                        LOG.debug( I18n.msg( I18n.MSG_05181_ATTRIBUTE_VALUE, value ) );
101                    }
102                }
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}