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 GmbH & Co. KG, 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 java.io.ByteArrayOutputStream;
031import java.io.IOException;
032import java.io.InputStream;
033import java.io.OutputStream;
034
035import javax.activation.DataSource;
036
037/**
038 *
039 * DataSource implementation that may be filled with content from an {@link java.io.InputStream}.
040 * <p>
041 * It's intended use is for creation of mail attachments from strings without having to create RFS
042 * or VFS resources. Note that this data source will only support read operations and operations
043 * related to writing will throw an {@link java.lang.UnsupportedOperationException}.
044 * <p>
045 *
046 * @since 6.1.7
047 */
048public class CmsInputStreamDataSource implements DataSource {
049
050    /** The content type to use for the data source. */
051    private String m_contentType;
052
053    /** The underlying input stream of this data source. */
054    private InputStream m_inputStream;
055
056    /** The name of this data source. */
057    private String m_name;
058
059    /**
060     * Constructor with mandatory input stream, content type and name.
061     * <p>
062     * Note that the given input stream has to be resettable. During a mail creation and
063     * transmission cycle it is potentially read twice (commons-email-1.0.jar in combination with
064     * activation.jar) and the 2nd time the actual transport to the serial data to transmit is done.
065     * So a reset will be made here whenever the internal input stream is retrieved to avoid that
066     * the attachments remain empty.
067     * <p>
068     * The contentType argument should always be a valid MIME type. It is suggested that it is
069     * "application/octet-stream" if the DataSource implementation can not determine the data type.
070     * For textual data it should be "text/&lt;subtype&gt;; charset=&lt;encoding&gt;" to give a hint
071     * about the ecoding. Note that some textual documents like xml have their own encoding
072     * directive contained and the charset given here (for the mail part header) should not be
073     * different from the contained one.
074     *
075     *
076     * @param in the underlying source of data.
077     *
078     * @param contentType the correct MIME type of the data along with the charset in the form of a
079     *            string (see comment above).
080     *
081     * @param name the name that describes the data in the underyling input stream. E.g. the name of
082     *            a file that the input stream reads from.
083     *
084     */
085    public CmsInputStreamDataSource(InputStream in, String contentType, String name) {
086
087        m_inputStream = in;
088        m_contentType = contentType;
089        m_name = name;
090    }
091
092    /**
093     * @see javax.activation.DataSource#getContentType()
094     */
095    public String getContentType() {
096
097        return m_contentType;
098    }
099
100    /**
101     * Retunrs the underlying input stream of this data source.
102     *
103     * @return the underlying input stream of this data source.
104     *
105     * @throws IOException if the constructor-given input stream is not "resettable" ({@link InputStream#reset()}).
106     *
107     * @see javax.activation.DataSource#getInputStream()
108     *
109     */
110    public InputStream getInputStream() throws IOException {
111
112        m_inputStream.reset();
113        return m_inputStream;
114    }
115
116    /**
117     * @see javax.activation.DataSource#getName()
118     */
119    public String getName() {
120
121        return m_name;
122    }
123
124    /**
125     * Don't use this method, VFS resources can't be written using this datasource class.
126     * <p>
127     *
128     * This method will just return a new <code>{@link ByteArrayOutputStream}</code>.
129     * <p>
130     *
131     * @see javax.activation.DataSource#getOutputStream()
132     */
133    public OutputStream getOutputStream() {
134
135        // maybe throw an Exception here to avoid errors
136        return new ByteArrayOutputStream();
137    }
138
139}