-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
603 additions
and
12 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
core/datacap-common/src/main/java/io/edurt/datacap/common/response/SignResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.edurt.datacap.common.response; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.ToString; | ||
|
||
@Data | ||
@Builder | ||
@ToString | ||
public class SignResponse | ||
{ | ||
private Long timestamp; | ||
private String sign; | ||
} |
39 changes: 39 additions & 0 deletions
39
core/datacap-common/src/main/java/io/edurt/datacap/common/utils/SignUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.edurt.datacap.common.utils; | ||
|
||
import io.edurt.datacap.common.response.SignResponse; | ||
|
||
import javax.crypto.Mac; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
public class SignUtils | ||
{ | ||
private SignUtils() | ||
{} | ||
|
||
/** | ||
* Generates a signature using the provided secret. | ||
* | ||
* @param secret the secret key used for signing | ||
* @return the SignResponse object containing the generated signature and timestamp | ||
* @throws Exception if an error occurs during the signing process | ||
*/ | ||
public static SignResponse sign(String secret) | ||
throws Exception | ||
{ | ||
Long timestamp = System.currentTimeMillis(); | ||
String stringToSign = timestamp + "\n" + secret; | ||
Mac mac = Mac.getInstance("HmacSHA256"); | ||
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); | ||
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8)); | ||
Base64.Encoder encoder = Base64.getEncoder(); | ||
String sign = URLEncoder.encode(encoder.encodeToString(signData), "UTF-8"); | ||
return SignResponse.builder() | ||
.timestamp(timestamp) | ||
.sign(sign) | ||
.build(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
core/datacap-common/src/test/java/io/edurt/datacap/common/utils/SignUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.edurt.datacap.common.utils; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class SignUtilsTest | ||
{ | ||
@Test | ||
public void testSign() | ||
throws Exception | ||
{ | ||
Assert.assertNotNull(SignUtils.sign("SSSS") | ||
.getSign()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>io.edurt.datacap</groupId> | ||
<artifactId>datacap</artifactId> | ||
<version>2024.03.5-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>datacap-notify-dingtalk</artifactId> | ||
<description>DataCap - Parser for dingtalk</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.edurt.datacap</groupId> | ||
<artifactId>datacap-notify-spi</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.edurt.datacap</groupId> | ||
<artifactId>datacap-http</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.jetbrains.dokka</groupId> | ||
<artifactId>dokka-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
15 changes: 15 additions & 0 deletions
15
...atacap-notify-dingtalk/src/main/kotlin/io/edurt/datacap/notify/dingtalk/DingTalkModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.edurt.datacap.notify.dingtalk | ||
|
||
import com.google.inject.multibindings.Multibinder | ||
import io.edurt.datacap.notify.Notify | ||
import io.edurt.datacap.notify.NotifyModule | ||
|
||
class DingTalkModule : NotifyModule() | ||
{ | ||
override fun configure() | ||
{ | ||
Multibinder.newSetBinder(this.binder(), Notify::class.java) | ||
.addBinding() | ||
.to(DingTalkNotify::class.java) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...atacap-notify-dingtalk/src/main/kotlin/io/edurt/datacap/notify/dingtalk/DingTalkNotify.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.edurt.datacap.notify.dingtalk | ||
|
||
import io.edurt.datacap.notify.Notify | ||
import io.edurt.datacap.notify.model.NotifyRequest | ||
import io.edurt.datacap.notify.model.NotifyResponse | ||
|
||
class DingTalkNotify : Notify | ||
{ | ||
override fun send(request: NotifyRequest): NotifyResponse | ||
{ | ||
return DingTalkUtils.send(request) | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...datacap-notify-dingtalk/src/main/kotlin/io/edurt/datacap/notify/dingtalk/DingTalkUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.edurt.datacap.notify.dingtalk | ||
|
||
import io.edurt.datacap.common.utils.JsonUtils | ||
import io.edurt.datacap.common.utils.SignUtils | ||
import io.edurt.datacap.lib.http.HttpClient | ||
import io.edurt.datacap.lib.http.HttpConfigure | ||
import io.edurt.datacap.lib.http.HttpMethod | ||
import io.edurt.datacap.notify.NotifyType | ||
import io.edurt.datacap.notify.dingtalk.model.ReturnModel | ||
import io.edurt.datacap.notify.dingtalk.model.TextModel | ||
import io.edurt.datacap.notify.model.NotifyRequest | ||
import io.edurt.datacap.notify.model.NotifyResponse | ||
import org.apache.commons.lang3.StringUtils.isNotEmpty | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory.getLogger | ||
|
||
object DingTalkUtils | ||
{ | ||
private val log: Logger = getLogger(DingTalkUtils::class.java) | ||
|
||
@JvmStatic | ||
fun send(request: NotifyRequest): NotifyResponse | ||
{ | ||
val configure = HttpConfigure() | ||
configure.autoConnected = false | ||
configure.retry = 0 | ||
configure.protocol = "https" | ||
configure.host = "oapi.dingtalk.com" | ||
configure.port = 443 | ||
configure.path = "robot/send" | ||
configure.method = HttpMethod.POST | ||
|
||
val params = mutableMapOf("access_token" to request.access) | ||
if (isNotEmpty(request.secret)) | ||
{ | ||
val signResponse = SignUtils.sign(request.secret) | ||
log.info("Sign response: ${JsonUtils.toJSON(signResponse)}") | ||
params["sign"] = signResponse.sign | ||
params["timestamp"] = signResponse.timestamp.toString() | ||
} | ||
configure.params = params | ||
log.info("Notify request params: ${JsonUtils.toJSON(params)}") | ||
|
||
val text = TextModel() | ||
text.content = request.content | ||
configure.body = JsonUtils.toJSON(mapOf("text" to text, "msgtype" to formatMessageType(request))) | ||
log.info("Notify request body: ${configure.body}") | ||
|
||
val client = HttpClient(configure) | ||
val returnModel = JsonUtils.toObject(client.execute(), ReturnModel::class.java) | ||
val response = NotifyResponse() | ||
if (returnModel.code == 0) | ||
{ | ||
response.successful = true | ||
response.message = null | ||
} | ||
else | ||
{ | ||
response.successful = false | ||
response.message = returnModel.message | ||
} | ||
return response | ||
} | ||
|
||
private fun formatMessageType(request: NotifyRequest): String | ||
{ | ||
if (request.type == NotifyType.TEXT) | ||
{ | ||
return "text" | ||
} | ||
else | ||
{ | ||
return "markdown" | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...cap-notify-dingtalk/src/main/kotlin/io/edurt/datacap/notify/dingtalk/model/ReturnModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.edurt.datacap.notify.dingtalk.model | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
class ReturnModel | ||
{ | ||
@JsonProperty(value = "errcode") | ||
var code: Number? = null | ||
|
||
@JsonProperty(value = "errmsg") | ||
var message: String? = null | ||
} |
6 changes: 6 additions & 0 deletions
6
...tacap-notify-dingtalk/src/main/kotlin/io/edurt/datacap/notify/dingtalk/model/TextModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.edurt.datacap.notify.dingtalk.model | ||
|
||
class TextModel | ||
{ | ||
var content: String? = null | ||
} |
1 change: 1 addition & 0 deletions
1
...notify-dingtalk/src/main/resources/META-INF/services/io.edurt.datacap.notify.NotifyModule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.edurt.datacap.notify.dingtalk.DingTalkModule |
Oops, something went wrong.