TeslaCN commented on a change in pull request #1462:
URL: 
https://github.com/apache/shardingsphere-elasticjob/pull/1462#discussion_r489995810



##########
File path: 
elasticjob-error-handler/elasticjob-error-handler-dingtalk/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/impl/fixture/DingtalkInternalController.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.apache.shardingsphere.elasticjob.error.handler.impl.fixture;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableMap;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.elasticjob.infra.json.GsonFactory;
+import org.apache.shardingsphere.elasticjob.restful.Http;
+import org.apache.shardingsphere.elasticjob.restful.RestfulController;
+import org.apache.shardingsphere.elasticjob.restful.annotation.Mapping;
+import org.apache.shardingsphere.elasticjob.restful.annotation.Param;
+import org.apache.shardingsphere.elasticjob.restful.annotation.ParamSource;
+import org.apache.shardingsphere.elasticjob.restful.annotation.RequestBody;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import java.io.UnsupportedEncodingException;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.util.Base64;
+import java.util.Map;
+
+@Slf4j
+public final class DingtalkInternalController implements RestfulController {
+    
+    private static final String ACCESS_TOKEN = 
"42eead064e81ce81fc6af2c107fbe10a4339a3d40a7db8abf5b34d8261527a3f";
+    
+    private static final String KEYWORD = "keyword";
+    
+    private static final String SECRET = 
"SEC0b0a6b13b6823b95737dd83491c23adee5d8a7a649899a12217e038eddc84ff4";
+    
+    
+    /**
+     * Send Dingtalk message.
+     *
+     * @param accessToken access token
+     * @param timestamp timestamp
+     * @param sign sign
+     * @param body body
+     * @return Send Result.
+     */
+    @SneakyThrows
+    @Mapping(method = Http.POST, path = "/send")
+    public String send(@Param(name = "access_token", source = 
ParamSource.QUERY) final String accessToken,
+                       @Param(name = "timestamp", source = ParamSource.QUERY, 
required = false) final Long timestamp,
+                       @Param(name = "sign", source = ParamSource.QUERY, 
required = false) final String sign,
+                       @RequestBody final Map<String, Object> body) {
+        if (!ACCESS_TOKEN.equals(accessToken)) {
+            return GsonFactory.getGson().toJson(ImmutableMap.of("errcode", 
300001, "errmsg", "token is not exist"));
+        }
+        String content = 
Map.class.cast(body.get("text")).get("content").toString();
+        if (!content.startsWith(KEYWORD)) {
+            return GsonFactory.getGson().toJson(ImmutableMap.of("errcode", 
310000, "errmsg", "keywords not in content, more: 
[https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq]";));
+        }
+        if (!Strings.isNullOrEmpty(sign)) {
+            Preconditions.checkNotNull(timestamp);
+            String checkSign = sign(timestamp);
+            if (!sign.equals(checkSign)) {
+                return GsonFactory.getGson().toJson(ImmutableMap.of("errcode", 
310000, "errmsg", "sign not match, more: 
[https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq]";));
+            }
+        }
+        return GsonFactory.getGson().toJson(ImmutableMap.of("errcode", 0, 
"errmsg", "ok"));
+    }
+    
+    private String sign(final Long timestamp) throws NoSuchAlgorithmException, 
UnsupportedEncodingException, InvalidKeyException {
+        String stringToSign = timestamp + "\n" + SECRET;
+        System.out.println(stringToSign);
+        Mac mac = Mac.getInstance("HmacSHA256");
+        mac.init(new SecretKeySpec(SECRET.getBytes("UTF-8"), "HmacSHA256"));
+        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
+        return new String(Base64.getEncoder().encode(signData), "UTF-8");

Review comment:
       Consider using `java.nio.charset.StandardCharsets#UTF_8`

##########
File path: 
elasticjob-error-handler/elasticjob-error-handler-dingtalk/src/main/java/org/apache/shardingsphere/elasticjob/error/handler/impl/DingtalkJobErrorHandler.java
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.apache.shardingsphere.elasticjob.error.handler.impl;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableMap;
+import com.google.gson.JsonObject;
+import lombok.Setter;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import 
org.apache.shardingsphere.elasticjob.error.handler.config.DingtalkConfiguration;
+import 
org.apache.shardingsphere.elasticjob.error.handler.env.DingtalkEnvironment;
+import 
org.apache.shardingsphere.elasticjob.infra.handler.error.JobErrorHandler;
+import org.apache.shardingsphere.elasticjob.infra.json.GsonFactory;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URLEncoder;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.util.Base64;
+import java.util.Collections;
+
+/**
+ * Job error handler for dingtalk error message.
+ */
+@Slf4j
+public final class DingtalkJobErrorHandler implements JobErrorHandler {
+    
+    @Setter
+    private DingtalkConfiguration dingtalkConfiguration;
+    
+    @SneakyThrows
+    @Override
+    public void handleException(final String jobName, final Throwable cause) {
+        if (null == dingtalkConfiguration) {
+            dingtalkConfiguration = 
DingtalkEnvironment.getInstance().getDingtalkConfiguration();
+        }
+        CloseableHttpClient httpclient = HttpClients.createDefault();
+        try {
+            HttpPost httpPost = new HttpPost(getUrl());
+            String paramJson = getParamJson(getMsg(jobName, cause));
+            StringEntity entity = new StringEntity(paramJson, "UTF-8");
+            entity.setContentEncoding("UTF-8");
+            entity.setContentType("application/json");

Review comment:
       Maybe there are some constants can be used to get the String?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to