1 19 package org.apache.james.imapserver.client; 20 21 import java.io.IOException ; 22 import java.util.Arrays ; 23 import java.util.Iterator ; 24 import java.util.LinkedHashMap ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import javax.mail.Flags ; 29 import javax.mail.MessagingException ; 30 import javax.mail.internet.MimeMessage ; 31 32 public class StatusClientCommand implements Command { 33 34 private String folder; 35 36 private LinkedHashMap statusMap = new LinkedHashMap (); 37 38 private long uidNext; 39 40 private long uidValidity; 41 42 private MimeMessage [] msgs; 43 44 public StatusClientCommand(String folder, MimeMessage [] msgs, long uidNext, 45 long uidValidity) throws MessagingException { 46 this.msgs=msgs; 47 this.folder = folder; 48 this.uidNext = uidNext ; 49 this.uidValidity = uidValidity; 50 } 51 52 public String getCommand() { 53 String command = "STATUS \"" + folder + "\" "; 54 command += getStatusList(false); 55 return command; 56 } 57 58 public List getExpectedResponseList() throws MessagingException , IOException { 59 String response="* STATUS "+folder+" "; 60 response += getStatusList(true); 61 return Arrays.asList(new String [] {response}); 62 } 63 64 protected String getStatusList(boolean withValues) { 65 String list="("; 66 for (Iterator iter = statusMap.entrySet().iterator(); iter.hasNext();) { 67 Map.Entry entry= (Map.Entry ) 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 getExpectedStatusResponse() { 81 return "OK STATUS completed."; 82 } 83 84 protected void selectStatus(String status, boolean add,long value) { 85 if (add) { 86 statusMap.put(status,new Long (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 { 97 98 selectStatus("RECENT", statusRecent,countFlags(Flags.Flag.RECENT,true)); 99 } 100 101 protected int countFlags(Flags.Flag flag, boolean value) throws MessagingException { 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 { 120 selectStatus("UNSEEN", statusUnseen,countFlags(Flags.Flag.SEEN,false)); 121 } 122 123 124 125 } 126 | Popular Tags |