View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.helper;
17  
18  import java.io.IOException;
19  
20  import org.apache.commons.text.StringEscapeUtils;
21  import org.apache.logging.log4j.LogManager;
22  import org.apache.logging.log4j.Logger;
23  import org.codelibs.core.lang.StringUtil;
24  import org.codelibs.core.stream.StreamUtil;
25  import org.codelibs.curl.Curl;
26  import org.codelibs.curl.CurlResponse;
27  import org.codelibs.fess.mylasta.direction.FessConfig;
28  import org.codelibs.fess.util.ComponentUtil;
29  import org.dbflute.mail.CardView;
30  import org.dbflute.mail.send.supplement.SMailPostingDiscloser;
31  
32  public class NotificationHelper {
33      private static final Logger logger = LogManager.getLogger(NotificationHelper.class);
34  
35      protected static final char LF = '\n';
36  
37      public void send(final CardView cardView, final SMailPostingDiscloser discloser) {
38          sendToSlack(cardView, discloser);
39          sendToGoogleChat(cardView, discloser);
40      }
41  
42      protected void sendToSlack(final CardView cardView, final SMailPostingDiscloser discloser) {
43          // https://api.slack.com/messaging/webhooks#posting_with_webhooks
44          final FessConfig fessConfig = ComponentUtil.getFessConfig();
45          final String slackWebhookUrls = fessConfig.getSlackWebhookUrls();
46          if (StringUtil.isBlank(slackWebhookUrls)) {
47              return;
48          }
49          final String body = toSlackMessage(discloser);
50          StreamUtil.split(slackWebhookUrls, "[,\\s]").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(url -> {
51              try (CurlResponse response = Curl.post(url).header("Content-Type", "application/json").body(body).execute()) {
52                  if (response.getHttpStatusCode() == 200) {
53                      if (logger.isDebugEnabled()) {
54                          logger.debug("Sent {} to {}.", body, url);
55                      }
56                  } else {
57                      logger.warn("Failed to send {} to {}. HTTP Status is {}. {}", body, url, response.getHttpStatusCode(),
58                              response.getContentAsString());
59                  }
60              } catch (final IOException e) {
61                  logger.warn("Failed to send {} to {}.", body, url, e);
62              }
63          }));
64      }
65  
66      protected String toSlackMessage(final SMailPostingDiscloser discloser) {
67          final StringBuilder buf = new StringBuilder(100);
68          buf.append("{\"text\":\"");
69          buf.append(LF);
70          buf.append(StringEscapeUtils.escapeJson(discloser.getSavedSubject().orElse(StringUtil.EMPTY).trim()));
71          buf.append(LF).append("```");
72          buf.append(LF).append(StringEscapeUtils.escapeJson(discloser.getSavedPlainText().orElse(StringUtil.EMPTY).trim()));
73          buf.append(LF).append("```\"}");
74          return buf.toString();
75      }
76  
77      protected void sendToGoogleChat(final CardView cardView, final SMailPostingDiscloser discloser) {
78          // https://developers.google.com/hangouts/chat/how-tos/webhooks
79          final FessConfig fessConfig = ComponentUtil.getFessConfig();
80          final String googleChatWebhookUrls = fessConfig.getGoogleChatWebhookUrls();
81          if (StringUtil.isBlank(googleChatWebhookUrls)) {
82              return;
83          }
84          final String body = toGoogleChatMessage(discloser);
85          StreamUtil.split(googleChatWebhookUrls, "[,\\s]").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(url -> {
86              try (CurlResponse response = Curl.post(url).header("Content-Type", "application/json").body(body).execute()) {
87                  if (response.getHttpStatusCode() == 200) {
88                      if (logger.isDebugEnabled()) {
89                          logger.debug("Sent {} to {}.", body, url);
90                      }
91                  } else {
92                      logger.warn("Failed to send {} to {}. HTTP Status is {}. {}", body, url, response.getHttpStatusCode(),
93                              response.getContentAsString());
94                  }
95              } catch (final IOException e) {
96                  logger.warn("Failed to send {} to {}.", body, url, e);
97              }
98          }));
99      }
100 
101     protected String toGoogleChatMessage(final SMailPostingDiscloser discloser) {
102         final StringBuilder buf = new StringBuilder(100);
103         buf.append("{\"text\":\"");
104         buf.append(LF);
105         buf.append(StringEscapeUtils.escapeJson(discloser.getSavedSubject().orElse(StringUtil.EMPTY).trim()));
106         buf.append(LF).append("```");
107         buf.append(LF).append(StringEscapeUtils.escapeJson(discloser.getSavedPlainText().orElse(StringUtil.EMPTY).trim()));
108         buf.append(LF).append("```\"}");
109         return buf.toString();
110     }
111 }