OSDN Git Service

CVS最新版の全ファイルを追加
[spring-ext/ozacc-mail.git] / src / java / com / ozacc / mail / fetch / impl / sk_jp / MailUtility.java
1 /*\r
2  * @(#) $Id: MailUtility.java,v 1.1.2.1 2005/01/18 07:20:59 otsuka Exp $\r
3  * Copyright (c) 2000-2004 Shin Kinoshita All Rights Reserved.\r
4  */\r
5 package com.ozacc.mail.fetch.impl.sk_jp;\r
6 \r
7 import java.io.ByteArrayInputStream;\r
8 import java.io.IOException;\r
9 import java.io.InputStream;\r
10 import java.io.UnsupportedEncodingException;\r
11 import java.util.Date;\r
12 \r
13 import javax.activation.DataHandler;\r
14 import javax.mail.BodyPart;\r
15 import javax.mail.Message;\r
16 import javax.mail.MessagingException;\r
17 import javax.mail.Multipart;\r
18 import javax.mail.Part;\r
19 import javax.mail.internet.AddressException;\r
20 import javax.mail.internet.ContentDisposition;\r
21 import javax.mail.internet.ContentType;\r
22 import javax.mail.internet.HeaderTokenizer;\r
23 import javax.mail.internet.InternetAddress;\r
24 import javax.mail.internet.MailDateFormat;\r
25 import javax.mail.internet.MimeUtility;\r
26 import javax.mail.internet.ParseException;\r
27 \r
28 import com.ozacc.mail.fetch.impl.sk_jp.io.CharCodeConverter;\r
29 import com.ozacc.mail.fetch.impl.sk_jp.io.UnicodeCorrector;\r
30 import com.ozacc.mail.fetch.impl.sk_jp.text.EntityRefEncoder;\r
31 import com.ozacc.mail.fetch.impl.sk_jp.util.StringValues;\r
32 import com.sun.mail.util.BASE64EncoderStream;\r
33 \r
34 /**\r
35  * JavaMailのサポートクラスです。\r
36  * <P>\r
37  * 主にヘッダに対するさまざまな加工機能を提供します。\r
38  * </P>\r
39  * @author Shin\r
40  * @version $Revision: 1.1.2.1 $ $Date: 2005/01/18 07:20:59 $\r
41  */\r
42 public class MailUtility {\r
43 \r
44         public static String getPersonal(InternetAddress a) {\r
45                 if (a.getPersonal() != null)\r
46                         return a.getPersonal();\r
47                 return a.toString();\r
48         }\r
49 \r
50         /** get comma separated E-Mail addresses. */\r
51         public static String getMailAddresses(InternetAddress[] addresses) {\r
52                 if (addresses == null)\r
53                         return null;\r
54                 StringValues buf = new StringValues();\r
55                 for (int i = 0; i < addresses.length; i++) {\r
56                         buf.add(addresses[i].getAddress());\r
57                 }\r
58                 return buf.getString();\r
59         }\r
60 \r
61         /** get comma separated personal names. */\r
62         public static String getPersonalNames(InternetAddress[] addresses) {\r
63                 if (addresses == null)\r
64                         return null;\r
65                 StringValues buf = new StringValues();\r
66                 String name;\r
67                 for (int i = 0; i < addresses.length; i++) {\r
68                         name = decodeText(unfold(addresses[i].getPersonal()));\r
69                         if (name == null) {\r
70                                 name = addresses[i].toString();\r
71                         }\r
72                         buf.add(name);\r
73                 }\r
74                 return buf.getString();\r
75         }\r
76 \r
77         public static String getAddressesHTML(InternetAddress[] addresses) {\r
78                 if (addresses == null)\r
79                         return null;\r
80                 StringValues buf = new StringValues();\r
81                 StringBuffer href = new StringBuffer();\r
82                 String name;\r
83                 for (int i = 0; i < addresses.length; i++) {\r
84                         href.append("<a href=\"mailto:");\r
85                         href.append(addresses[i].getAddress());\r
86                         href.append("\">");\r
87                         name = addresses[i].getPersonal();\r
88                         if (name != null) {\r
89                                 name = decodeText(name);\r
90                         }\r
91                         if (name == null) {\r
92                                 name = addresses[i].toString();\r
93                         }\r
94                         href.append(EntityRefEncoder.encode(name));\r
95                         href.append("</a>");\r
96                         buf.add(new String(href));\r
97                         href.setLength(0);\r
98                 }\r
99                 return buf.getString();\r
100         }\r
101 \r
102         /** get the Content-Transfer-Encoding: header value. */\r
103         public static String getTransferEncoding(byte[] b) {\r
104                 int nonAscii = 0;\r
105                 for (int i = 0; i < b.length; i++) {\r
106                         if (b[i] < 0) {\r
107                                 nonAscii++;\r
108                         }\r
109                 }\r
110                 if (nonAscii == 0)\r
111                         return "7bit";\r
112                 if (nonAscii < b.length - nonAscii)\r
113                         return "quoted-printable";\r
114                 return "base64";\r
115         }\r
116 \r
117         /**\r
118          * パートを保有する親Messageオブジェクトを返します。\r
119          * @param part パート\r
120          * @return ツリー構造の最上位にあたるメッセージオブジェクト\r
121          */\r
122         public static Message getParentMessage(Part part) {\r
123                 Part current = part;\r
124                 Multipart mp;\r
125                 while (!(current instanceof Message)) {\r
126                         mp = ((BodyPart)current).getParent();\r
127                         if (mp == null)\r
128                                 return null; // Should it throw exception?\r
129                         current = mp.getParent();\r
130                         if (current == null)\r
131                                 return null; // Should it throw exception?\r
132                 }\r
133                 return (Message)current;\r
134         }\r
135 \r
136         //////////////////////////////////////////////////////////////////////////\r
137         // note: JavaMail1.2 later\r
138         private static MailDateFormat mailDateFormat = new MailDateFormat();\r
139 \r
140         /**\r
141          * Date構文の誤った"JST"タイムゾーンの補正を行います。\r
142          * <P>\r
143          * JavaMailは"JST"と記述されるタイムゾーンを解釈しません。 ここは本来"+0900"でなければならないところです。 <BR>\r
144          * 仕方がないので" JST"が含まれる文字列の場合は"+0900"を補完して\r
145          * MailDateFormat#parse()を通すようなparse()のラッパを用意します。\r
146          * </P>\r
147          * <P>\r
148          * この実装は一時回避的なものであり、完全なものではありません。\r
149          * </P>\r
150          */\r
151         public static Date parseDate(String rfc822DateString) {\r
152                 if (rfc822DateString == null) {\r
153                         return null;\r
154                 }\r
155                 try {\r
156                         if (rfc822DateString.indexOf(" JST") == -1 || rfc822DateString.indexOf('+') >= 0) {\r
157                                 synchronized (mailDateFormat) {\r
158                                         return mailDateFormat.parse(rfc822DateString);\r
159                                 }\r
160                         }\r
161                         // correct the pseudo header\r
162                         StringBuffer buf = new StringBuffer(rfc822DateString.substring(0, rfc822DateString\r
163                                         .indexOf("JST")));\r
164                         buf.append("+0900");\r
165                         synchronized (mailDateFormat) {\r
166                                 return mailDateFormat.parse(new String(buf));\r
167                         }\r
168                 } catch (java.text.ParseException e) {\r
169                         return null;\r
170                 }\r
171         }\r
172 \r
173         //////////////////////////////////////////////////////////////////////////\r
174         /**\r
175          * Subject:に"Re: "を付加します。\r
176          * <P>\r
177          * ある程度寛容に"Re: "に近い文字列と"[hoge]"を取り除きます。 <BR>\r
178          * ただし、意図しない部分が消されてしまう事もあり得ます。 <BR>\r
179          * JavaMailのreply()では"Re: "がエンコードされていた場合に 正しく"Re: "を取り除いてくれません。\r
180          * </P>\r
181          */\r
182         public static String createReplySubject(String src) {\r
183                 if (src == null || src.length() == 0) {\r
184                         return "Re: (no subject)";\r
185                 }\r
186                 String work = src;\r
187                 if (work.charAt(0) == '[' && work.indexOf(']') > 0) {\r
188                         int afterBracket = indexOfNonLWSP(work, work.indexOf(']') + 1, false);\r
189                         if (afterBracket < 0) {\r
190                                 work = "";\r
191                         } else {\r
192                                 work = work.substring(afterBracket);\r
193                         }\r
194                 }\r
195                 if (work.length() > 3 && "Re:".equalsIgnoreCase(work.substring(0, 3))) {\r
196                         int afterRe = indexOfNonLWSP(work, 3, false);\r
197                         if (afterRe < 0) {\r
198                                 work = "";\r
199                         } else {\r
200                                 work = work.substring(afterRe);\r
201                         }\r
202                 }\r
203                 return "Re: " + work;\r
204         }\r
205 \r
206         //////////////////////////////////////////////////////////////////////////\r
207         /**\r
208          * 入力されたアドレスをInternetAddress形式に変換します。\r
209          * <p>\r
210          * "名無し君 <abc@example.com>(コメント)"等の文字列(エンコード無し)を\r
211          * 渡されても、正しくpersonal文字列が設定されるようにします。 <br>\r
212          * InternetAddress#parse()はエンコード済みの文字列を前提にしているため、 このメソッドの目的には沿いません。\r
213          * </p>\r
214          * @param addresses メイルアドレス文字列(カンマ区切り)\r
215          */\r
216         public static InternetAddress[] parseAddresses(String addressesString) throws AddressException {\r
217                 return parseAddresses(addressesString, true);\r
218         }\r
219 \r
220         public static InternetAddress[] parseAddresses(String addressesString, boolean strict)\r
221                                                                                                                                                                                         throws AddressException {\r
222                 if (addressesString == null)\r
223                         return null;\r
224                 try {\r
225                         InternetAddress[] addresses = InternetAddress.parse(addressesString, strict);\r
226                         // correct personals\r
227                         for (int i = 0; i < addresses.length; i++) {\r
228                                 addresses[i].setPersonal(addresses[i].getPersonal(), "ISO-2022-JP");\r
229                         }\r
230                         return addresses;\r
231                 } catch (UnsupportedEncodingException e) {\r
232                         throw new InternalError(e.toString());\r
233                 }\r
234         }\r
235 \r
236         // InternetAddress.parse(\r
237         //          encodeText(addressesString, "ISO-2022-JP", "B"), strict);\r
238         // で良さそうなものだが、これでは・・たしかなんか問題があったはず。\r
239         //////////////////////////////////////////////////////////////////////////\r
240         /**\r
241          * header valueの unfolding を行います。 空白を厳密に扱うためには decodeText より先に呼び出す必要があります。\r
242          */\r
243         public static String unfold(String source) {\r
244                 if (source == null)\r
245                         return null;\r
246                 StringBuffer buf = new StringBuffer();\r
247                 boolean skip = false;\r
248                 char c;\r
249                 // <CRLF>シーケンスを前提とするならindexOf()で十分ですが、\r
250                 // 念のためCR、LFいずれも許容します。\r
251                 for (int i = 0; i < source.length(); i++) {\r
252                         c = source.charAt(i);\r
253                         if (skip) {\r
254                                 if (isLWSP(c)) {\r
255                                         continue;\r
256                                 }\r
257                                 skip = false;\r
258                         }\r
259                         if (c != '\r' && c != '\n') {\r
260                                 buf.append(c);\r
261                         } else {\r
262                                 buf.append(' ');\r
263                                 skip = true;\r
264                         }\r
265                 }\r
266                 return new String(buf);\r
267         }\r
268 \r
269         /**\r
270          * header valueの folding を行います。\r
271          * <P>\r
272          * white spaceをfolding対象にします。 <BR>\r
273          * 76bytesを超えないwhite space位置に <CRLF>を挿入します。\r
274          * </P>\r
275          * <P>\r
276          * 注:quoteを無視しますので、structured fieldでは不都合が 発生する可能性があります。\r
277          * </P>\r
278          * @param used ヘッダの':'までの文字数。76 - usedが最初のfolding候補桁\r
279          * @return foldingされた( <CRLF>SPACEが挿入された)文字列\r
280          */\r
281         public static String fold(String source, int used) {\r
282                 if (source == null)\r
283                         return null;\r
284                 StringBuffer buf = new StringBuffer();\r
285                 String work = source;\r
286                 int lineBreakIndex;\r
287                 while (work.length() > 76) {\r
288                         lineBreakIndex = work.lastIndexOf(' ', 76);\r
289                         if (lineBreakIndex == -1)\r
290                                 break;\r
291                         buf.append(work.substring(0, lineBreakIndex));\r
292                         buf.append("\r\n");\r
293                         work = work.substring(lineBreakIndex);\r
294                 }\r
295                 buf.append(work);\r
296                 return new String(buf);\r
297         }\r
298 \r
299         //////////////////////////////////////////////////////////////////////////\r
300         /**\r
301          * パートにテキストをセットします。\r
302          * Part#setText() の代わりにこちらを使うことで、\r
303          * "ISO-2022-JP" コンバータではエンコードできない CP932 の\r
304          * 文字をエンコードできます。\r
305          */\r
306         public static void setTextContent(Part p, String s) throws MessagingException {\r
307                 //p.setText(content, "ISO-2022-JP");\r
308                 p.setDataHandler(new DataHandler(new JISDataSource(s)));\r
309                 p.setHeader("Content-Transfer-Encoding", "7bit");\r
310         }\r
311 \r
312         /**\r
313          * 日本語を含むヘッダ用テキストを生成します。\r
314          * 変換結果は ASCII なので、これをそのまま setSubject や InternetAddress\r
315          * のパラメタとして使用してください。\r
316          * "ISO-2022-JP" コンバータではエンコードできない CP932 の\r
317          * 文字をエンコードできます。ただし、encodeText() と異なり、\r
318          * folding の意識をしておらず、また ASCII 部分を除いて分割\r
319          * エンコードを行うこともできません。\r
320          */\r
321         public static String encodeWordJIS(String s) {\r
322                 try {\r
323                         return "=?ISO-2022-JP?B?"\r
324                                         + new String(BASE64EncoderStream.encode(CharCodeConverter\r
325                                                         .sjisToJis(UnicodeCorrector.getInstance("Windows-31J").correct(s)\r
326                                                                         .getBytes("Windows-31J")))) + "?=";\r
327                 } catch (UnsupportedEncodingException e) {\r
328                         throw new RuntimeException("CANT HAPPEN");\r
329                 }\r
330         }\r
331 \r
332         //////////////////////////////////////////////////////////////////////////\r
333         /**\r
334          * ヘッダ内の文字列をデコードします。\r
335          * <p>\r
336          * MimeUtilityの制約を緩めて日本で流通するエンコード形式に対応。\r
337          * 本来は、encoded-wordとnon-encoded-wordの間にはlinear-white-spaceが必要\r
338          * なのですが、空白が無い場所でエンコードするタコメイラが多いので。\r
339          * </p>\r
340          * <p>\r
341          * JISコードをエンコード無しで記述するタコメイラもあります。 <br>\r
342          * ソースにESCが含まれていたら生JISと見なします。\r
343          * </p>\r
344          * <p>\r
345          * =?utf-8?Q?・・・JISコード・・?=なんてさらにタコなメイラも。 <br>\r
346          * 試しにデコード後にまだESCが残ってたらISO-2022-JPと見なすことにします。\r
347          * </p>\r
348          * <p>\r
349          * さらに、multibyte character の前後で別の encoded-word に切ってしまう メイラも…。隣接する\r
350          * encoded-word の CES が同じ場合はバイト列の 結合を行ってから CES デコードを行うようにした…。\r
351          * </p>\r
352          * <p>\r
353          * 日本語に特化してますねえ・・・。\r
354          * </p>\r
355          * @param source encoded text\r
356          * @return decoded text\r
357          */\r
358         public static String decodeText(String source) {\r
359                 if (source == null)\r
360                         return null;\r
361                 // specially for Japanese\r
362                 if (source.indexOf('\u001b') >= 0) {\r
363                         // ISO-2022-JP\r
364                         try {\r
365                                 return new String(source.getBytes("ISO-8859-1"), "ISO-2022-JP");\r
366                         } catch (UnsupportedEncodingException e) {\r
367                                 throw new InternalError();\r
368                         }\r
369                 }\r
370                 String decodedText = new RFC2047Decoder(source).get();\r
371                 if (decodedText.indexOf('\u001b') >= 0) {\r
372                         try {\r
373                                 return new String(decodedText.getBytes("ISO-8859-1"), "ISO-2022-JP");\r
374                         } catch (UnsupportedEncodingException e) {\r
375                                 throw new InternalError();\r
376                         }\r
377                 }\r
378                 return decodedText;\r
379         }\r
380 \r
381         // 日本語をデコードする上で問題があるので、encoded-wordの切り出しはすべて独自に\r
382         // Netscapeなどは"()."等の文字でencoded-wordを切ってしまうが、JavaMailは\r
383         // このときencoded-wordの終わりを判定できず、一部の文字を欠落させてしまう。\r
384         // また、encoded-word を文字デコードするのを遅延させ、隣接する encoded-word\r
385         // の CES が同じ場合は、先に TES デコードを行ったバイト列を結合してから\r
386         // CES に従ったデコードを行う。マルチバイト文字を分断する sender がいるから。\r
387         static class RFC2047Decoder {\r
388 \r
389                 private String source;\r
390 \r
391                 private String pooledCES;\r
392 \r
393                 private byte[] pooledBytes;\r
394 \r
395                 private StringBuffer buf;\r
396 \r
397                 private int pos = 0;\r
398 \r
399                 private int startIndex;\r
400 \r
401                 private int endIndex;\r
402 \r
403                 public RFC2047Decoder(String source) {\r
404                         this.source = source;\r
405                         buf = new StringBuffer(source.length());\r
406                         parse();\r
407                 }\r
408 \r
409                 private void parse() {\r
410                         while (hasEncodedWord()) {\r
411                                 String work = source.substring(pos, startIndex);\r
412                                 if (indexOfNonLWSP(work, 0, false) > -1) {\r
413                                         sweepPooledBytes();\r
414                                         buf.append(work);\r
415                                 } // encoded-word同士の間のLWSPは削除\r
416                                 parseWord();\r
417                         }\r
418                         sweepPooledBytes();\r
419                         buf.append(source.substring(pos));\r
420                 }\r
421 \r
422                 // encoded-word があった場合、startIndex/endIndex をセットする\r
423                 private boolean hasEncodedWord() {\r
424                         startIndex = source.indexOf("=?", pos);\r
425                         if (startIndex == -1)\r
426                                 return false;\r
427                         endIndex = source.indexOf("?=", startIndex + 2);\r
428                         if (endIndex == -1)\r
429                                 return false;\r
430                         // 本来は encoded-word 中に LWSP があってはいけないが\r
431                         // encoded-word の途中で folding してしまう sender がいるらしい\r
432                         // 以下をコメントにすることで encoded-word の誤認識の可能性も\r
433                         // 出てくるが、誤認識になる確率以上に前記のような illegal な\r
434                         // メッセージの方が多いのが実情のようだ。\r
435                         // thx > YOSI\r
436                         //int i = indexOfLWSP(source, startIndex + 2, false, (char)0);\r
437                         //if (i >= 0 && i < endIndex)\r
438                         //    return false;\r
439                         endIndex += 2;\r
440                         return true;\r
441                 }\r
442 \r
443                 private void parseWord() {\r
444                         try {\r
445                                 int s = startIndex + 2;\r
446                                 int e = source.indexOf('?', s);\r
447                                 if (e == endIndex - 2)\r
448                                         throw new RuntimeException();\r
449                                 String ces = source.substring(s, e);\r
450                                 try {\r
451                                         "".getBytes(ces); // FIXME: check whether supported or not\r
452                                 } catch (UnsupportedEncodingException ex) {\r
453                                         ces = "JISAutoDetect";\r
454                                 }\r
455                                 s = e + 1;\r
456                                 e = source.indexOf('?', s);\r
457                                 if (e == endIndex - 2)\r
458                                         throw new RuntimeException();\r
459                                 String tes = source.substring(s, e);\r
460                                 byte[] bytes = decodeByTES(source.substring(e + 1, endIndex - 2), tes);\r
461                                 if (ces.equals(pooledCES)) {\r
462                                         // append bytes\r
463                                         byte[] w = new byte[pooledBytes.length + bytes.length];\r
464                                         System.arraycopy(pooledBytes, 0, w, 0, pooledBytes.length);\r
465                                         System.arraycopy(bytes, 0, w, pooledBytes.length, bytes.length);\r
466                                         pooledBytes = w;\r
467                                 } else {\r
468                                         sweepPooledBytes();\r
469                                         pooledCES = ces;\r
470                                         pooledBytes = bytes;\r
471                                 }\r
472                         } catch (Exception ex) {\r
473                                 ex.printStackTrace();\r
474                                 // contains RuntimeException\r
475                                 buf.append(source.substring(startIndex, endIndex));\r
476                         }\r
477                         pos = endIndex;\r
478                 }\r
479 \r
480                 private void sweepPooledBytes() {\r
481                         if (pooledBytes == null)\r
482                                 return;\r
483                         try {\r
484                                 buf.append(new String(pooledBytes, pooledCES));\r
485                         } catch (UnsupportedEncodingException e) {\r
486                                 throw new InternalError("CANT HAPPEN: Illegal encoding = " + pooledCES);\r
487                         }\r
488                         pooledCES = null;\r
489                         pooledBytes = null;\r
490                 }\r
491 \r
492                 public String get() {\r
493                         return new String(buf);\r
494                 }\r
495         }\r
496 \r
497         private static byte[] decodeByTES(String s, String tes) {\r
498                 // 通常あり得ないが、LWSP を詰める\r
499                 int i;\r
500                 while ((i = indexOfLWSP(s, 0, false, (char)0)) >= 0)\r
501                         s = s.substring(0, i) + s.substring(i + 1);\r
502                 if (tes.equalsIgnoreCase("B") && s.length() % 4 != 0) {\r
503                         // BASE64DecoderStream は正確にパディングされていないと\r
504                         // IOException になるので、無理やり矯正。\r
505                         switch (4 - s.length() % 4) {\r
506                                 case 1:\r
507                                         s += '=';\r
508                                         break;\r
509                                 case 2:\r
510                                         s += "==";\r
511                                         break;\r
512                                 case 3:\r
513                                         if (s.charAt(s.length() - 1) != '=')\r
514                                                 s += "===";\r
515                                         else\r
516                                                 s = s.substring(0, s.length() - 1);\r
517                                         break;\r
518                         }\r
519                 }\r
520                 try {\r
521                         ByteArrayInputStream bis = new ByteArrayInputStream(com.sun.mail.util.ASCIIUtility\r
522                                         .getBytes(s));\r
523                         InputStream is;\r
524                         if (tes.equalsIgnoreCase("B"))\r
525                                 is = new com.sun.mail.util.BASE64DecoderStream(bis);\r
526                         else if (tes.equalsIgnoreCase("Q"))\r
527                                 is = new com.sun.mail.util.QDecoderStream(bis);\r
528                         else\r
529                                 throw new UnsupportedEncodingException(tes);\r
530                         int count = bis.available();\r
531                         byte[] bytes = new byte[count];\r
532                         count = is.read(bytes, 0, count);\r
533                         if (count != bytes.length) {\r
534                                 byte[] w = new byte[count];\r
535                                 System.arraycopy(bytes, 0, w, 0, count);\r
536                                 bytes = w;\r
537                         }\r
538                         return bytes;\r
539                 } catch (IOException e) {\r
540                         e.printStackTrace();\r
541                         throw new RuntimeException("CANT HAPPEN");\r
542                 }\r
543         }\r
544 \r
545         /**\r
546          * 文字列をエンコードします。\r
547          * <p>\r
548          * MimeUtility(強いてはMimeMessage等も)では、1字でも非ASCII文字が含まれる\r
549          * と文字列全体をエンコードしてしまいます。\r
550          * <br>\r
551          * このメソッドでは空白で区切られた範囲だけをエンコードします。 <br>\r
552          * Subjectの"Re: "等がエンコードされていると、この文字列でIn-Reply-To:\r
553          * References:の代わりにスレッドを形成しようとしても失敗することになる\r
554          * ため、こちらのエンコード方式を用いたがる人もいるかもしれません・・。\r
555          * </p>\r
556          * <p>\r
557          * 方針は、ASCII部に前後の空白一つを含ませ、それ以外は空白も含めて全て\r
558          * encoded-wordとします。()の内側は空白無しでもエンコード対象です。\r
559          * </p>\r
560          * @param source text\r
561          * @return encoded text\r
562          */\r
563         // "()" の扱いにこだわりすぎて異常に汚い-_-。\r
564         // "()"なんか無視してまとめて encode するようにすればすっきるするけど…。\r
565         public static String encodeText(String source, String charset, String encoding)\r
566                                                                                                                                                                         throws UnsupportedEncodingException {\r
567                 if (source == null)\r
568                         return null;\r
569                 int boundaryIndex;\r
570                 int startIndex;\r
571                 int endIndex = 0;\r
572                 int lastLWSPIndex;\r
573                 StringBuffer buf = new StringBuffer();\r
574                 while (true) {\r
575                         // check the end of ASCII part\r
576                         boundaryIndex = indexOfNonAscii(source, endIndex);\r
577                         if (boundaryIndex == -1) {\r
578                                 buf.append(source.substring(endIndex));\r
579                                 return new String(buf);\r
580                         }\r
581                         // any LWSP has taken (back track).\r
582                         lastLWSPIndex = indexOfLWSP(source, boundaryIndex, true, '(');\r
583                         startIndex = indexOfNonLWSP(source, lastLWSPIndex, true) + 1;\r
584                         // ASCII part の終了位置は、次の non ASCII と比べて\r
585                         // 最も ASCII 文字よりの空白文字位置または'('の次位置\r
586                         startIndex = (endIndex > startIndex) ? endIndex : startIndex;\r
587                         if (startIndex > endIndex) {\r
588                                 // ASCII part\r
589                                 buf.append(source.substring(endIndex, startIndex));\r
590                                 // JavaMailはencodeWord内でfoldingするけどそれはencodedWord\r
591                                 // に対してのみ。ヘッダそのものに対するfoldingはしてくれない。\r
592                                 if (isLWSP(source.charAt(startIndex))) {\r
593                                         // folding により 空白一つが確保されるのでスキップ\r
594                                         buf.append("\r\n ");\r
595                                         startIndex++;\r
596                                         // なお、'('の場合は空白を入れないので folding しない\r
597                                 }\r
598                         }\r
599                         // any LWSP has taken.\r
600                         endIndex = indexOfNonLWSP(source, boundaryIndex, false);\r
601                         while ((endIndex = indexOfLWSP(source, endIndex, false, ')')) != -1) {\r
602                                 endIndex = indexOfNonLWSP(source, endIndex, false);\r
603                                 int nextBoundary = indexOfLWSP(source, endIndex, false, (char)0);\r
604                                 if (nextBoundary == -1) {\r
605                                         if (indexOfNonAscii(source, endIndex) != -1) {\r
606                                                 endIndex = -1;\r
607                                                 break;\r
608                                         }\r
609                                 } else {\r
610                                         int nonAscii = indexOfNonAscii(source, endIndex);\r
611                                         if (nonAscii != -1 && nonAscii < nextBoundary) {\r
612                                                 endIndex = nextBoundary;\r
613                                                 continue;\r
614                                         }\r
615                                 }\r
616                                 break;\r
617                         }\r
618                         boolean needFolding = false;\r
619                         if (endIndex < 0) {\r
620                                 endIndex = source.length();\r
621                         } else if (isLWSP(source.charAt(endIndex - 1))) {\r
622                                 // folding により 空白一つが確保される(予定)なので減らす\r
623                                 endIndex--;\r
624                                 needFolding = true;\r
625                         }\r
626                         String encodeTargetText = source.substring(startIndex, endIndex);\r
627                         buf.append(MimeUtility.encodeWord(encodeTargetText, charset, encoding));\r
628                         if (needFolding) {\r
629                                 // folding により 空白一つが確保されるのでスキップ\r
630                                 endIndex++;\r
631                                 buf.append("\r\n ");\r
632                         }\r
633                 }\r
634         }\r
635 \r
636         /**\r
637          * 指定位置から最初に見つかった非ASCII文字のIndexを返します。 startIndex が範囲外の場合は -1 を返します。\r
638          * (IndexOutOfBoundsException ではない)\r
639          * @param source 検索する文字列\r
640          * @param startIndex 検索開始位置\r
641          * @return 検出した非ASCII文字Index。見つからなければ-1。\r
642          */\r
643         public static int indexOfNonAscii(String source, int startIndex) {\r
644                 for (int i = startIndex; i < source.length(); i++) {\r
645                         if (source.charAt(i) > 0x7f) {\r
646                                 return i;\r
647                         }\r
648                 }\r
649                 return -1;\r
650         }\r
651 \r
652         /**\r
653          * 指定位置から最初に見つかったLWSP以外の文字のIndexを返します。 startIndex が範囲外の場合は -1 を返します。\r
654          * (IndexOutOfBoundsException ではない)\r
655          * @param source 検索する文字列\r
656          * @param startIndex 検索開始位置\r
657          * @param decrease trueで後方検索\r
658          * @return 検出した非ASCII文字Index。見つからなければ-1。\r
659          */\r
660         public static int indexOfNonLWSP(String source, int startIndex, boolean decrease) {\r
661                 char c;\r
662                 int inc = 1;\r
663                 if (decrease)\r
664                         inc = -1;\r
665                 for (int i = startIndex; i >= 0 && i < source.length(); i += inc) {\r
666                         c = source.charAt(i);\r
667                         if (!isLWSP(c)) {\r
668                                 return i;\r
669                         }\r
670                 }\r
671                 return -1;\r
672         }\r
673 \r
674         /**\r
675          * 指定位置から最初に見つかったLWSPのIndexを返します。 startIndex が範囲外の場合は -1 を返します。\r
676          * (IndexOutOfBoundsException ではない)\r
677          * @param source 検索する文字列\r
678          * @param startIndex 検索開始位置\r
679          * @param decrease trueで後方検索\r
680          * @param additionalDelimiter LWSP以外に区切りとみなす文字(1字のみ)\r
681          * @return 検出した非ASCII文字Index。見つからなければ-1。\r
682          */\r
683         public static int indexOfLWSP(String source, int startIndex, boolean decrease,\r
684                                                                         char additionalDelimiter) {\r
685                 char c;\r
686                 int inc = 1;\r
687                 if (decrease)\r
688                         inc = -1;\r
689                 for (int i = startIndex; i >= 0 && i < source.length(); i += inc) {\r
690                         c = source.charAt(i);\r
691                         if (isLWSP(c) || c == additionalDelimiter) {\r
692                                 return i;\r
693                         }\r
694                 }\r
695                 return -1;\r
696         }\r
697 \r
698         public static boolean isLWSP(char c) {\r
699                 return c == '\r' || c == '\n' || c == ' ' || c == '\t';\r
700         }\r
701 \r
702         //////////////////////////////////////////////////////////////////////////\r
703         /**\r
704          * This method set Content-Disposition: with RFC2231 encoding. It is\r
705          * required JavaMail1.2.\r
706          */\r
707         /**\r
708          * Part#setFileName()のマルチバイト対応版です。 JavaMail1.2でなければコンパイルできません\r
709          */\r
710         public static void setFileName(Part part, String filename, String charset, String lang)\r
711                                                                                                                                                                                         throws MessagingException {\r
712                 // Set the Content-Disposition "filename" parameter\r
713                 ContentDisposition disposition;\r
714                 String[] strings = part.getHeader("Content-Disposition");\r
715                 if (strings == null || strings.length < 1) {\r
716                         disposition = new ContentDisposition(Part.ATTACHMENT);\r
717                 } else {\r
718                         disposition = new ContentDisposition(strings[0]);\r
719                         disposition.getParameterList().remove("filename");\r
720                 }\r
721                 part.setHeader("Content-Disposition", disposition.toString()\r
722                                 + encodeParameter("filename", filename, charset, lang));\r
723                 ContentType cType;\r
724                 strings = part.getHeader("Content-Type");\r
725                 if (strings == null || strings.length < 1) {\r
726                         cType = new ContentType(part.getDataHandler().getContentType());\r
727                 } else {\r
728                         cType = new ContentType(strings[0]);\r
729                 }\r
730                 try {\r
731                         // I want to public the MimeUtility#doEncode()!!!\r
732                         String mimeString = MimeUtility.encodeWord(filename, charset, "B");\r
733                         // cut <CRLF>...\r
734                         StringBuffer sb = new StringBuffer();\r
735                         int i;\r
736                         while ((i = mimeString.indexOf('\r')) != -1) {\r
737                                 sb.append(mimeString.substring(0, i));\r
738                                 mimeString = mimeString.substring(i + 2);\r
739                         }\r
740                         sb.append(mimeString);\r
741                         cType.setParameter("name", new String(sb));\r
742                 } catch (UnsupportedEncodingException e) {\r
743                         throw new MessagingException("Encoding error", e);\r
744                 }\r
745                 part.setHeader("Content-Type", cType.toString());\r
746         }\r
747 \r
748         /**\r
749          * This method encodes the parameter.\r
750          * <P>\r
751          * But most MUA cannot decode the encoded parameters by this method. <BR>\r
752          * I recommend using the "Content-Type:"'s name parameter both.\r
753          * </P>\r
754          */\r
755         /**\r
756          * ヘッダのパラメタ部のエンコードを行います。\r
757          * <P>\r
758          * 現状は受信できないものが多いのでこのメソッドだけでは使えません。 <BR>\r
759          * Content-Disposition:のfilenameのみに使用し、さらに Content-Type:のnameにMIME\r
760          * encodingでの記述も行うのが妥当でしょう。 <BR>\r
761          * パラメタは必ず行頭から始まるものとします。 (ヘッダの開始行から折り返された位置を開始位置とします)\r
762          * </P>\r
763          * <P>\r
764          * foldingの方針はascii/non ascii境界のみをチェックします。 現状は連続するascii/non\r
765          * asciiの長さのチェックは現状行っていません。 (エンコード後のバイト数でチェックしなければならないのでかなり面倒)\r
766          * </P>\r
767          * @param name パラメタ名\r
768          * @param value エンコード対象のパラメタ値\r
769          * @param encoding 文字エンコーディング\r
770          * @param lang 言語指定子\r
771          * @return エンコード済み文字列 ";\r\n name*0*=ISO-8859-2''・・・;\r\n name*1*=・・"\r
772          */\r
773         // 1.全体をエンコードして長かったら半分に切ってエンコードを繰り返す\r
774         public static String encodeParameter(String name, String value, String encoding, String lang) {\r
775                 StringBuffer result = new StringBuffer();\r
776                 StringBuffer encodedPart = new StringBuffer();\r
777                 boolean needWriteCES = !isAllAscii(value);\r
778                 boolean CESWasWritten = false;\r
779                 boolean encoded;\r
780                 boolean needFolding = false;\r
781                 int sequenceNo = 0;\r
782                 int column;\r
783                 while (value.length() > 0) {\r
784                         // index of boundary of ascii/non ascii\r
785                         int lastIndex;\r
786                         boolean isAscii = value.charAt(0) < 0x80;\r
787                         for (lastIndex = 1; lastIndex < value.length(); lastIndex++) {\r
788                                 if (value.charAt(lastIndex) < 0x80) {\r
789                                         if (!isAscii)\r
790                                                 break;\r
791                                 } else {\r
792                                         if (isAscii)\r
793                                                 break;\r
794                                 }\r
795                         }\r
796                         if (lastIndex != value.length())\r
797                                 needFolding = true;\r
798                         RETRY: while (true) {\r
799                                 encodedPart.setLength(0);\r
800                                 String target = value.substring(0, lastIndex);\r
801                                 byte[] bytes;\r
802                                 try {\r
803                                         if (isAscii) {\r
804                                                 bytes = target.getBytes("us-ascii");\r
805                                         } else {\r
806                                                 bytes = target.getBytes(encoding);\r
807                                         }\r
808                                 } catch (UnsupportedEncodingException e) {\r
809                                         bytes = target.getBytes(); // use default encoding\r
810                                         encoding = MimeUtility.mimeCharset(MimeUtility.getDefaultJavaCharset());\r
811                                 }\r
812                                 encoded = false;\r
813                                 // It is not strict.\r
814                                 column = name.length() + 7; // size of " " and "*nn*=" and ";"\r
815                                 for (int i = 0; i < bytes.length; i++) {\r
816                                         if ((bytes[i] >= '0' && bytes[i] <= '9')\r
817                                                         || (bytes[i] >= 'A' && bytes[i] <= 'Z')\r
818                                                         || (bytes[i] >= 'a' && bytes[i] <= 'z') || bytes[i] == '$'\r
819                                                         || bytes[i] == '.' || bytes[i] == '!') {\r
820                                                 // 2001/09/01 しかるべき文字が符号化されない問題修正\r
821                                                 // attribute-char(符号化しなくてもよい文字)の定義は\r
822                                                 // <any (US-ASCII) CHAR except SPACE, CTLs,\r
823                                                 // "*", "'", "%", or tspecials>\r
824                                                 // だが、ややこしいので英数字のみとしておく\r
825                                                 // "$.!"はおまけ^^。エンコード時は大して意識はいらない\r
826                                                 encodedPart.append((char)bytes[i]);\r
827                                                 column++;\r
828                                         } else {\r
829                                                 encoded = true;\r
830                                                 encodedPart.append('%');\r
831                                                 String hex = Integer.toString(bytes[i] & 0xff, 16);\r
832                                                 if (hex.length() == 1) {\r
833                                                         encodedPart.append('0');\r
834                                                 }\r
835                                                 encodedPart.append(hex);\r
836                                                 column += 3;\r
837                                         }\r
838                                         if (column > 76) {\r
839                                                 needFolding = true;\r
840                                                 lastIndex /= 2;\r
841                                                 continue RETRY;\r
842                                         }\r
843                                 }\r
844                                 result.append(";\r\n ").append(name);\r
845                                 if (needFolding) {\r
846                                         result.append('*').append(sequenceNo);\r
847                                         sequenceNo++;\r
848                                 }\r
849                                 if (!CESWasWritten && needWriteCES) {\r
850                                         result.append("*=");\r
851                                         CESWasWritten = true;\r
852                                         result.append(encoding).append('\'');\r
853                                         if (lang != null)\r
854                                                 result.append(lang);\r
855                                         result.append('\'');\r
856                                 } else if (encoded) {\r
857                                         result.append("*=");\r
858                                         /*\r
859                                          * 本当にcharacter encodingは先頭パートに書かないとだめなのか? if (encoded) {\r
860                                          * result.append("*="); if (!CESWasWritten && needWriteCES) {\r
861                                          * CESWasWritten = true;\r
862                                          * result.append(encoding).append('\''); if (lang != null)\r
863                                          * result.append(lang); result.append('\''); }\r
864                                          */\r
865                                 } else {\r
866                                         result.append('=');\r
867                                 }\r
868                                 result.append(new String(encodedPart));\r
869                                 value = value.substring(lastIndex);\r
870                                 break;\r
871                         }\r
872                 }\r
873                 return new String(result);\r
874         }\r
875 \r
876         /** check if contains only ascii characters in text. */\r
877         public static boolean isAllAscii(String text) {\r
878                 for (int i = 0; i < text.length(); i++) {\r
879                         if (text.charAt(i) > 0x7f) { // non-ascii\r
880                                 return false;\r
881                         }\r
882                 }\r
883                 return true;\r
884         }\r
885 \r
886         //////////////////////////////////////////////////////////////////////////\r
887         /**\r
888          * This method decode the RFC2231 encoded filename parameter instead of\r
889          * Part#getFileName().\r
890          */\r
891         /**\r
892          * Part#getFileName()のマルチバイト対応版です。\r
893          */\r
894         public static String getFileName(Part part) throws MessagingException {\r
895                 String[] disposition = part.getHeader("Content-Disposition");\r
896                 // A patch by YOSI (Thanx)\r
897                 // http://www.sk-jp.com/cgibin/treebbs.cgi?kako=1&all=227&s=227\r
898                 String filename;\r
899                 if (disposition == null || disposition.length < 1\r
900                                 || (filename = getParameter(disposition[0], "filename")) == null) {\r
901                         filename = part.getFileName();\r
902                         if (filename != null) {\r
903                                 return decodeParameterSpciallyJapanese(filename);\r
904                         }\r
905                         return null;\r
906                 }\r
907                 return filename;\r
908         }\r
909 \r
910         static class Encoding {\r
911 \r
912                 String encoding = "us-ascii";\r
913 \r
914                 String lang = "";\r
915         }\r
916 \r
917         /**\r
918          * This method decodes the parameter which be encoded (folded) by RFC2231\r
919          * method.\r
920          * <P>\r
921          * The parameter's order should be considered.\r
922          * </P>\r
923          */\r
924         /**\r
925          * ヘッダのパラメタ部のデコードを行います。\r
926          * <P>\r
927          * RFC2231形式でfolding(分割)されたパラメタを結合し、デコードします。\r
928          * 尚、RFC2231にはパラメタの順番に依存するなと書かれていますが、 それを実装すると大変面倒(一度分割された全てのパートを\r
929          * 保持してソートしなければならない)なので、 シーケンス番号に関係なく(0から)順番に 並んでいるものとみなして処理することにします。\r
930          * </P>\r
931          * @param header ヘッダの値全体\r
932          * @param name 取得したいパラメタ名\r
933          * @return デコード済み文字列 (パラメタが存在しない場合は null)\r
934          */\r
935         public static String getParameter(String header, String name) throws ParseException {\r
936                 if (header == null)\r
937                         return null;\r
938                 // 本来これは不要。日本固有のデコード処理です。\r
939                 // 2001/07/22 書籍版では"あ.txt"の生JISパラメタ値がデコードできない\r
940                 // これは、ISO-2022-JPバイト列のままHeaderTokenizerにかけると、\r
941                 // "あ"のバイトシーケンスに含まれる0x22がダブルクォートと\r
942                 // 解釈されるため。\r
943                 // JIS/Shift_JISの生バイトと思われるもののデコードを先に行う事で回避\r
944                 header = decodeParameterSpciallyJapanese(header);\r
945                 HeaderTokenizer tokenizer = new HeaderTokenizer(header, ";=\t ", true);\r
946                 HeaderTokenizer.Token token;\r
947                 StringBuffer sb = new StringBuffer();\r
948                 // It is specified in first encoded-part.\r
949                 Encoding encoding = new Encoding();\r
950                 String n;\r
951                 String v;\r
952                 try {\r
953                         while (true) {\r
954                                 token = tokenizer.next();\r
955                                 if (token.getType() == HeaderTokenizer.Token.EOF)\r
956                                         break;\r
957                                 if (token.getType() != ';')\r
958                                         continue;\r
959                                 token = tokenizer.next();\r
960                                 checkType(token);\r
961                                 n = token.getValue();\r
962                                 token = tokenizer.next();\r
963                                 if (token.getType() != '=') {\r
964                                         throw new ParseException("Illegal token : " + token.getValue());\r
965                                 }\r
966                                 token = tokenizer.next();\r
967                                 checkType(token);\r
968                                 v = token.getValue();\r
969                                 if (n.equalsIgnoreCase(name)) {\r
970                                         // It is not divided and is not encoded.\r
971                                         return v;\r
972                                 }\r
973                                 int index = name.length();\r
974                                 if (!n.startsWith(name) || n.charAt(index) != '*') {\r
975                                         // another parameter\r
976                                         continue;\r
977                                 }\r
978                                 // be folded, or be encoded\r
979                                 int lastIndex = n.length() - 1;\r
980                                 if (n.charAt(lastIndex) == '*') {\r
981                                         // http://www.sk-jp.com/cgibin/treebbs.cgi?all=399&s=399\r
982                                         if (index == lastIndex || n.charAt(index + 1) == '0') {\r
983                                                 // decode as initial-section\r
984                                                 sb.append(decodeRFC2231(v, encoding, true));\r
985                                         } else {\r
986                                                 // decode as other-sections\r
987                                                 sb.append(decodeRFC2231(v, encoding, false));\r
988                                         }\r
989                                 } else {\r
990                                         sb.append(v);\r
991                                 }\r
992                                 if (index == lastIndex) {\r
993                                         // not folding\r
994                                         break;\r
995                                 }\r
996                         }\r
997                         if (sb.length() == 0)\r
998                                 return null;\r
999                         return new String(sb);\r
1000                 } catch (UnsupportedEncodingException e) {\r
1001                         throw new ParseException(e.toString());\r
1002                 }\r
1003         }\r
1004 \r
1005         private static void checkType(HeaderTokenizer.Token token) throws ParseException {\r
1006                 int t = token.getType();\r
1007                 if (t != HeaderTokenizer.Token.ATOM && t != HeaderTokenizer.Token.QUOTEDSTRING) {\r
1008                         throw new ParseException("Illegal token : " + token.getValue());\r
1009                 }\r
1010         }\r
1011 \r
1012         // "lang" tag is ignored...\r
1013         private static String decodeRFC2231(String s, Encoding encoding, boolean isInitialSection)\r
1014                                                                                                                                                                                                 throws ParseException,\r
1015                                                                                                                                                                                                 UnsupportedEncodingException {\r
1016                 StringBuffer sb = new StringBuffer();\r
1017                 int i = 0;\r
1018                 if (isInitialSection) {\r
1019                         int work = s.indexOf('\'');\r
1020                         if (work > 0) {\r
1021                                 encoding.encoding = s.substring(0, work);\r
1022                                 work++;\r
1023                                 i = s.indexOf('\'', work);\r
1024                                 if (i < 0) {\r
1025                                         throw new ParseException("lang tag area was missing.");\r
1026                                 }\r
1027                                 encoding.lang = s.substring(work, i);\r
1028                                 i++;\r
1029                         }\r
1030                 }\r
1031                 try {\r
1032                         for (; i < s.length(); i++) {\r
1033                                 if (s.charAt(i) == '%') {\r
1034                                         sb.append((char)Integer.parseInt(s.substring(i + 1, i + 3), 16));\r
1035                                         i += 2;\r
1036                                         continue;\r
1037                                 }\r
1038                                 sb.append(s.charAt(i));\r
1039                         }\r
1040                         return new String(new String(sb).getBytes("ISO-8859-1"), encoding.encoding);\r
1041                 } catch (IndexOutOfBoundsException e) {\r
1042                         throw new ParseException(s + " :: this string were not decoded.");\r
1043                 }\r
1044         }\r
1045 \r
1046         // 日本語向けデコード\r
1047         private static String decodeParameterSpciallyJapanese(String s) throws ParseException {\r
1048                 try {\r
1049                         // decode by character encoding.\r
1050                         // if string are all ASCII, it is not translated.\r
1051                         s = new String(s.getBytes("ISO-8859-1"), "JISAutoDetect");\r
1052                         // decode by RFC2047.\r
1053                         // if string doesn't contain encoded-word, it is not translated.\r
1054                         return decodeText(s);\r
1055                 } catch (UnsupportedEncodingException e) {}\r
1056                 throw new ParseException("Unsupported Encoding");\r
1057         }\r
1058 \r
1059         private MailUtility() {}\r
1060 }