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.james.dsn.DSNStatus;
31  import org.apache.james.smtpserver.CommandHandler;
32  import org.apache.james.smtpserver.SMTPSession;
33  
34  
35  public class MaxRcptHandler extends AbstractJunkHandler implements
36          CommandHandler, Configurable {
37  
38      private int maxRcpt = 0;
39  
40      /**
41       * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
42       */
43      public void configure(Configuration handlerConfiguration)
44              throws ConfigurationException {
45          Configuration configuration = handlerConfiguration.getChild("maxRcpt",
46                  false);
47          if (configuration != null) {
48              setMaxRcpt(configuration.getValueAsInteger(0));
49          } else {
50              throw new ConfigurationException(
51                      "Please set the maxRcpt configuration value");
52          }
53          
54          super.configure(handlerConfiguration);
55      }
56  
57      /**
58       * Set the max rcpt for wich should be accepted
59       * 
60       * @param maxRcpt
61       *            The max rcpt count
62       */
63      public void setMaxRcpt(int maxRcpt) {
64          this.maxRcpt = maxRcpt;
65      }
66  
67      /**
68       * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession)
69       */
70      public void onCommand(SMTPSession session) {
71          doProcessing(session);
72      }
73      
74      /**
75       * @see org.apache.james.smtpserver.CommandHandler#getImplCommands()
76       */
77      public Collection getImplCommands() {
78          Collection implCommands = new ArrayList();
79          implCommands.add("RCPT");
80          
81          return implCommands;
82      }
83  
84      /**
85       * @see org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#check(org.apache.james.smtpserver.SMTPSession)
86       */
87      protected boolean check(SMTPSession session) {
88          // check if the max recipients has reached
89          return ((session.getRcptCount() + 1) > maxRcpt);
90      }
91  
92      /**
93       * @see org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getJunkHandlerData(org.apache.james.smtpserver.SMTPSession)
94       */
95      public JunkHandlerData getJunkHandlerData(SMTPSession session) {
96          JunkHandlerData data = new JunkHandlerData();
97      
98          data.setRejectResponseString("452 "  + DSNStatus.getStatus(DSNStatus.NETWORK, DSNStatus.DELIVERY_TOO_MANY_REC)
99                  + " Requested action not taken: max recipients reached");
100         data.setJunkScoreLogString("Maximum recipients of " + maxRcpt + " reached. Add JunkScore: " +getScore());
101         data.setRejectLogString("Maximum recipients of " + maxRcpt + " reached");
102         data.setScoreName("MaxRcptCheck");
103         return data;
104     }
105 }