Skip to content

Commit

Permalink
New feature (#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed May 23, 2024
2 parents 7cdb09e + d90cbd2 commit 0bb96c4
Show file tree
Hide file tree
Showing 32 changed files with 603 additions and 12 deletions.
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;
}
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();
}
}
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());
}
}
6 changes: 6 additions & 0 deletions core/datacap-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@
<artifactId>datacap-fs-alioss</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Notify -->
<dependency>
<groupId>io.edurt.datacap</groupId>
<artifactId>datacap-notify-dingtalk</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.inject.Injector;
import io.edurt.datacap.executor.ExecutorManager;
import io.edurt.datacap.fs.FsManager;
import io.edurt.datacap.notify.NotifyManager;
import io.edurt.datacap.parser.ParserManager;
import io.edurt.datacap.scheduler.ScheduleManager;
import io.edurt.datacap.spi.PluginLoader;
Expand All @@ -20,6 +21,7 @@ public Injector injector()
new FsManager(),
new ParserManager(),
new ScheduleManager(),
new ExecutorManager());
new ExecutorManager(),
new NotifyManager());
}
}
6 changes: 5 additions & 1 deletion core/datacap-ui/src/views/auth/signin/AuthSignin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ export default defineComponent({
captchaImage.value = response.data.image
}
})
.finally(() => captchaLoading.value = false)
.finally(() => {
captchaLoading.value = false
loading.value = false
})
}
const onSubmit = handleSubmit(() => {
Expand Down Expand Up @@ -198,6 +201,7 @@ export default defineComponent({
methods: {
handlerInitialize()
{
this.loading = true
this.refererCaptcha()
}
}
Expand Down
6 changes: 5 additions & 1 deletion core/datacap-ui/src/views/auth/signup/AuthSignup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ export default defineComponent({
captchaImage.value = response.data.image
}
})
.finally(() => captchaLoading.value = false)
.finally(() => {
captchaLoading.value = false
loading.value = false
})
}
const onSubmit = handleSubmit(() => {
Expand Down Expand Up @@ -193,6 +196,7 @@ export default defineComponent({
methods: {
handlerInitialize()
{
this.loading = true
this.refererCaptcha()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.edurt.datacap.lib.http;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.extern.slf4j.Slf4j;
import okhttp3.ConnectionPool;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
Expand All @@ -13,7 +14,8 @@
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@SuppressFBWarnings(value = {"NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"},
@Slf4j
@SuppressFBWarnings(value = {"NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE"},
justification = "I prefer to suppress these FindBugs warnings")
public class HttpClient
{
Expand Down Expand Up @@ -66,25 +68,31 @@ private String get()
builder.addQueryParameter(key, configure.getParams().get(key));
}
}
log.info("Request url {}", builder.build());
Request request = new Request.Builder()
.addHeader("Accept-Encoding", "identity")
.url(builder.build().toString()).build();
.url(builder.build().toString())
.build();
return execute(request);
}

private String post()
{
RequestBody requestBody = RequestBody.create(configure.getMediaType(), configure.getBody());
HttpUrl.Builder builder = HttpUrl.parse(this.formatJdbcUrl()).newBuilder();
HttpUrl.Builder builder = HttpUrl.parse(this.formatJdbcUrl())
.newBuilder();

// Adding Path Parameters
if (ObjectUtils.isNotEmpty(configure.getParams())) {
for (String key : configure.getParams().keySet()) {
builder.addQueryParameter(key, configure.getParams().get(key));
builder.addEncodedQueryParameter(key, configure.getParams()
.get(key));
}
}

Request request = new Request.Builder().post(requestBody)
log.info("Request url {}", builder.build());
Request request = new Request.Builder()
.post(requestBody)
.addHeader("Accept-Encoding", "identity")
.url(builder.build().toString()).build();
return execute(request);
Expand All @@ -93,12 +101,13 @@ private String post()
private String execute(Request request)
{
String responseBody = null;
try {
Response response = okHttpClient.newCall(request).execute();
responseBody = response.body().string();
try (Response response = okHttpClient.newCall(request)
.execute()) {
responseBody = response.body()
.string();
}
catch (IOException | NullPointerException e) {
e.printStackTrace();
log.warn("Request error", e);
}
return responseBody;
}
Expand Down
36 changes: 36 additions & 0 deletions notify/datacap-notify-dingtalk/pom.xml
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>
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)
}
}
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)
}
}
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"
}
}
}
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
}
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.edurt.datacap.notify.dingtalk.DingTalkModule

0 comments on commit 0bb96c4

Please sign in to comment.