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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package foundation.icall.connector;
 
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import foundation.dao.DataPackage;
import foundation.dao.DataReader;
import foundation.dao.bizlogic.DocDescription;
import foundation.dao.bizlogic.IRequest;
import foundation.dao.bizlogic.ResponseType;
import foundation.data.entity.Entity;
import foundation.handler.DataPool;
import foundation.handler.ResultPool;
import foundation.icall.ContentType;
import foundation.icall.ICall;
import foundation.icall.ICallDirection;
import foundation.icall.callout.HttpServerMeta;
import foundation.icall.callout.ICallRequest;
import foundation.icall.callout.IRemoteSource;
import foundation.icall.callout.JSONResponse;
import foundation.icall.callout.MethodType;
import foundation.icall.callout.StreamResponse;
import foundation.io.template.Template;
import foundation.json.JObjectReader;
import foundation.server.ObjectCreator;
import foundation.util.MapList;
import foundation.workflow.WorkStep;
import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;
import okhttp3.Request;
import okhttp3.Response;
 
public abstract class HttpServerConn implements IRemoteSource {
 
    protected static Logger logger;
    public static int ConnectTimeout = 60;
    public static int WriteTimeout = 60;
    public static int ReadTimeout = 60;
    
    protected HttpServerMeta meta;
    private static OkHttpClient httpClient = null;
 
    static {
        logger = LogManager.getLogger(HttpServerConn.class);
    }
    
    protected HttpServerConn() {
        Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(ConnectTimeout, TimeUnit.SECONDS);
        builder.writeTimeout(WriteTimeout, TimeUnit.SECONDS);
        builder.readTimeout(ReadTimeout, TimeUnit.SECONDS);
        
        httpClient = builder.build();
    }
    
    public void init(HttpServerMeta meta) {
        this.meta = meta;
    }
 
    public static HttpServerConn newInstance(HttpServerMeta meta) throws Exception {
        String className = meta.getClassName();
        HttpServerConn instance = ObjectCreator.create(className);
        instance.init(meta);
        
        return instance;
    }
    
    public JObjectReader formatReader(DataReader requestReader) throws Exception {
        return null;
    }
    
    public ResultPool echoSignature(ResultPool resultPool, DataPool dataPool) throws Exception {
        return null;
    }
 
    public Entity systemLogin(DataPool dataPool) throws Exception {
        return null;
    }
    
    public abstract void login(WorkStep step, ICall iCall) throws Exception;
    
    public abstract void logout(WorkStep step, ICall iCall) throws Exception;
 
    public abstract IRequest createRequest(String url);
 
    public IRequest buildBody(IRequest request, WorkStep step, ICall iCall) throws Exception {
        DataPackage dataPackage = step.getDataPackage();
        MapList<String, Template> templateList = iCall.getRequestTemplateList();
        Entity master = dataPackage.getMasterEntity(iCall.getRequestDataSources());
        
        //1. 填充url
        String url = request.getURL();
        url = Template.fillStringVariants(url, dataPackage);
        request.setURL(url);
        
        //2. 填充参数
        ContentType contentType = iCall.getContentType();
        for (Entry<String, Template> entry : templateList.getItemMap().entrySet()) {
            Template template = entry.getValue();
            String key = entry.getKey();
            
            String value = template.fillVariants(dataPackage);
            System.out.println("value:" + value);
            if ("jsonbody".equals(key)) {
                request.setJSONBody(value);
            }
            else if (ContentType.TextPlain == contentType ){
                request.addOneParam(entry.getKey(), value);
            }
            else {
                request.addFormData(entry.getKey(), value);
            }
        }
        
        if (master != null) {
            request.setDocDescription(new DocDescription(master.getId(), master.getString("code")));
        }
        
        request.setContentType(iCall.getContentType().getCode());
        return request;
    }
    
    @SuppressWarnings("unchecked")
    public <T> T post(boolean simulate, IRequest request, ResponseType responseType) throws Exception {
        //1. 如果测试状态,直接退出
        if (simulate) {
            if (ResponseType.JSON == responseType) {
                return (T) new JSONResponse(null);
            }
            else if (ResponseType.Stream == responseType) {
                return (T) new StreamResponse(null);
            }
        }
        
        //2. 构建请求
        T result = null;
        ICallRequest iRequest = (ICallRequest)request;
        Request.Builder builder = new Request.Builder();
        builder.url(iRequest.getURL());
        
        //3. 将  URL 设置到请求中
        if (iRequest.existsParams()) {
            iRequest.applyParams(builder);
        }
                
        //4. 将 headers 设置到请求中
        if (iRequest.existsHeaders()) {
            iRequest.applyHeaders(builder);
        }
        
        //5.  将 JSON Body 设置到请求中
        if (iRequest.existsJSONBody()) {
            iRequest.applyJSONBody(builder, MethodType.POST);
        }
        
        //6. 将 Form Body 设置到请求中
        if (iRequest.existsFormBody()) {
            iRequest.applyFormBody(builder, MethodType.POST);
        }
        
        //7. 发送请求
        System.out.println("请求URL:" + iRequest.getURL());
        Response response = httpClient.newCall(builder.build()).execute();
 
        if (ResponseType.JSON == responseType){
            result = (T) new JSONResponse(response);
        }
        else if (ResponseType.Stream == responseType) {
            result = (T) new StreamResponse(response);
        }
        
        //8. 返回
        return result;
    }
    
    @SuppressWarnings("unchecked")
    public <T> T get(IRequest request, ResponseType responseType) throws Exception {
        ICallRequest iRequest = (ICallRequest)request;
        
        T result = null;
        Request.Builder builder = new Request.Builder().get();
        builder.url(iRequest.getURL());
        //1. 将  URL 设置到请求中
        if (iRequest.existsParams()) {
            iRequest.applyParams(builder);
        }
        
        //2. 将 headers 设置到请求中
        if (iRequest.existsHeaders()) {
            iRequest.applyHeaders(builder);
        }
        
        //3. 发送请求
        Response response = httpClient.newCall(builder.build()).execute();
        
        if (ResponseType.JSON == responseType) {
            result = (T) new JSONResponse(response);
        }
        else if (ResponseType.Stream == responseType) {
            result = (T) new StreamResponse(response);
        }
        
        //5. 返回
        return result;
    }
    
    public <T> T put(boolean simulate, IRequest request, ResponseType responseType) throws Exception {
        //1. 如果测试状态,直接退出
        if (simulate) {
            if (ResponseType.JSON == responseType) {
                return (T) new JSONResponse(null);
            }
            else if (ResponseType.Stream == responseType) {
                return (T) new StreamResponse(null);
            }
        }
        
        //2. 构建请求
        T result = null;
        ICallRequest iRequest = (ICallRequest)request;
        Request.Builder builder = new Request.Builder();
        builder.url(iRequest.getURL());
        
        //3. 将  URL 设置到请求中
        if (iRequest.existsParams()) {
            iRequest.applyParams(builder);
        }
        
        //4. 将 headers 设置到请求中
        if (iRequest.existsHeaders()) {
            iRequest.applyHeaders(builder);
        }
        
        //5.  将 JSON Body 设置到请求中
        if (iRequest.existsJSONBody()) {
            iRequest.applyJSONBody(builder, MethodType.PUT);
        }
        
        //6. 将 Form Body 设置到请求中
        if (iRequest.existsFormBody()) {
            iRequest.applyFormBody(builder, MethodType.PUT);
        }
        
        //7. 发送请求
        Response response = httpClient.newCall(builder.build()).execute();
 
        if (ResponseType.JSON == responseType){
            result = (T) new JSONResponse(response);
        }
        else if (ResponseType.Stream == responseType) {
            result = (T) new StreamResponse(response);
        }
        
        //8. 返回
        return result;
    }
    
    @Override
    public Object get() {
        return this;
    }
 
    public ICallDirection getDirection() {
        return ICallDirection.RemoteServer;
    }
    
    public HttpServerMeta getMeta() {
        return meta;
    }
 
    @Override
    public String getName() {
        return meta.getName();
    }
 
    public String getString(String elementKey) {
        return meta.getString(elementKey);
    }
    
    public String getAccessToken() {
        return null;
    }
}