You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/12/21 05:52:48 UTC

[shenyu] branch master updated: [ISSUE #4171] Bugfix sandbox json parsing (#4170)

This is an automated email from the ASF dual-hosted git repository.

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 3bfbe50a0 [ISSUE #4171] Bugfix sandbox json parsing (#4170)
3bfbe50a0 is described below

commit 3bfbe50a0c9820e9279679bb93b09e8d4cad7255
Author: kyeongsun <en...@gmail.com>
AuthorDate: Wed Dec 21 14:52:43 2022 +0900

    [ISSUE #4171] Bugfix sandbox json parsing (#4170)
    
    * fix bug: sandbox json toMap
    
    * refac arrange logic moving service
    
    * javadoc fix unmatched exception info
    
    Co-authored-by: xiaoyu <xi...@apache.org>
---
 .../shenyu/admin/controller/SandboxController.java | 134 ++-------------------
 .../shenyu/admin/service/SandboxService.java       |  38 ++++++
 .../impl/SandboxServiceImpl.java}                  |  86 ++++++-------
 3 files changed, 92 insertions(+), 166 deletions(-)

diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/SandboxController.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/SandboxController.java
index 4b48195c2..01fb7c5f0 100755
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/SandboxController.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/SandboxController.java
@@ -17,43 +17,16 @@
 
 package org.apache.shenyu.admin.controller;
 
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import java.time.LocalDateTime;
-import java.time.ZoneOffset;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.stream.Collectors;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.validation.Valid;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.StringEscapeUtils;
-import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.admin.model.dto.ProxyGatewayDTO;
-import org.apache.shenyu.admin.model.entity.AppAuthDO;
-import org.apache.shenyu.admin.service.AppAuthService;
-import org.apache.shenyu.admin.utils.Assert;
-import org.apache.shenyu.admin.utils.HttpUtils;
-import org.apache.shenyu.admin.utils.ShenyuSignatureUtils;
-import org.apache.shenyu.admin.utils.UploadUtils;
-import org.apache.shenyu.common.constant.Constants;
-import org.apache.shenyu.common.utils.JsonUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.shenyu.admin.service.SandboxService;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
-import org.springframework.web.multipart.MultipartFile;
-import org.springframework.web.util.UriComponents;
-import org.springframework.web.util.UriComponentsBuilder;
-import org.springframework.web.util.UriUtils;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.validation.Valid;
 
 /**
  * Sandbox environment.
@@ -61,14 +34,11 @@ import org.springframework.web.util.UriUtils;
 @RestController
 @RequestMapping("/sandbox")
 public class SandboxController {
-    private static final Logger LOG = LoggerFactory.getLogger(SandboxController.class);
-
-    private static final HttpUtils HTTP_UTILS = new HttpUtils();
 
-    private final AppAuthService appAuthService;
+    private final SandboxService sandboxService;
 
-    public SandboxController(final AppAuthService appAuthService) {
-        this.appAuthService = appAuthService;
+    public SandboxController(final SandboxService sandboxService) {
+        this.sandboxService = sandboxService;
     }
 
     /**
@@ -77,96 +47,12 @@ public class SandboxController {
      * @param proxyGatewayDTO proxyGatewayDTO
      * @param request         request
      * @param response        response
-     * @throws IOException IOException
      */
     @PostMapping(path = "/proxyGateway")
     public void proxyGateway(@RequestBody @Valid final ProxyGatewayDTO proxyGatewayDTO,
                             final HttpServletRequest request,
-                            final HttpServletResponse response) throws IOException {
-        // Public request headers.
-        Map<String, String> header = this.buildReqHeaders(proxyGatewayDTO);
-
-        String appKey = proxyGatewayDTO.getAppKey();
-        UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(proxyGatewayDTO.getRequestUrl()).build();
-        String signContent = null;
-        String sign = null;
-        if (StringUtils.isNotEmpty(appKey)) {
-            String timestamp = String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
-            String secureKey = getSecureKey(appKey);
-            Assert.notBlack(secureKey, Constants.SIGN_APP_KEY_IS_NOT_EXIST);
-            signContent = ShenyuSignatureUtils.getSignContent(secureKey, timestamp, uriComponents.getPath());
-            sign = ShenyuSignatureUtils.generateSign(signContent);
-
-            header.put("timestamp", timestamp);
-            header.put("appKey", appKey);
-            header.put("sign", sign);
-            header.put("version", ShenyuSignatureUtils.VERSION);
-        }
+                            final HttpServletResponse response) {
 
-        // Public request parameters.
-        Map<String, Object> reqParams = this.buildReqBizParams(proxyGatewayDTO);
-        List<HttpUtils.UploadFile> files = this.uploadFiles(request);
-        Response resp = HTTP_UTILS.requestCall(uriComponents.toUriString(), reqParams, header, HttpUtils.HTTPMethod.fromValue(proxyGatewayDTO.getHttpMethod()), files);
-        ResponseBody body = resp.body();
-        if (Objects.isNull(body)) {
-            return;
-        }
-        if (StringUtils.isNotEmpty(appKey)) {
-            response.addHeader("sandbox-beforesign", UriUtils.encode(signContent, StandardCharsets.UTF_8));
-            response.addHeader("sandbox-sign", UriUtils.encode(sign, StandardCharsets.UTF_8));
-        }
-        IOUtils.copy(body.byteStream(), response.getOutputStream());
-        response.flushBuffer();
+        sandboxService.requestProxyGateway(proxyGatewayDTO, request, response);
     }
-
-    private Map<String, String> buildReqHeaders(final ProxyGatewayDTO proxyGatewayDTO) {
-        Map<String, String> reqHeaders = new HashMap<>();
-        reqHeaders.put("Cookie", proxyGatewayDTO.getCookie());
-        try {
-            String reqJson = JsonUtils.toJson(proxyGatewayDTO.getHeaders());
-            reqJson = StringEscapeUtils.escapeHtml4(reqJson);
-            Map<String, String> reqMap = JsonUtils.jsonToMap(reqJson, String.class);
-            LOG.info("bizParam toMap= {}", JsonUtils.toJson(reqMap));
-            reqHeaders.putAll(reqMap);
-        } catch (Exception e) {
-            LOG.error("proxyGateway JsonUtils.toMap error={}", e);
-        }
-        return reqHeaders;
-    }
-
-    private Map<String, Object> buildReqBizParams(final ProxyGatewayDTO proxyGatewayDTO) {
-        Map<String, Object> reqParams = new HashMap<>();
-        try {
-            String reqJson = JsonUtils.toJson(proxyGatewayDTO.getBizParam());
-            reqJson = StringEscapeUtils.escapeHtml4(reqJson);
-            Map<String, Object> reqMap = JsonUtils.toMap(reqJson);
-            LOG.info("bizParam toMap= {}", JsonUtils.toJson(reqMap));
-            reqParams.putAll(reqMap);
-        } catch (Exception e) {
-            LOG.error("proxyGateway JsonUtils.toMap error={}", e);
-        }
-        return reqParams;
-    }
-
-    private String getSecureKey(final String appKey) {
-        AppAuthDO appAuthDO = appAuthService.findByAppKey(appKey);
-        return Objects.nonNull(appAuthDO) ? appAuthDO.getAppSecret() : null;
-    }
-
-    private List<HttpUtils.UploadFile> uploadFiles(final HttpServletRequest request) {
-        Collection<MultipartFile> uploadFiles = UploadUtils.getUploadFiles(request);
-        List<HttpUtils.UploadFile> files = uploadFiles.stream()
-            .map(multipartFile -> {
-                try {
-                    return new HttpUtils.UploadFile(multipartFile.getName(), multipartFile.getOriginalFilename(), multipartFile.getBytes());
-                } catch (IOException e) {
-                    LOG.error("upload file fail", e);
-                    return null;
-                }
-            })
-            .filter(Objects::nonNull)
-            .collect(Collectors.toList());
-        return files;
-    }
-
 }
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/SandboxService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/SandboxService.java
new file mode 100644
index 000000000..b15fd9005
--- /dev/null
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/SandboxService.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.service;
+
+import org.apache.shenyu.admin.model.dto.ProxyGatewayDTO;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * sandbox service.
+ */
+public interface SandboxService {
+
+    /**
+     * proxy Gateway.
+     *
+     * @param proxyGatewayDTO proxyGatewayDTO
+     * @param request         request
+     * @param response        response
+     */
+    void requestProxyGateway(ProxyGatewayDTO proxyGatewayDTO, HttpServletRequest request, HttpServletResponse response);
+}
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/SandboxController.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SandboxServiceImpl.java
old mode 100755
new mode 100644
similarity index 77%
copy from shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/SandboxController.java
copy to shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SandboxServiceImpl.java
index 4b48195c2..e32d688c7
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/SandboxController.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SandboxServiceImpl.java
@@ -15,21 +15,8 @@
  * limitations under the License.
  */
 
-package org.apache.shenyu.admin.controller;
+package org.apache.shenyu.admin.service.impl;
 
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import java.time.LocalDateTime;
-import java.time.ZoneOffset;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.stream.Collectors;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.validation.Valid;
 import okhttp3.Response;
 import okhttp3.ResponseBody;
 import org.apache.commons.io.IOUtils;
@@ -38,6 +25,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.admin.model.dto.ProxyGatewayDTO;
 import org.apache.shenyu.admin.model.entity.AppAuthDO;
 import org.apache.shenyu.admin.service.AppAuthService;
+import org.apache.shenyu.admin.service.SandboxService;
 import org.apache.shenyu.admin.utils.Assert;
 import org.apache.shenyu.admin.utils.HttpUtils;
 import org.apache.shenyu.admin.utils.ShenyuSignatureUtils;
@@ -46,28 +34,38 @@ import org.apache.shenyu.common.constant.Constants;
 import org.apache.shenyu.common.utils.JsonUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.util.UriComponents;
 import org.springframework.web.util.UriComponentsBuilder;
 import org.springframework.web.util.UriUtils;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.time.Instant;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.List;
+import java.util.Collection;
+import java.util.stream.Collectors;
+
 /**
- * Sandbox environment.
+ * sandbox service.
  */
-@RestController
-@RequestMapping("/sandbox")
-public class SandboxController {
-    private static final Logger LOG = LoggerFactory.getLogger(SandboxController.class);
+@Service
+public class SandboxServiceImpl implements SandboxService {
+
+    private static final Logger LOG = LoggerFactory.getLogger(SandboxServiceImpl.class);
 
     private static final HttpUtils HTTP_UTILS = new HttpUtils();
 
     private final AppAuthService appAuthService;
 
-    public SandboxController(final AppAuthService appAuthService) {
+    public SandboxServiceImpl(final AppAuthService appAuthService) {
         this.appAuthService = appAuthService;
     }
 
@@ -77,12 +75,9 @@ public class SandboxController {
      * @param proxyGatewayDTO proxyGatewayDTO
      * @param request         request
      * @param response        response
-     * @throws IOException IOException
      */
-    @PostMapping(path = "/proxyGateway")
-    public void proxyGateway(@RequestBody @Valid final ProxyGatewayDTO proxyGatewayDTO,
-                            final HttpServletRequest request,
-                            final HttpServletResponse response) throws IOException {
+    @Override
+    public void requestProxyGateway(final ProxyGatewayDTO proxyGatewayDTO, final HttpServletRequest request, final HttpServletResponse response) {
         // Public request headers.
         Map<String, String> header = this.buildReqHeaders(proxyGatewayDTO);
 
@@ -91,12 +86,11 @@ public class SandboxController {
         String signContent = null;
         String sign = null;
         if (StringUtils.isNotEmpty(appKey)) {
-            String timestamp = String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
+            String timestamp = String.valueOf(Instant.now().toEpochMilli());
             String secureKey = getSecureKey(appKey);
             Assert.notBlack(secureKey, Constants.SIGN_APP_KEY_IS_NOT_EXIST);
             signContent = ShenyuSignatureUtils.getSignContent(secureKey, timestamp, uriComponents.getPath());
             sign = ShenyuSignatureUtils.generateSign(signContent);
-
             header.put("timestamp", timestamp);
             header.put("appKey", appKey);
             header.put("sign", sign);
@@ -106,8 +100,14 @@ public class SandboxController {
         // Public request parameters.
         Map<String, Object> reqParams = this.buildReqBizParams(proxyGatewayDTO);
         List<HttpUtils.UploadFile> files = this.uploadFiles(request);
-        Response resp = HTTP_UTILS.requestCall(uriComponents.toUriString(), reqParams, header, HttpUtils.HTTPMethod.fromValue(proxyGatewayDTO.getHttpMethod()), files);
-        ResponseBody body = resp.body();
+        ResponseBody body = null;
+        try {
+            Response resp = HTTP_UTILS.requestCall(uriComponents.toUriString(), reqParams, header, HttpUtils.HTTPMethod.fromValue(proxyGatewayDTO.getHttpMethod()), files);
+            body = resp.body();
+        } catch (Exception e) {
+            LOG.error("sandbox proxy request response error. msg={}", e.getMessage());
+        }
+
         if (Objects.isNull(body)) {
             return;
         }
@@ -115,8 +115,12 @@ public class SandboxController {
             response.addHeader("sandbox-beforesign", UriUtils.encode(signContent, StandardCharsets.UTF_8));
             response.addHeader("sandbox-sign", UriUtils.encode(sign, StandardCharsets.UTF_8));
         }
-        IOUtils.copy(body.byteStream(), response.getOutputStream());
-        response.flushBuffer();
+        try {
+            IOUtils.copy(body.byteStream(), response.getOutputStream());
+            response.flushBuffer();
+        } catch (Exception e) {
+            LOG.error("sandbox proxy request response to byte error. msg={}", e.getMessage());
+        }
     }
 
     private Map<String, String> buildReqHeaders(final ProxyGatewayDTO proxyGatewayDTO) {
@@ -126,10 +130,10 @@ public class SandboxController {
             String reqJson = JsonUtils.toJson(proxyGatewayDTO.getHeaders());
             reqJson = StringEscapeUtils.escapeHtml4(reqJson);
             Map<String, String> reqMap = JsonUtils.jsonToMap(reqJson, String.class);
-            LOG.info("bizParam toMap= {}", JsonUtils.toJson(reqMap));
+            LOG.info("Sandbox Request Headers. toMap={}", JsonUtils.toJson(reqMap));
             reqHeaders.putAll(reqMap);
         } catch (Exception e) {
-            LOG.error("proxyGateway JsonUtils.toMap error={}", e);
+            LOG.error("proxyGateway JsonUtils.toMap error={}", e.getMessage());
         }
         return reqHeaders;
     }
@@ -137,13 +141,11 @@ public class SandboxController {
     private Map<String, Object> buildReqBizParams(final ProxyGatewayDTO proxyGatewayDTO) {
         Map<String, Object> reqParams = new HashMap<>();
         try {
-            String reqJson = JsonUtils.toJson(proxyGatewayDTO.getBizParam());
-            reqJson = StringEscapeUtils.escapeHtml4(reqJson);
-            Map<String, Object> reqMap = JsonUtils.toMap(reqJson);
-            LOG.info("bizParam toMap= {}", JsonUtils.toJson(reqMap));
+            Map<String, Object> reqMap = JsonUtils.toMap(proxyGatewayDTO.getBizParam());
+            LOG.info("sandbox Request Params. toMap={}", JsonUtils.toJson(reqMap));
             reqParams.putAll(reqMap);
         } catch (Exception e) {
-            LOG.error("proxyGateway JsonUtils.toMap error={}", e);
+            LOG.error("proxyGateway JsonUtils.toMap error={}", e.getMessage());
         }
         return reqParams;
     }