KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > util > UnsolicitedResponseGenerator


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20 package org.apache.james.imapserver.util;
21
22 import java.util.HashSet JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import javax.mail.MessagingException JavaDoc;
26 import javax.mail.Flags.Flag;
27 import javax.mail.internet.MimeMessage JavaDoc;
28
29 public class UnsolicitedResponseGenerator
30 {
31     Set JavaDoc responseSet = new HashSet JavaDoc();
32     int recent=0;
33     
34     public void addByMessages(MimeMessage JavaDoc[] msgs) throws MessagingException JavaDoc {
35         addExists(msgs.length);
36         int recent=0;
37         int firstUnseen=0;
38         for (int i=0; i<msgs.length; i++) {
39             if (msgs[i].isSet(Flag.RECENT)) {
40                 recent++;
41             }
42             if (firstUnseen==0 && !msgs[i].isSet(Flag.SEEN)) {
43                 firstUnseen=i+1;
44             }
45             
46         }
47         addRecent(recent);
48         addFlags();
49         addFirstUnseen(firstUnseen);
50         addPermanentFlags();
51     }
52
53     public void addExists(int i)
54     {
55         responseSet.add("* " + i + " EXISTS");
56     }
57
58     public void addRecent(int i)
59     {
60         recent=i;
61         responseSet.add("* " + i + " RECENT");
62     }
63
64     public void addFlags()
65     {
66         responseSet
67                 .add("* FLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen)");
68     }
69
70     public void addUidValidity(long uidv)
71     {
72         responseSet.add("* OK [UIDVALIDITY " + uidv + "]");
73     }
74
75     public void addFirstUnseen(int firstUnseen)
76     {
77         if (firstUnseen > 0) {
78             responseSet.add("* OK [UNSEEN "+firstUnseen+"] Message "+firstUnseen+" is the first unseen");
79         } else {
80             responseSet.add("* OK No messages unseen");
81         }
82     }
83
84     public void addPermanentFlags()
85     {
86         responseSet
87                 .add("* OK [PERMANENTFLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen)]");
88     }
89
90     public Set JavaDoc getResponseSet()
91     {
92
93         return responseSet;
94     }
95
96     public int getRecent() {
97
98         return recent;
99     }
100
101 }
102
Popular Tags