You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by te...@apache.org on 2020/08/30 04:00:15 UTC

[shardingsphere-elasticjob] branch master updated: The property Content-Type of HTTP job only works with x-www-form-urlencode (#1422) (#1425)

This is an automated email from the ASF dual-hosted git repository.

technoboy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git


The following commit(s) were added to refs/heads/master by this push:
     new ecd82e0  The property Content-Type of HTTP job only works with x-www-form-urlencode (#1422) (#1425)
ecd82e0 is described below

commit ecd82e00d6a2b8d6ed2ff91ccd5a59f8e7739024
Author: Tboy <gu...@immomo.com>
AuthorDate: Sun Aug 30 12:00:04 2020 +0800

    The property Content-Type of HTTP job only works with x-www-form-urlencode (#1422) (#1425)
---
 .../elasticjob-lite/usage/job-api/job-interface.cn.md        |  4 ++--
 .../elasticjob-lite/usage/job-api/job-interface.en.md        |  4 ++--
 .../elasticjob/http/executor/HttpJobExecutor.java            | 12 +++++++-----
 3 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/docs/content/user-manual/elasticjob-lite/usage/job-api/job-interface.cn.md b/docs/content/user-manual/elasticjob-lite/usage/job-api/job-interface.cn.md
index 23af1f9..1dd2a0d 100644
--- a/docs/content/user-manual/elasticjob-lite/usage/job-api/job-interface.cn.md
+++ b/docs/content/user-manual/elasticjob-lite/usage/job-api/job-interface.cn.md
@@ -107,7 +107,7 @@ sharding execution context is {"jobName":"scriptElasticDemoJob","shardingTotalCo
 ## HTTP作业
 
 可通过属性配置`http.url`,`http.method`,`http.data`等配置待请求的http信息。
-如果设置了`http.data`, 分片信息也将以`shardingContext`为key传递到url接口,值为json格式。
+分片信息以Header形式传递,key为`shardingContext`,值为json格式。
 
 ```java
 
@@ -129,7 +129,7 @@ public class HttpJobMain {
 public class HttpJobController {
     
     @RequestMapping(path = "/execute", method = RequestMethod.POST)
-    public void execute(String source, String shardingContext) {
+    public void execute(String source, @RequestHeader String shardingContext) {
         log.info("execute from source : {}, shardingContext : {}", source, shardingContext);
     }
 }
diff --git a/docs/content/user-manual/elasticjob-lite/usage/job-api/job-interface.en.md b/docs/content/user-manual/elasticjob-lite/usage/job-api/job-interface.en.md
index 4fa7217..4ef55e6 100644
--- a/docs/content/user-manual/elasticjob-lite/usage/job-api/job-interface.en.md
+++ b/docs/content/user-manual/elasticjob-lite/usage/job-api/job-interface.en.md
@@ -107,7 +107,7 @@ sharding execution context is {"jobName":"scriptElasticDemoJob","shardingTotalCo
 ## HTTP job
 
 The http information to be requested can be configured through the properties of `http.url`, `http.method`, `http.data`, etc.
-If `http.data` is set, sharding information will also be passed to the url interface with `shardingContext` as the key, and the value is in json format.
+Sharding information is transmitted in the form of Header, the key is `shardingContext`, and the value is in json format.
 
 ```java
 
@@ -129,7 +129,7 @@ public class HttpJobMain {
 public class HttpJobController {
     
     @RequestMapping(path = "/execute", method = RequestMethod.POST)
-    public void execute(String source, String shardingContext) {
+    public void execute(String source, @RequestHeader String shardingContext) {
         log.info("execute from source : {}, shardingContext : {}", source, shardingContext);
     }
 }
diff --git a/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/main/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutor.java b/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/main/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutor.java
index 63b735d..bea3f8c 100644
--- a/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/main/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutor.java
+++ b/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/main/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutor.java
@@ -37,6 +37,7 @@ import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
 import java.util.Properties;
 
 /**
@@ -60,13 +61,10 @@ public final class HttpJobExecutor implements TypedJobItemExecutor {
             if (!Strings.isNullOrEmpty(httpParam.getContentType())) {
                 connection.setRequestProperty("Content-Type", httpParam.getContentType());
             }
+            connection.setRequestProperty(HttpJobProperties.SHARDING_CONTEXT_KEY, GsonFactory.getGson().toJson(shardingContext));
             connection.connect();
             String data = httpParam.getData();
-            if (!Strings.isNullOrEmpty(data)) {
-                StringBuilder builder = new StringBuilder(data);
-                builder.append("&").append(HttpJobProperties.SHARDING_CONTEXT_KEY);
-                builder.append("=").append(GsonFactory.getGson().toJson(shardingContext));
-                data = builder.toString();
+            if (isWriteMethod(httpParam.getMethod()) && !Strings.isNullOrEmpty(data)) {
                 DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
                 dataOutputStream.write(data.getBytes(StandardCharsets.UTF_8));
                 dataOutputStream.flush();
@@ -114,6 +112,10 @@ public final class HttpJobExecutor implements TypedJobItemExecutor {
         return new HttpParam(url, method, data, connectTimeout, readTimeout, contentType);
     }
     
+    private boolean isWriteMethod(final String method) {
+        return Arrays.asList("POST", "PUT", "DELETE").contains(method.toUpperCase());
+    }
+    
     @Override
     public String getType() {
         return "HTTP";