KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > ImapRequestHandler


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

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 JavaDoc;
25 import java.io.OutputStream JavaDoc;
26
27 /**
28  *
29  *
30  * @version $Revision: 1.2.2.3 $
31  */

32 public final class ImapRequestHandler
33 {
34     private ImapCommandFactory imapCommands = new ImapCommandFactory();
35     private CommandParser parser = new CommandParser();
36     private static final String JavaDoc REQUEST_SYNTAX = "Protocol Error: Was expecting <tag SPACE command [arguments]>";
37
38     /**
39      * This method parses POP3 commands read off the wire in handleConnection.
40      * Actual processing of the command (possibly including additional back and
41      * forth communication with the client) is delegated to one of a number of
42      * command specific handler methods. The primary purpose of this method is
43      * to parse the raw command string to determine exactly which handler should
44      * be called. It returns true if expecting additional commands, false otherwise.
45      *
46      * @return whether additional commands are expected.
47      */

48     public boolean handleRequest( InputStream JavaDoc input,
49                                   OutputStream JavaDoc 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         // Consume the rest of the line, throwing away any extras. This allows us
66
// to clean up after a protocol error.
67
request.consumeLine();
68
69         return true;
70     }
71
72     private void doProcessRequest( ImapRequestLineReader request,
73                                    ImapResponse response,
74                                    ImapSession session)
75     {
76         String JavaDoc tag = null;
77         String JavaDoc 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 // System.out.println( "Got <tag>: " + tag );
88
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 // System.out.println( "Got <command>: " + commandName );
98
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