DefaultHTMLTemplate.java 6.1 KB
Newer Older
D
DK.Pino 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.dolphinscheduler.alert.template.impl;

S
simon824 已提交
19 20
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
D
DK.Pino 已提交
21 22 23 24 25 26
import org.apache.dolphinscheduler.alert.template.AlertTemplate;
import org.apache.dolphinscheduler.alert.utils.Constants;
import org.apache.dolphinscheduler.common.enums.ShowType;
import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
张世鸣 已提交
27
import org.apache.dolphinscheduler.common.utils.*;
D
DK.Pino 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

import java.util.*;

import static org.apache.dolphinscheduler.common.utils.Preconditions.*;

/**
 * the default html alert message template
 */
public class DefaultHTMLTemplate implements AlertTemplate {

    public static final Logger logger = LoggerFactory.getLogger(DefaultHTMLTemplate.class);

    @Override
    public String getMessageFromTemplate(String content, ShowType showType,boolean showAll) {

        switch (showType){
            case TABLE:
                return getTableTypeMessage(content,showAll);
            case TEXT:
                return getTextTypeMessage(content,showAll);
            default:
                throw new IllegalArgumentException(String.format("not support showType: %s in DefaultHTMLTemplate",showType));
        }
    }

    /**
     * get alert message which type is TABLE
     * @param content message content
     * @param showAll weather to show all
     * @return alert message
     */
    private String getTableTypeMessage(String content,boolean showAll){

        if (StringUtils.isNotEmpty(content)){
            List<LinkedHashMap> mapItemsList = JSONUtils.toList(content, LinkedHashMap.class);

            if(!showAll && mapItemsList.size() > Constants.NUMBER_1000){
                mapItemsList = mapItemsList.subList(0,Constants.NUMBER_1000);
            }

            StringBuilder contents = new StringBuilder(200);

            boolean flag = true;

            String title = "";
            for (LinkedHashMap mapItems : mapItemsList){

                Set<Map.Entry<String, Object>> entries = mapItems.entrySet();

                Iterator<Map.Entry<String, Object>> iterator = entries.iterator();

                StringBuilder t = new StringBuilder(Constants.TR);
                StringBuilder cs = new StringBuilder(Constants.TR);
                while (iterator.hasNext()){

                    Map.Entry<String, Object> entry = iterator.next();
                    t.append(Constants.TH).append(entry.getKey()).append(Constants.TH_END);
                    cs.append(Constants.TD).append(String.valueOf(entry.getValue())).append(Constants.TD_END);

                }
                t.append(Constants.TR_END);
                cs.append(Constants.TR_END);
                if (flag){
                    title = t.toString();
                }
                flag = false;
                contents.append(cs);
            }

            return getMessageFromHtmlTemplate(title,contents.toString());
        }

        return content;
    }

    /**
     * get alert message which type is TEXT
     * @param content message content
     * @param showAll weather to show all
     * @return alert message
     */
    private String getTextTypeMessage(String content,boolean showAll){

        if (StringUtils.isNotEmpty(content)){
S
simon824 已提交
112
            ArrayNode list = JSONUtils.parseArray(content);
D
DK.Pino 已提交
113
            StringBuilder contents = new StringBuilder(100);
S
simon824 已提交
114
            for (JsonNode jsonNode : list){
D
DK.Pino 已提交
115
                contents.append(Constants.TR);
S
simon824 已提交
116
                contents.append(Constants.TD).append(jsonNode.toString()).append(Constants.TD_END);
D
DK.Pino 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                contents.append(Constants.TR_END);
            }

            return getMessageFromHtmlTemplate(null,contents.toString());

        }

        return content;
    }

    /**
     * get alert message from a html template
     * @param title     message title
     * @param content   message content
     * @return alert message which use html template
     */
    private String getMessageFromHtmlTemplate(String title,String content){

        checkNotNull(content);
        String htmlTableThead = StringUtils.isEmpty(title) ? "" : String.format("<thead>%s</thead>\n",title);

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        return "<html>\n" +
                "    <head>\n" +
                "        <title>dolphinscheduler</title>\n" +
                "        <meta name='Keywords' content=''>\n" +
                "        <meta name='Description' content=''>\n" +
                "        <style type=\"text/css\">\n" +
                "            table {margin-top:0px;padding-top:0px;border:1px solid;font-size: 14px;color: #333333;border-width: 1px;border-color: #666666;border-collapse: collapse;}\n" +
                "            table th {border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #dedede;text-align: right;}\n" +
                "            table td {border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;text-align: right;}\n" +
                "        </style>\n" +
                "    </head>\n" +
                "    <body style=\"margin:0;padding:0\">\n" +
                "        <table border=\"1px\" cellpadding=\"5px\" cellspacing=\"-10px\">\n" + htmlTableThead + content +
                "        </table>\n" +
                "    </body>\n" +
                "</html>";
D
DK.Pino 已提交
154 155
    }
}