1 17 18 package org.apache.james.imapserver; 19 20 import org.apache.james.imapserver.AccessControlException; 21 import org.apache.james.imapserver.AuthorizationException; 22 import org.apache.james.imapserver.commands.ImapCommand; 23 24 import java.io.OutputStream ; 25 import java.io.PrintWriter ; 26 import java.util.List ; 27 import java.util.StringTokenizer ; 28 30 36 public class CommandStore 37 extends BaseCommand implements ImapCommand 38 { 39 private static final boolean DEEP_DEBUG = true; 41 42 private static final String OK = "OK"; 43 private static final String NO = "NO"; 44 private static final String BAD = "BAD"; 45 private static final String UNTAGGED = "*"; 46 private static final String SP = " "; 47 48 private StringTokenizer commandLine; 49 private boolean useUIDs; 50 private ACLMailbox currentMailbox; 51 private String commandRaw; 52 private PrintWriter out; 53 private OutputStream outs; 54 private String tag; 55 private String user; 56 private SingleThreadedConnectionHandler caller; 57 private String currentFolder; 58 59 public boolean validForState( ImapSessionState state ) 60 { 61 return ( state == ImapSessionState.SELECTED ); 62 } 63 64 65 public boolean process( ImapRequest request, ImapSession session ) 66 { 67 setRequest( request ); 68 if ( request.arguments() < 3 ) { 69 session.badResponse( "Command '"+request.getCommandLine().nextToken()+"' should be <tag> <STORE> <message set> <message data item names> <value for message data item>" ); 70 return true; 71 } 72 service(); 73 return true; 74 } 75 76 79 public void setRequest(ImapRequest request) { 80 commandLine = request.getCommandLine(); 81 useUIDs = request.useUIDs(); 82 currentMailbox = request.getCurrentMailbox(); 83 commandRaw = request.getCommandRaw(); 84 tag = request.getTag(); 85 currentFolder = request.getCurrentFolder(); 86 87 caller = request.getCaller(); 88 out = caller.getPrintWriter(); 89 outs = caller.getOutputStream(); 90 user = caller.getUser(); 91 } 92 93 97 public void service() { 98 List set; 99 if (useUIDs) { 100 set = decodeUIDSet(commandLine.nextToken(), 101 currentMailbox.listUIDs(user)); 102 } else { 103 set = decodeSet(commandLine.nextToken(), 104 currentMailbox.getExists()); 105 } 106 StringBuffer buf = new StringBuffer (); 107 while (commandLine.hasMoreTokens()) { 108 buf.append(commandLine.nextToken()); 109 } 110 String request = buf.toString(); 111 try { 112 for (int i = 0; i < set.size(); i++) { 113 if (useUIDs) { 114 Integer uidObject = (Integer )set.get(i); 115 int uid = uidObject.intValue(); 116 if (currentMailbox.setFlagsUID(uid, user, request)) { 117 if (request.toUpperCase().indexOf("SILENT") == -1) { 118 String newflags 119 = currentMailbox.getFlagsUID(uid, user); 120 out.println(UNTAGGED + SP + SP 121 + "FETCH (FLAGS " + newflags 122 + " UID " + uid + ")"); 123 } else { 124 } 126 } else { 127 out.println(tag + SP + NO + SP 129 + "Unable to store flags for message: " 130 + uid); 131 } 132 } else { 133 int msn = ((Integer )set.get(i)).intValue(); 134 if (currentMailbox.setFlags(msn, user, request)) { 135 if (request.toUpperCase().indexOf("SILENT") == -1) { 136 String newflags 137 = currentMailbox.getFlags(msn, user); 138 out.println(UNTAGGED + SP + msn + SP 139 + "FETCH (FLAGS " + newflags + ")"); 140 } else { 141 } 143 } else { 144 out.println(tag + SP + NO + SP 146 + "Unable to store flags for message: " 147 + msn); 148 } 149 } 150 } 151 caller.checkSize(); 152 out.println(tag + SP + OK + SP + "STORE completed"); 153 } catch (AccessControlException ace) { 154 out.println(tag + SP + NO + SP + "No such mailbox"); 155 caller.logACE(ace); 156 return; 157 } catch (AuthorizationException aze) { 158 out.println(tag + SP + NO + SP 159 + "You do not have the rights to store those flags"); 160 caller.logAZE(aze); 161 return; 162 } catch (IllegalArgumentException iae) { 163 out.println(tag + SP + BAD + SP 164 + "Arguments to store not recognised."); 165 getLogger().error("Unrecognised arguments for STORE by user " + user 166 + " with " + commandRaw); 167 return; 168 } 169 return; 170 } 171 } 172 | Popular Tags |