KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > CommandStore


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;
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 JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 //import org.apache.james.core.EnhancedMimeMessage;
29

30 /**
31  * Implements the IMAP STORE command for a given ImapRequestImpl.
32  *
33  * References: rfc 2060, rfc 2193, rfc 2221
34  * @version 0.2 on 04 Aug 2002
35  */

36 public class CommandStore
37     extends BaseCommand implements ImapCommand
38 {
39     //mainly to switch on stack traces and catch responses;
40
private static final boolean DEEP_DEBUG = true;
41
42     private static final String JavaDoc OK = "OK";
43     private static final String JavaDoc NO = "NO";
44     private static final String JavaDoc BAD = "BAD";
45     private static final String JavaDoc UNTAGGED = "*";
46     private static final String JavaDoc SP = " ";
47
48     private StringTokenizer JavaDoc commandLine;
49     private boolean useUIDs;
50     private ACLMailbox currentMailbox;
51     private String JavaDoc commandRaw;
52     private PrintWriter JavaDoc out;
53     private OutputStream JavaDoc outs;
54     private String JavaDoc tag;
55     private String JavaDoc user;
56     private SingleThreadedConnectionHandler caller;
57     private String JavaDoc 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     /**
77      * Debugging method - will probably disappear
78      */

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     /**
94      * Implements IMAP store commands given an ImapRequestImpl.
95      * <p>Warning - maybecome service(ImapRequestImpl request)
96      */

97     public void service() {
98         List JavaDoc 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 JavaDoc buf = new StringBuffer JavaDoc();
107         while (commandLine.hasMoreTokens()) {
108             buf.append(commandLine.nextToken());
109         }
110         String JavaDoc request = buf.toString();
111         try {
112             for (int i = 0; i < set.size(); i++) {
113                 if (useUIDs) {
114                     Integer JavaDoc uidObject = (Integer JavaDoc)set.get(i);
115                     int uid = uidObject.intValue();
116                     if (currentMailbox.setFlagsUID(uid, user, request)) {
117                         if (request.toUpperCase().indexOf("SILENT") == -1) {
118                             String JavaDoc newflags
119                                 = currentMailbox.getFlagsUID(uid, user);
120                             out.println(UNTAGGED + SP + SP
121                                         + "FETCH (FLAGS " + newflags
122                                         + " UID " + uid + ")");
123                         } else {
124                                 //silent
125
}
126                     } else {
127                         //failed
128
out.println(tag + SP + NO + SP
129                                     + "Unable to store flags for message: "
130                                     + uid);
131                     }
132                 } else {
133                     int msn = ((Integer JavaDoc)set.get(i)).intValue();
134                     if (currentMailbox.setFlags(msn, user, request)) {
135                         if (request.toUpperCase().indexOf("SILENT") == -1) {
136                             String JavaDoc newflags
137                                 = currentMailbox.getFlags(msn, user);
138                             out.println(UNTAGGED + SP + msn + SP
139                                         + "FETCH (FLAGS " + newflags + ")");
140                         } else {
141                                 //silent
142
}
143                     } else {
144                         //failed
145
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 JavaDoc 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