001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.util;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.HtmlUtil;
019    import com.liferay.portal.kernel.util.ObjectValuePair;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.io.ByteArrayInputStream;
023    import java.io.InputStream;
024    
025    import java.util.ArrayList;
026    import java.util.List;
027    
028    /**
029     * @author Jorge Ferrer
030     */
031    public class MBMailMessage {
032    
033            public void addBytes(String fileName, byte[] bytes) {
034                    _bytesOVPs.add(new ObjectValuePair<String, byte[]>(fileName, bytes));
035            }
036    
037            public String getBody() {
038                    if (Validator.isNotNull(_plainBody)) {
039                            return GetterUtil.getString(_plainBody);
040                    }
041                    else if (Validator.isNotNull(_htmlBody)) {
042                            return HtmlUtil.extractText(_htmlBody);
043                    }
044                    else {
045                            return "-";
046                    }
047            }
048    
049            public String getHtmlBody() {
050                    return _htmlBody;
051            }
052    
053            public List<ObjectValuePair<String, InputStream>> getInputStreamOVPs() {
054                    List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
055                            new ArrayList<ObjectValuePair<String, InputStream>>(
056                                    _bytesOVPs.size());
057    
058                    for (ObjectValuePair<String, byte[]> bytesOVP : _bytesOVPs) {
059                            String key = bytesOVP.getKey();
060                            byte[] bytes = bytesOVP.getValue();
061    
062                            ByteArrayInputStream byteArrayInputStream =
063                                                            new ByteArrayInputStream(bytes);
064    
065                            ObjectValuePair<String, InputStream> inputStreamOVP =
066                                    new ObjectValuePair<String, InputStream>(
067                                            key, byteArrayInputStream);
068    
069                            inputStreamOVPs.add(inputStreamOVP);
070                    }
071    
072                    return inputStreamOVPs;
073            }
074    
075            public String getPlainBody() {
076                    return _plainBody;
077            }
078    
079            public void setHtmlBody(String htmlBody) {
080                    _htmlBody = htmlBody;
081            }
082    
083            public void setPlainBody(String plainBody) {
084                    _plainBody = plainBody;
085            }
086    
087            private List<ObjectValuePair<String, byte[]>> _bytesOVPs =
088                    new ArrayList<ObjectValuePair<String, byte[]>>();
089            private String _htmlBody;
090            private String _plainBody;
091    
092    }