001    /**
002     * Copyright (c) 2000-present 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    import com.liferay.portlet.messageboards.model.MBMessageConstants;
022    
023    import java.io.ByteArrayInputStream;
024    import java.io.InputStream;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    /**
030     * @author Jorge Ferrer
031     */
032    public class MBMailMessage {
033    
034            public void addBytes(String fileName, byte[] bytes) {
035                    _bytesOVPs.add(new ObjectValuePair<String, byte[]>(fileName, bytes));
036            }
037    
038            public String getBody() {
039                    String body = null;
040    
041                    if (MBMessageConstants.DEFAULT_FORMAT.equals("html")) {
042                            if (Validator.isNotNull(_htmlBody)) {
043                                    body = GetterUtil.getString(_htmlBody);
044                            }
045                            else if (Validator.isNotNull(_plainBody)) {
046                                    body = GetterUtil.getString(_plainBody);
047                            }
048                    }
049                    else if (MBMessageConstants.DEFAULT_FORMAT.equals("text")) {
050                            if (Validator.isNotNull(_plainBody)) {
051                                    body = GetterUtil.getString(_plainBody);
052                            }
053                            else if (Validator.isNotNull(_htmlBody)) {
054                                    body = HtmlUtil.extractText(_htmlBody);
055                            }
056                    }
057    
058                    if (Validator.isNull(body)) {
059                            body = "-";
060                    }
061    
062                    return body;
063            }
064    
065            public String getHtmlBody() {
066                    return _htmlBody;
067            }
068    
069            public List<ObjectValuePair<String, InputStream>> getInputStreamOVPs() {
070                    List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
071                            new ArrayList<>(_bytesOVPs.size());
072    
073                    for (ObjectValuePair<String, byte[]> bytesOVP : _bytesOVPs) {
074                            String key = bytesOVP.getKey();
075                            byte[] bytes = bytesOVP.getValue();
076    
077                            ByteArrayInputStream byteArrayInputStream =
078                                    new ByteArrayInputStream(bytes);
079    
080                            ObjectValuePair<String, InputStream> inputStreamOVP =
081                                    new ObjectValuePair<String, InputStream>(
082                                            key, byteArrayInputStream);
083    
084                            inputStreamOVPs.add(inputStreamOVP);
085                    }
086    
087                    return inputStreamOVPs;
088            }
089    
090            public String getPlainBody() {
091                    return _plainBody;
092            }
093    
094            public void setHtmlBody(String htmlBody) {
095                    _htmlBody = htmlBody;
096            }
097    
098            public void setPlainBody(String plainBody) {
099                    _plainBody = plainBody;
100            }
101    
102            private final List<ObjectValuePair<String, byte[]>> _bytesOVPs =
103                    new ArrayList<>();
104            private String _htmlBody;
105            private String _plainBody;
106    
107    }