KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > commands > CommandTemplate


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.commands;
19
20 import org.apache.avalon.framework.logger.AbstractLogEnabled;
21 import org.apache.james.imapserver.AccessControlException;
22 import org.apache.james.util.Assert;
23 import org.apache.james.imapserver.*;
24
25 import java.util.StringTokenizer JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 /**
31  * Base Class for all Commands.
32  *
33  * @version 0.2 on 04 Aug 2002
34  */

35
36 abstract class CommandTemplate
37         extends AbstractLogEnabled implements ImapCommand, ImapConstants
38 {
39     protected String JavaDoc commandName;
40     private List JavaDoc args = new ArrayList JavaDoc();
41
42     protected String JavaDoc getCommand()
43     {
44         return this.commandName;
45     }
46
47     /**
48      * @return a List of <code>ImapArgument</code> objects.
49      */

50     protected List JavaDoc getArgs()
51     {
52         return this.args;
53     }
54
55     protected String JavaDoc getExpectedMessage()
56     {
57         StringBuffer JavaDoc msg = new StringBuffer JavaDoc( "<tag> " );
58         msg.append( getCommand() );
59
60         List JavaDoc args = getArgs();
61         for ( Iterator JavaDoc iter = args.iterator(); iter.hasNext(); ) {
62             msg.append( " " );
63             ImapArgument arg = (ImapArgument) iter.next();
64             msg.append( arg.format() );
65         }
66         return msg.toString();
67     }
68
69     /**
70      * Template methods for handling command processing.
71      */

72     public boolean process( ImapRequest request, ImapSession session )
73     {
74         StringTokenizer JavaDoc tokens = request.getCommandLine();
75
76         List JavaDoc args = getArgs();
77         List JavaDoc argValues = new ArrayList JavaDoc();
78
79     System.out.println("CommandTemplate.process command: '"+getCommand()+"'");
80         for ( Iterator JavaDoc iter = args.iterator(); iter.hasNext(); ) {
81             System.out.println("CommandTemplate.process ARGUMENT");
82             Object JavaDoc o = iter.next();
83             ImapArgument arg = (ImapArgument) o;
84             try {
85                 argValues.add( arg.parse( tokens ) );
86             }
87             catch ( Exception JavaDoc e ) {
88                 String JavaDoc badMsg = e.getMessage() + ": Command should be " + getExpectedMessage();
89                 session.badResponse( badMsg );
90                 return true;
91             }
92         }
93
94         if ( tokens.hasMoreTokens() ) {
95             String JavaDoc badMsg = "Extra token found: Command should be " + getExpectedMessage();
96             session.badResponse( badMsg );
97             return true;
98         }
99         System.out.println("CommandTemplate.process starting doProcess");
100         return doProcess( request, session, argValues );
101
102     }
103
104     protected abstract boolean doProcess( ImapRequest request, ImapSession session, List JavaDoc argValues );
105
106     /**
107      * By default, valid in any state (unless overridden by subclass.
108      */

109     public boolean validForState( ImapSessionState state )
110     {
111         return true;
112     }
113
114     protected void logCommand( ImapRequest request, ImapSession session )
115     {
116         getLogger().debug( request.getCommand() + " command completed for " +
117                            session.getRemoteHost() + "(" +
118                            session.getRemoteIP() + ")" );
119     }
120
121     protected ACLMailbox getMailbox( ImapSession session, String JavaDoc mailboxName, String JavaDoc command )
122     {
123         if ( session.getState() == ImapSessionState.SELECTED && session.getCurrentFolder().equals( mailboxName ) ) {
124             return session.getCurrentMailbox();
125         }
126         else {
127             try {
128                 return session.getImapHost().getMailbox( session.getCurrentUser(), mailboxName );
129             } catch ( MailboxException me ) {
130                 if ( me.isRemote() ) {
131                     session.noResponse( "[REFERRAL " + me.getRemoteServer() + "]" + SP + "Remote mailbox" );
132                 } else {
133                     session.noResponse( command, "Unknown mailbox" );
134                     getLogger().info( "MailboxException in method getBox for user: "
135                                       + session.getCurrentUser() + " mailboxName: " + mailboxName + " was "
136                                       + me.getMessage() );
137                 }
138                 return null;
139             }
140             catch ( AccessControlException e ) {
141                 session.noResponse( command, "Unknown mailbox" );
142                 return null;
143             }
144         }
145     }
146
147     /** TODO - decode quoted strings properly, with escapes. */
148     public static String JavaDoc readAstring( StringTokenizer JavaDoc tokens )
149     {
150         if ( ! tokens.hasMoreTokens() ) {
151             throw new RuntimeException JavaDoc( "Not enough tokens" );
152         }
153         String JavaDoc token = tokens.nextToken();
154         Assert.isTrue( token.length() > 0 );
155
156         StringBuffer JavaDoc astring = new StringBuffer JavaDoc( token );
157
158         if ( astring.charAt(0) == '\"' ) {
159             while ( astring.length() == 1 ||
160                     astring.charAt( astring.length() - 1 ) != '\"' ) {
161                 if ( tokens.hasMoreTokens() ) {
162                     astring.append( tokens.nextToken() );
163                 }
164                 else {
165                     throw new RuntimeException JavaDoc( "Missing closing quote" );
166                 }
167             }
168             astring.deleteCharAt( 0 );
169             astring.deleteCharAt( astring.length() - 1 );
170         }
171
172         return astring.toString();
173     }
174
175     public String JavaDoc decodeAstring( String JavaDoc rawAstring )
176     {
177
178         if ( rawAstring.length() == 0 ) {
179             return rawAstring;
180         }
181
182         if ( rawAstring.startsWith( "\"" ) ) {
183             //quoted string
184
if ( rawAstring.endsWith( "\"" ) ) {
185                 if ( rawAstring.length() == 2 ) {
186                     return new String JavaDoc(); //ie blank
187
}
188                 else {
189                     return rawAstring.substring( 1, rawAstring.length() - 1 );
190                 }
191             }
192             else {
193                 getLogger().error( "Quoted string with no closing quote." );
194                 return null;
195             }
196         }
197         else {
198             //atom
199
return rawAstring;
200         }
201     }
202
203     public void setArgs( List JavaDoc args )
204     {
205         this.args = args;
206     }
207 }
208
Popular Tags