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