source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/storage/src/main/java/org/apache/james/mime4j/storage/StorageBodyFactory.java @ 6785

Revision 6785, 12.3 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
20package org.apache.james.mime4j.storage;
21
22import java.io.IOException;
23import java.io.InputStream;
24import java.nio.charset.Charset;
25
26import org.apache.james.mime4j.codec.DecodeMonitor;
27import org.apache.james.mime4j.dom.BinaryBody;
28import org.apache.james.mime4j.dom.Disposable;
29import org.apache.james.mime4j.dom.SingleBody;
30import org.apache.james.mime4j.dom.TextBody;
31import org.apache.james.mime4j.message.BodyFactory;
32import org.apache.james.mime4j.storage.DefaultStorageProvider;
33import org.apache.james.mime4j.storage.MultiReferenceStorage;
34import org.apache.james.mime4j.storage.Storage;
35import org.apache.james.mime4j.storage.StorageProvider;
36import org.apache.james.mime4j.util.CharsetUtil;
37
38/**
39 * Factory for creating message bodies.
40 */
41public class StorageBodyFactory implements BodyFactory {
42
43    private static final Charset FALLBACK_CHARSET = CharsetUtil.DEFAULT_CHARSET;
44
45    private final StorageProvider storageProvider;
46    private final DecodeMonitor monitor;
47
48    /**
49     * Creates a new <code>BodyFactory</code> instance that uses the default
50     * storage provider for creating message bodies from input streams.
51     */
52    public StorageBodyFactory() {
53        this(null, null);
54    }
55
56    /**
57     * Creates a new <code>BodyFactory</code> instance that uses the given
58     * storage provider for creating message bodies from input streams.
59     *
60     * @param storageProvider
61     *            a storage provider or <code>null</code> to use the default
62     *            one.
63     */
64    public StorageBodyFactory(
65            final StorageProvider storageProvider,
66            final DecodeMonitor monitor) {
67        this.storageProvider =
68            storageProvider != null ? storageProvider : DefaultStorageProvider.getInstance();
69        this.monitor =
70            monitor != null ? monitor : DecodeMonitor.SILENT;
71    }
72
73    /**
74     * Returns the <code>StorageProvider</code> this <code>BodyFactory</code>
75     * uses to create message bodies from input streams.
76     *
77     * @return a <code>StorageProvider</code>.
78     */
79    public StorageProvider getStorageProvider() {
80        return storageProvider;
81    }
82
83    /**
84     * Creates a {@link BinaryBody} that holds the content of the given input
85     * stream.
86     *
87     * @param is
88     *            input stream to create a message body from.
89     * @return a binary body.
90     * @throws IOException
91     *             if an I/O error occurs.
92     */
93    public BinaryBody binaryBody(InputStream is) throws IOException {
94        if (is == null)
95            throw new IllegalArgumentException();
96
97        Storage storage = storageProvider.store(is);
98        return new StorageBinaryBody(new MultiReferenceStorage(storage));
99    }
100
101    /**
102     * Creates a {@link BinaryBody} that holds the content of the given
103     * {@link Storage}.
104     * <p>
105     * Note that the caller must not invoke {@link Storage#delete() delete()} on
106     * the given <code>Storage</code> object after it has been passed to this
107     * method. Instead the message body created by this method takes care of
108     * deleting the storage when it gets disposed of (see
109     * {@link Disposable#dispose()}).
110     *
111     * @param storage
112     *            storage to create a message body from.
113     * @return a binary body.
114     * @throws IOException
115     *             if an I/O error occurs.
116     */
117    public BinaryBody binaryBody(Storage storage) throws IOException {
118        if (storage == null)
119            throw new IllegalArgumentException();
120
121        return new StorageBinaryBody(new MultiReferenceStorage(storage));
122    }
123
124    /**
125     * Creates a {@link TextBody} that holds the content of the given input
126     * stream.
127     * <p>
128     * &quot;us-ascii&quot; is used to decode the byte content of the
129     * <code>Storage</code> into a character stream when calling
130     * {@link TextBody#getReader() getReader()} on the returned object.
131     *
132     * @param is
133     *            input stream to create a message body from.
134     * @return a text body.
135     * @throws IOException
136     *             if an I/O error occurs.
137     */
138    public TextBody textBody(InputStream is) throws IOException {
139        if (is == null)
140            throw new IllegalArgumentException();
141
142        Storage storage = storageProvider.store(is);
143        return new StorageTextBody(new MultiReferenceStorage(storage),
144                CharsetUtil.DEFAULT_CHARSET);
145    }
146
147    /**
148     * Creates a {@link TextBody} that holds the content of the given input
149     * stream.
150     * <p>
151     * The charset corresponding to the given MIME charset name is used to
152     * decode the byte content of the input stream into a character stream when
153     * calling {@link TextBody#getReader() getReader()} on the returned object.
154     * If the MIME charset has no corresponding Java charset or the Java charset
155     * cannot be used for decoding then &quot;us-ascii&quot; is used instead.
156     *
157     * @param is
158     *            input stream to create a message body from.
159     * @param mimeCharset
160     *            name of a MIME charset.
161     * @return a text body.
162     * @throws IOException
163     *             if an I/O error occurs.
164     */
165    public TextBody textBody(InputStream is, String mimeCharset)
166            throws IOException {
167        if (is == null)
168            throw new IllegalArgumentException();
169        if (mimeCharset == null)
170            throw new IllegalArgumentException();
171
172        Storage storage = storageProvider.store(is);
173        Charset charset = toJavaCharset(mimeCharset, false, monitor);
174        return new StorageTextBody(new MultiReferenceStorage(storage), charset);
175    }
176
177    /**
178     * Creates a {@link TextBody} that holds the content of the given
179     * {@link Storage}.
180     * <p>
181     * &quot;us-ascii&quot; is used to decode the byte content of the
182     * <code>Storage</code> into a character stream when calling
183     * {@link TextBody#getReader() getReader()} on the returned object.
184     * <p>
185     * Note that the caller must not invoke {@link Storage#delete() delete()} on
186     * the given <code>Storage</code> object after it has been passed to this
187     * method. Instead the message body created by this method takes care of
188     * deleting the storage when it gets disposed of (see
189     * {@link Disposable#dispose()}).
190     *
191     * @param storage
192     *            storage to create a message body from.
193     * @return a text body.
194     * @throws IOException
195     *             if an I/O error occurs.
196     */
197    public TextBody textBody(Storage storage) throws IOException {
198        if (storage == null)
199            throw new IllegalArgumentException();
200
201        return new StorageTextBody(new MultiReferenceStorage(storage),
202                CharsetUtil.DEFAULT_CHARSET);
203    }
204
205    /**
206     * Creates a {@link TextBody} that holds the content of the given
207     * {@link Storage}.
208     * <p>
209     * The charset corresponding to the given MIME charset name is used to
210     * decode the byte content of the <code>Storage</code> into a character
211     * stream when calling {@link TextBody#getReader() getReader()} on the
212     * returned object. If the MIME charset has no corresponding Java charset or
213     * the Java charset cannot be used for decoding then &quot;us-ascii&quot; is
214     * used instead.
215     * <p>
216     * Note that the caller must not invoke {@link Storage#delete() delete()} on
217     * the given <code>Storage</code> object after it has been passed to this
218     * method. Instead the message body created by this method takes care of
219     * deleting the storage when it gets disposed of (see
220     * {@link Disposable#dispose()}).
221     *
222     * @param storage
223     *            storage to create a message body from.
224     * @param mimeCharset
225     *            name of a MIME charset.
226     * @return a text body.
227     * @throws IOException
228     *             if an I/O error occurs.
229     */
230    public TextBody textBody(Storage storage, String mimeCharset)
231            throws IOException {
232        if (storage == null)
233            throw new IllegalArgumentException();
234        if (mimeCharset == null)
235            throw new IllegalArgumentException();
236
237        Charset charset = toJavaCharset(mimeCharset, false, monitor);
238        return new StorageTextBody(new MultiReferenceStorage(storage), charset);
239    }
240
241    /**
242     * Creates a {@link TextBody} that holds the content of the given string.
243     * <p>
244     * &quot;us-ascii&quot; is used to encode the characters of the string into
245     * a byte stream when calling
246     * {@link SingleBody#writeTo(java.io.OutputStream) writeTo(OutputStream)} on
247     * the returned object.
248     *
249     * @param text
250     *            text to create a message body from.
251     * @return a text body.
252     */
253    public TextBody textBody(String text) {
254        if (text == null)
255            throw new IllegalArgumentException();
256
257        return new StringTextBody(text, CharsetUtil.DEFAULT_CHARSET);
258    }
259
260    /**
261     * Creates a {@link TextBody} that holds the content of the given string.
262     * <p>
263     * The charset corresponding to the given MIME charset name is used to
264     * encode the characters of the string into a byte stream when calling
265     * {@link SingleBody#writeTo(java.io.OutputStream) writeTo(OutputStream)} on
266     * the returned object. If the MIME charset has no corresponding Java
267     * charset or the Java charset cannot be used for encoding then
268     * &quot;us-ascii&quot; is used instead.
269     *
270     * @param text
271     *            text to create a message body from.
272     * @param mimeCharset
273     *            name of a MIME charset.
274     * @return a text body.
275     */
276    public TextBody textBody(String text, String mimeCharset) {
277        if (text == null)
278            throw new IllegalArgumentException();
279        if (mimeCharset == null)
280            throw new IllegalArgumentException();
281
282        Charset charset = toJavaCharset(mimeCharset, true, monitor);
283        return new StringTextBody(text, charset);
284    }
285
286    private static Charset toJavaCharset(
287            final String mimeCharset,
288            boolean forEncoding,
289            final DecodeMonitor monitor) {
290        String charset = CharsetUtil.toJavaCharset(mimeCharset);
291        if (charset == null) {
292            if (monitor.isListening()) {
293                monitor.warn(
294                        "MIME charset '" + mimeCharset + "' has no "
295                        + "corresponding Java charset", "Using "
296                        + FALLBACK_CHARSET + " instead.");
297            }
298            return FALLBACK_CHARSET;
299        }
300
301        if (forEncoding && !CharsetUtil.isEncodingSupported(charset)) {
302            if (monitor.isListening()) {
303                monitor.warn(
304                        "MIME charset '" + mimeCharset
305                        + "' does not support encoding", "Using "
306                        + FALLBACK_CHARSET + " instead.");
307            }
308            return FALLBACK_CHARSET;
309        }
310
311        if (!forEncoding && !CharsetUtil.isDecodingSupported(charset)) {
312            if (monitor.isListening()) {
313                monitor.warn(
314                        "MIME charset '" + mimeCharset
315                        + "' does not support decoding", "Using "
316                        + FALLBACK_CHARSET + " instead.");
317            }
318            return FALLBACK_CHARSET;
319        }
320
321        return Charset.forName(charset);
322    }
323
324}
Note: See TracBrowser for help on using the repository browser.