src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/MIMEPart.java

changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
equal deleted inserted replaced
366:8c0b6bccfe47 368:0989ad8c0860
1 /* 1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
27 27
28 import java.io.File; 28 import java.io.File;
29 import java.io.InputStream; 29 import java.io.InputStream;
30 import java.nio.ByteBuffer; 30 import java.nio.ByteBuffer;
31 import java.util.List; 31 import java.util.List;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
32 34
33 /** 35 /**
34 * Represents an attachment part in a MIME message. MIME message parsing is done 36 * Represents an attachment part in a MIME message. MIME message parsing is done
35 * lazily using a pull parser, so the part may not have all the data. {@link #read} 37 * lazily using a pull parser, so the part may not have all the data. {@link #read}
36 * and {@link #readOnce} may trigger the actual parsing the message. In fact, 38 * and {@link #readOnce} may trigger the actual parsing the message. In fact,
37 * parsing of an attachment part may be triggered by calling {@link #read} methods 39 * parsing of an attachment part may be triggered by calling {@link #read} methods
38 * on some other attachemnt parts. All this happens behind the scenes so the 40 * on some other attachment parts. All this happens behind the scenes so the
39 * application developer need not worry about these details. 41 * application developer need not worry about these details.
40 * 42 *
41 * @author Jitendra Kotamraju 43 * @author Jitendra Kotamraju, Martin Grebac
42 */ 44 */
43 public class MIMEPart { 45 public class MIMEPart {
46
47 private static final Logger LOGGER = Logger.getLogger(MIMEPart.class.getName());
44 48
45 private volatile InternetHeaders headers; 49 private volatile InternetHeaders headers;
46 private volatile String contentId; 50 private volatile String contentId;
47 private String contentType; 51 private String contentType;
52 private String contentTransferEncoding;
53
48 volatile boolean parsed; // part is parsed or not 54 volatile boolean parsed; // part is parsed or not
49 final MIMEMessage msg; 55 final MIMEMessage msg;
50 private final DataHead dataHead; 56 private final DataHead dataHead;
51 57
52 MIMEPart(MIMEMessage msg) { 58 MIMEPart(MIMEMessage msg) {
67 * into a object that returns InputStream for e.g DataHandler) 73 * into a object that returns InputStream for e.g DataHandler)
68 * 74 *
69 * @return data for the part's content 75 * @return data for the part's content
70 */ 76 */
71 public InputStream read() { 77 public InputStream read() {
72 return dataHead.read(); 78 InputStream is = null;
79 try {
80 is = MimeUtility.decode(dataHead.read(), contentTransferEncoding);
81 } catch (DecodingException ex) { //ignore
82 if (LOGGER.isLoggable(Level.WARNING)) {
83 LOGGER.log(Level.WARNING, null, ex);
84 }
85 }
86 return is;
73 } 87 }
74 88
75 /** 89 /**
76 * Cleans up any resources that are held by this part (for e.g. deletes 90 * Cleans up any resources that are held by this part (for e.g. deletes
77 * the temp file that is used to serve this part's content). After 91 * the temp file that is used to serve this part's content). After
78 * calling this, one shouldn't call {@link #read()} or {@link #readOnce()} 92 * calling this, one shouldn't call {@link #read()} or {@link #readOnce()}
79 */ 93 */
80 public void close() { 94 public void close() {
81 dataHead.close(); 95 dataHead.close();
82 } 96 }
83
84 97
85 /** 98 /**
86 * Can get the attachment part's content only once. The content 99 * Can get the attachment part's content only once. The content
87 * will be lost after the method. Content data is not be stored 100 * will be lost after the method. Content data is not be stored
88 * on the file system or is not kept in the memory for the 101 * on the file system or is not kept in the memory for the
93 * once. 106 * once.
94 * 107 *
95 * @return data for the part's content 108 * @return data for the part's content
96 */ 109 */
97 public InputStream readOnce() { 110 public InputStream readOnce() {
98 return dataHead.readOnce(); 111 InputStream is = null;
112 try {
113 is = MimeUtility.decode(dataHead.readOnce(), contentTransferEncoding);
114 } catch (DecodingException ex) { //ignore
115 if (LOGGER.isLoggable(Level.WARNING)) {
116 LOGGER.log(Level.WARNING, null, ex);
117 }
118 }
119 return is;
99 } 120 }
100 121
101 public void moveTo(File f) { 122 public void moveTo(File f) {
102 dataHead.moveTo(f); 123 dataHead.moveTo(f);
103 } 124 }
110 public String getContentId() { 131 public String getContentId() {
111 if (contentId == null) { 132 if (contentId == null) {
112 getHeaders(); 133 getHeaders();
113 } 134 }
114 return contentId; 135 return contentId;
136 }
137
138 /**
139 * Returns Content-Transfer-Encoding MIME header for this attachment part
140 *
141 * @return Content-Transfer-Encoding of the part
142 */
143 public String getContentTransferEncoding() {
144 if (contentTransferEncoding == null) {
145 getHeaders();
146 }
147 return contentTransferEncoding;
115 } 148 }
116 149
117 /** 150 /**
118 * Returns Content-Type MIME header for this attachment part 151 * Returns Content-Type MIME header for this attachment part
119 * 152 *
169 */ 202 */
170 void setHeaders(InternetHeaders headers) { 203 void setHeaders(InternetHeaders headers) {
171 this.headers = headers; 204 this.headers = headers;
172 List<String> ct = getHeader("Content-Type"); 205 List<String> ct = getHeader("Content-Type");
173 this.contentType = (ct == null) ? "application/octet-stream" : ct.get(0); 206 this.contentType = (ct == null) ? "application/octet-stream" : ct.get(0);
207 List<String> cte = getHeader("Content-Transfer-Encoding");
208 this.contentTransferEncoding = (cte == null) ? "binary" : cte.get(0);
174 } 209 }
175 210
176 /** 211 /**
177 * Callback to notify that there is a partial content for the part 212 * Callback to notify that there is a partial content for the part
178 * 213 *
197 */ 232 */
198 void setContentId(String cid) { 233 void setContentId(String cid) {
199 this.contentId = cid; 234 this.contentId = cid;
200 } 235 }
201 236
237 /**
238 * Callback to set Content-Transfer-Encoding for this part
239 * @param cte Content-Transfer-Encoding of the part
240 */
241 void setContentTransferEncoding(String cte) {
242 this.contentTransferEncoding = cte;
243 }
244
202 @Override 245 @Override
203 public String toString() { 246 public String toString() {
204 return "Part="+contentId; 247 return "Part="+contentId+":"+contentTransferEncoding;
205 } 248 }
206 249
207 } 250 }

mercurial