Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
yanhom1314 committed Jun 25, 2023
1 parent 4f66c9b commit 31c878b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
/*
* 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.dromara.dynamictp.core.notifier.base;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import lombok.val;
import org.dromara.dynamictp.common.entity.NotifyPlatform;

import java.util.Objects;
Expand All @@ -15,18 +32,17 @@
* @since 1.1.3
*/
@Slf4j
public abstract class AbstractHttpNotifier extends AbstractNotifier{
public abstract class AbstractHttpNotifier extends AbstractNotifier {

/**
* Message sending mode
* @author kyao
* @param platform
* @param content
* @param platform platform
* @param content content
*/
@Override
protected void sendMode(NotifyPlatform platform, String content) {
var url = buildUrl(platform);
var msgBody = buildMsgBody(platform, content);
val url = buildUrl(platform);
val msgBody = buildMsgBody(platform, content);
HttpResponse response = HttpRequest.post(url).body(msgBody).execute();
if (Objects.nonNull(response)) {
log.info("DynamicTp notify, {} send success, response: {}, request: {}",
Expand All @@ -36,18 +52,15 @@ protected void sendMode(NotifyPlatform platform, String content) {

/**
* build http message body
* @author hanli
* @param platform
* @param content
* @param platform platform
* @param content content
* @return java.lang.String
*/
protected abstract String buildMsgBody(NotifyPlatform platform, String content);

/**
* build http url
* @author hanli
* @date 2023/6/25 12:09 PM
* @param platform
* @param platform platform
* @return java.lang.String
*/
protected abstract String buildUrl(NotifyPlatform platform);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* 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.dromara.dynamictp.core.notifier.base;

import cn.hutool.core.util.StrUtil;
Expand Down Expand Up @@ -29,9 +46,8 @@ public final void send(NotifyPlatform platform, String content) {

/**
* Message sending mode
* @author kyao
* @param platform
* @param content
* @param platform platform
* @param content content
*/
protected abstract void sendMode(NotifyPlatform platform, String content);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

package org.dromara.dynamictp.core.notifier.base;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.dromara.dynamictp.common.constant.DingNotifyConst;
import org.dromara.dynamictp.common.em.NotifyPlatformEnum;
import org.dromara.dynamictp.common.entity.MarkdownReq;
import org.dromara.dynamictp.common.entity.NotifyPlatform;
import org.dromara.dynamictp.common.em.NotifyPlatformEnum;
import org.dromara.dynamictp.common.util.DingSignUtil;
import org.dromara.dynamictp.common.util.JsonUtil;
import org.dromara.dynamictp.common.util.TimeUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

Expand All @@ -37,6 +37,7 @@
* DingNotifier related
*
* @author yanhom
* @author Kyao
* @since 1.0.0
**/
@Slf4j
Expand All @@ -47,21 +48,6 @@ public String platform() {
return NotifyPlatformEnum.DING.name().toLowerCase();
}

/**
* Build target url.
* @param secret secret
* @param accessToken accessToken
* @return url
*/
private String getTargetUrl(String secret, String accessToken) {
if (StringUtils.isBlank(secret)) {
return DingNotifyConst.DING_WEBHOOK + accessToken;
}
long timestamp = TimeUtil.currentTimeMillis();
String sign = DingSignUtil.dingSign(secret, timestamp);
return DingNotifyConst.DING_WEBHOOK + accessToken + "&timestamp=" + timestamp + "&sign=" + sign;
}

@Override
protected String buildMsgBody(NotifyPlatform platform, String content) {
MarkdownReq.Markdown markdown = new MarkdownReq.Markdown();
Expand All @@ -87,4 +73,19 @@ protected String buildMsgBody(NotifyPlatform platform, String content) {
protected String buildUrl(NotifyPlatform platform) {
return getTargetUrl(platform.getSecret(), platform.getUrlKey());
}

/**
* Build target url.
* @param secret secret
* @param accessToken accessToken
* @return url
*/
private String getTargetUrl(String secret, String accessToken) {
if (StringUtils.isBlank(secret)) {
return DingNotifyConst.DING_WEBHOOK + accessToken;
}
long timestamp = TimeUtil.currentTimeMillis();
String sign = DingSignUtil.dingSign(secret, timestamp);
return DingNotifyConst.DING_WEBHOOK + accessToken + "&timestamp=" + timestamp + "&sign=" + sign;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

package org.dromara.dynamictp.core.notifier.base;

import org.dromara.dynamictp.common.constant.LarkNotifyConst;
import org.dromara.dynamictp.common.entity.NotifyPlatform;
import org.dromara.dynamictp.common.em.NotifyPlatformEnum;
import org.dromara.dynamictp.common.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.dromara.dynamictp.common.constant.LarkNotifyConst;
import org.dromara.dynamictp.common.em.NotifyPlatformEnum;
import org.dromara.dynamictp.common.entity.NotifyPlatform;
import org.dromara.dynamictp.common.util.TimeUtil;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
Expand Down Expand Up @@ -79,14 +79,15 @@ protected String genSign(String secret, Long timestamp) throws NoSuchAlgorithmEx

@Override
protected String buildMsgBody(NotifyPlatform platform, String content) {
if (StringUtils.isNotBlank(platform.getSecret())) {
try {
val secondsTimestamp = TimeUtil.currentTimeSeconds();
val sign = genSign(platform.getSecret(), secondsTimestamp);
content = content.replace(SIGN_REPLACE, String.format(SIGN_PARAM, secondsTimestamp, sign));
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
log.error("DynamicTp notify, lark generate signature failed...", e);
}
if (StringUtils.isBlank(platform.getSecret())) {
return content;
}
try {
val secondsTimestamp = TimeUtil.currentTimeSeconds();
val sign = genSign(platform.getSecret(), secondsTimestamp);
content = content.replace(SIGN_REPLACE, String.format(SIGN_PARAM, secondsTimestamp, sign));
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
log.error("DynamicTp notify, lark generate signature failed...", e);
}
return content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@

package org.dromara.dynamictp.core.notifier.base;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.dromara.dynamictp.common.constant.WechatNotifyConst;
import org.dromara.dynamictp.common.em.NotifyPlatformEnum;
import org.dromara.dynamictp.common.entity.MarkdownReq;
import org.dromara.dynamictp.common.entity.NotifyPlatform;
import org.dromara.dynamictp.common.em.NotifyPlatformEnum;
import org.dromara.dynamictp.common.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
* WechatNotifier related
Expand Down

0 comments on commit 31c878b

Please sign in to comment.