Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * /
 
 
 package org.apache.james.protocols.smtp.core.fastfail;
 
 
 import  org.apache.mailet.MailAddress;


Abstract base class which implement GreyListing.
 
 public abstract class AbstractGreylistHandler implements RcptHook {

    
1 hour
 
     private long tempBlockTime = 3600000;

    
36 days
 
     private long autoWhiteListLifeTime = 3110400000L;

    
4 hours
 
     private long unseenLifeTime = 14400000;
 
 
     
     public void setUnseenLifeTime(long unseenLifeTime) {
         this. = unseenLifeTime;
     }
     
     public void setAutoWhiteListLifeTime(long autoWhiteListLifeTime) {
         this. = autoWhiteListLifeTime;
     }
     
     public void setTempBlockTime(long tempBlockTime) {
         this. = tempBlockTime;
     }
 
 
     private HookResult doGreyListCheck(SMTPSession session, MailAddress senderAddress, MailAddress recipAddress) {
         String recip = "";
         String sender = "";
 
         if (recipAddress != nullrecip = recipAddress.toString();
         if (senderAddress != nullsender = senderAddress.toString();
     
         long time = System.currentTimeMillis();
         String ipAddress = session.getRemoteIPAddress();
         
         try {
             long createTimeStamp = 0;
             int count = 0;
             
             // get the timestamp when he triplet was last seen
             Iterator<Stringdata = getGreyListData(ipAddresssenderrecip);
             
             if (data.hasNext()) {
                 createTimeStamp = Long.parseLong(data.next());
                 count = Integer.parseInt(data.next());
             }
             
             session.getLogger().debug("Triplet " + ipAddress + " | " + sender + " | " + recip  +" -> TimeStamp: " + createTimeStamp);
 
 
             // if the timestamp is bigger as 0 we have allready a triplet stored
             if (createTimeStamp > 0) {
                 long acceptTime = createTimeStamp + ;
         
                 if ((time < acceptTime) && (count == 0)) {
                     return new HookResult(.., DSNStatus.getStatus(..
                         + " Temporary rejected: Reconnect to fast. Please try again later");
                 } else {
                     
                     session.getLogger().debug("Update triplet " + ipAddress + " | " + sender + " | " + recip + " -> timestamp: " + time);
                     
                    // update the triplet..
                    updateTriplet(ipAddresssenderrecipcounttime);
                }
            } else {
                session.getLogger().debug("New triplet " + ipAddress + " | " + sender + " | " + recip );
           
                // insert a new triplet
                insertTriplet(ipAddresssenderrecipcounttime);
      
                // Tempory block on new triplet!
                    + " Temporary rejected: Please try again later");
            }
            // some kind of random cleanup process
            if (Math.random() > 0.99) {
                // cleanup old entries
            
                session.getLogger().debug("Delete old entries");
            
                cleanupAutoWhiteListGreyList(time - );
                cleanupGreyList(time - );
            }
        } catch (Exception e) {
            // just log the exception
            session.getLogger().error("Error on greylist method: " + e.getMessage());
        }
        return new HookResult(.);
    }

    
Get all necessary data for greylisting based on provided triplet

Parameters:
ipAddress The ipAddress of the client
sender The mailFrom
recip The rcptTo
Returns:
data The data
Throws:
Exception
    protected abstract  Iterator<StringgetGreyListData(String ipAddressString senderString recipthrows Exception;

    
Insert new triplet in the store

Parameters:
ipAddress The ipAddress of the client
sender The mailFrom
recip The rcptTo
count The count
createTime The createTime
Throws:
SQLException
    protected abstract void insertTriplet(String ipAddressString senderString recipint countlong createTime)
        throws Exception;

    
Update the triplet

Parameters:
ipAddress The ipAddress of the client
sender The mailFrom
recip The rcptTo
count The count
time the current time in ms
Throws:
Exception
    protected abstract void updateTriplet(String ipAddressString senderString recipint countlong timethrows Exception;
       

    
Cleanup the autowhitelist

Parameters:
time The time which must be reached before delete the records
Throws:
Exception
    protected abstract void cleanupAutoWhiteListGreyList(long time)throws Exception;     

    
Delete old entries from the Greylist datarecord

Parameters:
time The time which must be reached before delete the records
Throws:
Exception
    protected abstract void cleanupGreyList(long timethrows Exception;

  

    

See also:
org.apache.james.protocols.smtp.hook.RcptHook.doRcpt(org.apache.james.protocols.smtp.SMTPSession, org.apache.mailet.MailAddress, org.apache.mailet.MailAddress)
    public HookResult doRcpt(SMTPSession session, MailAddress sender, MailAddress rcpt) {
        if (!session.isRelayingAllowed()) {
            return doGreyListCheck(sessionsender,rcpt);
        } else {
            session.getLogger().info("IpAddress " + session.getRemoteIPAddress() + " is allowed to send. Skip greylisting.");
        }
        return new HookResult(.);
    }
New to GrepCode? Check out our FAQ X