24
24
25
25
import java .net .URLDecoder ;
26
26
import java .rmi .ServerException ;
27
+ import java .util .ArrayList ;
28
+ import java .util .Enumeration ;
29
+ import java .util .List ;
27
30
import java .util .Random ;
28
31
import java .util .concurrent .TimeoutException ;
29
32
33
+ import javax .servlet .http .HttpServletRequest ;
34
+ import javax .servlet .http .HttpServletResponse ;
30
35
import javax .servlet .http .HttpSession ;
31
36
37
+ import org .springframework .beans .factory .annotation .Autowired ;
38
+ import org .springframework .http .HttpEntity ;
39
+ import org .springframework .http .HttpHeaders ;
40
+ import org .springframework .http .HttpMethod ;
41
+ import org .springframework .http .ResponseEntity ;
42
+ import org .springframework .stereotype .Service ;
43
+ import org .springframework .web .bind .annotation .GetMapping ;
32
44
import org .springframework .web .bind .annotation .PathVariable ;
33
45
import org .springframework .web .bind .annotation .PostMapping ;
34
46
import org .springframework .web .bind .annotation .RequestBody ;
35
47
import org .springframework .web .bind .annotation .RequestMapping ;
48
+ import org .springframework .web .bind .annotation .RequestParam ;
49
+ import org .springframework .web .bind .annotation .ResponseBody ;
36
50
import org .springframework .web .bind .annotation .RestController ;
51
+ import org .springframework .web .client .RestTemplate ;
37
52
53
+ import com .alibaba .fastjson .JSONArray ;
38
54
import com .alibaba .fastjson .JSONObject ;
39
55
40
56
import apijson .demo .server .model .BaseModel ;
63
79
* <br > 3.调试方便 - 建议使用 APIJSON在线测试工具 或 Postman
64
80
* @author Lemon
65
81
*/
82
+ @ Service
66
83
@ RestController
67
84
@ RequestMapping ("" )
68
85
public class Controller {
@@ -200,15 +217,15 @@ public String openHead(@PathVariable String request, HttpSession session) {
200
217
public static final String FUNCTION_ ;
201
218
public static final String REQUEST_ ;
202
219
public static final String ACCESS_ ;
203
-
220
+
204
221
public static final String USER_ ;
205
222
public static final String PRIVACY_ ;
206
223
public static final String VERIFY_ ; //加下划线后缀是为了避免 Verify 和 verify 都叫VERIFY,分不清
207
224
static {
208
225
FUNCTION_ = Function .class .getSimpleName ();
209
226
REQUEST_ = Request .class .getSimpleName ();
210
227
ACCESS_ = Access .class .getSimpleName ();
211
-
228
+
212
229
USER_ = User .class .getSimpleName ();
213
230
PRIVACY_ = Privacy .class .getSimpleName ();
214
231
VERIFY_ = Verify .class .getSimpleName ();
@@ -233,8 +250,8 @@ public String openHead(@PathVariable String request, HttpSession session) {
233
250
234
251
public static final String TYPE = "type" ;
235
252
236
-
237
-
253
+
254
+
238
255
/**重新加载配置
239
256
* @param request
240
257
* @return
@@ -261,17 +278,17 @@ public JSONObject reload(@RequestBody String request) {
261
278
} catch (Exception e ) {
262
279
return DemoParser .extendErrorResult (requestObject , e );
263
280
}
264
-
281
+
265
282
JSONResponse response = new JSONResponse (headVerify (Verify .TYPE_RELOAD , phone , verify ));
266
283
response = response .getJSONResponse (VERIFY_ );
267
284
if (JSONResponse .isExist (response ) == false ) {
268
285
return DemoParser .extendErrorResult (requestObject , new ConditionErrorException ("手机号或验证码错误" ));
269
286
}
270
287
271
288
JSONObject result = DemoParser .newSuccessResult ();
272
-
289
+
273
290
boolean reloadAll = StringUtil .isEmpty (type , true ) || "ALL" .equals (type );
274
-
291
+
275
292
if (reloadAll || "FUNCTION" .equals (type )) {
276
293
try {
277
294
result .put (FUNCTION_ , DemoFunction .init ());
@@ -280,7 +297,7 @@ public JSONObject reload(@RequestBody String request) {
280
297
result .put (FUNCTION_ , DemoParser .newErrorResult (e ));
281
298
}
282
299
}
283
-
300
+
284
301
if (reloadAll || "REQUEST" .equals (type )) {
285
302
try {
286
303
result .put (REQUEST_ , StructureUtil .init ());
@@ -289,7 +306,7 @@ public JSONObject reload(@RequestBody String request) {
289
306
result .put (REQUEST_ , DemoParser .newErrorResult (e ));
290
307
}
291
308
}
292
-
309
+
293
310
if (reloadAll || "ACCESS" .equals (type )) {
294
311
try {
295
312
result .put (ACCESS_ , DemoVerifier .init ());
@@ -298,7 +315,7 @@ public JSONObject reload(@RequestBody String request) {
298
315
result .put (ACCESS_ , DemoParser .newErrorResult (e ));
299
316
}
300
317
}
301
-
318
+
302
319
return result ;
303
320
}
304
321
@@ -952,4 +969,59 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
952
969
}
953
970
954
971
972
+ @ Autowired
973
+ HttpServletRequest request ;
974
+ @ Autowired
975
+ HttpServletResponse response ;
976
+
977
+ @ RequestMapping (value = "/delegate" )
978
+ // @ResponseBody
979
+ public String delegate (@ RequestParam ("$_delegate_url" ) String url , @ RequestBody String body , HttpMethod method , HttpSession session ){
980
+ Enumeration <String > names = request .getHeaderNames ();
981
+ HttpHeaders headers = null ;
982
+ String name ;
983
+ if (names != null ) {
984
+ headers = new HttpHeaders ();
985
+ while (names .hasMoreElements ()) {
986
+ name = names .nextElement ();
987
+ headers .add (name , request .getHeader (name ));
988
+ }
989
+
990
+ List <String > cookie = session == null ? null : (List <String >) session .getAttribute ("Cookie" );
991
+ if (cookie != null && cookie .isEmpty () == false ) {
992
+ List <String > c = headers .get ("Cookie" );
993
+ if (c == null ) {
994
+ c = new ArrayList <>();
995
+ }
996
+ c .addAll (cookie );
997
+ headers .put ("Cookie" , c );
998
+ }
999
+ }
1000
+ try {
1001
+ request .getParameterMap ().remove ("$_delegate_url" );
1002
+ } catch (Exception e ) {
1003
+ // TODO: handle exception
1004
+ }
1005
+
1006
+ RestTemplate client = new RestTemplate ();
1007
+ // 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
1008
+ HttpEntity <String > requestEntity = new HttpEntity <>(method == HttpMethod .GET ? JSON .toJSONString (request .getParameterMap ()) : body , headers );
1009
+ // 执行HTTP请求
1010
+ ResponseEntity <String > entity = client .exchange (url , method , requestEntity , String .class );
1011
+
1012
+ HttpHeaders hs = entity .getHeaders ();
1013
+ if (session != null && hs != null ) {
1014
+ List <String > cookie = hs .get ("Set-Cookie" );
1015
+ if (cookie != null && cookie .isEmpty () == false ) {
1016
+ session .setAttribute ("Cookie" , cookie );
1017
+ }
1018
+ }
1019
+ return entity .getBody ();
1020
+ }
1021
+
1022
+ @ GetMapping ("v2/api-docs" )
1023
+ public JSONObject swaggerAPIDocs () {
1024
+ return new DemoParser ().parseResponse (new JSONRequest ("Swagger" , new JSONRequest ())).getJSONObject ("Swagger" );
1025
+ }
1026
+
955
1027
}
0 commit comments