Skip to content

Commit 973c094

Browse files
committed
Restapi改造
1 parent b613909 commit 973c094

File tree

15 files changed

+238
-76
lines changed

15 files changed

+238
-76
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.dtstack.flinkx.restapi.common;
2+
3+
import com.dtstack.flinkx.util.GsonUtil;
4+
5+
import java.util.Map;
6+
import java.util.Objects;
7+
8+
public class MapUtils {
9+
10+
11+
public static Object getData(Map<String, Object> data, String[] names) {
12+
Map<String, Object> tempHashMap = data;
13+
for (int i = 0; i < names.length; i++) {
14+
if (tempHashMap.containsKey(names[i]) && i != names.length - 1) {
15+
if (Objects.isNull(tempHashMap.get(names[i]))) {
16+
return null;
17+
}
18+
if (tempHashMap.get(names[i]) instanceof Map) {
19+
tempHashMap = (Map<String, Object>) tempHashMap.get(names[i]);
20+
} else if (tempHashMap.get(names[i]) instanceof String) {
21+
try {
22+
tempHashMap = GsonUtil.GSON.fromJson((String) tempHashMap.get(names[i]), GsonUtil.gsonMapTypeToken);
23+
} catch (Exception e) {
24+
return null;
25+
}
26+
} else {
27+
return null;
28+
}
29+
} else if (i == names.length - 1) {
30+
return tempHashMap.get(names[i]);
31+
} else {
32+
return null;
33+
}
34+
}
35+
return null;
36+
}
37+
}

flinkx-restapi/flinkx-restapi-core/src/main/java/com/dtstack/flinkx/restapi/common/exception.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

flinkx-restapi/flinkx-restapi-core/src/main/java/com/dtstack/flinkx/restapi/common/handler/ReadRecordException.java renamed to flinkx-restapi/flinkx-restapi-core/src/main/java/com/dtstack/flinkx/restapi/common/exception/ReadRecordException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dtstack.flinkx.restapi.common.handler;
1+
package com.dtstack.flinkx.restapi.common.exception;
22

33
public class ReadRecordException extends RuntimeException {
44
public ReadRecordException() {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.dtstack.flinkx.restapi.common.exception;
2+
3+
public class ResponseBreakException extends RuntimeException{
4+
public ResponseBreakException() {
5+
}
6+
7+
public ResponseBreakException(String message) {
8+
super(message);
9+
}
10+
11+
public ResponseBreakException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public ResponseBreakException(Throwable cause) {
16+
super(cause);
17+
}
18+
19+
public ResponseBreakException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
20+
super(message, cause, enableSuppression, writableStackTrace);
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.dtstack.flinkx.restapi.common.exception;
2+
3+
public class ResponseRetryException extends RuntimeException {
4+
public ResponseRetryException() {
5+
}
6+
7+
public ResponseRetryException(String message) {
8+
super(message);
9+
}
10+
11+
public ResponseRetryException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public ResponseRetryException(Throwable cause) {
16+
super(cause);
17+
}
18+
19+
public ResponseRetryException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
20+
super(message, cause, enableSuppression, writableStackTrace);
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.dtstack.flinkx.restapi.common.handler;
2+
3+
import com.dtstack.flinkx.restapi.common.MapUtils;
4+
import com.dtstack.flinkx.restapi.common.exception.ResponseBreakException;
5+
import com.dtstack.flinkx.restapi.common.exception.ResponseRetryException;
6+
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
public class BreakDataHandler extends DataHandler {
11+
12+
public BreakDataHandler(String key, Set<String> value) {
13+
super(key, value);
14+
}
15+
16+
@Override
17+
public void execute(Map<String, Object> responseData) {
18+
String[] strings = new String[0];
19+
strings[0] = key;
20+
Object data = MapUtils.getData(responseData, strings);
21+
if (value.contains(data.toString())) {
22+
throw new ResponseBreakException("key:"+key+" contains"+data.toString()+" ,need break");
23+
}
24+
25+
}
26+
}

flinkx-restapi/flinkx-restapi-core/src/main/java/com/dtstack/flinkx/restapi/common/handler/Handler.java renamed to flinkx-restapi/flinkx-restapi-core/src/main/java/com/dtstack/flinkx/restapi/common/handler/DataHandler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import java.util.Map;
44
import java.util.Set;
55

6-
public abstract class Handler {
6+
public abstract class DataHandler {
77

8-
private String key;
8+
protected String key;
99

10-
private Set<String> value;
10+
protected Set<String> value;
1111

12-
public Handler(String key, Set<String> value) {
12+
public DataHandler(String key, Set<String> value) {
1313
this.key = key;
1414
this.value = value;
1515
}
@@ -19,4 +19,5 @@ public boolean isPipei(Map<String, Object> responseData) {
1919
}
2020

2121
public abstract void execute(Map<String, Object> responseData);
22+
2223
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.dtstack.flinkx.restapi.common.handler;
2+
3+
import org.apache.commons.lang.StringUtils;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class DataHandlerFactory {
9+
10+
11+
private static HashMap<String, DataHandler> handlerHashMap = new HashMap<>(16);
12+
13+
static {
14+
15+
}
16+
17+
public static DataHandler getDataHandler(Map dataHandlerParam) {
18+
if (StringUtils.isNotBlank(dataHandlerParam.get("key").toString())) {
19+
return handlerHashMap.get(dataHandlerParam.get("key").toString());
20+
}else{
21+
throw new IllegalArgumentException("dataHandler key not allow blank");
22+
}
23+
}
24+
25+
public static void destroy() {
26+
//gc
27+
handlerHashMap = null;
28+
}
29+
}

flinkx-restapi/flinkx-restapi-core/src/main/java/com/dtstack/flinkx/restapi/common/handler/ResponseRetryException.java

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.dtstack.flinkx.restapi.common.handler;
2+
3+
import com.dtstack.flinkx.restapi.common.MapUtils;
4+
import com.dtstack.flinkx.restapi.common.exception.ResponseRetryException;
5+
6+
import java.util.Map;
7+
import java.util.Set;
8+
9+
public class RetryDataHandler extends DataHandler {
10+
11+
public RetryDataHandler(String key, Set<String> value) {
12+
super(key, value);
13+
}
14+
15+
@Override
16+
public void execute(Map<String, Object> responseData) {
17+
String[] strings = new String[0];
18+
strings[0] = key;
19+
Object data = MapUtils.getData(responseData, strings);
20+
if (value.contains(data.toString())) {
21+
throw new ResponseRetryException("key:"+key+" contains"+data.toString()+" ,need retry");
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)