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;
 
 import java.util.List;
 
Abstract class which Handle hook-aware CommanHandler.
 
 public abstract class AbstractHookableCmdHandler<Hook extends org.apache.james.protocols.smtp.hook.Hookimplements CommandHandler<SMTPSession>, ExtensibleHandler {
 
 
     private List<Hook> hooks;
     private List<HookResultHookrHooks;

    
Handle command processing

See also:
org.apache.james.smtpserver.protocol.CommandHandler#onCommand(org.apache.james.protocols.smtp.SMTPSession , java.lang.String , java.lang.String )
 
     public Response onCommand(SMTPSession sessionRequest request) {
         String command = request.getCommand();
         String parameters = request.getArgument();
         SMTPResponse response = doFilterChecks(sessioncommandparameters);
 
         if (response == null) {
 
             response = processHooks(sessioncommandparameters);
             if (response == null) {
                 return doCoreCmd(sessioncommandparameters);
             } else {
                 return response;
             }
         } else {
             return response;
         }
 
     }

    
Process all hooks for the given command

Parameters:
session the SMTPSession object
command the command
parameters the paramaters
Returns:
SMTPResponse
 
     private SMTPResponse processHooks(SMTPSession sessionString command,
             String parameters) {
         List<Hook> hooks = getHooks();
         if (hooks != null) {
             int count = hooks.size();
             for (int i = 0; i < counti++) {
                 Hook rawHook = hooks.get(i);
                 session.getLogger().debug("executing hook " + rawHook.getClass().getName());
                 long start = System.currentTimeMillis();
                 
                 HookResult hRes = callHook(rawHooksessionparameters);
                 long executionTime = System.currentTimeMillis() - start;
 
                 if ( != null) {
                     for (int i2 = 0; i2 < .size(); i2++) {
                         Object rHook = .get(i2);
                         session.getLogger().debug("executing hook " + rHook);
                         hRes = ((HookResultHookrHook).onHookResult(sessionhResexecutionTimerawHook);
                    }
                }
                
                // call the core cmd if we receive a ok return code of the hook so no other hooks are executed
                if ((hRes.getResult() & .) == .) {
                    SMTPResponse response = doCoreCmd(sessioncommandparameters);
                    if ((hRes.getResult() & .) == .) {
                        response.setEndSession(true);
                    }
                      return response;
                } else {
                	SMTPResponse res = calcDefaultSMTPResponse(hRes);
                	if (res != null) {
                		return res;
                	}
                }
            }
        }
        return null;
    }

    
Must be implemented by hookable cmd handlers to make the effective call to an hook.

Parameters:
rawHook the hook
session the session
parameters the parameters
Returns:
the HookResult, will be calculated using HookResultToSMTPResponse.
    protected abstract HookResult callHook(Hook rawHookSMTPSession sessionString parameters);

    
Convert the HookResult to SMTPResponse using default values. Should be override for using own values

Parameters:
result HookResult
Returns:
SMTPResponse
    public static SMTPResponse calcDefaultSMTPResponse(HookResult result) {
        if (result != null) {
            int rCode = result.getResult();
            String smtpRetCode = result.getSmtpRetCode();
            String smtpDesc = result.getSmtpDescription();
    
            if ((rCode &.) == .) {
                if (smtpRetCode == null)
                    smtpRetCode = .;
                if (smtpDesc == null)
                    smtpDesc = "Email rejected";
    
                SMTPResponse response =  new SMTPResponse(smtpRetCodesmtpDesc);
                if ((rCode & .) == .) {
                    response.setEndSession(true);
                }
                return response;
            } else if (rCode == .) {
                if (smtpRetCode == null)
                    smtpRetCode = .;
                if (smtpDesc == null)
                    smtpDesc = "Temporary problem. Please try again later";
    
                SMTPResponse response = new SMTPResponse(smtpRetCodesmtpDesc);
                if ((rCode & .) == .) {
                    response.setEndSession(true);
                }
                return response;
            } else if ((rCode & .) == .) {
                if (smtpRetCode == null)
                    smtpRetCode = .;
                if (smtpDesc == null)
                    smtpDesc = "Command accepted";
    
                SMTPResponse response = new SMTPResponse(smtpRetCodesmtpDesc);
                if ((rCode & .) == .) {
                    response.setEndSession(true);
                }
                return response;
            } else if ((rCode & .) == .) {
                SMTPResponse response = new SMTPResponse("");
                response.setEndSession(true);
                return response;
            } else {
                // Return null as default
                return null;
            }
        } else {
            return null;
        }
    }

    
Execute Syntax checks and return a SMTPResponse if a syntax error was detected, otherwise null.

Parameters:
session
command
parameters
Returns:
    protected abstract SMTPResponse doFilterChecks(SMTPSession session,
            String commandString parameters);

    
Execute the core commandHandling.

Parameters:
session
command
parameters
Returns:
    protected abstract SMTPResponse doCoreCmd(SMTPSession session,
            String commandString parameters);
    

    
    public List<Class<?>> getMarkerInterfaces() {
        List<Class<?>> classes = new ArrayList<Class<?>>(2);
        classes.add(getHookInterface());
        classes.add(HookResultHook.class);
        return classes;
    }

    
Return the interface which hooks need to implement to hook in

Returns:
interface
    protected abstract Class<Hook> getHookInterface();

    
    @SuppressWarnings("unchecked")
    public void wireExtensions(Class interfaceNameList extension) {
        if (getHookInterface().equals(interfaceName)) {
            this. = extension;
        } else if (HookResultHook.class.equals(interfaceName)) {
            this. = extension;
        }
    }

    
Return a list which holds all hooks for the cmdHandler

Returns:
    protected List<Hook> getHooks() {
        return ;
    }
New to GrepCode? Check out our FAQ X