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 |
29 import java.io.InputStream; |
29 import java.io.InputStream; |
30 import java.io.UnsupportedEncodingException; |
30 import java.io.UnsupportedEncodingException; |
31 import java.net.URLDecoder; |
31 import java.net.URLDecoder; |
32 import java.nio.ByteBuffer; |
32 import java.nio.ByteBuffer; |
33 import java.util.*; |
33 import java.util.*; |
|
34 import java.util.logging.Level; |
34 import java.util.logging.Logger; |
35 import java.util.logging.Logger; |
35 |
36 |
36 /** |
37 /** |
37 * Represents MIME message. MIME message parsing is done lazily using a |
38 * Represents MIME message. MIME message parsing is done lazily using a |
38 * pull parser. |
39 * pull parser. |
102 * |
103 * |
103 * @param index sequential order of the part. starts with zero. |
104 * @param index sequential order of the part. starts with zero. |
104 * @return attachemnt part |
105 * @return attachemnt part |
105 */ |
106 */ |
106 public MIMEPart getPart(int index) { |
107 public MIMEPart getPart(int index) { |
107 LOGGER.fine("index="+index); |
108 LOGGER.log(Level.FINE, "index={0}", index); |
108 MIMEPart part = (index < partsList.size()) ? partsList.get(index) : null; |
109 MIMEPart part = (index < partsList.size()) ? partsList.get(index) : null; |
109 if (parsed && part == null) { |
110 if (parsed && part == null) { |
110 throw new MIMEParsingException("There is no "+index+" attachment part "); |
111 throw new MIMEParsingException("There is no "+index+" attachment part "); |
111 } |
112 } |
112 if (part == null) { |
113 if (part == null) { |
113 // Parsing will done lazily and will be driven by reading the part |
114 // Parsing will done lazily and will be driven by reading the part |
114 part = new MIMEPart(this); |
115 part = new MIMEPart(this); |
115 partsList.add(index, part); |
116 partsList.add(index, part); |
116 } |
117 } |
117 LOGGER.fine("Got attachment at index="+index+" attachment="+part); |
118 LOGGER.log(Level.FINE, "Got attachment at index={0} attachment={1}", new Object[]{index, part}); |
118 return part; |
119 return part; |
119 } |
120 } |
120 |
121 |
121 /** |
122 /** |
122 * Creates a lazy attachment for a given Content-ID. It doesn't validate |
123 * Creates a lazy attachment for a given Content-ID. It doesn't validate |
126 * |
127 * |
127 * @param contentId Content-ID of the part, expects Content-ID without <, > |
128 * @param contentId Content-ID of the part, expects Content-ID without <, > |
128 * @return attachemnt part |
129 * @return attachemnt part |
129 */ |
130 */ |
130 public MIMEPart getPart(String contentId) { |
131 public MIMEPart getPart(String contentId) { |
131 LOGGER.fine("Content-ID="+contentId); |
132 LOGGER.log(Level.FINE, "Content-ID={0}", contentId); |
132 MIMEPart part = getDecodedCidPart(contentId); |
133 MIMEPart part = getDecodedCidPart(contentId); |
133 if (parsed && part == null) { |
134 if (parsed && part == null) { |
134 throw new MIMEParsingException("There is no attachment part with Content-ID = "+contentId); |
135 throw new MIMEParsingException("There is no attachment part with Content-ID = "+contentId); |
135 } |
136 } |
136 if (part == null) { |
137 if (part == null) { |
137 // Parsing is done lazily and is driven by reading the part |
138 // Parsing is done lazily and is driven by reading the part |
138 part = new MIMEPart(this, contentId); |
139 part = new MIMEPart(this, contentId); |
139 partsMap.put(contentId, part); |
140 partsMap.put(contentId, part); |
140 } |
141 } |
141 LOGGER.fine("Got attachment for Content-ID="+contentId+" attachment="+part); |
142 LOGGER.log(Level.FINE, "Got attachment for Content-ID={0} attachment={1}", new Object[]{contentId, part}); |
142 return part; |
143 return part; |
143 } |
144 } |
144 |
145 |
145 // this is required for Indigo interop, it writes content-id without escaping |
146 // this is required for Indigo interop, it writes content-id without escaping |
146 private MIMEPart getDecodedCidPart(String cid) { |
147 private MIMEPart getDecodedCidPart(String cid) { |
182 |
183 |
183 MIMEEvent event = it.next(); |
184 MIMEEvent event = it.next(); |
184 |
185 |
185 switch(event.getEventType()) { |
186 switch(event.getEventType()) { |
186 case START_MESSAGE : |
187 case START_MESSAGE : |
187 LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.START_MESSAGE); |
188 LOGGER.log(Level.FINE, "MIMEEvent={0}", MIMEEvent.EVENT_TYPE.START_MESSAGE); |
188 break; |
189 break; |
189 |
190 |
190 case START_PART : |
191 case START_PART : |
191 LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.START_PART); |
192 LOGGER.log(Level.FINE, "MIMEEvent={0}", MIMEEvent.EVENT_TYPE.START_PART); |
192 break; |
193 break; |
193 |
194 |
194 case HEADERS : |
195 case HEADERS : |
195 LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.HEADERS); |
196 LOGGER.log(Level.FINE, "MIMEEvent={0}", MIMEEvent.EVENT_TYPE.HEADERS); |
196 MIMEEvent.Headers headers = (MIMEEvent.Headers)event; |
197 MIMEEvent.Headers headers = (MIMEEvent.Headers)event; |
197 InternetHeaders ih = headers.getHeaders(); |
198 InternetHeaders ih = headers.getHeaders(); |
198 List<String> cids = ih.getHeader("content-id"); |
199 List<String> cids = ih.getHeader("content-id"); |
199 String cid = (cids != null) ? cids.get(0) : currentIndex+""; |
200 String cid = (cids != null) ? cids.get(0) : currentIndex+""; |
200 if (cid.length() > 2 && cid.charAt(0)=='<') { |
201 if (cid.length() > 2 && cid.charAt(0)=='<') { |
217 } |
218 } |
218 currentPart.setHeaders(ih); |
219 currentPart.setHeaders(ih); |
219 break; |
220 break; |
220 |
221 |
221 case CONTENT : |
222 case CONTENT : |
222 LOGGER.finer("MIMEEvent="+MIMEEvent.EVENT_TYPE.CONTENT); |
223 LOGGER.log(Level.FINER, "MIMEEvent={0}", MIMEEvent.EVENT_TYPE.CONTENT); |
223 MIMEEvent.Content content = (MIMEEvent.Content)event; |
224 MIMEEvent.Content content = (MIMEEvent.Content)event; |
224 ByteBuffer buf = content.getData(); |
225 ByteBuffer buf = content.getData(); |
225 currentPart.addBody(buf); |
226 currentPart.addBody(buf); |
226 break; |
227 break; |
227 |
228 |
228 case END_PART : |
229 case END_PART : |
229 LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.END_PART); |
230 LOGGER.log(Level.FINE, "MIMEEvent={0}", MIMEEvent.EVENT_TYPE.END_PART); |
230 currentPart.doneParsing(); |
231 currentPart.doneParsing(); |
231 ++currentIndex; |
232 ++currentIndex; |
232 break; |
233 break; |
233 |
234 |
234 case END_MESSAGE : |
235 case END_MESSAGE : |
235 LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.END_MESSAGE); |
236 LOGGER.log(Level.FINE, "MIMEEvent={0}", MIMEEvent.EVENT_TYPE.END_MESSAGE); |
236 parsed = true; |
237 parsed = true; |
237 try { |
238 try { |
238 in.close(); |
239 in.close(); |
239 } catch(IOException ioe) { |
240 } catch(IOException ioe) { |
240 throw new MIMEParsingException(ioe); |
241 throw new MIMEParsingException(ioe); |