1 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 ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 30 35 36 abstract class CommandTemplate 37 extends AbstractLogEnabled implements ImapCommand, ImapConstants 38 { 39 protected String commandName; 40 private List args = new ArrayList (); 41 42 protected String getCommand() 43 { 44 return this.commandName; 45 } 46 47 50 protected List getArgs() 51 { 52 return this.args; 53 } 54 55 protected String getExpectedMessage() 56 { 57 StringBuffer msg = new StringBuffer ( "<tag> " ); 58 msg.append( getCommand() ); 59 60 List args = getArgs(); 61 for ( Iterator 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 72 public boolean process( ImapRequest request, ImapSession session ) 73 { 74 StringTokenizer tokens = request.getCommandLine(); 75 76 List args = getArgs(); 77 List argValues = new ArrayList (); 78 79 System.out.println("CommandTemplate.process command: '"+getCommand()+"'"); 80 for ( Iterator iter = args.iterator(); iter.hasNext(); ) { 81 System.out.println("CommandTemplate.process ARGUMENT"); 82 Object o = iter.next(); 83 ImapArgument arg = (ImapArgument) o; 84 try { 85 argValues.add( arg.parse( tokens ) ); 86 } 87 catch ( Exception e ) { 88 String badMsg = e.getMessage() + ": Command should be " + getExpectedMessage(); 89 session.badResponse( badMsg ); 90 return true; 91 } 92 } 93 94 if ( tokens.hasMoreTokens() ) { 95 String 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 argValues ); 105 106 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 mailboxName, String 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 148 public static String readAstring( StringTokenizer tokens ) 149 { 150 if ( ! tokens.hasMoreTokens() ) { 151 throw new RuntimeException ( "Not enough tokens" ); 152 } 153 String token = tokens.nextToken(); 154 Assert.isTrue( token.length() > 0 ); 155 156 StringBuffer astring = new StringBuffer ( 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 ( "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 decodeAstring( String rawAstring ) 176 { 177 178 if ( rawAstring.length() == 0 ) { 179 return rawAstring; 180 } 181 182 if ( rawAstring.startsWith( "\"" ) ) { 183 if ( rawAstring.endsWith( "\"" ) ) { 185 if ( rawAstring.length() == 2 ) { 186 return new String (); } 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 return rawAstring; 200 } 201 } 202 203 public void setArgs( List args ) 204 { 205 this.args = args; 206 } 207 } 208 | Popular Tags |