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
package foundation.icall.callout;
 
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.Set;
 
import foundation.dao.bizlogic.DocDescription;
import foundation.dao.bizlogic.IRequest;
import foundation.util.ContentBuilder;
import foundation.util.MapList;
import foundation.util.Util;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.Request.Builder;
import okhttp3.RequestBody;
 
public class ICallRequest implements IRequest {
 
    private String url;
    private MapList<String, String> params;
    private MapList<String, String> headers;
    private MapList<String, String> formBody;
    private String jsonBody;
    private DocDescription docDescription;
    private String contentType;
    
    public ICallRequest(String url) {
        this.url = url;
        params = new MapList<String, String>(false);
        headers = new MapList<String, String>(false);
        formBody = new MapList<String, String>(false);
    }
    
//    public String paramsToURL() throws UnsupportedEncodingException {
//        if (params.isEmpty()) {
//            return "";
//        }
//        
//        ContentBuilder builder = new ContentBuilder("&");
//        Set<String> keySet = params.getKeySet();
//        
//        for (String key: keySet) {
//            String value = params.get(key);
//            if (value == null) {
//                continue;
//            }
//            
//            builder.append(key + "=" + value);
//            
//            String name = URLEncoder.encode(key, "utf-8");
//            value = URLEncoder.encode(value, "utf-8");
//            
//            builder.append(name + "=" + value);
//        }
//        
//        return builder.toString();
//    }
    
    public void applyHeaders(Builder builder) {
        Set<String> keySet = headers.getKeySet();
        
        for (String key: keySet) {
            String value = headers.get(key);
            
            if (value == null) {
                continue;
            }
            
            builder.addHeader(key, value);
        }
    }
 
    public void applyJSONBody(Builder builder, MethodType methodType) {
        MediaType mediaType = MediaType.parse(contentType);
        RequestBody body = RequestBody.create(mediaType, jsonBody);
        System.out.println("jsonBody:" + jsonBody);
        
        if( methodType == MethodType.PUT) {
            builder.put(body);
            return ; 
        }
        
        builder.post(body);
    }
 
    public void applyFormBody(Builder builder, MethodType methodType) throws UnsupportedEncodingException {
        Charset charset = Charset.forName("UTF-8");
        FormBody.Builder formBuilder = new FormBody.Builder(charset);
        
        Set<String> keySet = formBody.getKeySet();
        
        for (String key: keySet) {
            String value = formBody.get(key);
            formBuilder.add(key, value);
        }
        
        FormBody result = formBuilder.build();
        
        if( methodType == MethodType.PUT) {
            builder.put(result);
            return ; 
        }
        
        builder.post(result);
    }
    
    public void applyParams(Builder builder) throws Exception {
        Set<String> keySet = params.getKeySet();
        
        ContentBuilder paramsbuilder = new ContentBuilder("&");
        for (String key: keySet) {
            String value = params.get(key);
            if (value == null) {
                continue;
            }
            
            value = URLEncoder.encode(value, "utf-8");
            
            paramsbuilder.append(key + "=" + value);
        }
        
        if (!url.contains("?")) {
            builder.url(url + "?" + paramsbuilder.toString());
        }
        else {
            builder.url(url + "&" + paramsbuilder.toString());
        }
    }
    
    public void addHeader(String name, String value) {
        headers.add(name, value);
    }
    
    public void addFormData(String name, String value) {
        formBody.add(name, value);
    }
    
    @Override
    public void setJSONBody(String jsonBody) {
        this.jsonBody = jsonBody;
    }
    
    @Override
    public String getURL() {
        return url;
    }
 
    public void setURL(String url) {
        this.url = url;
    }
 
    @Override
    public void setContentType(String contentType) {
        this.contentType = contentType;
    }
 
    @Override
    public String getContentType() {
        return contentType;
    }
 
    @Override
    public DocDescription getDocDescription() {
        return docDescription;
    }
 
    @Override
    public void setDocDescription(DocDescription docDesciption) {
        this.docDescription = docDesciption;
    }
 
    public boolean existsHeaders() {
        return !headers.isEmpty();
    }
 
    public boolean existsJSONBody() {
        return !Util.isEmpty(jsonBody);
    }
 
    public boolean existsFormBody() {
        return formBody != null && formBody.size() > 0 ;
    }
 
    public boolean existsParams() {
        return params != null && params.size() > 0 ;
    }
 
    public void addOneParam(String key, String value) {
        params.add(key, value);
    }
    
    @Override
    public String getRequestHeader() {
        return headers.mapToString(",");
    }
    
    @Override
    public String getRequestBody() {
        return formBody.mapToString(",");
    }
 
    @Override
    public String getBody() {
        ContentBuilder json = new ContentBuilder(",");
        json.append("\"url\":\"" + url + "\"");
        
        json.append("\"params\":" + params.mapToString(","));
        json.append("\"headers\":" + headers.mapToString(","));
        json.append("\"formBody\":" + formBody.mapToString(","));
        
        return json.toString();
    }
}