Appendix A – Command Response Protocol
A typical command response protocol sequence client> command [argument] james> response code and description client> command [argument] james> response code client> message and message terminator james> response code
Breaking the steps of in-protocol handling: 1. As soon as the client initiates connection, apply connection rules and terminate session upon violation. 2. Get the corresponding command handler for the command. 3. Apply Command rules for validating the arguments. 4. If all necessary validations succeed, continue. 5. If a message is expected, read the whole message. 6. Apply Message rules for verifying the message content. 7. If all the message validations succeed, hand over the mail to spooler for further processing.
Appendix B – Interface details
A set of framework classes placed under a new package org.apache.james.protocol
//configure and add command
void addCommand(Command cmdHandler);
//configure and add rules
void addRule(Rule rule);
void addRule(Rule rule, Command cmdHandler);
//get rules
Rules[] getConnectionRules();
Rules[] getCommandRules(Command cmdHamdler);
Rules[] getMessageRules(Command cmdHandler);
//get all the registered command handlers
CommandHandler[] getCommandHandlers();
//state info
boolean isCommandAllowed(String cmdName);
void SetCommandStatus(String cmdName, boolean status);
CommandHandler getCurrentCommandHandler()
//access function used by command and message processing
InputStream getInputStream();
InputStream getOutputStream();
String nextCommand();
}
public interface CommandHandler {
configure(...);
void onCommand(cmdStr); //for command processing
boolean waitForMessage(); //if message is expected for the command
void onMessage(); //for message processing
byte[] getMessage(); //return message buffer
String getName(); //get command Name
String getArgument(); //return the arguments for the command
void setProtocolHandler(ProtocolHandler handler); // to access to I/O and state
boolean equals(String cmdString);//to check if the command string can be processed by the handler
}
public interface Rule {
public final type; // 3 types rules as mentioned
void apply() throws Exception;//continue session if there is no exception
configure(..); //configure the rule
}
Appendix C – SMTPHandler implementation example
SMTPHandler implements the ProtocolHandler interface.
///initialization steps
read config.xml
create and configure command objects
create and configure connection rules
create and configure command and message rules
////processing commands and messages
//get all connection rules and apply
Rules[] connectionRules = getConnectionRules();
for(int i = 0; i < connectionRules.length; i++) {
connectionRules[i].apply();
}
while(true) {
//Get matching command handler
cmdString = nextComand();
Command[] commands = getCommands();
Command currCmd = null;
for(int i = 0; i < commands.length; i++)
{
if(commands[i].equals(cmdString))
{
currCmd = commands[i];
break;
}
}
//handle command and message
if(currCmd == null) //command not supported
else {
//apply command rules
Rules[] commandRules = getCommandRules(currCmd);
for(int i = 0; i < commandRules.length; i++) {
commandRules[i].apply();
}
currCmd.onCommand(); //process command
//if there is no associate message stop
if(!currCmd.waitForMessage()) continue;
//else process message
currCmd.onMessage();
//apply message rules
Rules[] msgRules = getMessageRules(currCmd);
for(int i = 0; i < msgRules.length; i++) {
msgRulesRules[i].apply();
}
}