1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.smtpserver.core.filter.fastfail;
23
24 import org.apache.avalon.framework.configuration.Configurable;
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.avalon.framework.logger.AbstractLogEnabled;
28 import org.apache.james.smtpserver.SMTPSession;
29 import org.apache.james.smtpserver.junkscore.JunkScore;
30 import org.apache.james.smtpserver.junkscore.JunkScoreConfigUtil;
31
32
33
34
35 public abstract class AbstractJunkHandler extends AbstractLogEnabled implements Configurable {
36 private String action = "reject";
37 private double score = 0;
38
39
40
41
42 public void configure(Configuration handlerConfiguration) throws ConfigurationException {
43
44 Configuration configAction = handlerConfiguration.getChild("action",false);
45 if(configAction != null) {
46 String configString = configAction.getValue();
47 if (configString.startsWith(JunkScoreConfigUtil.JUNKSCORE)) {
48 setAction(JunkScoreConfigUtil.JUNKSCORE);
49 setScore(JunkScoreConfigUtil.getJunkScore(configAction.getValue()));
50 }
51 }
52
53 }
54
55
56
57
58
59
60
61 public void setAction(String action) {
62 this.action = action.toLowerCase();
63 }
64
65
66
67
68
69
70 public void setScore(double score) {
71 this.score = score;
72 }
73
74
75
76
77
78
79 protected double getScore() {
80 return score;
81 }
82
83
84
85
86
87
88
89 protected String getAction() {
90 return action;
91 }
92
93
94
95
96
97
98 protected void doProcessing(SMTPSession session) {
99 if (check(session)) {
100 JunkHandlerData data = getJunkHandlerData(session);
101
102 if (getAction().equals(JunkScoreConfigUtil.JUNKSCORE)) {
103 getLogger().info(getJunkHandlerData(session).getJunkScoreLogString());
104 JunkScore junk = getJunkScore(session);
105
106 junk.setStoredScore(data.getScoreName(), getScore());
107
108 } else {
109 String response = data.getRejectResponseString();
110
111 if (data.getRejectLogString() != null) getLogger().info(data.getRejectLogString());
112
113 session.writeResponse(response);
114
115 session.setStopHandlerProcessing(true);
116 }
117 }
118 }
119
120
121
122
123
124
125
126 protected abstract boolean check(SMTPSession session);
127
128
129
130
131
132
133
134 public abstract JunkHandlerData getJunkHandlerData(SMTPSession session);
135
136
137
138
139
140
141 protected JunkScore getJunkScore(SMTPSession session) {
142 return (JunkScore) session.getState().get(JunkScore.JUNK_SCORE);
143 }
144 }