Skip to content

Commit 2ddcaab

Browse files
committed
#939 Modify to possible to change the type of JSON to send with Webhook
1 parent 3c8d7bd commit 2ddcaab

File tree

17 files changed

+516
-114
lines changed

17 files changed

+516
-114
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ out.txt
1616
.env
1717
.es_data
1818
.knowledge
19+
.vscode

src/main/java/org/support/project/knowledge/logic/WebhookLogic.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.support.project.knowledge.dao.WebhooksDao;
2626
import org.support.project.knowledge.entity.WebhookConfigsEntity;
2727
import org.support.project.knowledge.entity.WebhooksEntity;
28+
import org.support.project.knowledge.logic.notification.webhook.CommentInsertWebhookNotification;
29+
import org.support.project.knowledge.logic.notification.webhook.KnowledgeUpdateWebHookNotification;
2830
import org.support.project.web.dao.ProxyConfigsDao;
2931
import org.support.project.web.entity.ProxyConfigsEntity;
3032

@@ -63,7 +65,8 @@ public void startNotifyWebhook() {
6365
try {
6466
List<WebhookConfigsEntity> configEntities = configs.get(entity.getHook());
6567
for (WebhookConfigsEntity configEntity : configEntities) {
66-
WebhookLogic.get().notify(proxyConfig, configEntity, entity.getContent());
68+
String json = createJson(entity, configEntity);
69+
notify(proxyConfig, configEntity, json);
6770
}
6871
dao.physicalDelete(entity);
6972
count++;
@@ -74,6 +77,20 @@ public void startNotifyWebhook() {
7477
}
7578
LOG.info("Webhook sended. count: " + count);
7679
}
80+
/**
81+
* 送信するJSONを生成する
82+
* @param entity
83+
* @param configEntity
84+
* @return
85+
*/
86+
public String createJson(WebhooksEntity entity, WebhookConfigsEntity configEntity) throws Exception {
87+
if (entity.getHook().equals(WebhookConfigsEntity.HOOK_KNOWLEDGES)) {
88+
return KnowledgeUpdateWebHookNotification.get().createSendJson(entity, configEntity);
89+
} else if (entity.getHook().equals(WebhookConfigsEntity.HOOK_COMMENTS)) {
90+
return CommentInsertWebhookNotification.get().createSendJson(entity, configEntity);
91+
}
92+
return entity.getContent();
93+
}
7794

7895
/**
7996
* 設定を返す

src/main/java/org/support/project/knowledge/logic/notification/webhook/CommentInsertWebhookNotification.java

+137-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package org.support.project.knowledge.logic.notification.webhook;
22

3+
import java.io.IOException;
4+
import java.io.StringReader;
5+
import java.io.UnsupportedEncodingException;
36
import java.text.SimpleDateFormat;
47
import java.util.HashMap;
8+
import java.util.Iterator;
59
import java.util.List;
610
import java.util.Map;
711

812
import org.support.project.common.log.Log;
913
import org.support.project.common.log.LogFactory;
14+
import org.support.project.common.util.FileUtil;
1015
import org.support.project.common.util.PropertyUtil;
16+
import org.support.project.common.util.StringUtils;
1117
import org.support.project.di.Container;
1218
import org.support.project.di.DI;
1319
import org.support.project.di.Instance;
20+
import org.support.project.knowledge.dao.CommentsDao;
21+
import org.support.project.knowledge.dao.KnowledgesDao;
1422
import org.support.project.knowledge.dao.WebhookConfigsDao;
1523
import org.support.project.knowledge.dao.WebhooksDao;
1624
import org.support.project.knowledge.entity.CommentsEntity;
@@ -20,9 +28,17 @@
2028
import org.support.project.knowledge.logic.MailLogic;
2129
import org.support.project.knowledge.logic.NotifyLogic;
2230
import org.support.project.knowledge.logic.WebhookLogic;
31+
import org.support.project.knowledge.logic.notification.QueueNotification;
32+
import org.support.project.knowledge.vo.notification.webhook.WebhookCommentJson;
2333
import org.support.project.web.dao.UsersDao;
2434
import org.support.project.web.entity.UsersEntity;
2535

36+
import com.google.gson.JsonArray;
37+
import com.google.gson.JsonElement;
38+
import com.google.gson.JsonObject;
39+
import com.google.gson.JsonParser;
40+
import com.google.gson.JsonPrimitive;
41+
2642
import net.arnx.jsonic.JSON;
2743

2844
/**
@@ -38,6 +54,8 @@ public static CommentInsertWebhookNotification get() {
3854
return Container.getComp(CommentInsertWebhookNotification.class);
3955
}
4056

57+
private KnowledgeUpdateWebHookNotification knowledgeUpdateWebHookNotification = KnowledgeUpdateWebHookNotification.get();
58+
4159
/**
4260
* コメント追加のWebhookの登録を行う
4361
* @param comment
@@ -46,34 +64,144 @@ public static CommentInsertWebhookNotification get() {
4664
public void sendCommentWebhook(CommentsEntity comment, KnowledgesEntity knowledge) {
4765
WebhookConfigsDao webhookConfigsDao = WebhookConfigsDao.get();
4866
List<WebhookConfigsEntity> webhookConfigsEntities = webhookConfigsDao.selectOnHook(WebhookConfigsEntity.HOOK_COMMENTS);
49-
5067
if (0 == webhookConfigsEntities.size()) {
5168
return;
5269
}
53-
54-
Map<String, Object> commentData = getCommentData(comment, knowledge);
55-
70+
String json = createWebhookJson(comment, knowledge);
5671
WebhooksEntity webhooksEntity = new WebhooksEntity();
5772
String webhookId = MailLogic.get().idGen("Notify");
5873
webhooksEntity.setWebhookId(webhookId);
5974
webhooksEntity.setStatus(WebhookLogic.WEBHOOK_STATUS_UNSENT);
6075
webhooksEntity.setHook(WebhookConfigsEntity.HOOK_COMMENTS);
61-
webhooksEntity.setContent(JSON.encode(commentData));
62-
76+
webhooksEntity.setContent(json);
6377
if (LOG.isTraceEnabled()) {
6478
LOG.trace(PropertyUtil.reflectionToString(webhooksEntity));
6579
}
66-
6780
WebhooksDao.get().insert(webhooksEntity);
6881
}
82+
/**
83+
* Webhook通知情報に格納するJSONを生成する 送信先に合わせて送るJSONの型を変更するため、初めはキーの値のみをJSONとして保存している
84+
*
85+
* @param knowledge
86+
* @param comment
87+
* @return
88+
*/
89+
private String createWebhookJson(CommentsEntity comment, KnowledgesEntity knowledge) {
90+
WebhookCommentJson json = new WebhookCommentJson();
91+
json.commentId = comment.getCommentNo();
92+
return JSON.encode(json);
93+
}
6994

70-
95+
/**
96+
* Webhookで送信するJSONを生成する 送信先毎に送る型を変化させることが可能
97+
*
98+
* @param entity
99+
* @param configEntity
100+
* @throws IOException
101+
* @throws UnsupportedEncodingException
102+
*/
103+
public String createSendJson(WebhooksEntity entity, WebhookConfigsEntity configEntity) throws UnsupportedEncodingException, IOException {
104+
String template = configEntity.getTemplate();
105+
if (StringUtils.isEmpty(template)) {
106+
template = FileUtil.read(getClass().getResourceAsStream("comment_template.json"), "UTF-8");
107+
}
108+
WebhookCommentJson json = JSON.decode(entity.getContent(), WebhookCommentJson.class);
109+
CommentsEntity comment = CommentsDao.get().selectOnKey(json.commentId);
110+
UsersEntity insertUser = UsersDao.get().selectOnKey(comment.getInsertUser());
111+
if (insertUser != null) {
112+
comment.setInsertUserName(insertUser.getUserName());
113+
}
114+
UsersEntity updateUser = UsersDao.get().selectOnKey(comment.getInsertUser());
115+
if (updateUser != null) {
116+
comment.setUpdateUserName(updateUser.getUserName());
117+
}
118+
KnowledgesEntity knowledge = KnowledgesDao.get().selectOnKeyWithUserName(comment.getKnowledgeId());
119+
JsonElement send = new JsonParser().parse(new StringReader(template));
120+
buildJson(send.getAsJsonObject(), comment, knowledge);
121+
return send.toString();
122+
}
123+
private void buildJson(JsonObject obj, CommentsEntity comment, KnowledgesEntity knowledge) {
124+
Iterator<String> props = obj.keySet().iterator();
125+
while (props.hasNext()) {
126+
String prop = (String) props.next();
127+
JsonElement e = obj.get(prop);
128+
if (e.isJsonPrimitive()) {
129+
JsonElement conv = convValue(e.getAsJsonPrimitive(), comment, knowledge);
130+
if (conv != null) {
131+
obj.add(prop, conv);
132+
}
133+
} else if (e.isJsonObject()) {
134+
LOG.info("property:" + prop + " is object.");
135+
JsonObject child = e.getAsJsonObject();
136+
buildJson(child, comment, knowledge);
137+
} else if (e.isJsonArray()) {
138+
JsonArray array = e.getAsJsonArray();
139+
for (int i = 0; i < array.size(); i++) {
140+
JsonElement item = array.get(i);
141+
if (item.isJsonObject()) {
142+
JsonObject child = item.getAsJsonObject();
143+
buildJson(child, comment, knowledge);
144+
}
145+
}
146+
}
147+
}
148+
}
149+
private JsonElement convValue(JsonPrimitive primitive, CommentsEntity comment, KnowledgesEntity knowledge) {
150+
JsonElement conv = convValueOnComment(primitive, comment, knowledge);
151+
if (conv != null) {
152+
return conv;
153+
}
154+
return knowledgeUpdateWebHookNotification.convValue(primitive, knowledge, QueueNotification.TYPE_KNOWLEDGE_UPDATE, false);
155+
}
156+
private JsonElement convValueOnComment(JsonPrimitive primitive, CommentsEntity comment, KnowledgesEntity knowledge) {
157+
if (!primitive.isString()) {
158+
return null;
159+
}
160+
String val = primitive.getAsString();
161+
if (!val.startsWith("{") || !val.endsWith("}")) {
162+
return null;
163+
}
164+
String item = val.substring(1, val.length() - 1);
165+
String option = "";
166+
if (item.indexOf(",") != -1) {
167+
option = item.substring(item.indexOf(",") + 1);
168+
item = item.substring(0, item.indexOf(","));
169+
}
170+
LOG.debug(item + " : " + option);
171+
String[] sp = item.split("\\.");
172+
LOG.debug(sp);
173+
if (sp.length == 2) {
174+
String name = sp[0];
175+
String prop = sp[1];
176+
if (name.equals("comment")) {
177+
if (prop.equals("text")) {
178+
/** This code make JSON to send Slack */
179+
String linktop = "<";
180+
String linkpipe = "|";
181+
String linkend = ">";
182+
183+
/** This code make JSON to send Slack */
184+
StringBuffer SendBuff = new StringBuffer();
185+
SendBuff.append(linktop);
186+
SendBuff.append(NotifyLogic.get().makeURL(knowledge.getKnowledgeId()));
187+
SendBuff.append(linkpipe);
188+
SendBuff.append(knowledge.getTitle());
189+
SendBuff.append(linkend);
190+
String SendString = SendBuff.toString();
191+
return new JsonPrimitive(SendString);
192+
}
193+
return knowledgeUpdateWebHookNotification.convValue(comment, prop, option);
194+
}
195+
}
196+
return null;
197+
}
71198
/**
72199
* コメントのjsonデータを取得する
73200
*
74201
* @param comment
75202
* @param knowledge
76203
* @return
204+
* @deprecated
77205
*/
78206
public Map<String, Object> getCommentData(CommentsEntity comment, KnowledgesEntity knowledge) {
79207
Map<String, Object> jsonObject = new HashMap<String, Object>();
@@ -117,6 +245,7 @@ public Map<String, Object> getCommentData(CommentsEntity comment, KnowledgesEnti
117245
jsonObject.put("knowledge", KnowledgeUpdateWebHookNotification.get().getKnowledgeData(knowledge, null));
118246
return jsonObject;
119247
}
248+
120249

121250

122251
}

0 commit comments

Comments
 (0)