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.searchResultEntry;
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.SearchResultEntryDecorator;
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 search result entry
035 * <pre>
036 * PartialAttributeList ::= 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 StoreSearchResultAttributeValue extends GrammarAction<LdapMessageContainer<SearchResultEntryDecorator>>
045{
046    /** The logger */
047    private static final Logger LOG = LoggerFactory.getLogger( StoreSearchResultAttributeValue.class );
048
049    /** Speedup for logs */
050    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
051
052
053    /**
054     * Instantiates a new search result attribute value action.
055     */
056    public StoreSearchResultAttributeValue()
057    {
058        super( "Stores AttributeValue" );
059    }
060
061
062    /**
063     * {@inheritDoc}
064     */
065    public void action( LdapMessageContainer<SearchResultEntryDecorator> container )
066    {
067        SearchResultEntryDecorator searchResultEntry = container.getMessage();
068
069        TLV tlv = container.getCurrentTLV();
070
071        // Store the value
072        Object value = null;
073
074        try
075        {
076            if ( tlv.getLength() == 0 )
077            {
078                searchResultEntry.addAttributeValue( "" );
079
080                LOG.debug( "The attribute value is null" );
081            }
082            else
083            {
084                if ( container.isBinary( searchResultEntry.getCurrentAttribute().getId() ) )
085                {
086                    value = tlv.getValue().getData();
087
088                    if ( IS_DEBUG )
089                    {
090                        LOG.debug( "Attribute value {}", Strings.dumpBytes( ( byte[] ) value ) );
091                    }
092                }
093                else
094                {
095                    value = Strings.utf8ToString( tlv.getValue().getData() );
096
097                    LOG.debug( "Attribute value {}", value );
098                }
099
100                searchResultEntry.addAttributeValue( value );
101            }
102        }
103        catch ( LdapException le )
104        {
105            // Just swallow the exception, it can't occur here
106        }
107
108        // We can have an END transition
109        container.setGrammarEndAllowed( true );
110    }
111}