001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.net.smtp;
020
021import java.io.ByteArrayInputStream;
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.OutputStream;
025
026import javax.activation.DataSource;
027
028import lucee.commons.lang.StringUtil;
029
030import org.apache.commons.lang.WordUtils;
031
032public class StringDataSource implements DataSource {
033        
034        private String text;
035        private String ct;
036        private String charset;
037
038        public StringDataSource(String text, String ct, String charset, int maxLineLength) {
039                this.text=WordUtils.wrap(text, maxLineLength);
040                this.ct=ct;
041                this.charset=charset;
042        }
043
044        @Override
045        public String getContentType() {
046                return ct;
047        }
048
049        @Override
050        public InputStream getInputStream() throws IOException {
051                return new ByteArrayInputStream(StringUtil.isEmpty(charset)?text.getBytes():text.getBytes(charset));
052        }
053
054        @Override
055        public String getName() {
056                return "StringDataSource";
057        }
058
059        @Override
060        public OutputStream getOutputStream() throws IOException {
061                throw new IOException("no access to write");
062        }
063
064}