P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
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
package foundation.icall.connector;
 
import java.util.Date;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
 
import foundation.icall.ICall;
import foundation.icall.callout.ICallRequest;
import foundation.util.Util;
import foundation.workflow.WorkStep;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
 
 
public class FoundationConn extends HttpServerConn {
    
    private static FoundationConn instance;
    private static int TimeOutMinute = 60;
    private static String monitorId = "FoundationSource";
    public Date lastTime;
    private String token;
    
    private FoundationConn() {
        
    }
    
    public static synchronized FoundationConn getInstance() {
        if (instance == null) {
            instance = new FoundationConn();
        }
        
        return instance;
    }
 
    @Override
    public void login(WorkStep step, ICall iCall) throws Exception {
        if (!tokenExpired()) {
            return;
        }
        
        //1. 获取 token
        getToken();
    }
 
    @Override
    public void logout(WorkStep step, ICall iCall) throws Exception {
        
    }
 
    @Override
    public ICallRequest createRequest(String url) {
        String requestUrl = meta.getString("url") + url;
        ICallRequest request = new ICallRequest(requestUrl);
 
        request.addHeader("Content-Type", "application/json");
        request.addFormData("token", token);
        
        return request;
    }
    
    private boolean tokenExpired() {
        if (Util.isEmpty(token)) {
            return true;
        }
        
        Date now = new Date();
        boolean result = now.getTime() - lastTime.getTime() >= TimeOutMinute * 60 * 1000;
        
        logger.info("是否重新获取token:{}, 上次获取token时间:{}, 本次获取token时间:{}", result, lastTime, now);
        
        return result;
    }
 
    private void getToken() throws Exception {
        //1. create request
        String baseUrl = meta.getString("base_url");
        String loginUrl = meta.getString("login_url");
        
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("username", meta.getString("username"));
        jsonBody.put("password", meta.getString("password"));
        String bodyStr = JSON.toJSONString(jsonBody);
        
        Request request = new Request.Builder()
                .url(baseUrl + loginUrl)
                .post(RequestBody.create(MediaType.get("application/json"), bodyStr))
                .build();
        
        //2. get response
        OkHttpClient httpClient = new OkHttpClient();
        Response response = httpClient.newCall(request).execute();
        
        if (!response.isSuccessful()) {
            return ;
        }
        
        //2. get token
        ResponseBody body = response.body();
        bodyStr = body.string();
        JSONObject tokenJson = JSONObject.parseObject(bodyStr);
        
        token = tokenJson.getString("token");
        lastTime = new Date();
    }
 
    public String getName() {
        return meta.getName();
    }
}