KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/mail/CmsVfsDataSource.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) 2002 - 2005 Alkacon Software (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, 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 org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsResource;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.OpenCms;
39
40 import java.io.ByteArrayInputStream JavaDoc;
41 import java.io.ByteArrayOutputStream JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.io.OutputStream JavaDoc;
44
45 import javax.activation.DataSource JavaDoc;
46
47 /**
48  * DataSource wrapper for VFS resources, allows easy sending of VFS resources as email attachments.<p>
49  *
50  * @author Alexander Kandzior
51  * @author Jan Baudisch
52  *
53  * @version $Revision: 1.2 $
54  *
55  * @since 6.2.0
56  */

57 public class CmsVfsDataSource implements DataSource JavaDoc {
58
59     /** The content type to use for the data source. */
60     private String JavaDoc m_contentType;
61
62     /** The file that accessed by this data source. */
63     private CmsFile m_file;
64
65     /**
66      * Creates a new data source for the given VFS resource.<p>
67      *
68      * @param cms the current users OpenCms context
69      * @param resource the resource to use
70      *
71      * @throws CmsException in case of errors accessing the resource in the VFS
72      */

73     public CmsVfsDataSource(CmsObject cms, CmsResource resource)
74     throws CmsException {
75
76         m_file = CmsFile.upgrade(resource, cms);
77         // identify the mime-type for the data source
78
m_contentType = OpenCms.getResourceManager().getMimeType(
79             m_file.getName(), cms.getRequestContext().getEncoding());
80     }
81
82     /**
83      * @see javax.activation.DataSource#getContentType()
84      */

85     public String JavaDoc getContentType() {
86
87         return m_contentType;
88     }
89
90     /**
91      * Returns an input stream baded on the file contents.<p>
92      *
93      * @see javax.activation.DataSource#getInputStream()
94      */

95     public InputStream JavaDoc getInputStream() {
96
97         return new ByteArrayInputStream JavaDoc(m_file.getContents());
98     }
99
100     /**
101      * Returns the root path of the given resource.<p>
102      *
103      * @see javax.activation.DataSource#getName()
104      */

105     public String JavaDoc getName() {
106
107         return m_file.getRootPath();
108     }
109
110     /**
111      * Don't use this method, VFS resources can't be written using this datasource class.<p>
112      *
113      * This method will just return a new <code>{@link ByteArrayOutputStream}</code>.<p>
114      *
115      * @see javax.activation.DataSource#getOutputStream()
116      */

117     public OutputStream JavaDoc getOutputStream() {
118
119         // maybe throw an Exception here to avoid errors
120
return new ByteArrayOutputStream JavaDoc();
121     }
122 }
Popular Tags