summaryrefslogtreecommitdiff
path: root/src/main/java/com/geedge/common/util/TsgUtil.java
blob: 9bf9134f567f4130049a195d398ce3159639f34b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.geedge.common.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.common.base.Stopwatch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @Classname LoginTsgUtil
 * @Date 2024/1/3 09:34
 * @Author wWei
 */
@Slf4j
@Component
public class TsgUtil {

    public static String TSG_URL;
    private static String TSG_TOKEN;
    private static Integer httpTimeOut;
    public static Boolean isLatestVersion;

    @Value("${tsg.system.isLatestVersion}")
    public void isLatestVersion(Boolean latestVersion) {
        isLatestVersion = latestVersion;
    }

    @Value("${tsg.system.httpTimeout}")
    public void setTsgUrl(Integer timeOut) {
        httpTimeOut = timeOut;
    }

    @Value("${tsg.system.url}")
    public void setTsgUrl(String url) {
        TSG_URL = url;
    }

    @Value("${tsg.system.token}")
    private void setTsgToken(String token) {
        TSG_TOKEN = token;
    }

    public static String getToken() {
        if (StrUtil.isNotEmpty(TSG_TOKEN)) {
            return TSG_TOKEN;
        }
        log.error("failed to get TSG system token.");
        throw new IllegalArgumentException("failed to get TSG system token.");
    }

    public static JSONObject getObjectItemList(Integer objectId, String objectType) {
        Stopwatch watch = Stopwatch.createStarted();
        String response = HttpRequest.get(TSG_URL + "/v1/policy/object/" + objectId + "/item?page_no=1&page_size=1&type=" + objectType)
                .header(Header.AUTHORIZATION, getToken())
                .timeout(httpTimeOut)
                .execute().body();
        log.info("get tsg-api, cost {} seconds", watch.elapsed(TimeUnit.SECONDS));
        if (StrUtil.isBlank(response)) {
            log.error("get {} Object error, response: {}", objectId, response);
            throw new RuntimeException("get " + objectId + " Object error, response: " + response);
        }
        JSONObject jsonObject = JSONUtil.parseObj(response);
        if (!"200".equals(jsonObject.get("code").toString())) {
            log.error("get {} Object error, response: {}", objectId, response);
            throw new RuntimeException("get " + objectId + " Object error, response: " + response);
        }
        return jsonObject;
    }

    public static void updateObjectById(Integer id, Map<String, Object> body) {
        Stopwatch watch = Stopwatch.createStarted();
        String response = HttpRequest.put(TSG_URL + "/v1/policy/object/" + id)
                .header(Header.AUTHORIZATION, getToken())
                .body(JSONUtil.toJsonStr(body))
                .timeout(httpTimeOut)
                .execute().body();
        log.info("update tsg-api, cost {} seconds", watch.elapsed(TimeUnit.SECONDS));
        if (StrUtil.isBlank(response)) {
            log.error("update {} Object error, response: {}", id, response);
            throw new RuntimeException("update " + id + " Object error, response: " + response);
        }
        JSONObject jsonObject = JSONUtil.parseObj(response);
        if (!"200".equals(jsonObject.get("code").toString())) {
            log.error("update {} Object error, response: {}", id, response);
            throw new RuntimeException("update " + id + " Object error, response: " + response);
        }
    }

    public static void updateObjectOld(Map<String, Object> body) {
        Stopwatch watch = Stopwatch.createStarted();
        String response = HttpRequest.put(TSG_URL + "/v1/policy/object")
                .header(Header.AUTHORIZATION, getToken())
                .body(JSONUtil.toJsonStr(body))
                .timeout(httpTimeOut)
                .execute().body();
        log.info("update tsg-api, cost {} seconds", watch.elapsed(TimeUnit.SECONDS));
        if (StrUtil.isBlank(response)) {
            log.error("update {} Object error, response: {}", body, response);
            throw new RuntimeException("update " + body + " Object error, response: " + response);
        }
        JSONObject jsonObject = JSONUtil.parseObj(response);
        if (!"200".equals(jsonObject.get("code").toString())) {
            log.error("update {} Object error, response: {}", body, response);
            throw new RuntimeException("update " + body + " Object error, response: " + response);
        }
    }

    public static void deleteItemOfObjectById(Integer id, Map<String, Object> form) {
        Stopwatch watch = Stopwatch.createStarted();
        String response = HttpRequest.delete(TSG_URL + "/v1/policy/object/" + id + "/item")
                .header(Header.AUTHORIZATION, getToken())
                .form(form)
                .timeout(httpTimeOut)
                .execute().body();
        log.info("delete tsg-api, cost {} seconds", watch.elapsed(TimeUnit.SECONDS));
        if (StrUtil.isBlank(response)) {
            log.error("update {} Object error, response: {}", id, response);
            throw new RuntimeException("update " + id + " Object error, response: " + response);
        }
        JSONObject jsonObject = JSONUtil.parseObj(response);
        if (!"200".equals(jsonObject.get("code").toString())) {
            log.error("update {} Object error, response: {}", id, response);
            throw new RuntimeException("update " + id + " Object error, response: " + response);
        }
    }

    public static void deleteItemOfObjectOld(Map<String, Object> body) {
        Stopwatch watch = Stopwatch.createStarted();
        String response = HttpRequest.delete(TSG_URL + "/v1/policy/items")
                .header(Header.AUTHORIZATION, getToken())
                .body(JSONUtil.toJsonStr(body))
                .timeout(httpTimeOut)
                .execute().body();
        log.info("delete tsg-api, cost {} seconds", watch.elapsed(TimeUnit.SECONDS));
        if (StrUtil.isBlank(response)) {
            log.error("update {} Object error, response: {}", body, response);
            throw new RuntimeException("update " + body + " Object error, response: " + response);
        }
        JSONObject jsonObject = JSONUtil.parseObj(response);
        if (!"200".equals(jsonObject.get("code").toString())) {
            log.error("update {} Object error, response: {}", body, response);
            throw new RuntimeException("update " + body + " Object error, response: " + response);
        }
    }
}