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.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.SearchRequest;
033import org.apache.directory.api.ldap.model.message.SearchScope;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037
038/**
039 * The action used to store the SearchRequest scope
040 * <pre>
041 * SearchRequest ::= [APPLICATION 3] SEQUENCE {
042 *     ...
043 *     scope ENUMERATED {
044 *         baseObject   (0),
045 *         singleLevel  (1),
046 *         wholeSubtree (2) },
047 *     ...
048 * </pre>
049 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
050 */
051public class StoreSearchRequestScope extends GrammarAction<LdapMessageContainer<SearchRequest>>
052{
053    /** The logger */
054    private static final Logger LOG = LoggerFactory.getLogger( StoreSearchRequestScope.class );
055
056    /**
057     * Instantiates a new action.
058     */
059    public StoreSearchRequestScope()
060    {
061        super( "Store SearchRequest scope" );
062    }
063
064
065    /**
066     * {@inheritDoc}
067     */
068    public void action( LdapMessageContainer<SearchRequest> container ) throws DecoderException
069    {
070        SearchRequest searchRequest = container.getMessage();
071
072        TLV tlv = container.getCurrentTLV();
073
074        // We have to check that this is a correct scope
075        BerValue value = tlv.getValue();
076
077        try
078        {
079            int scope = IntegerDecoder.parse( value, LdapCodecConstants.SCOPE_BASE_OBJECT,
080                LdapCodecConstants.SCOPE_WHOLE_SUBTREE );
081            searchRequest.setScope( SearchScope.getSearchScope( scope ) );
082        }
083        catch ( IntegerDecoderException ide )
084        {
085            String msg = I18n.err( I18n.ERR_05149_BAD_SCOPE, value.toString() );
086            LOG.error( msg );
087            throw new DecoderException( msg, ide );
088        }
089
090
091        if ( LOG.isDebugEnabled() )
092        {
093            switch ( searchRequest.getScope() )
094            {
095                case OBJECT :
096                    LOG.debug( I18n.msg( I18n.MSG_05162_SEARCHING_WITH_SCOPE, "BASE_OBJECT" ) );
097                    break;
098
099                case ONELEVEL :
100                    LOG.debug( I18n.msg( I18n.MSG_05162_SEARCHING_WITH_SCOPE, "SINGLE_LEVEL" ) );
101                    break;
102
103                case SUBTREE :
104                    LOG.debug( I18n.msg( I18n.MSG_05162_SEARCHING_WITH_SCOPE, "WHOLE_SUBTREE" ) );
105                    break;
106
107                default:
108                    LOG.debug( I18n.msg( I18n.MSG_05162_SEARCHING_WITH_SCOPE, "UNKNOWN" ) );
109            }
110        }
111    }
112}