From de090c82e30e039ac04e0415cd4fe603a6bbc9f8 Mon Sep 17 00:00:00 2001 From: Yixian Date: Thu, 7 Apr 2022 18:55:25 +0800 Subject: [PATCH] shopify request body read --- .../payment/manage/CacheRequestFilter.java | 5 +- .../manage/CachedBodyServletRequest.java | 63 +++++++++++++++++++ .../shopify/support/ShopifyHttpUtils.java | 31 +++------ 3 files changed, 74 insertions(+), 25 deletions(-) create mode 100644 src/main/java/au/com/royalpay/payment/manage/CachedBodyServletRequest.java diff --git a/src/main/java/au/com/royalpay/payment/manage/CacheRequestFilter.java b/src/main/java/au/com/royalpay/payment/manage/CacheRequestFilter.java index 6d9f72c74..76ac19671 100644 --- a/src/main/java/au/com/royalpay/payment/manage/CacheRequestFilter.java +++ b/src/main/java/au/com/royalpay/payment/manage/CacheRequestFilter.java @@ -1,7 +1,6 @@ package au.com.royalpay.payment.manage; import org.springframework.web.filter.OncePerRequestFilter; -import org.springframework.web.util.ContentCachingRequestWrapper; import javax.servlet.FilterChain; import javax.servlet.ServletException; @@ -12,10 +11,10 @@ import java.io.IOException; public class CacheRequestFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { - if (request instanceof ContentCachingRequestWrapper) { + if (request instanceof CachedBodyServletRequest) { filterChain.doFilter(request, response); } else { - ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request); + CachedBodyServletRequest wrappedRequest = new CachedBodyServletRequest(request); filterChain.doFilter(wrappedRequest, response); } } diff --git a/src/main/java/au/com/royalpay/payment/manage/CachedBodyServletRequest.java b/src/main/java/au/com/royalpay/payment/manage/CachedBodyServletRequest.java new file mode 100644 index 000000000..5ceabe716 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/CachedBodyServletRequest.java @@ -0,0 +1,63 @@ +package au.com.royalpay.payment.manage; + +import org.springframework.util.StreamUtils; + +import javax.servlet.ReadListener; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import java.io.*; + +public class CachedBodyServletRequest extends HttpServletRequestWrapper { + private final byte[] content; + + public CachedBodyServletRequest(HttpServletRequest request) throws IOException { + super(request); + try (InputStream in = request.getInputStream()) { + content = StreamUtils.copyToByteArray(in); + } + } + + @Override + public ServletInputStream getInputStream() throws IOException { + return new CachedBodyServletInputStream(content); + } + + @Override + public BufferedReader getReader() throws IOException { + ByteArrayInputStream boi = new ByteArrayInputStream(content); + return new BufferedReader(new InputStreamReader(boi)); + } + + public static class CachedBodyServletInputStream extends ServletInputStream { + private final InputStream delegate; + + public CachedBodyServletInputStream(byte[] content) { + this.delegate = new ByteArrayInputStream(content); + } + + @Override + public boolean isFinished() { + try { + return delegate.available() == 0; + } catch (IOException e) { + return true; + } + } + + @Override + public boolean isReady() { + return true; + } + + @Override + public void setReadListener(ReadListener readListener) { + + } + + @Override + public int read() throws IOException { + return delegate.read(); + } + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/shopify/support/ShopifyHttpUtils.java b/src/main/java/au/com/royalpay/payment/manage/shopify/support/ShopifyHttpUtils.java index 894fc161d..5351314a1 100644 --- a/src/main/java/au/com/royalpay/payment/manage/shopify/support/ShopifyHttpUtils.java +++ b/src/main/java/au/com/royalpay/payment/manage/shopify/support/ShopifyHttpUtils.java @@ -1,11 +1,15 @@ package au.com.royalpay.payment.manage.shopify.support; +import au.com.royalpay.payment.tools.exceptions.ServerErrorException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.util.StreamUtils; import javax.servlet.http.HttpServletRequest; import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; public class ShopifyHttpUtils { private ShopifyHttpUtils() { @@ -14,29 +18,12 @@ public class ShopifyHttpUtils { private static final Logger logger = LoggerFactory.getLogger(ShopifyHttpUtils.class); public static String getRequestBody(HttpServletRequest request) { - BufferedReader br = null; - StringBuilder sb = new StringBuilder(""); - try { - br = request.getReader(); - String str; - while ((str = br.readLine()) != null) { - sb.append(str); - } - br.close(); - + try (InputStream in = request.getInputStream()) { + String body = StreamUtils.copyToString(in, StandardCharsets.UTF_8); + logger.debug("Shopify read body |-{}", body); + return body; } catch (IOException e) { - e.printStackTrace(); - } finally { - if (null != br) { - try { - br.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } + throw new ServerErrorException("Failed to read request content"); } - String body = sb.toString(); - logger.debug("Shopify read body |-{}", body); - return body; } }