P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package foundation.icall;
 
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import foundation.action.ActionContext;
import foundation.action.SingletonActionProvider;
import foundation.action.WorkStep;
import foundation.dao.DataPackage;
import foundation.dao.DataReader;
import foundation.dao.DataWriter;
import foundation.dao.IDataLetter;
import foundation.data.entity.Entity;
import foundation.data.object.DataObject;
import foundation.handler.DataPool;
import foundation.handler.ResultPool;
import foundation.icall.callin.BodyReader;
import foundation.icall.callout.HttpServerSource;
import foundation.icall.callout.RemoteSourceBucket;
import foundation.icall.log.LogRecord;
import foundation.io.object.DownloadAction;
import foundation.io.object.DownloadWriter;
import foundation.json.JObjectReader;
import foundation.json.JType;
import foundation.route.Operation;
import foundation.util.Util;
 
 
public class ICallCenter extends SingletonActionProvider {
 
    protected static Logger logger;
    private static ICallCenter instance;
    private static ICallBucket icallBucket;
    
    static {
        logger = LogManager.getLogger(ICallCenter.class);
    }
    
    private ICallCenter() {
        icallBucket = ICallBucket.getInstance();
    }
 
    public static synchronized ICallCenter getInstance() {
        if (instance == null) {
            instance = new ICallCenter();
        }
 
        return instance;
    }
    
    @Override
    protected void publishMethod() {
        //1. 执行接口
        addMethod("callRemote");
        
        //2. inBound
        addMethod("callIn");
        
        //3. call remote
        addMethod("callRemoteServer");
        
        //4. remote database
        addMethod("callRemoteDB");
        
        //5. 执行 Echo 接口
        addMethod("echo");        
        
        //6. 获取接口描述
        addMethod("getMeta");
        
        //7. 获取接口日志分类树
        addMethod("getLogCategory");
        
        //8. 获取某个具体接口的日志列表
        addMethod("getLogList");
        
        //9. 获取某个具体请求的Request日志
        addMethod("getRequestFile");
        
        //10. 获取某个具体请求的Response日志
        addMethod("getResponseFile");
        
        //11. 重新加载接口参数
        addMethod("reloadInterfaceSource");
    }
    
    public void callRemote(ActionContext context, ICall iCall) throws Exception {
        ICallDirection direction = iCall.getType();
        
        if (ICallDirection.RemoteServer == direction) {
            callRemoteServer(context, iCall);
        }
        else if (ICallDirection.RemoteDB == direction) {
            callRemoteDB(context, iCall);
        }
    }
    
    public void callIn(ActionContext context, ICall iCall) throws Exception {
        DataWriter dataWriter = context.getDataWriter();
        InboundResult result = new InboundResult(iCall);
        
        //1. get data package
        String dataName = iCall.getDataName();
        DataPackage dataPackage = DataPackage.getInstance(dataName);
        
        if (dataPackage == null) {
            throw new Exception("call in bound error, invalid data name: " + dataName);
        }
        
        //2. get request data
        DataReader requestReader = context.getDataReader();
        
        if (!requestReader.has(IDataLetter.Data)) {
            dataWriter.reportOneError("DataNotExists", "data not exists in request");
            logger.error("absent data interface call, skip");
            return;
        }
        
        JObjectReader dataReader = requestReader.getReader(IDataLetter.Data, JType.Object);
        
        //3. 加载DataPackage
        BodyReader bodyReader = new BodyReader(dataReader, dataPackage, iCall.getMappings());
        int received = bodyReader.parse();
        result.setReceived(received);
        
        //4. 保存数据到数据库
        if (!dataPackage.isEmpty()) {
            int saved = dataPackage.saveOneDataToPersist();
            result.setSaved(saved);
        }
        
        //5. result
        dataWriter.addValue(result);        
    }
    
    private void callRemoteServer(ActionContext context, ICall iCall) throws Exception {
        DataWriter dataWriter = context.getDataWriter();
        OutboundResult result = new OutboundResult(iCall);
        
        //1. get data package
        DataPackage dataPackage = context.getDataPackage();
        
        if (dataPackage != null) {
            DataPackage host = dataPackage.getHost();
 
            if (host != null) {
                host.setMasterId(dataPackage.getHostID());
                host.loadOneDataFromFile(); 
                
                dataPackage = host;
            }
        }
        
        //2. 调用 远端接口
        HttpServerSource source = iCall.remoteSource();
        
        source.login(context, iCall);
        try {
            iCall.sendRemoteServerCall(context, result);
        }
        finally {
            source.logout(context, iCall);
        }            
        
        //4. result
        dataWriter.addValue(result);
    }
    
    private void callRemoteDB(ActionContext context, ICall iCall) throws Exception {
        DataWriter dataWriter = context.getDataWriter();
        OutboundResult result = new OutboundResult(iCall);
        LogRecord record = new LogRecord(iCall);
        result.setRecord(record);
        
        //1. 运行远程SQL
        iCall.sendRemoteDBCall(context, result);
        
        //2. result
        if (dataWriter != null) {
            dataWriter.addValue(result);
        }
    }
 
    public void echo(ActionContext context, ICall iCall) throws Exception {
        DataWriter dataWriter = context.getDataWriter();
        InboundResult result = new InboundResult(iCall);
        
        //1. try get data package
        String dataName = iCall.getDataName();
        DataPackage dataPackage = DataPackage.getInstance(dataName);
        
        //2. try get request data
        DataReader requestReader = context.getDataReader();
        JObjectReader dataReader = requestReader.getReader(IDataLetter.Data, JType.Object);
        
        //3. 加载DataPackage
        BodyReader bodyReader = new BodyReader(dataReader, dataPackage, iCall.getMappings());
        int received = bodyReader.parse();
        result.setReceived(received);
        
        //4. 保存数据到数据库
        result.setResponseBody(requestReader.getBody());
        
        //5. result
        dataWriter.addValue(result);
    }
    
    public void getMeta(ActionContext context, ICall iCall) throws Exception {
        DataWriter dataWriter = context.getDataWriter();
        ICallMeta meta = iCall.getMeta();
        dataWriter.addValue(meta);
    }
 
    public void getRequestFile(ActionContext context, ICall iCall) throws Exception {
        DataWriter dataWriter = context.getDataWriter();
        DataReader dataReader = context.getDataReader();
        
        //1. 从请求中得到日志ID
        String id = dataReader.getString("id");
        
        if (Util.isEmpty(id)) {
            logger.error("request has no id");
            return;
        }
        
        //2. 获取数据文件
        DataObject dataObject = DataObject.getInstance("sys_interface_log");
        Entity entity = dataObject.getTableEntity(id);
        
        if (entity == null) {
            logger.error("log record not exists");
            return;
        }
        
        String path = entity.getString("request_content");
        File file = new File(path);
        
        if (!file.exists()) { 
            logger.error("file not exists: {}", file);
            return;
        }
        
        //3. 返回文件流
        DownloadWriter downloadWriter = new DownloadWriter(dataWriter);
        downloadWriter.write(file, "requestbody", DownloadAction.ASTexT);
    }
    
    public void getResponseFile(ActionContext context, String method) throws Exception {
        DataWriter dataWriter = context.getDataWriter();
        DataReader dataReader = context.getDataReader();
        
        //1. 从请求中得到日志ID
        String id = dataReader.getString("id");
        
        if (Util.isEmpty(id)) {
            logger.error("request has no id");
            return;
        }
        
        //2. 获取数据文件
        DataObject dataObject = DataObject.getInstance("sys_interface_log");
        Entity entity = dataObject.getTableEntity(id);
        
        if (entity == null) {
            logger.error("log record not exists");
            return;
        }
        
        String path = entity.getString("return_content");
        File file = new File(path);
        
        if (!file.exists()) {
            logger.error("file not exists: {}", file);
            return;
        }
        
        //3. 返回文件流
        DownloadWriter downloadWriter = new DownloadWriter(dataWriter);
        downloadWriter.write(file, "responsebody", DownloadAction.ASTexT);
    }
    
    @Override
    public void exec(ActionContext context, String methodName) throws Exception {
        String iCallName = context.getStepParam();
        
        if (Util.isEmpty(iCallName)) {
            DataReader dataReader = context.getDataReader();
            iCallName = dataReader.getString("icall");
        }
        
        ICall iCall = icallBucket.getOne(iCallName);
        
        Method method = getMethod(methodName);
        try {
            method.invoke(this, context, iCall);
        }
        catch (InvocationTargetException e) {
            e.printStackTrace();
            Throwable throwable = e.getTargetException();
 
            if (throwable == null) {
                throw e;
            }
            else {
                throw (Exception) throwable;
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
 
    @Override
    protected void invokeMethod(Method method, DataPool dataPool, ResultPool resultPool) throws Exception {
        //1. 创建 reader 和 writer
        Operation operation = dataPool.getOperation();
        DataReader dataReader = new DataReader(operation, dataPool);
        
        DataWriter dataWriter = new DataWriter(resultPool, dataReader.getDomain());
        resultPool.setLetterWriter(dataWriter);
        
        //2. 创建 ICall, 请求样例-inbound:root/interface/call/md_org_data
        //2. 每次请求系统都会根据 meta 创建一个新的 iCall 和 remote source
        String iCallName = dataReader.getString("icall");
        ICall iCall = icallBucket.getOne(iCallName);
        
        //3. 创建 action context
        String dataName = dataReader.getDomain().getDataName();
        WorkStep step = new WorkStep(dataName, operation.getOperator());
        ActionContext context = new ActionContext(dataReader, dataWriter, step);
        
        //4. 调用方法
        try {
            method.invoke(this, context, iCall);
        }
        catch (InvocationTargetException e) {
            e.printStackTrace();
            Throwable throwable = e.getTargetException();
 
            if (throwable == null) {
                throw e;
            }
            else {
                throw (Exception) throwable;
            }
        }        
    }
    
    @Override
    protected boolean validateMethodParameter(Class<?>[] parameters) {
        if (parameters == null || parameters.length != 2) {
            return false;
        }
        
        Class<?> type = parameters[0];
        if (!type.isAssignableFrom(ActionContext.class)) {
            return false;
        };
        
        type = parameters[1];
        if (!type.isAssignableFrom(ICall.class)) {
            return false;
        };
        
        return true;
    }
 
    public void reloadInterfaceSource(ActionContext context, ICall iCall) throws Exception {
        RemoteSourceBucket sourceBucket = RemoteSourceBucket.clean();
        
        ICallLoader.loadRemoteDataBases(sourceBucket);
        ICallLoader.loadHttpServers(sourceBucket);
    }
}