1 19 20 package org.apache.james.imapserver.client; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 28 import javax.mail.Flags ; 29 import javax.mail.MessagingException ; 30 import javax.mail.Flags.Flag; 31 import javax.mail.internet.MimeMessage ; 32 33 import org.apache.james.imapserver.client.fetch.FetchBody; 34 35 public class FetchCommand extends AbstractCommand { 36 37 final long from; 38 39 final long to; 40 41 boolean uid; 42 43 private MimeMessage [] msgs; 44 45 private long[] uids; 46 47 private boolean fetchFlags; 48 49 private boolean fetchRfc822Size; 50 51 private FetchBody body; 52 53 private boolean useParenthesis = true; 54 55 private boolean oneSeqNumberOnly = false; 56 57 public FetchCommand(MimeMessage [] msgs, long from, long to) { 58 statusResponse = "OK FETCH completed."; 59 this.msgs = msgs; 60 this.from = from; 61 this.to = to; 62 } 63 64 public FetchCommand(MimeMessage [] msgs, long no) { 65 statusResponse = "OK FETCH completed."; 66 this.msgs = msgs; 67 this.from = no; 68 this.to = no; 69 this.oneSeqNumberOnly = true; 70 } 71 72 public void setUseParenthesis(boolean useParenthesis) { 73 this.useParenthesis = useParenthesis; 74 } 75 76 public void setUids(long[] uids) { 77 this.uids=uids; 78 this.uid=true; 79 } 80 81 public String getCommand() { 82 String command = ""; 83 if (uid) { 84 command += "UID "; 85 } 86 command += "fetch " + from; 87 if (!oneSeqNumberOnly) { 88 if (to > 0) { 89 command += ":" + to; 90 } else { 91 command += ":*"; 92 } 93 } 94 95 command += " "; 96 if (useParenthesis) { 97 command += "("; 98 } 99 100 String items = ""; 101 if (fetchFlags) { 103 items += " FLAGS"; 104 } 105 if (fetchRfc822Size) { 107 items += " RFC822.SIZE"; 108 } 109 if (body != null) { 111 items += " " + body.getCommand(); 112 } 113 114 if (items.length() > 0) { 115 items = items.substring(1); 116 } 117 command += items; 118 if (useParenthesis) { 119 command += ")"; 120 } 121 command += "\n"; 122 return command; 123 } 124 125 private List getSelectedMessageNumbers() { 126 List selectedNumbers = new ArrayList (); 127 if (uid) { 128 final long to; 129 if (this.to>0) { 130 to=this.to; 131 } else { 132 to=Long.MAX_VALUE; 133 } 134 for (int i=0; i< msgs.length; i++) { 135 if (uids[i]>=from && uids[i]<=to) { 136 selectedNumbers.add(new Integer ((int)i+1)); 137 } 138 } 139 140 } else { 141 final long from; 142 if (this.from > 0) { 143 from = this.from; 144 } else { 145 from = 1; 146 } 147 148 final long to; 149 if (this.to > 0) { 150 if (this.to > msgs.length) { 151 to = msgs.length; 152 } else { 153 to = this.to; 154 } 155 } else { 156 to = msgs.length; 157 } 158 159 for (long i = from; i <= to; i++) { 160 selectedNumbers.add(new Integer ((int)i)); 161 } 162 } 163 164 return selectedNumbers; 165 } 166 public String getResultForMessageNumber(int no) throws MessagingException , IOException { 167 final MimeMessage mm = msgs[no-1]; 168 String result = ""; 169 170 if (fetchFlags) { 172 result +=" FLAGS ("+flagsToString(mm.getFlags())+")"; 173 } 174 175 if (uid) { 177 final long uid=uids[no-1]; 178 result += " UID "+uid; 179 } 180 181 if (fetchRfc822Size) { 183 final int size=mm.getSize(); 184 result += " RFC822.SIZE "+size; 185 } 186 187 if (body!=null) { 189 result += " "+body.getResult(mm); 190 } 191 192 if (result.length()>0) { 193 result=result.substring(1); 195 } 196 return result; 197 } 198 199 public List getExpectedResponseList() throws MessagingException , IOException { 200 List responseList = new LinkedList (); 201 202 List selectedNumbers = getSelectedMessageNumbers(); 203 204 for (Iterator it = selectedNumbers.iterator(); it.hasNext();) { 205 final int no=((Integer )it.next()).intValue(); 206 String line = "* " + no + " FETCH ("; 207 line += getResultForMessageNumber(no); 208 line += ")"; 209 responseList.add(line); 210 } 211 return responseList; 212 213 } 214 215 public static String flagToString(Flag flag) { 216 if (flag.equals(Flag.ANSWERED)) { 217 return "\\Answered"; 218 } 219 if (flag.equals(Flag.DELETED)) { 220 return "\\Deleted"; 221 } 222 if (flag.equals(Flag.DRAFT)) { 223 return "\\Draft"; 224 } 225 if (flag.equals(Flag.FLAGGED)) { 226 return "\\Flagged"; 227 } 228 if (flag.equals(Flag.RECENT)) { 229 return "\\Recent"; 230 } 231 if (flag.equals(Flag.SEEN)) { 232 return "\\Seen"; 233 } 234 throw new IllegalArgumentException ("unknown Flag: "+flag); 235 236 } 237 238 public static String flagsToString(Flags flags) { 239 String result=""; 240 Flag[] f=flags.getSystemFlags(); 241 for (int i = 0; i < f.length; i++) { 242 result +=" "+flagToString(f[i]); 243 } 244 if (result.length()>0) { 245 result=result.substring(1); 247 } 248 return result; 249 } 250 251 public void setFetchFlags(boolean fetchFlags) { 252 this.fetchFlags=fetchFlags; 253 254 } 255 256 public void setFetchRfc822Size(boolean fetchRfc822Size) { 257 this.fetchRfc822Size=fetchRfc822Size; 258 259 } 260 261 262 public void setFetchBody(FetchBody body) { 263 this.body=body; 264 265 } 266 267 } 268 | Popular Tags |