|
54 | 54 | import java.io.IOException;
|
55 | 55 | import java.lang.reflect.Array;
|
56 | 56 | import java.net.URLDecoder;
|
| 57 | +import java.net.URLEncoder; |
| 58 | +import java.nio.charset.StandardCharsets; |
57 | 59 | import java.rmi.ServerException;
|
58 | 60 | import java.sql.PreparedStatement;
|
59 | 61 | import java.sql.ResultSet;
|
@@ -370,12 +372,84 @@ public String deleteByTag(@PathVariable("tag") String tag, @RequestParam Map<Str
|
370 | 372 | */
|
371 | 373 | @GetMapping("get/{request}")
|
372 | 374 | public String openGet(@PathVariable("request") String request, HttpSession session) {
|
| 375 | + if (! Log.DEBUG) { // 一般情况这样简单使用 |
| 376 | + try { |
| 377 | + request = URLDecoder.decode(request, StandardCharsets.UTF_8); |
| 378 | + } catch (Exception e) { |
| 379 | + // Parser会报错 |
| 380 | + } |
| 381 | + |
| 382 | + return get(request, session); |
| 383 | + } |
| 384 | + |
| 385 | + // 以下代码是为了方便调试,引导手动/自动跳转 http://apijson.cn/api 来测试接口 |
| 386 | + String newUrl = null; |
373 | 387 | try {
|
374 |
| - request = URLDecoder.decode(request, StringUtil.UTF_8); |
| 388 | + HttpServletRequest httpReq = httpServletRequest; |
| 389 | + |
| 390 | + String path = httpReq.getServletPath(); |
| 391 | + int index = path.lastIndexOf("/"); |
| 392 | + path = index < 0 ? path : path.substring(0, index); |
| 393 | + String host = httpReq.getHeader("origin"); |
| 394 | + if (StringUtil.isEmpty(host)) { |
| 395 | + host = httpReq.getHeader("host"); |
| 396 | + String prefix = httpReq.getProtocol().trim().toLowerCase().contains("https") ? "https://" : "http://"; |
| 397 | + if (StringUtil.isEmpty(host)) { |
| 398 | + host = prefix + httpReq.getServerName() + ":" + httpReq.getServerPort(); |
| 399 | + } else { |
| 400 | + host = prefix + host; |
| 401 | + } |
| 402 | + } |
| 403 | + |
| 404 | + String url = host + path; |
| 405 | + String query = StringUtil.getTrimedString(httpReq.getQueryString()); |
| 406 | + if (StringUtil.isNotEmpty(query)) { |
| 407 | + try { |
| 408 | + query = "?" + URLEncoder.encode(query, StandardCharsets.UTF_8); |
| 409 | + } catch (Throwable e) { |
| 410 | + e.printStackTrace(); |
| 411 | + query = "&" + query; |
| 412 | + } |
| 413 | + } |
| 414 | + |
| 415 | + newUrl = "http://apijson.cn/api?send=false&redirect=false&type=JSON&unquote=true&url=" |
| 416 | + + url + query + "&json=" + request; |
| 417 | + |
| 418 | + // httpServletResponse.setHeader("Referer", newUrl); |
| 419 | + // httpServletResponse.setHeader("Redirect-Ref", newUrl); |
| 420 | + // httpServletResponse.sendRedirect(newUrl); |
| 421 | + } catch (Throwable e) { |
| 422 | + e.printStackTrace(); |
| 423 | + } |
| 424 | + |
| 425 | + try { |
| 426 | + request = URLDecoder.decode(request, StandardCharsets.UTF_8); |
375 | 427 | } catch (Exception e) {
|
376 | 428 | // Parser会报错
|
377 | 429 | }
|
378 |
| - return get(request, session); |
| 430 | + |
| 431 | + JSONObject rsp = newParser(session, GET).parseResponse(request); |
| 432 | + rsp.put("@link", newUrl); |
| 433 | + String str = JSON.format(rsp); // rsp.toJSONString(); |
| 434 | + if (StringUtil.isEmpty(newUrl) || Math.random() < 0.5) { |
| 435 | + return str; |
| 436 | + } |
| 437 | + |
| 438 | + String html = "<!DOCTYPE html>\n" + |
| 439 | + "<html>\n" + |
| 440 | + "<head>\n" + |
| 441 | + " <title>APIAuto-机器学习零代码测试、生成代码与静态检查、生成文档与光标悬浮注释</title>\n" + |
| 442 | + " <meta charset=\"utf-8\" />\n" + |
| 443 | + "</head>\n" + |
| 444 | + "\n" + |
| 445 | + "<body>\n" + |
| 446 | + " <span style=\"white-space: pre\">" + str + "</span>\n" + |
| 447 | + " <script language=\"javascript\">setTimeout(function() {\n" + |
| 448 | + " window.open('" + newUrl.replaceAll("'", "\\\\'") + "') \n" + |
| 449 | + " }, 5000)</script>\n" + |
| 450 | + "</body>"; |
| 451 | + |
| 452 | + return html; |
379 | 453 | }
|
380 | 454 |
|
381 | 455 | /**计数
|
|
0 commit comments