| 1 22 23 package gnu.mail.providers.imap4; 24 25 import java.io.ByteArrayInputStream ; 26 import java.io.InputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 import java.util.Enumeration ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import javax.activation.DataHandler ; 35 import javax.mail.Flags ; 36 import javax.mail.Folder ; 37 import javax.mail.Message ; 38 import javax.mail.MessagingException ; 39 import javax.mail.internet.InternetHeaders ; 40 import javax.mail.internet.MimeMessage ; 41 42 import gnu.mail.providers.ReadOnlyMessage; 43 44 50 public class IMAPMessage extends ReadOnlyMessage implements IMAPConstants 51 { 52 53 protected static final String FETCH_HEADERS = "BODY.PEEK[HEADER]"; 54 protected static final String FETCH_CONTENT = "BODY.PEEK[]"; 55 56 protected IMAPMessage(IMAPFolder folder, InputStream in, int msgnum) 57 throws MessagingException 58 { 59 super(folder, in, msgnum); 60 flags = null; 61 } 62 63 protected IMAPMessage(IMAPFolder folder, int msgnum) 64 throws MessagingException 65 { 66 super(folder, msgnum); 67 flags = null; 68 } 69 70 73 protected void fetchFlags() 74 throws MessagingException  75 { 76 String [] commands = new String [] { FLAGS }; 77 fetch(commands); 78 } 79 80 83 protected void fetchHeaders() 84 throws MessagingException  85 { 86 String [] commands = new String [] { FETCH_HEADERS }; 87 fetch(commands); 88 } 89 90 93 protected void fetchContent() 94 throws MessagingException  95 { 96 String [] commands = new String [] { FETCH_CONTENT }; 97 fetch(commands); 98 } 99 100 103 protected void fetch(String [] commands) 104 throws MessagingException  105 { 106 try 107 { 108 IMAPStore store = (IMAPStore)folder.getStore(); 109 if (!folder.isOpen()) 111 folder.open(Folder.READ_WRITE); 112 int[] messages = new int[] { msgnum }; 113 MessageStatus[] ms = store.connection.fetch(messages, commands); 114 for (int i=0; i<ms.length; i++) 115 { 116 if (ms[i].getMessageNumber()==msgnum) 117 update(ms[i]); 118 } 119 } 120 catch (IOException e) 121 { 122 throw new MessagingException (e.getMessage(), e); 123 } 124 } 125 126 129 protected void update(MessageStatus status) 130 throws MessagingException  131 { 132 for (Iterator i = status.keySet().iterator(); i.hasNext(); ) 133 { 134 String key = (String )i.next(); 135 if (key==FLAGS) 136 { 137 List fl = (List )status.get(key); 138 flags = new Flags (); 139 for (Iterator j = fl.iterator(); j.hasNext(); ) 140 { 141 Object f = j.next(); 142 if (f==FLAG_ANSWERED) 143 flags.add(Flags.Flag.ANSWERED); 144 else if (f==FLAG_DELETED) 145 flags.add(Flags.Flag.DELETED); 146 else if (f==FLAG_DRAFT) 147 flags.add(Flags.Flag.DRAFT); 148 else if (f==FLAG_FLAGGED) 149 flags.add(Flags.Flag.FLAGGED); 150 else if (f==FLAG_RECENT) 151 flags.add(Flags.Flag.RECENT); 152 else if (f==FLAG_SEEN) 153 flags.add(Flags.Flag.SEEN); 154 else if (f instanceof String ) 155 flags.add((String )f); 156 } 157 } 158 else if (key==BODYHEADER) 159 { 160 InputStream in = new ByteArrayInputStream (status.getContent()); 161 headers = createInternetHeaders(in); 162 } 163 else if (key==BODY) 164 { 165 InputStream in = new ByteArrayInputStream (status.getContent()); 166 parse(in); 167 } 168 else 169 throw new MessagingException ("Unknown message status key: "+key); 170 } 171 172 } 173 174 176 179 public DataHandler getDataHandler() 180 throws MessagingException  181 { 182 if (content==null) 183 fetchContent(); 184 return super.getDataHandler(); 185 } 186 187 190 protected InputStream getContentStream() 191 throws MessagingException 192 { 193 if (content==null) 194 fetchContent(); 195 return super.getContentStream(); 196 } 197 198 200 203 public String [] getHeader(String name) 204 throws MessagingException 205 { 206 if (headers==null) 207 fetchHeaders(); 208 return super.getHeader(name); 209 } 210 211 214 public String getHeader(String name, String delimiter) 215 throws MessagingException 216 { 217 if (headers==null) 218 fetchHeaders(); 219 return super.getHeader(name, delimiter); 220 } 221 222 public Enumeration getAllHeaders() 223 throws MessagingException 224 { 225 if (headers==null) 226 fetchHeaders(); 227 return super.getAllHeaders(); 228 } 229 230 public Enumeration getAllHeaderLines() 231 throws MessagingException 232 { 233 if (headers==null) 234 fetchHeaders(); 235 return super.getAllHeaderLines(); 236 } 237 238 public Enumeration getMatchingHeaders(String [] names) 239 throws MessagingException 240 { 241 if (headers==null) 242 fetchHeaders(); 243 return super.getMatchingHeaders(names); 244 } 245 246 public Enumeration getMatchingHeaderLines(String [] names) 247 throws MessagingException 248 { 249 if (headers==null) 250 fetchHeaders(); 251 return super.getMatchingHeaderLines(names); 252 } 253 254 public Enumeration getNonMatchingHeaders(String [] names) 255 throws MessagingException 256 { 257 if (headers==null) 258 fetchHeaders(); 259 return super.getNonMatchingHeaders(names); 260 } 261 262 public Enumeration getNonMatchingHeaderLines(String [] names) 263 throws MessagingException 264 { 265 if (headers==null) 266 fetchHeaders(); 267 return super.getNonMatchingHeaderLines(names); 268 } 269 270 272 public Flags getFlags() 273 throws MessagingException  274 { 275 if (flags==null) 276 fetchFlags(); 277 return super.getFlags(); 278 } 279 280 public boolean isSet(Flags.Flag flag) 281 throws MessagingException  282 { 283 if (flags==null) 284 fetchFlags(); 285 return super.isSet(flag); 286 } 287 288 291 public void setFlags(Flags flag, boolean set) 292 throws MessagingException  293 { 294 if (flags==null) 295 fetchFlags(); 296 try 297 { 298 if (set) 299 flags.add(flag); 300 else 301 flags.remove(flag); 302 Flags.Flag [] sflags = flags.getSystemFlags(); 304 String [] uflags = flags.getUserFlags(); 305 List iflags = new ArrayList (sflags.length+uflags.length); 306 for (int i=0; i<sflags.length; i++) 307 { 308 Flags.Flag f = sflags[i]; 309 if (f==Flags.Flag.ANSWERED) 310 iflags.add(FLAG_ANSWERED); 311 else if (f==Flags.Flag.DELETED) 312 iflags.add(FLAG_DELETED); 313 else if (f==Flags.Flag.DRAFT) 314 iflags.add(FLAG_DRAFT); 315 else if (f==Flags.Flag.FLAGGED) 316 iflags.add(FLAG_FLAGGED); 317 else if (f==Flags.Flag.RECENT) 318 iflags.add(FLAG_RECENT); 319 else if (f==Flags.Flag.SEEN) 320 iflags.add(FLAG_SEEN); 321 } 322 iflags.addAll(Arrays.asList(uflags)); 323 String [] aflags = new String [iflags.size()]; 324 iflags.toArray(aflags); 325 IMAPStore store = (IMAPStore)folder.getStore(); 327 int[] messages = new int[] { msgnum }; 328 MessageStatus[] ms = store.connection.store(messages, FLAGS, aflags); 329 for (int i=0; i<ms.length; i++) 330 { 331 if (ms[i].getMessageNumber()==msgnum) 332 update(ms[i]); 333 } 334 } 335 catch (IOException e) 336 { 337 flags = null; throw new MessagingException (e.getMessage(), e); 339 } 340 } 341 342 344 public void writeTo(OutputStream msgStream) 345 throws IOException , MessagingException 346 { 347 if (content==null) 348 fetchContent(); 349 super.writeTo(msgStream); 350 } 351 352 public void writeTo(OutputStream msgStream, String [] ignoreList) 353 throws IOException , MessagingException 354 { 355 if (content==null) 356 fetchContent(); 357 super.writeTo(msgStream, ignoreList); 358 } 359 360 } 361 | Popular Tags |