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 package org.apache.james.protocols.smtp.core.log;
020
021 import org.apache.james.protocols.smtp.SMTPSession;
022 import org.apache.james.protocols.smtp.hook.Hook;
023 import org.apache.james.protocols.smtp.hook.HookResult;
024 import org.apache.james.protocols.smtp.hook.HookResultHook;
025 import org.apache.james.protocols.smtp.hook.HookReturnCode;
026
027 /**
028 *
029 * Log the {@link HookResult}. If {@link HookReturnCode#DENY}, {@link HookReturnCode#DENYSOFT} or {@link HookReturnCode#DISCONNECT} was used it will get
030 * logged to INFO. If not to DEBUG
031 *
032 */
033 public class HookResultLogger implements HookResultHook{
034
035 public HookResult onHookResult(SMTPSession session, HookResult hResult, long executionTime, Hook hook) {
036 boolean match = false;
037 boolean info = false;
038 int result = hResult.getResult();
039 StringBuilder sb = new StringBuilder();
040 sb.append(hook.getClass().getName());
041 sb.append(": result=");
042 sb.append(result);
043 sb.append(" (");
044 if ((result & HookReturnCode.DECLINED) == HookReturnCode.DECLINED) {
045 sb.append("DECLINED");
046 match = true;
047 }
048 if ((result & HookReturnCode.OK) == HookReturnCode.OK) {
049 sb.append("OK");
050 match = true;
051 }
052 if ((result & HookReturnCode.DENY) == HookReturnCode.DENY) {
053 sb.append("DENY");
054 match = true;
055 info = true;
056 }
057 if ((result & HookReturnCode.DENYSOFT) == HookReturnCode.DENYSOFT) {
058 sb.append("DENYSOFT");
059 match = true;
060 info = true;
061 }
062 if ((result & HookReturnCode.DISCONNECT) == HookReturnCode.DISCONNECT) {
063 if(match) {
064 sb.append("|");
065 }
066 sb.append("DISCONNECT");
067 info = true;
068 }
069 sb.append(")");
070
071 if (info) {
072 session.getLogger().info(sb.toString());
073 } else {
074 session.getLogger().debug(sb.toString());
075 }
076 return hResult;
077 }
078
079 }