wangyunpeng 4 ヶ月 前
コミット
6f5d2e6f18

+ 15 - 0
.idea/git_toolbox_prj.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="GitToolBoxProjectSettings">
+    <option name="commitMessageIssueKeyValidationOverride">
+      <BoolValueOverride>
+        <option name="enabled" value="true" />
+      </BoolValueOverride>
+    </option>
+    <option name="commitMessageValidationEnabledOverride">
+      <BoolValueOverride>
+        <option name="enabled" value="true" />
+      </BoolValueOverride>
+    </option>
+  </component>
+</project>

+ 70 - 0
src/main/java/com/tzld/supply/proxy/controller/ProxyController.java

@@ -8,7 +8,11 @@ import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.util.EntityUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
@@ -16,11 +20,14 @@ import javax.servlet.http.HttpServletRequest;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.util.Enumeration;
+import java.util.UUID;
 
 @RestController
 @RequestMapping("/proxy")
 public class ProxyController {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(ProxyController.class);
+
     private HttpClient httpClient;
 
     public ProxyController() {
@@ -34,9 +41,13 @@ public class ProxyController {
     @RequestMapping(value = "/**", method = { RequestMethod.GET, RequestMethod.POST })
     public ResponseEntity<String> proxy(HttpServletRequest request, @RequestBody(required = false) String body) {
         String targetUrl = request.getHeader("Target-URL");
+        String traceId = UUID.randomUUID().toString();
         if (targetUrl == null || targetUrl.isEmpty()) {
             return ResponseEntity.badRequest().body("Missing Target-URL header");
         }
+        LOGGER.info("TraceId: {}, Proxying request to: {}", traceId, targetUrl);
+        LOGGER.info("TraceId: {}, Request method: {}", traceId, request.getMethod());
+        LOGGER.info("TraceId: {}, Request body: {}", traceId, body);
 
         try {
             HttpRequestBase proxyRequest;
@@ -66,11 +77,70 @@ public class ProxyController {
             String responseBody = EntityUtils.toString(response.getEntity());
             int statusCode = response.getStatusLine().getStatusCode();
 
+            LOGGER.info("TraceId: {}, Response status: {}", traceId, response.getStatusLine());
+            LOGGER.info("TraceId: {}, Response body: {}", traceId, responseBody);
             return ResponseEntity.status(statusCode).body(responseBody);
 
         } catch (IOException e) {
+            LOGGER.error("TraceId: {}, Error forwarding request: {}", traceId, e.getMessage());
             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                     .body("Error forwarding request: " + e.getMessage());
         }
     }
+
+    @RequestMapping(value = "/byte", method = { RequestMethod.GET, RequestMethod.POST })
+    public ResponseEntity<byte[]> byteProxy(HttpServletRequest request, @RequestBody(required = false) String body) {
+        String targetUrl = request.getHeader("Target-URL");
+        if (targetUrl == null || targetUrl.isEmpty()) {
+            return ResponseEntity.badRequest().body(null);
+        }
+        String traceId = UUID.randomUUID().toString();
+        LOGGER.info("TraceId: {}, Proxying request to: {}", traceId, targetUrl);
+        LOGGER.info("TraceId: {}, Request method: {}", traceId, request.getMethod());
+        LOGGER.info("TraceId: {}, Request body: {}", traceId, body);
+
+        try {
+            HttpRequestBase proxyRequest;
+            if (request.getMethod().equalsIgnoreCase("GET")) {
+                proxyRequest = new HttpGet(targetUrl);
+            } else if (request.getMethod().equalsIgnoreCase("POST")) {
+                HttpPost httpPost = new HttpPost(targetUrl);
+                if (body != null) {
+                    httpPost.setEntity(new StringEntity(body, StandardCharsets.UTF_8));
+                }
+                proxyRequest = httpPost;
+            } else {
+                return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(null);
+            }
+
+            // Copy headers
+            Enumeration<String> headerNames = request.getHeaderNames();
+            while (headerNames.hasMoreElements()) {
+                String headerName = headerNames.nextElement();
+                if (!headerName.equalsIgnoreCase("Target-URL") && !headerName.equalsIgnoreCase("content-length")
+                        && !headerName.equalsIgnoreCase("host")) {
+                    proxyRequest.setHeader(headerName, request.getHeader(headerName));
+                }
+            }
+
+            HttpResponse response = httpClient.execute(proxyRequest);
+            byte[] responseBodyBytes = EntityUtils.toByteArray(response.getEntity()); // Get binary data as bytes
+            int statusCode = response.getStatusLine().getStatusCode();
+
+            // Copy Content-Type from target response
+            HttpHeaders responseHeaders = new HttpHeaders();
+            if (response.getEntity() != null && response.getEntity().getContentType() != null) {
+                responseHeaders.setContentType(MediaType.parseMediaType(response.getEntity().getContentType().getValue()));
+            }
+
+            LOGGER.info("TraceId: {}, Response status: {}", traceId, response.getStatusLine());
+            LOGGER.info("TraceId: {}, Response body length: {}", traceId, responseBodyBytes.length);
+            return ResponseEntity.status(statusCode).headers(responseHeaders).body(responseBodyBytes);
+
+        } catch (IOException e) {
+            LOGGER.error("TraceId: {}, Error forwarding request: {}", traceId, e.getMessage());
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
+                    .body(null);
+        }
+    }
 }