1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
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 * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
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 * Set the Action which should be taken if the mail from has no valid domain.
57 * Supported are: junkScore and reject
58 *
59 * @param action the action
60 */
61 public void setAction(String action) {
62 this.action = action.toLowerCase();
63 }
64
65 /**
66 * Set the score which will get added to the JunkScore object if the action is junkScore andt the sender has no valid domain
67 *
68 * @param score the score
69 */
70 public void setScore(double score) {
71 this.score = score;
72 }
73
74 /**
75 * Return the configured score
76 *
77 * @return score
78 */
79 protected double getScore() {
80 return score;
81 }
82
83
84 /**
85 * Return the action
86 *
87 * @return action
88 */
89 protected String getAction() {
90 return action;
91 }
92
93 /**
94 * Process the checking
95 *
96 * @param session the SMTPSession
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 // After this filter match we should not call any other handler!
115 session.setStopHandlerProcessing(true);
116 }
117 }
118 }
119
120 /**
121 * All checks must be done in this method
122 *
123 * @param session the SMTPSession
124 * @return true if the check match
125 */
126 protected abstract boolean check(SMTPSession session);
127
128 /**
129 * Get the JunkHandlerData to work with
130 *
131 * @param session the SMTPSession
132 * @return junkHandlerData
133 */
134 public abstract JunkHandlerData getJunkHandlerData(SMTPSession session);
135
136 /**
137 * Return the JunkScore object.
138 *
139 * @return junkScore
140 */
141 protected JunkScore getJunkScore(SMTPSession session) {
142 return (JunkScore) session.getState().get(JunkScore.JUNK_SCORE);
143 }
144 }