KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > mail > CmsInputStreamDataSource


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/mail/CmsInputStreamDataSource.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.mail;
33
34 import java.io.ByteArrayOutputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.OutputStream JavaDoc;
38
39 import javax.activation.DataSource JavaDoc;
40
41 /**
42  *
43  * DataSource implementation that may be filled with content from an {@link java.io.InputStream}.
44  * <p>
45  * It's intended use is for creation of mail attachments from strings without having to create RFS
46  * or VFS resources. Note that this data source will only support read operations and operations
47  * related to writing will throw an {@link java.lang.UnsupportedOperationException}.
48  * <p>
49  *
50  * @author Achim Westermann
51  *
52  * @version $Revision: 1.2 $
53  *
54  * @since 6.1.7
55  */

56 public class CmsInputStreamDataSource implements DataSource JavaDoc {
57
58     /** The content type to use for the data source. */
59     private String JavaDoc m_contentType;
60
61     /** The underlying input stream of this data source. */
62     private InputStream JavaDoc m_inputStream;
63
64     /** The name of this data source. */
65     private String JavaDoc m_name;
66
67     /**
68      * Constructor with mandatory input stream, content type and name.
69      * <p>
70      * Note that the given input stream has to be resettable. During a mail creation and
71      * transmission cycle it is potentially read twice (commons-email-1.0.jar in combination with
72      * activation.jar) and the 2nd time the actual transport to the serial data to transmit is done.
73      * So a reset will be made here whenever the internal input stream is retrieved to avoid that
74      * the attachments remain empty.
75      * <p>
76      * The contentType argument should always be a valid MIME type. It is suggested that it is
77      * "application/octet-stream" if the DataSource implementation can not determine the data type.
78      * For textual data it should be "text/&lt;subtype&gt;; charset=&lt;encoding&gt;" to give a hint
79      * about the ecoding. Note that some textual documents like xml have their own encoding
80      * directive contained and the charset given here (for the mail part header) should not be
81      * different from the contained one.
82      *
83      *
84      * @param in the underlying source of data.
85      *
86      * @param contentType the correct MIME type of the data along with the charset in the form of a
87      * string (see comment above).
88      *
89      * @param name the name that describes the data in the underyling input stream. E.g. the name of
90      * a file that the input stream reads from.
91      *
92      */

93     public CmsInputStreamDataSource(InputStream JavaDoc in, String JavaDoc contentType, String JavaDoc name) {
94
95         m_inputStream = in;
96         m_contentType = contentType;
97         m_name = name;
98     }
99
100     /**
101      * @see javax.activation.DataSource#getContentType()
102      */

103     public String JavaDoc getContentType() {
104
105         return m_contentType;
106     }
107
108     /**
109      * Retunrs the underlying input stream of this data source.
110      *
111      * @return the underlying input stream of this data source.
112      *
113      * @throws IOException if the constructor-given input stream is not "resettable" ({@link InputStream#reset()}).
114      *
115      * @see javax.activation.DataSource#getInputStream()
116      *
117      */

118     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
119
120         m_inputStream.reset();
121         return m_inputStream;
122     }
123
124     /**
125      * @see javax.activation.DataSource#getName()
126      */

127     public String JavaDoc getName() {
128
129         return m_name;
130     }
131
132     /**
133      * Don't use this method, VFS resources can't be written using this datasource class.
134      * <p>
135      *
136      * This method will just return a new <code>{@link ByteArrayOutputStream}</code>.
137      * <p>
138      *
139      * @see javax.activation.DataSource#getOutputStream()
140      */

141     public OutputStream JavaDoc getOutputStream() {
142
143         // maybe throw an Exception here to avoid errors
144
return new ByteArrayOutputStream JavaDoc();
145     }
146
147 }
148
Popular Tags