1 17 18 package org.apache.james.imapserver.commands; 19 20 import org.apache.james.imapserver.AccessControlException; 21 import org.apache.james.imapserver.AuthorizationException; 22 import org.apache.james.imapserver.commands.ImapCommand; 23 import org.apache.james.imapserver.BaseCommand; 24 import org.apache.james.imapserver.ACLMailbox; 25 import org.apache.james.imapserver.ImapRequest; 26 import org.apache.james.imapserver.ImapSession; 27 import org.apache.james.imapserver.MessageAttributes; 28 import org.apache.james.imapserver.ImapSessionState; 29 import org.apache.james.imapserver.SingleThreadedConnectionHandler; 30 import javax.mail.internet.MimeMessage ; 31 import org.apache.james.imapserver.Flags; 32 33 import java.io.OutputStream ; 34 import java.io.PrintWriter ; 35 import java.util.List ; 36 import java.util.StringTokenizer ; 37 39 45 public class CopyCommand 46 extends BaseCommand implements ImapCommand 47 { 48 private static final boolean DEEP_DEBUG = true; 50 51 private static final String OK = "OK"; 52 private static final String NO = "NO"; 53 private static final String BAD = "BAD"; 54 private static final String UNTAGGED = "*"; 55 private static final String SP = " "; 56 57 private StringTokenizer commandLine; 58 private boolean useUIDs; 59 private ACLMailbox currentMailbox; 60 private String commandRaw; 61 private PrintWriter out; 62 private OutputStream outs; 63 private String tag; 64 private String user; 65 private SingleThreadedConnectionHandler caller; 66 private String currentFolder; 67 private ImapSession session; 68 public final String commandName = "COPY"; 69 70 public boolean validForState( ImapSessionState state ) { 71 return ( state == ImapSessionState.SELECTED ); 72 } 73 74 75 public boolean process( ImapRequest request, ImapSession session ) { 76 setRequest( request ); 77 this.session = session; 78 if ( request.arguments() < 2 ) { 79 session.badResponse( "Command '"+request.getCommandLine().nextToken()+"' should be <tag> <COPY> <message set> <destination mailbox>" ); 80 return true; 81 } 82 service(); 83 return true; 84 } 85 86 89 public void setRequest(ImapRequest request) { 90 commandLine = request.getCommandLine(); 91 useUIDs = request.useUIDs(); 92 currentMailbox = request.getCurrentMailbox(); 93 commandRaw = request.getCommandRaw(); 94 tag = request.getTag(); 95 currentFolder = request.getCurrentFolder(); 96 97 caller = request.getCaller(); 98 out = caller.getPrintWriter(); 99 outs = caller.getOutputStream(); 100 user = caller.getUser(); 101 } 102 103 106 public void service() { 107 List set; 108 if (useUIDs) { 109 set = decodeUIDSet(commandLine.nextToken(), 110 currentMailbox.listUIDs(user)); 111 } else { 112 set = decodeSet(commandLine.nextToken(), 113 currentMailbox.getExists()); 114 } 115 StringBuffer buf = new StringBuffer (); 116 String foldername = commandLine.nextToken(); 117 118 while (commandLine.hasMoreTokens()) { 119 buf.append(" "+commandLine.nextToken()); 120 } 121 foldername += buf.toString(); 122 123 foldername = foldername.replace('"',' ').trim(); 124 System.out.println("FOLDERNAME FOR COPIING: " + foldername); 125 try { 126 ACLMailbox targetMailbox = getMailbox( session, foldername, this.commandName ); 127 if ( targetMailbox == null ) { 128 return; 129 } 130 if ( !targetMailbox.hasInsertRights( session.getCurrentUser() ) ) { 131 session.noResponse( this.commandName, "Insert access not granted." ); 132 return; 133 } 134 for (int i = 0; i < set.size(); i++) { 135 if (useUIDs) { 136 Integer uidObject = (Integer )set.get(i); 137 int uid = uidObject.intValue(); 138 MimeMessage message = (MimeMessage ) 139 session.getCurrentMailbox().retrieveUID(uid, session.getCurrentUser() ); 140 146 targetMailbox.store(message, session.getCurrentUser()); 147 } else { 148 int msn = ((Integer )set.get( 0 ) ).intValue(); 149 MimeMessage message = (MimeMessage ) 150 session.getCurrentMailbox().retrieve(msn, session.getCurrentUser() ); 151 157 targetMailbox.store(message, session.getCurrentUser()); 158 } 159 } 160 161 caller.checkSize(); 162 out.println(tag + SP + OK + SP + "COPY completed"); 163 } catch (AccessControlException ace) { 164 out.println(tag + SP + NO + SP + "No such mailbox"); 165 caller.logACE(ace); 166 return; 167 } catch (AuthorizationException aze) { 168 out.println(tag + SP + NO + SP 169 + "You do not have the rights to store those flags"); 170 caller.logAZE(aze); 171 return; 172 } catch (IllegalArgumentException iae) { 173 out.println(tag + SP + BAD + SP 174 + "Arguments to store not recognised."); 175 getLogger().error("Unrecognised arguments for STORE by user " + user 176 + " with " + commandRaw); 177 return; 178 } 179 return; 180 } 181 } 182 | Popular Tags |