1 21 22 27 28 package com.sun.mail.imap; 29 30 import java.io.*; 31 32 import java.util.Enumeration ; 33 import javax.mail.*; 34 import javax.mail.internet.*; 35 import javax.activation.*; 36 37 import com.sun.mail.util.*; 38 import com.sun.mail.iap.*; 39 import com.sun.mail.imap.protocol.*; 40 41 47 48 public class IMAPBodyPart extends MimeBodyPart { 49 private IMAPMessage message; 50 private BODYSTRUCTURE bs; 51 private String sectionId; 52 53 private String type; 55 private String description; 56 57 private boolean headersLoaded = false; 58 59 protected IMAPBodyPart(BODYSTRUCTURE bs, String sid, IMAPMessage message) { 60 super(); 61 this.bs = bs; 62 this.sectionId = sid; 63 this.message = message; 64 ContentType ct = new ContentType(bs.type, bs.subtype, bs.cParams); 66 type = ct.toString(); 67 } 68 69 74 protected void updateHeaders() { 75 return; 76 } 77 78 public int getSize() throws MessagingException { 79 return bs.size; 80 } 81 82 public int getLineCount() throws MessagingException { 83 return bs.lines; 84 } 85 86 public String getContentType() throws MessagingException { 87 return type; 88 } 89 90 public String getDisposition() throws MessagingException { 91 return bs.disposition; 92 } 93 94 public void setDisposition(String disposition) throws MessagingException { 95 throw new IllegalWriteException("IMAPBodyPart is read-only"); 96 } 97 98 public String getEncoding() throws MessagingException { 99 return bs.encoding; 100 } 101 102 public String getContentID() throws MessagingException { 103 return bs.id; 104 } 105 106 public String getContentMD5() throws MessagingException { 107 return bs.md5; 108 } 109 110 public void setContentMD5(String md5) throws MessagingException { 111 throw new IllegalWriteException("IMAPBodyPart is read-only"); 112 } 113 114 public String getDescription() throws MessagingException { 115 if (description != null) return description; 117 118 if (bs.description == null) 119 return null; 120 121 try { 122 description = MimeUtility.decodeText(bs.description); 123 } catch (UnsupportedEncodingException ex) { 124 description = bs.description; 125 } 126 127 return description; 128 } 129 130 public void setDescription(String description, String charset) 131 throws MessagingException { 132 throw new IllegalWriteException("IMAPBodyPart is read-only"); 133 } 134 135 public String getFileName() throws MessagingException { 136 String filename = null; 137 if (bs.dParams != null) 138 filename = bs.dParams.get("filename"); 139 if (filename == null && bs.cParams != null) 140 filename = bs.cParams.get("name"); 141 return filename; 142 } 143 144 public void setFileName(String filename) throws MessagingException { 145 throw new IllegalWriteException("IMAPBodyPart is read-only"); 146 } 147 148 protected InputStream getContentStream() throws MessagingException { 149 InputStream is = null; 150 boolean pk = message.getPeek(); 152 synchronized(message.getMessageCacheLock()) { 154 IMAPProtocol p = message.getProtocol(); 155 if (p.isREV1() && (message.getFetchBlockSize() != -1)) 156 return new IMAPInputStream(message, sectionId, bs.size, pk); 157 158 160 message.checkExpunged(); 162 163 int seqnum = message.getSequenceNumber(); 164 try { 165 BODY b; 166 if (message.getPeek()) 167 b = p.peekBody(seqnum, sectionId); 168 else 169 b = p.fetchBody(seqnum, sectionId); 170 if (b != null) 171 is = b.getByteArrayInputStream(); 172 } catch (ConnectionException cex) { 173 throw new FolderClosedException( 174 message.getFolder(), cex.getMessage()); 175 } catch (ProtocolException pex) { 176 throw new MessagingException(pex.getMessage(), pex); 177 } 178 } 179 180 if (is == null) 181 throw new MessagingException("No content"); 182 else 183 return is; 184 } 185 186 public synchronized DataHandler getDataHandler() 187 throws MessagingException { 188 if (dh == null) { 189 if (bs.isMulti()) 190 dh = new DataHandler( 191 new IMAPMultipartDataSource( 192 this, bs.bodies, sectionId, message) 193 ); 194 else if (bs.isNested() && message.getProtocol().isREV1()) 195 dh = new DataHandler( 196 new IMAPNestedMessage(message, 197 bs.bodies[0], 198 bs.envelope, 199 sectionId), 200 type 201 ); 202 } 203 204 return super.getDataHandler(); 205 } 206 207 public void setDataHandler(DataHandler content) throws MessagingException { 208 throw new IllegalWriteException("IMAPBodyPart is read-only"); 209 } 210 211 public void setContent(Object o, String type) throws MessagingException { 212 throw new IllegalWriteException("IMAPBodyPart is read-only"); 213 } 214 215 public void setContent(Multipart mp) throws MessagingException { 216 throw new IllegalWriteException("IMAPBodyPart is read-only"); 217 } 218 219 public String [] getHeader(String name) throws MessagingException { 220 loadHeaders(); 221 return super.getHeader(name); 222 } 223 224 public void setHeader(String name, String value) 225 throws MessagingException { 226 throw new IllegalWriteException("IMAPBodyPart is read-only"); 227 } 228 229 public void addHeader(String name, String value) 230 throws MessagingException { 231 throw new IllegalWriteException("IMAPBodyPart is read-only"); 232 } 233 234 public void removeHeader(String name) throws MessagingException { 235 throw new IllegalWriteException("IMAPBodyPart is read-only"); 236 } 237 238 public Enumeration getAllHeaders() throws MessagingException { 239 loadHeaders(); 240 return super.getAllHeaders(); 241 } 242 243 public Enumeration getMatchingHeaders(String [] names) 244 throws MessagingException { 245 loadHeaders(); 246 return super.getMatchingHeaders(names); 247 } 248 249 public Enumeration getNonMatchingHeaders(String [] names) 250 throws MessagingException { 251 loadHeaders(); 252 return super.getNonMatchingHeaders(names); 253 } 254 255 public void addHeaderLine(String line) throws MessagingException { 256 throw new IllegalWriteException("IMAPBodyPart is read-only"); 257 } 258 259 public Enumeration getAllHeaderLines() throws MessagingException { 260 loadHeaders(); 261 return super.getAllHeaderLines(); 262 } 263 264 public Enumeration getMatchingHeaderLines(String [] names) 265 throws MessagingException { 266 loadHeaders(); 267 return super.getMatchingHeaderLines(names); 268 } 269 270 public Enumeration getNonMatchingHeaderLines(String [] names) 271 throws MessagingException { 272 loadHeaders(); 273 return super.getNonMatchingHeaderLines(names); 274 } 275 276 private synchronized void loadHeaders() throws MessagingException { 277 if (headersLoaded) 278 return; 279 280 if (headers == null) 281 headers = new InternetHeaders(); 282 283 IMAPProtocol p = message.getProtocol(); 285 if (p.isREV1()) { 286 BODY b = null; 287 288 synchronized(message.getMessageCacheLock()) { 290 p = message.getProtocol(); 292 293 message.checkExpunged(); 295 296 int seqnum = message.getSequenceNumber(); 297 try { 298 b = p.peekBody(seqnum, sectionId + ".MIME"); 299 } catch (ConnectionException cex) { 300 throw new FolderClosedException( 301 message.getFolder(), cex.getMessage()); 302 } catch (ProtocolException pex) { 303 throw new MessagingException(pex.getMessage(), pex); 304 } 305 } 306 307 if (b == null) 308 throw new MessagingException("Failed to fetch headers"); 309 310 ByteArrayInputStream bis = b.getByteArrayInputStream(); 311 if (bis == null) 312 throw new MessagingException("Failed to fetch headers"); 313 314 headers.load(bis); 315 316 } else { 317 318 321 headers.addHeader("Content-Type", type); 323 headers.addHeader("Content-Transfer-Encoding", bs.encoding); 325 if (bs.description != null) 327 headers.addHeader("Content-Description", bs.description); 328 if (bs.id != null) 330 headers.addHeader("Content-ID", bs.id); 331 if (bs.md5 != null) 333 headers.addHeader("Content-MD5", bs.md5); 334 } 335 headersLoaded = true; 336 } 337 } 338 | Popular Tags |