001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.mail;
029
030import org.opencms.file.CmsFile;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsResource;
033import org.opencms.main.CmsException;
034import org.opencms.main.OpenCms;
035
036import java.io.ByteArrayInputStream;
037import java.io.ByteArrayOutputStream;
038import java.io.InputStream;
039import java.io.OutputStream;
040
041import javax.activation.DataSource;
042
043/**
044 * DataSource wrapper for VFS resources, allows easy sending of VFS resources as email attachments.<p>
045 *
046 * @since 6.2.0
047 */
048public class CmsVfsDataSource implements DataSource {
049
050    /** The content type to use for the data source. */
051    private String m_contentType;
052
053    /** The file that accessed by this data source. */
054    private CmsFile m_file;
055
056    /**
057     * Creates a new data source for the given VFS resource.<p>
058     *
059     * @param cms the current users OpenCms context
060     * @param resource the resource to use
061     *
062     * @throws CmsException in case of errors accessing the resource in the VFS
063     */
064    public CmsVfsDataSource(CmsObject cms, CmsResource resource)
065    throws CmsException {
066
067        m_file = cms.readFile(resource);
068        // identify the mime-type for the data source
069        m_contentType = OpenCms.getResourceManager().getMimeType(
070            m_file.getName(),
071            cms.getRequestContext().getEncoding());
072    }
073
074    /**
075     * @see javax.activation.DataSource#getContentType()
076     */
077    public String getContentType() {
078
079        return m_contentType;
080    }
081
082    /**
083     * Returns an input stream baded on the file contents.<p>
084     *
085     * @see javax.activation.DataSource#getInputStream()
086     */
087    public InputStream getInputStream() {
088
089        return new ByteArrayInputStream(m_file.getContents());
090    }
091
092    /**
093     * Returns the root path of the given resource.<p>
094     *
095     * @see javax.activation.DataSource#getName()
096     */
097    public String getName() {
098
099        return m_file.getRootPath();
100    }
101
102    /**
103     * Don't use this method, VFS resources can't be written using this datasource class.<p>
104     *
105     * This method will just return a new <code>{@link ByteArrayOutputStream}</code>.<p>
106     *
107     * @see javax.activation.DataSource#getOutputStream()
108     */
109    public OutputStream getOutputStream() {
110
111        // maybe throw an Exception here to avoid errors
112        return new ByteArrayOutputStream();
113    }
114}