View Javadoc

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 java.util.ArrayList;
25  import java.util.Collection;
26  
27  import org.apache.avalon.framework.configuration.Configurable;
28  import org.apache.avalon.framework.configuration.Configuration;
29  import org.apache.avalon.framework.configuration.ConfigurationException;
30  import org.apache.avalon.framework.logger.AbstractLogEnabled;
31  import org.apache.james.smtpserver.CommandHandler;
32  import org.apache.james.smtpserver.SMTPSession;
33  
34  public class TarpitHandler extends AbstractLogEnabled implements
35          CommandHandler, Configurable {
36  
37      private int tarpitRcptCount = 0;
38  
39      private long tarpitSleepTime = 5000;
40  
41      /**
42       * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
43       */
44      public void configure(Configuration handlerConfiguration)
45              throws ConfigurationException {
46  
47          Configuration configTarpitRcptCount = handlerConfiguration.getChild(
48                  "tarpitRcptCount", false);
49          if (configTarpitRcptCount != null) {
50              setTarpitRcptCount(configTarpitRcptCount.getValueAsInteger(0));
51          }
52  
53          if (tarpitRcptCount == 0)
54              throw new ConfigurationException(
55                      "Please set the tarpitRcptCount bigger values as 0");
56  
57          Configuration configTarpitSleepTime = handlerConfiguration.getChild(
58                  "tarpitSleepTime", false);
59          if (configTarpitSleepTime != null) {
60              setTarpitSleepTime(configTarpitSleepTime.getValueAsLong(5000));
61          }
62  
63          if (tarpitSleepTime == 0)
64              throw new ConfigurationException(
65                      "Please set the tarpitSleepTimeto a bigger values as 0");
66  
67      }
68  
69      /**
70       * Set the tarpit count after which the tarpit sleep time will be activated
71       * 
72       * @param tarpitRcptCount
73       */
74      public void setTarpitRcptCount(int tarpitRcptCount) {
75          this.tarpitRcptCount = tarpitRcptCount;
76      }
77  
78      /**
79       * Set the tarpit sleep time
80       * 
81       * @param tarpitSleepTime
82       *            Time in milliseconds
83       */
84      public void setTarpitSleepTime(long tarpitSleepTime) {
85          this.tarpitSleepTime = tarpitSleepTime;
86      }
87  
88      /**
89       * Add a sleep for the given milliseconds
90       * 
91       * @param timeInMillis
92       *            Time in ms
93       * @throws InterruptedException
94       */
95      private void sleep(float timeInMillis) throws InterruptedException {
96          Thread.sleep((long) timeInMillis);
97      }
98  
99      /**
100      * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession)
101      */
102     public void onCommand(SMTPSession session) {
103 
104         int rcptCount = 0;
105         rcptCount = session.getRcptCount();
106         rcptCount++;
107 
108         if (rcptCount > tarpitRcptCount) {
109             try {
110                 sleep(tarpitSleepTime);
111             } catch (InterruptedException e) {
112             }
113         }
114     }
115     
116     /**
117      * @see org.apache.james.smtpserver.CommandHandler#getImplCommands()
118      */
119     public Collection getImplCommands() {
120         Collection implCommands = new ArrayList();
121         implCommands.add("RCPT");
122         
123         return implCommands;
124     }
125 }