KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.util.*;
23 //import org.apache.james.core.EnhancedMimeMessage;
24

25 /**
26  * Provides methods useful for IMAP command objects.
27  *
28  * References: rfc 2060, rfc 2193, rfc 2221
29  * @version 0.2 on 29 Jul 2002
30  */

31
32 public abstract class BaseCommand
33     extends BaseConnectionHandler {
34
35     //mainly to switch on stack traces and catch responses;
36
private static final boolean DEEP_DEBUG = true;
37
38     /**
39      * Turns a protocol-compliant string representing a message sequence
40      * number set into a List of integers. Use of the wildcard * (star) relies
41      * on contiguous property of msns.
42      *
43      * @param rawSet the IMAP protocol compliant string to be decoded
44      * @param exists the number of messages in this mailbox
45      * @return a List of Integers, one per message in set
46      */

47     protected List decodeSet( String JavaDoc rawSet, int exists ) throws IllegalArgumentException JavaDoc {
48         if (rawSet == null) {
49             getLogger().debug("Null argument in decodeSet");
50             throw new IllegalArgumentException JavaDoc("Null argument");
51         } else if (rawSet.equals("")) {
52             getLogger().debug("Empty argument in decodeSet");
53             throw new IllegalArgumentException JavaDoc("Empty string argument");
54         }
55         getLogger().debug(" decodeSet called for: " + rawSet);
56         System.out.println(" decodeSet called for: " + rawSet);
57         List response = new ArrayList();
58
59         int checkComma = rawSet.indexOf(",");
60         if (checkComma == -1) {
61             // No comma present
62
int checkColon = rawSet.indexOf(":");
63             if (checkColon == -1) {
64                 // No colon present (single integer)
65
Integer JavaDoc seqNum;
66                 if ( rawSet.equals( "*" ) ) {
67                     seqNum = new Integer JavaDoc( -1 );
68                 }
69                 else {
70                     seqNum = new Integer JavaDoc(rawSet.trim());
71                     if (seqNum.intValue() < 1) {
72                         throw new IllegalArgumentException JavaDoc("Not a positive integer1");
73                     }
74                 }
75                 response.add(seqNum);
76             }
77             else {
78                 // Simple sequence
79

80                 // Add the first number in the range.
81
Integer JavaDoc firstNum = new Integer JavaDoc(rawSet.substring(0, checkColon));
82                 int first = firstNum.intValue();
83                 if ( first < 1 ) {
84                     throw new IllegalArgumentException JavaDoc("Not a positive integer2");
85                 }
86                 response.add( firstNum );
87
88                 Integer JavaDoc lastNum;
89                 int last;
90                 if (rawSet.indexOf("*") != -1) {
91                     // Range from firstNum to '*'
92
// Add -1, to indicate unended range.
93
lastNum = new Integer JavaDoc( -1 );
94                 }
95                 else {
96                     // Get the final num, and add all numbers in range.
97
lastNum = new Integer JavaDoc(rawSet.substring(checkColon + 1));
98                     last = lastNum.intValue();
99                     if ( last < 1 ) {
100                         throw new IllegalArgumentException JavaDoc("Not a positive integer3");
101                     }
102                     if ( last < first ) {
103                         throw new IllegalArgumentException JavaDoc("Not an increasing range");
104                     }
105
106                     for (int i = (first + 1); i <= last; i++) {
107                         response.add(new Integer JavaDoc(i));
108                     }
109                 }
110             }
111         }
112         else {
113             // Comma present, compound range.
114
try {
115                 String JavaDoc firstRawSet = rawSet.substring( 0, checkComma );
116                 String JavaDoc secondRawSet = rawSet.substring( checkComma + 1 );
117                 response.addAll(decodeSet(firstRawSet, exists));
118                 response.addAll(decodeSet(secondRawSet, exists));
119             } catch (IllegalArgumentException JavaDoc e) {
120                 getLogger().debug("Wonky arguments in: " + rawSet + " " + e);
121                 throw e;
122             }
123         }
124         return response;
125     }
126
127     /**
128      * Turns a protocol-compliant string representing a uid set into a
129      * List of integers. Where the string requests ranges or uses the * (star)
130      * wildcard, the results are uids that exist in the mailbox. This
131      * minimizes attempts to refer to non-existent messages.
132      *
133      * @param rawSet the IMAP protocol compliant string to be decoded
134      * @param uidsList List of uids of messages in mailbox
135      * @return a List of Integers, one per message in set
136      */

137     protected List decodeUIDSet( String JavaDoc rawSet, List uidsList )
138         throws IllegalArgumentException JavaDoc {
139         if (rawSet == null) {
140             getLogger().debug("Null argument in decodeSet");
141             throw new IllegalArgumentException JavaDoc("Null argument");
142         } else if (rawSet.equals("")) {
143             getLogger().debug("Empty argument in decodeSet");
144             throw new IllegalArgumentException JavaDoc("Empty string argument");
145         }
146         getLogger().debug(" decodeUIDSet called for: " + rawSet);
147         System.out.println(" decodeUIDSet called for: " + rawSet);
148         Iterator it = uidsList.iterator();
149         while (it.hasNext()) {
150             System.out.println ("uids present : " + (Integer JavaDoc)it.next() );
151         }
152         List response = new ArrayList();
153         int checkComma = rawSet.indexOf(",");
154         if (checkComma == -1) {
155             int checkColon = rawSet.indexOf(":");
156             if (checkColon == -1) {
157                 Integer JavaDoc seqNum = new Integer JavaDoc(rawSet.trim());
158                 if (seqNum.intValue() < 1) {
159                     throw new IllegalArgumentException JavaDoc("Not a positive integer4");
160                 } else {
161                     response.add(seqNum);
162                 }
163             } else {
164                 Integer JavaDoc firstNum = new Integer JavaDoc(rawSet.substring(0, checkColon));
165                 int first = firstNum.intValue();
166
167                 Integer JavaDoc lastNum;
168                 if (rawSet.indexOf("*") == -1) {
169                     lastNum = new Integer JavaDoc(rawSet.substring(checkColon + 1));
170                 } else {
171                     lastNum = (Integer JavaDoc)uidsList.get(uidsList.size()-1);
172                 }
173                 int last;
174                 
175                 last = lastNum.intValue();
176                 if (first < 1 || last < 1) {
177                     throw new IllegalArgumentException JavaDoc("Not a positive integer");
178                 } else if (first < last) {
179                     Collection uids;
180                     if(uidsList.size() > 50) {
181                         uids = new HashSet(uidsList);
182                     } else {
183                         uids = uidsList;
184                     }
185                     Iterator ite = uids.iterator();
186                     while (ite.hasNext()) {
187                         int uidsint = ((Integer JavaDoc) ite.next()).intValue();
188                         System.out.println("SCHLEIFEN f "+first+" l "+last+" uidsint "+uidsint);
189                         
190                         if (uidsint >= first && uidsint <= last) {
191                             response.add(new Integer JavaDoc(uidsint));
192                         }
193                     }
194                 } else if (first == last) {
195                     response.add(firstNum);
196                 } else {
197                     // Requests as 5:* are requested from Clients like Outlook to check, if there
198
// are new Mails incoming since the last request.
199
// So here no response (NULL List) and no error throwing
200
System.out.println("NULLLIST");
201                 }
202             }
203         } else {
204             try {
205                 String JavaDoc firstRawSet = rawSet.substring(0, checkComma);
206                 String JavaDoc secondRawSet = rawSet.substring(checkComma + 1);
207                 response.addAll(decodeUIDSet(firstRawSet, uidsList));
208                 response.addAll(decodeUIDSet(secondRawSet, uidsList));
209             } catch (IllegalArgumentException JavaDoc e) {
210                 getLogger().debug("Wonky arguments in: " + rawSet + " " + e);
211                 throw e;
212             }
213         }
214         System.out.println("RETURNING");
215         return response;
216     }
217     
218     protected ACLMailbox getMailbox( ImapSession session, String JavaDoc mailboxName, String JavaDoc command )
219     {
220         if ( session.getState() == ImapSessionState.SELECTED && session.getCurrentFolder().equals( mailboxName ) ) {
221             return session.getCurrentMailbox();
222         }
223         else {
224             try {
225                 return session.getImapHost().getMailbox( session.getCurrentUser(), mailboxName );
226             }
227             catch ( MailboxException me ) {
228                 if ( me.isRemote() ) {
229                     session.noResponse( "[REFERRAL " + me.getRemoteServer() + "]" + "Remote mailbox" );
230                 }
231                 else {
232                     session.noResponse( command, "Unknown mailbox" );
233                     getLogger().info( "MailboxException in method getBox for user: "
234                                       + session.getCurrentUser() + " mailboxName: " + mailboxName + " was "
235                                       + me.getMessage() );
236                 }
237                 return null;
238             }
239             catch ( AccessControlException e ) {
240                 session.noResponse( command, "Unknown mailbox" );
241                 return null;
242             }
243         }
244     }
245 }
246
Popular Tags