-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature-#1775][connector][http] http supports offline mode
- Loading branch information
gaoliang
committed
Feb 4, 2024
1 parent
786f68a
commit fd59a73
Showing
14 changed files
with
680 additions
and
14 deletions.
There are no files selected for viewing
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
81 changes: 81 additions & 0 deletions
81
...ttp/src/main/java/com/dtstack/chunjun/connector/http/client/OfflineJsonResponseParse.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,81 @@ | ||
package com.dtstack.chunjun.connector.http.client; | ||
|
||
import com.dtstack.chunjun.connector.http.common.HttpRestConfig; | ||
import com.dtstack.chunjun.converter.AbstractRowConverter; | ||
import com.dtstack.chunjun.util.GsonUtil; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.google.gson.Gson; | ||
import com.jayway.jsonpath.JsonPath; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
/** @Description 离线任务 @Author lianggao @Date 2023/6/1 下午5:50 */ | ||
public class OfflineJsonResponseParse extends ResponseParse { | ||
|
||
private String responseValue; | ||
private HttpRequestParam requestParam; | ||
private final Gson gson; | ||
private Iterator<Object> iterator; | ||
/** true:single true:array false:single false:array */ | ||
String parserFlag; | ||
|
||
String jsonPath; | ||
|
||
public OfflineJsonResponseParse(HttpRestConfig config, AbstractRowConverter converter) { | ||
super(config, converter); | ||
this.gson = GsonUtil.GSON; | ||
this.jsonPath = config.getJsonPath(); | ||
this.parserFlag = !StringUtils.isBlank(jsonPath) + ":" + config.getReturnedDataType(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return iterator.hasNext(); | ||
} | ||
|
||
@Override | ||
public ResponseValue next() throws Exception { | ||
String rowJson = gson.toJson(iterator.next()); | ||
Map<String, Object> data = | ||
DefaultRestHandler.gson.fromJson(rowJson, GsonUtil.gsonMapTypeToken); | ||
return new ResponseValue(converter.toInternal(data), requestParam, responseValue); | ||
} | ||
|
||
@Override | ||
public void parse(String responseValue, int responseStatus, HttpRequestParam requestParam) { | ||
this.responseValue = responseValue; | ||
this.requestParam = requestParam; | ||
runParseJson(); | ||
} | ||
|
||
public void runParseJson() { | ||
switch (parserFlag) { | ||
case "false:array": | ||
iterator = JsonPath.read(responseValue, "$"); | ||
break; | ||
case "true:single": | ||
Object read = JsonPath.read(responseValue, jsonPath); | ||
iterator = Lists.newArrayList(read).iterator(); | ||
break; | ||
case "true:array": | ||
Object eval = JsonPath.read(responseValue, jsonPath); | ||
if (eval instanceof net.minidev.json.JSONArray) { | ||
iterator = ((net.minidev.json.JSONArray) eval).iterator(); | ||
} else { | ||
// 如果为null 则直接报错,返回解析错误的数据 | ||
if (eval == null) { | ||
throw new RuntimeException( | ||
"response parsing is incorrect Please check the conf ,get response is" | ||
+ responseValue); | ||
} | ||
iterator = Lists.newArrayList(eval).iterator(); | ||
} | ||
break; | ||
default: | ||
Lists.newArrayList(responseValue); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.