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.james.protocols.smtp.hook;
021    
022    /**
023     * Result which get used for hooks
024     * 
025     */
026    public final class HookResult {
027    
028        private int result;
029        private String smtpRetCode;
030        private String smtpDescription;
031        
032        /**
033         * Construct new HookResult
034         * 
035         * @param result 
036         * @param smtpRetCode 
037         * @param smtpDescription
038         */
039        public HookResult(int result, String smtpRetCode, CharSequence smtpDescription) {
040            boolean match = false;
041    
042            if ((result & HookReturnCode.DECLINED) == HookReturnCode.DECLINED) {
043                if (match == true) throw new IllegalArgumentException();
044                match = true;
045            }
046            if ((result & HookReturnCode.OK) == HookReturnCode.OK) {
047                if (match == true) throw new IllegalArgumentException();
048                match = true;
049            }
050            if ((result & HookReturnCode.DENY) == HookReturnCode.DENY) {
051                if (match == true) throw new IllegalArgumentException();
052                match = true;
053            }
054            if ((result & HookReturnCode.DENYSOFT) == HookReturnCode.DENYSOFT) {
055                if (match == true) throw new IllegalArgumentException();
056                match = true;
057            }
058            this.result = result;
059            this.smtpRetCode = smtpRetCode;
060            this.smtpDescription = (smtpDescription == null) ? null : smtpDescription.toString();
061        }
062        
063        /**
064         * Construct new HookResult
065         * 
066         * @param result
067         * @param smtpDescription
068         */
069        public HookResult(int result, String smtpDescription) {
070            this(result,null,smtpDescription);
071        }
072        
073        /**
074         * Construct new HookResult
075         * 
076         * @param result
077         */
078        public HookResult(int result) {
079            this(result,null,null);
080        }
081        
082       
083        /**
084         * Return the result
085         * 
086         * @return result
087         */
088        public int getResult() {
089            return result;
090        }
091        
092        /**
093         * Return the SMTPRetCode which should used. If not set return null. 
094         * 
095         * @return smtpRetCode
096         */
097        public String getSmtpRetCode() {
098            return smtpRetCode;
099        }
100        
101        /**
102         * Return the SMTPDescription which should used. If not set return null
103         *  
104         * @return smtpDescription
105         */
106        public String getSmtpDescription() {
107            return smtpDescription;
108        }
109    }