KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
31 import org.apache.james.imapserver.Flags;
32
33 import java.io.OutputStream JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37 //import org.apache.james.core.EnhancedMimeMessage;
38

39 /**
40  * Implements the IMAP UID COPY command for a given ImapRequestImpl.
41  *
42  * References: rfc 2060
43  * @version 0.2 on 04 Aug 2002
44  */

45 public class CopyCommand
46     extends BaseCommand implements ImapCommand
47 {
48     //mainly to switch on stack traces and catch responses;
49
private static final boolean DEEP_DEBUG = true;
50
51     private static final String JavaDoc OK = "OK";
52     private static final String JavaDoc NO = "NO";
53     private static final String JavaDoc BAD = "BAD";
54     private static final String JavaDoc UNTAGGED = "*";
55     private static final String JavaDoc SP = " ";
56
57     private StringTokenizer JavaDoc commandLine;
58     private boolean useUIDs;
59     private ACLMailbox currentMailbox;
60     private String JavaDoc commandRaw;
61     private PrintWriter JavaDoc out;
62     private OutputStream JavaDoc outs;
63     private String JavaDoc tag;
64     private String JavaDoc user;
65     private SingleThreadedConnectionHandler caller;
66     private String JavaDoc currentFolder;
67     private ImapSession session;
68     public final String JavaDoc 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     /**
87      * Debugging method - will probably disappear
88      */

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     /**
104      * Implements IMAP UID COPY commands given an ImapRequestImpl.
105      */

106     public void service() {
107         List JavaDoc 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 JavaDoc buf = new StringBuffer JavaDoc();
116         String JavaDoc 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 JavaDoc uidObject = (Integer JavaDoc)set.get(i);
137                     int uid = uidObject.intValue();
138                     MimeMessage JavaDoc message = (MimeMessage JavaDoc)
139                         session.getCurrentMailbox().retrieveUID(uid, session.getCurrentUser() );
140                     /*MessageAttributes mattr =
141                         session.getCurrentMailbox().getMessageAttributesUID(uid, session.getCurrentUser() );
142                     Flags flags = new Flags();
143                     flags.setFlags(session.getCurrentMailbox().getFlagsUID(uid, session.getCurrentUser()),
144                                     session.getCurrentUser());
145                      */

146                      targetMailbox.store(message, session.getCurrentUser());
147                 } else {
148                     int msn = ((Integer JavaDoc)set.get( 0 ) ).intValue();
149                     MimeMessage JavaDoc message = (MimeMessage JavaDoc)
150                         session.getCurrentMailbox().retrieve(msn, session.getCurrentUser() );
151                     /*MessageAttributes mattr =
152                         session.getCurrentMailbox().getMessageAttributes(msn, session.getCurrentUser() );
153                     Flags flags = new Flags();
154                     flags.setFlags(session.getCurrentMailbox().getFlags(msn, session.getCurrentUser()),
155                                     session.getCurrentUser());
156                      */

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 JavaDoc 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