KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.MimeMessageInputStreamSource;
21 import org.apache.james.core.MimeMessageWrapper;
22 import org.apache.james.imapserver.AccessControlException;
23 import org.apache.james.imapserver.ACLMailbox;
24 import org.apache.james.imapserver.FileMailbox;
25 import org.apache.james.imapserver.ImapRequest;
26 import org.apache.james.imapserver.ImapSession;
27 import org.apache.james.imapserver.ImapSessionState;
28 import org.apache.james.imapserver.Flags;
29 import org.apache.james.imapserver.AuthorizationException;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.io.BufferedReader JavaDoc;
37 import java.io.ByteArrayInputStream JavaDoc;
38 import java.io.ByteArrayOutputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import javax.mail.internet.MimeMessage JavaDoc;
41 import javax.mail.MessagingException JavaDoc;
42 import com.sun.mail.iap.Literal;
43
44 /**
45  * Appends new Mails to the IMAP Store.
46  *
47  * @version 0.3 on 08 Aug 2002
48  */

49 class AppendCommand extends AuthenticatedSelectedStateCommand {
50     public AppendCommand(){
51         System.out.println("APPEND STARTED");
52         this.commandName = "APPEND";
53         this.getArgs().add( new AstringArgument( "mailbox" ) );
54         this.getArgs().add( new AstringArgument( "flags" ) );
55         this.getArgs().add( new AstringArgument( "date" ) );
56         this.getArgs().add( new AstringArgument( "message" ) );
57     }
58     
59     public boolean process( ImapRequest request, ImapSession session ) {
60         StringTokenizer JavaDoc tokens = request.getCommandLine();
61         List JavaDoc argValues = new ArrayList JavaDoc();
62
63         boolean fetchNextTogether = false;
64         String JavaDoc tokensave = "";
65         while(tokens.hasMoreTokens()) {
66             String JavaDoc token = tokens.nextToken();
67             System.out.println("FOUND TOKEN: "+token);
68             if (token.startsWith("(") || token.startsWith("\"")) {
69                 // Fetch token
70
token = token.substring(1);
71                 if (token.endsWith(")") || token.endsWith("\"")) {
72                     token = token.substring(0,token.length()-1);
73                     argValues.add(token);
74                     System.out.println("ADDED1 TOKEN: "+token);
75                 }else{
76                     fetchNextTogether = true;
77                     tokensave = tokensave+token;
78                 }
79             }else if(token.endsWith(")") || token.endsWith("\"")) {
80                 token = token.substring(0,token.length()-1);
81                 argValues.add(tokensave+" "+token);
82                 System.out.println("ADDED2 TOKEN: "+tokensave+" "+token);
83                 tokensave = "";
84                 fetchNextTogether = false;
85             }else if(fetchNextTogether) {
86                 tokensave = tokensave+" "+token;
87             }else{
88                 argValues.add(token);
89                 System.out.println("ADDED3 TOKEN: "+token);
90             }
91         }
92         return doProcess( request, session, argValues );
93     }
94     
95     protected boolean doProcess( ImapRequest request, ImapSession session, List JavaDoc argValues ) {
96         String JavaDoc command = this.getCommand();
97         ByteArrayOutputStream JavaDoc byteout = new ByteArrayOutputStream JavaDoc();
98         java.io.PrintWriter JavaDoc out = session.getOut();
99         BufferedReader JavaDoc bre = session.getIn();
100         
101         String JavaDoc folder = (String JavaDoc) argValues.get( 0 );
102         String JavaDoc ms = "";
103         String JavaDoc flags = "";
104         String JavaDoc date = "";
105         
106         String JavaDoc arg1 = "";
107         String JavaDoc arg2 = "";
108         String JavaDoc arg3 = "";
109         try {
110             arg1 = (String JavaDoc) argValues.get(1);
111             arg2 = (String JavaDoc) argValues.get(2);
112             arg3 = (String JavaDoc) argValues.get(3);
113         }catch(Exception JavaDoc e) {}
114         
115         if (arg1.startsWith("{")) {
116             // No Argues
117
ms = arg1;
118         }else if (arg1.startsWith("\\")) {
119             // arg1 = flags
120
flags = arg1;
121             if (arg3.startsWith("{")) {
122                 date = arg2;
123                 ms = arg3;
124             }else{
125                 ms = arg2;
126             }
127         }else if (arg2.startsWith("\\")) {
128             // arg2 = flags
129
flags = arg2;
130             ms = arg3;
131         }
132         System.out.println("APPEND: ms is "+ms);
133         System.out.println("APPEND: flags is "+flags);
134         System.out.println("APPEND: date is "+date);
135         
136         try{
137             long messagelen = Long.parseLong(ms.substring(1,ms.length()-2));
138             long messageleft = messagelen;
139            
140             session.setCanParseCommand(false);
141             out.println("+ go ahead");
142
143             while(messageleft > 0) {
144                 int buffer = 200;
145                 if (messageleft<200) buffer = (int) messageleft;
146                 char[] cbuf = new char[buffer] ;
147                 bre.read(cbuf);
148                 byteout.write(String.valueOf(cbuf).getBytes());
149                 messageleft = messageleft - buffer;
150             }
151             session.setCanParseCommand(true);
152         }catch(Exception JavaDoc e){
153             System.out.println("Error occured parsing input");
154             e.printStackTrace();
155         }
156         
157         
158         FileMailbox mailbox = (FileMailbox)getMailbox( session, folder, command );
159         
160         if ( mailbox == null ) {
161             session.noResponse( command, "Must specify a mailbox." );
162             return true;
163         } else {
164             session.setCurrentMailbox( mailbox );
165         }
166         try { // long tries clause against an AccessControlException
167
if ( !session.getCurrentMailbox().hasInsertRights( session.getCurrentUser() ) ) {
168                 session.noResponse( command, "Insert access not granted." );
169                 return true;
170             }
171             
172             MimeMessageInputStreamSource source;
173             try {
174                 source =
175                     new MimeMessageInputStreamSource("Mail" + System.currentTimeMillis() + "-" + mailbox.getNextUID(),
176                                                      new ByteArrayInputStream JavaDoc(byteout.toByteArray()));
177             } catch (MessagingException JavaDoc me) {
178                 me.printStackTrace();
179                 return false;
180             }
181             MimeMessageWrapper msg = new MimeMessageWrapper(source);
182
183             try{
184                 msg.setHeader("Received", date);
185                 msg.saveChanges();
186             }catch (MessagingException JavaDoc me){
187                 me.printStackTrace();
188             }
189             mailbox.store( (MimeMessage JavaDoc)msg, session.getCurrentUser() );
190             
191             session.okResponse( "append completed" );
192             return true;
193         } catch ( AuthorizationException aze ) {
194             System.out.println("AUTHERROR");
195             session.noResponse( command, "append error: can't append to that mailbox, error in flags or date/time or message text." );
196             session.logAZE( aze );
197             return true;
198         } catch ( AccessControlException ace ) {
199             System.out.println("ACCESSERROR");
200             session.noResponse( command, "[TRYCREATE " + folder + "] No such mailbox." );
201             session.logACE( ace );
202             return true;
203         }
204     }
205 }
206
207
Popular Tags