1 17 18 package org.apache.james.imapserver; 19 20 import org.apache.james.imapserver.commands.ImapCommandFactory; 21 import org.apache.james.imapserver.commands.CommandParser; 22 import org.apache.james.imapserver.commands.ImapCommand; 23 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 27 32 public final class ImapRequestHandler 33 { 34 private ImapCommandFactory imapCommands = new ImapCommandFactory(); 35 private CommandParser parser = new CommandParser(); 36 private static final String REQUEST_SYNTAX = "Protocol Error: Was expecting <tag SPACE command [arguments]>"; 37 38 48 public boolean handleRequest( InputStream input, 49 OutputStream output, 50 ImapSession session ) 51 throws ProtocolException 52 { 53 ImapRequestLineReader request = new ImapRequestLineReader( input, output ); 54 try { 55 request.nextChar(); 56 } 57 catch ( ProtocolException e ) { 58 return false; 59 } 60 61 ImapResponse response = new ImapResponse( output ); 62 63 doProcessRequest( request, response, session ); 64 65 request.consumeLine(); 68 69 return true; 70 } 71 72 private void doProcessRequest( ImapRequestLineReader request, 73 ImapResponse response, 74 ImapSession session) 75 { 76 String tag = null; 77 String commandName = null; 78 79 try { 80 tag = parser.tag( request ); 81 } 82 catch ( ProtocolException e ) { 83 response.badResponse( REQUEST_SYNTAX ); 84 return; 85 } 86 87 response.setTag( tag ); 89 try { 90 commandName = parser.atom( request ); 91 } 92 catch ( ProtocolException e ) { 93 response.commandError( REQUEST_SYNTAX ); 94 return; 95 } 96 97 ImapCommand command = imapCommands.getCommand( commandName ); 99 if ( command == null ) 100 { 101 response.commandError( "Invalid command."); 102 return; 103 } 104 105 if ( !command.validForState( session.getState() ) ) { 106 response.commandFailed( command, "Command not valid in this state" ); 107 return; 108 } 109 110 command.process( request, response, session ); 111 } 112 113 114 } 115 | Popular Tags |