001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.messageboards.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.HtmlUtil;
021    import com.liferay.portal.kernel.util.ObjectValuePair;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.io.ByteArrayInputStream;
025    import java.io.InputStream;
026    import java.io.UnsupportedEncodingException;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    import javax.mail.internet.MimeUtility;
032    
033    /**
034     * @author Jorge Ferrer
035     */
036    public class MBMailMessage {
037    
038            public void addBytes(String fileName, byte[] bytes) {
039                    try {
040                            fileName = MimeUtility.decodeText(fileName);
041                    }
042                    catch (UnsupportedEncodingException uee) {
043                            if (_log.isWarnEnabled()) {
044                                    _log.warn("Error decoding filename: " + fileName, uee);
045                            }
046                    }
047    
048                    _bytesOVPs.add(new ObjectValuePair<String, byte[]>(fileName, bytes));
049            }
050    
051            public String getBody() {
052                    if (Validator.isNotNull(_plainBody)) {
053                            return GetterUtil.getString(_plainBody);
054                    }
055                    else if (Validator.isNotNull(_htmlBody)) {
056                            return HtmlUtil.extractText(_htmlBody);
057                    }
058                    else {
059                            return "-";
060                    }
061            }
062    
063            public String getHtmlBody() {
064                    return _htmlBody;
065            }
066    
067            public List<ObjectValuePair<String, InputStream>> getInputStreamOVPs() {
068                    List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
069                            new ArrayList<ObjectValuePair<String, InputStream>>(
070                                    _bytesOVPs.size());
071    
072                    for (ObjectValuePair<String, byte[]> bytesOVP : _bytesOVPs) {
073                            String key = bytesOVP.getKey();
074                            byte[] bytes = bytesOVP.getValue();
075    
076                            ByteArrayInputStream byteArrayInputStream =
077                                                            new ByteArrayInputStream(bytes);
078    
079                            ObjectValuePair<String, InputStream> inputStreamOVP =
080                                    new ObjectValuePair<String, InputStream>(
081                                            key, byteArrayInputStream);
082    
083                            inputStreamOVPs.add(inputStreamOVP);
084                    }
085    
086                    return inputStreamOVPs;
087            }
088    
089            public String getPlainBody() {
090                    return _plainBody;
091            }
092    
093            public void setHtmlBody(String htmlBody) {
094                    _htmlBody = htmlBody;
095            }
096    
097            public void setPlainBody(String plainBody) {
098                    _plainBody = plainBody;
099            }
100    
101            private List<ObjectValuePair<String, byte[]>> _bytesOVPs =
102                    new ArrayList<ObjectValuePair<String, byte[]>>();
103            private String _htmlBody;
104            private final Log _log = LogFactoryUtil.getLog(
105                    MBMailMessage.class.getName());
106            private String _plainBody;
107    
108    }