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     */
020    package org.apache.directory.shared.ldap.codec.actions;
021    
022    
023    import org.apache.directory.shared.asn1.ber.IAsn1Container;
024    import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
025    import org.apache.directory.shared.asn1.ber.tlv.TLV;
026    import org.apache.directory.shared.asn1.ber.tlv.Value;
027    import org.apache.directory.shared.asn1.codec.DecoderException;
028    import org.apache.directory.shared.asn1.util.IntegerDecoder;
029    import org.apache.directory.shared.asn1.util.IntegerDecoderException;
030    import org.apache.directory.shared.i18n.I18n;
031    import org.apache.directory.shared.ldap.codec.LdapMessageContainer;
032    import org.apache.directory.shared.ldap.codec.LdapResponseCodec;
033    import org.apache.directory.shared.ldap.codec.LdapResultCodec;
034    import org.apache.directory.shared.ldap.message.ResultCodeEnum;
035    import org.apache.directory.shared.ldap.util.StringTools;
036    import org.slf4j.Logger;
037    import org.slf4j.LoggerFactory;
038    
039    
040    /**
041     * The action used to set the LdapResult result code.
042     * 
043     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044     * @version $Rev$, $Date$, 
045     */
046    public class ResultCodeAction extends GrammarAction
047    {
048        /** The logger */
049        private static final Logger log = LoggerFactory.getLogger( ResultCodeAction.class );
050    
051        /** Speedup for logs */
052        private static final boolean IS_DEBUG = log.isDebugEnabled();
053    
054        public ResultCodeAction()
055        {
056            super( "Store resultCode" );
057        }
058    
059        /**
060         * The initialization action
061         */
062        public void action( IAsn1Container container ) throws DecoderException
063        {
064            LdapMessageContainer ldapMessageContainer = ( LdapMessageContainer ) container;
065            LdapResponseCodec response = ldapMessageContainer.getLdapResponse();
066            LdapResultCodec ldapResult = new LdapResultCodec();
067            response.setLdapResult( ldapResult );
068    
069            // We don't have to allocate a LdapResult first.
070    
071            // The current TLV should be a integer
072            // We get it and store it in MessageId
073            TLV tlv = ldapMessageContainer.getCurrentTLV();
074    
075            Value value = tlv.getValue();
076            ResultCodeEnum resultCode = ResultCodeEnum.SUCCESS;
077    
078            try
079            {
080                resultCode = ResultCodeEnum.getResultCode( IntegerDecoder.parse( value, 0, ResultCodeEnum.UNKNOWN.getResultCode() ) );
081            }
082            catch ( IntegerDecoderException ide )
083            {
084                log.error( I18n.err( I18n.ERR_04018, StringTools.dumpBytes( value.getData() ), ide.getMessage() ) );
085    
086                throw new DecoderException( ide.getMessage() );
087            }
088    
089            // Treat the 'normal' cases !
090            switch ( resultCode )
091            {
092                case SUCCESS:
093                case OPERATIONS_ERROR:
094                case PROTOCOL_ERROR:
095                case TIME_LIMIT_EXCEEDED:
096                case SIZE_LIMIT_EXCEEDED:
097                case COMPARE_FALSE:
098                case COMPARE_TRUE:
099                case AUTH_METHOD_NOT_SUPPORTED:
100                case STRONG_AUTH_REQUIRED:
101                case REFERRAL:
102                case ADMIN_LIMIT_EXCEEDED:
103                case UNAVAILABLE_CRITICAL_EXTENSION:
104                case CONFIDENTIALITY_REQUIRED:
105                case SASL_BIND_IN_PROGRESS:
106                case NO_SUCH_ATTRIBUTE:
107                case UNDEFINED_ATTRIBUTE_TYPE:
108                case INAPPROPRIATE_MATCHING:
109                case CONSTRAINT_VIOLATION:
110                case ATTRIBUTE_OR_VALUE_EXISTS:
111                case INVALID_ATTRIBUTE_SYNTAX:
112                case NO_SUCH_OBJECT:
113                case ALIAS_PROBLEM:
114                case INVALID_DN_SYNTAX:
115                case ALIAS_DEREFERENCING_PROBLEM:
116                case INAPPROPRIATE_AUTHENTICATION:
117                case INVALID_CREDENTIALS:
118                case INSUFFICIENT_ACCESS_RIGHTS:
119                case BUSY:
120                case UNAVAILABLE:
121                case UNWILLING_TO_PERFORM:
122                case LOOP_DETECT:
123                case NAMING_VIOLATION:
124                case OBJECT_CLASS_VIOLATION:
125                case NOT_ALLOWED_ON_NON_LEAF:
126                case NOT_ALLOWED_ON_RDN:
127                case ENTRY_ALREADY_EXISTS:
128                case AFFECTS_MULTIPLE_DSAS:
129                case CANCELED:
130                case CANNOT_CANCEL:
131                case TOO_LATE:
132                case NO_SUCH_OPERATION:
133                    ldapResult.setResultCode( resultCode );
134                    break;
135    
136                default:
137                    log.warn( "The resultCode " + resultCode + " is unknown." );
138                    ldapResult.setResultCode( ResultCodeEnum.OTHER );
139                    break;
140            }
141    
142            if ( IS_DEBUG )
143            {
144                log.debug( "The result code is set to " + resultCode );
145            }
146        }
147    }