1
2
3
4
5
6 package com.ozacc.mail.fetch.impl.sk_jp;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.OutputStream;
11 import javax.activation.DataSource;
12 import javax.mail.MessageAware;
13 import javax.mail.MessageContext;
14 import javax.mail.MessagingException;
15 import javax.mail.Part;
16 import javax.mail.internet.ContentType;
17 import javax.mail.internet.ParseException;
18
19 /***
20 * Content-Type:の不適合をISO-2022-JPに補正します。
21 * 使用方法は<PRE>
22 * Object o = new DataHandler(
23 * new CorrectedContentTypeDataSource(part, charset)
24 * ).getContent();
25 * </PRE><P>のようになります。</P><P>
26 * スレッドセーフではありませんので利用者側で排他制御を行ってください。
27 * </P>
28 * @author Shin
29 * @version $Revision: 1.1.2.2 $ $Date: 2004/10/24 10:27:40 $
30 */
31 class CorrectedContentTypeDataSource implements DataSource, MessageAware {
32
33 protected DataSource source;
34
35 protected String defaultCharset;
36
37 protected String forceCharset;
38
39 public CorrectedContentTypeDataSource() {}
40
41 public CorrectedContentTypeDataSource(DataSource dataSource, String defaultCharset) {
42 setDataSource(dataSource);
43 setDefaultCharset(defaultCharset);
44 }
45
46 public CorrectedContentTypeDataSource(Part part, String defaultCharset)
47 throws MessagingException {
48 setPart(part);
49 setDefaultCharset(defaultCharset);
50 }
51
52 public void setPart(Part part) throws MessagingException {
53
54 setDataSource(part.getDataHandler().getDataSource());
55 }
56
57 public void setDataSource(DataSource newSource) {
58 source = newSource;
59 }
60
61 public void setDefaultCharset(String defaultCharset) {
62 this.defaultCharset = defaultCharset;
63 }
64
65 /***
66 * 指定された文字コードで既存の文字コードを上書きします。
67 *
68 * @param forceCharset 強制的に適用する文字コード
69 * @author Tomohiro Otsuka
70 */
71 public void setForceCharset(String forceCharset) {
72 this.forceCharset = forceCharset;
73 }
74
75 public String getContentType() {
76 ContentType contentType = null;
77 try {
78 contentType = new ContentType(source.getContentType());
79 } catch (ParseException e) {
80 return "text/plain; charset=" + defaultCharset;
81 }
82 String specifiedCharset = contentType.getParameter("charset");
83 if (specifiedCharset == null) {
84
85
86
87 contentType.setParameter("charset", defaultCharset);
88 } else if (forceCharset != null) {
89 contentType.setParameter("charset", forceCharset);
90 }
91 return contentType.toString();
92 }
93
94 public String getName() {
95 return source.getName();
96 }
97
98 public InputStream getInputStream() throws IOException {
99 return source.getInputStream();
100 }
101
102 public OutputStream getOutputStream() throws IOException {
103 return source.getOutputStream();
104 }
105
106 public synchronized MessageContext getMessageContext() {
107 if (source instanceof MessageAware) {
108 return ((MessageAware)source).getMessageContext();
109 }
110 throw new RuntimeException(source + " isn't MessageAware.");
111 }
112 }