P15GEN2\59518
2025-10-18 56638c01bb2cc61a92f5e03c9a1001be5b5d3699
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
package foundation.icall.connector;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Objects;
 
import foundation.icall.ICall;
import foundation.icall.callout.ICallRequest;
import foundation.icall.callout.JSONResponse;
import foundation.util.Util;
import foundation.workflow.WorkStep;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import sun.misc.BASE64Encoder;
 
public class BaiduAIConn extends HttpServerConn {
    
    private static BaiduAIConn instance;
    private static String monitorId = "BaiduAIConn";
    private static int TimeOutHour = 2;
    public Date lastTime;
    private String token;
    
    private BaiduAIConn() {
        
    }
    
    public static synchronized BaiduAIConn getInstance() {
        if (instance == null) {
            instance = new BaiduAIConn();
        }
        
        return instance;
    }
 
    @Override
    public void login(WorkStep step, ICall iCall) throws Exception {
//        if (!tokenExpired()) {
//            return;
//        }
        
        getToken();
    }
 
    @Override
    public void logout(WorkStep step, ICall iCall) throws Exception {
        
    }
 
    @Override
    public ICallRequest createRequest(String url) {
        String host = meta.getString("base_url");
        ICallRequest request = new ICallRequest(host + url + token);
        return request;
    }
    
    private boolean tokenExpired() {
        if (Util.isEmpty(token)) {
            return true;
        }
        
        Date now = new Date();
        boolean result = now.getTime() - lastTime.getTime() >= TimeOutHour * 60 * 60 * 1000;
        
        return result;
    }
    
    private void getToken() throws Exception {
        // 1. build request
        String clientId = meta.getString("client_id");
        String clientSecret = meta.getString("client_secret");
        String grantType = meta.getString("grant_type");
        String bodyStr =  "grant_type=" + grantType + "&client_id=" + clientId + "&client_secret=" + clientSecret;
 
        String tokenUrl = meta.getString("token_url");
        Request request = new Request.Builder()
                .url(tokenUrl)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .post(RequestBody.create(MediaType.get("application/x-www-form-urlencoded"), bodyStr))
                .build();
        
        // 2. send request
        OkHttpClient httpClient = new OkHttpClient();
        Response response = httpClient.newCall(request).execute();
        
        if (!response.isSuccessful()) {
            return ;
        }
        
        JSONResponse result = new JSONResponse(response);
        token = result.getString("access_token");
    }
    
    private static String imageToBase64(File file) {
        byte[] data = null;
 
        InputStream in = null;
        try {
            // 1. bytes
            in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
 
            // 2. encode
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(Objects.requireNonNull(data));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        return null;
    }
 
    public String getName() {
        return meta.getName();
    }
}