KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > client > StatusClientCommand


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 package org.apache.james.imapserver.client;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedHashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.mail.Flags JavaDoc;
29 import javax.mail.MessagingException JavaDoc;
30 import javax.mail.internet.MimeMessage JavaDoc;
31
32 public class StatusClientCommand implements Command {
33
34     private String JavaDoc folder;
35
36     private LinkedHashMap JavaDoc statusMap = new LinkedHashMap JavaDoc();
37
38     private long uidNext;
39
40     private long uidValidity;
41
42     private MimeMessage JavaDoc[] msgs;
43
44     public StatusClientCommand(String JavaDoc folder, MimeMessage JavaDoc[] msgs, long uidNext,
45             long uidValidity) throws MessagingException JavaDoc {
46         this.msgs=msgs;
47         this.folder = folder;
48         this.uidNext = uidNext ;
49         this.uidValidity = uidValidity;
50     }
51
52     public String JavaDoc getCommand() {
53         String JavaDoc command = "STATUS \"" + folder + "\" ";
54         command += getStatusList(false);
55         return command;
56     }
57     
58     public List JavaDoc getExpectedResponseList() throws MessagingException JavaDoc, IOException JavaDoc {
59         String JavaDoc response="* STATUS "+folder+" ";
60         response += getStatusList(true);
61         return Arrays.asList(new String JavaDoc[] {response});
62     }
63
64     protected String JavaDoc getStatusList(boolean withValues) {
65         String JavaDoc list="(";
66         for (Iterator JavaDoc iter = statusMap.entrySet().iterator(); iter.hasNext();) {
67             Map.Entry JavaDoc entry= (Map.Entry JavaDoc) iter.next();
68             list += entry.getKey().toString();
69             if (withValues) {
70                 list += " " + entry.getValue().toString();
71             }
72             if (iter.hasNext()) {
73                 list += " ";
74             }
75         }
76         list += ")";
77         return list;
78     }
79     
80     public String JavaDoc getExpectedStatusResponse() {
81         return "OK STATUS completed.";
82     }
83
84     protected void selectStatus(String JavaDoc status, boolean add,long value) {
85         if (add) {
86             statusMap.put(status,new Long JavaDoc(value));
87         } else {
88             statusMap.remove(status);
89         }
90     }
91
92     public void setStatusMessages(boolean statusMessages) {
93         selectStatus("MESSAGES", statusMessages,msgs.length);
94     }
95
96     public void setStatusRecent(boolean statusRecent) throws MessagingException JavaDoc {
97
98         selectStatus("RECENT", statusRecent,countFlags(Flags.Flag.RECENT,true));
99     }
100     
101     protected int countFlags(Flags.Flag JavaDoc flag, boolean value) throws MessagingException JavaDoc {
102         int count=0;
103         for (int i = 0; i < msgs.length; i++) {
104             if (msgs[i].getFlags().contains(flag) == value) {
105                 count++;
106             }
107         }
108         return count;
109     }
110
111     public void setStatusUidNext(boolean statusUidNext) {
112         selectStatus("UIDNEXT", statusUidNext,uidNext);
113     }
114
115     public void setStatusUidValidity(boolean statusUidValidity) {
116         selectStatus("UIDVALIDITY", statusUidValidity,uidValidity);
117     }
118
119     public void setStatusUnseen(boolean statusUnseen) throws MessagingException JavaDoc {
120         selectStatus("UNSEEN", statusUnseen,countFlags(Flags.Flag.SEEN,false));
121     }
122
123
124
125 }
126
Popular Tags