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.request.search;
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.BerValue;
026import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
027import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
028import org.apache.directory.api.asn1.ber.tlv.TLV;
029import org.apache.directory.api.i18n.I18n;
030import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
031import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
032import org.apache.directory.api.ldap.model.message.AliasDerefMode;
033import org.apache.directory.api.ldap.model.message.SearchRequest;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037
038/**
039 * The action used to store the SearchRequest derefAlias flag
040 * <pre>
041 * SearchRequest ::= [APPLICATION 3] SEQUENCE {
042 *     ...
043 *     derefAliases ENUMERATED {
044 *         neverDerefAliases   (0),
045 *         derefInSearching    (1),
046 *         derefFindingBaseObj (2),
047 *         derefAlways         (3) },
048 *     ...
049 * </pre>
050 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
051 */
052public class StoreSearchRequestDerefAlias extends GrammarAction<LdapMessageContainer<SearchRequest>>
053{
054    /** The logger */
055    private static final Logger LOG = LoggerFactory.getLogger( StoreSearchRequestDerefAlias.class );
056
057    /**
058     * Instantiates a new action.
059     */
060    public StoreSearchRequestDerefAlias()
061    {
062        super( "Store SearchRequest derefAlias flag" );
063    }
064
065
066    /**
067     * {@inheritDoc}
068     */
069    public void action( LdapMessageContainer<SearchRequest> container ) throws DecoderException
070    {
071        SearchRequest searchRequest = container.getMessage();
072
073        TLV tlv = container.getCurrentTLV();
074
075        // We have to check that this is a correct derefAliases
076        BerValue value = tlv.getValue();
077
078        try
079        {
080            int derefAliases = IntegerDecoder.parse( value, LdapCodecConstants.NEVER_DEREF_ALIASES,
081                LdapCodecConstants.DEREF_ALWAYS );
082            searchRequest.setDerefAliases( AliasDerefMode.getDerefMode( derefAliases ) );
083        }
084        catch ( IntegerDecoderException ide )
085        {
086            String msg = I18n.err( I18n.ERR_05150_BAD_DEREF_ALIAS, value.toString() );
087            LOG.error( msg );
088            throw new DecoderException( msg, ide );
089        }
090
091
092        if ( LOG.isDebugEnabled() )
093        {
094            switch ( searchRequest.getDerefAliases() )
095            {
096                case NEVER_DEREF_ALIASES:
097                    LOG.debug( I18n.msg( I18n.MSG_05161_HANDLING_OBJECT_STRATEGY, "NEVER_DEREF_ALIASES" ) );
098                    break;
099
100                case DEREF_IN_SEARCHING:
101                    LOG.debug( I18n.msg( I18n.MSG_05161_HANDLING_OBJECT_STRATEGY, "DEREF_IN_SEARCHING" ) );
102                    break;
103
104                case DEREF_FINDING_BASE_OBJ:
105                    LOG.debug( I18n.msg( I18n.MSG_05161_HANDLING_OBJECT_STRATEGY, "DEREF_FINDING_BASE_OBJ" ) );
106                    break;
107
108                case DEREF_ALWAYS:
109                    LOG.debug( I18n.msg( I18n.MSG_05161_HANDLING_OBJECT_STRATEGY, "DEREF_ALWAYS" ) );
110                    break;
111
112                default:
113                    LOG.debug( I18n.msg( I18n.MSG_05161_HANDLING_OBJECT_STRATEGY, "UNKNOWN" ) );
114            }
115        }
116    }
117}