1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.mime4j.message;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.james.mime4j.decoder.CodecUtil;
25 import org.apache.james.mime4j.message.storage.TempFile;
26 import org.apache.james.mime4j.message.storage.TempPath;
27 import org.apache.james.mime4j.message.storage.TempStorage;
28 import org.apache.james.mime4j.util.CharsetUtil;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.OutputStream;
34 import java.io.Reader;
35 import java.io.UnsupportedEncodingException;
36
37
38 /**
39 * Text body backed by a {@link org.apache.james.mime4j.message.storage.TempFile}.
40 *
41 *
42 * @version $Id$
43 */
44 class TempFileTextBody extends AbstractBody implements TextBody {
45 private static Log log = LogFactory.getLog(TempFileTextBody.class);
46
47 private String mimeCharset = null;
48 private TempFile tempFile = null;
49
50 public TempFileTextBody(final InputStream is, final String mimeCharset) throws IOException {
51
52 this.mimeCharset = mimeCharset;
53 TempPath tempPath = TempStorage.getInstance().getRootTempPath();
54 tempFile = tempPath.createTempFile("attachment", ".txt");
55
56 OutputStream out = tempFile.getOutputStream();
57 CodecUtil.copy(is, out);
58 out.close();
59 }
60
61 /**
62 * @see org.apache.james.mime4j.message.TextBody#getReader()
63 */
64 public Reader getReader() throws UnsupportedEncodingException, IOException {
65 String javaCharset = null;
66 if (mimeCharset != null) {
67 javaCharset = CharsetUtil.toJavaCharset(mimeCharset);
68 }
69
70 if (javaCharset == null) {
71 javaCharset = "ISO-8859-1";
72
73 if (log.isWarnEnabled()) {
74 if (mimeCharset == null) {
75 log.warn("No MIME charset specified. Using " + javaCharset
76 + " instead.");
77 } else {
78 log.warn("MIME charset '" + mimeCharset + "' has no "
79 + "corresponding Java charset. Using " + javaCharset
80 + " instead.");
81 }
82 }
83 }
84 /*
85 if (log.isWarnEnabled()) {
86 if (mimeCharset == null) {
87 log.warn("No MIME charset specified. Using the "
88 + "platform's default charset.");
89 } else {
90 log.warn("MIME charset '" + mimeCharset + "' has no "
91 + "corresponding Java charset. Using the "
92 + "platform's default charset.");
93 }
94 }
95
96 return new InputStreamReader(tempFile.getInputStream());
97 }*/
98
99 return new InputStreamReader(tempFile.getInputStream(), javaCharset);
100 }
101
102
103 /**
104 * @see org.apache.james.mime4j.message.Body#writeTo(java.io.OutputStream, int)
105 */
106 public void writeTo(OutputStream out, int mode) throws IOException {
107 final InputStream inputStream = tempFile.getInputStream();
108 CodecUtil.copy(inputStream, out);
109 }
110
111 /**
112 * Deletes the temporary file that stores the content of this text body.
113 *
114 * @see org.apache.james.mime4j.message.Disposable#dispose()
115 */
116 public void dispose() {
117 if (tempFile != null) {
118 tempFile.delete();
119 tempFile = null;
120 }
121 }
122
123 }