¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: Chill åºéª (smallchill@163.com) |
| | | */ |
| | | package org.springblade.core.boot.request; |
| | | |
| | | import org.springblade.core.tool.utils.WebUtil; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | |
| | | import javax.servlet.ReadListener; |
| | | import javax.servlet.ServletInputStream; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletRequestWrapper; |
| | | import java.io.BufferedReader; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | |
| | | /** |
| | | * å
¨å±Requestå
è£
|
| | | * |
| | | * @author Chill |
| | | */ |
| | | public class BladeHttpServletRequestWrapper extends HttpServletRequestWrapper { |
| | | |
| | | /** |
| | | * 没被å
è£
è¿çHttpServletRequestï¼ç¹æ®åºæ¯,éè¦èªå·±è¿æ»¤ï¼ |
| | | */ |
| | | private final HttpServletRequest orgRequest; |
| | | /** |
| | | * ç¼åæ¥æ,æ¯æå¤æ¬¡è¯»åæµ |
| | | */ |
| | | private byte[] body; |
| | | |
| | | |
| | | public BladeHttpServletRequestWrapper(HttpServletRequest request) { |
| | | super(request); |
| | | orgRequest = request; |
| | | } |
| | | |
| | | @Override |
| | | public BufferedReader getReader() throws IOException { |
| | | return new BufferedReader(new InputStreamReader(getInputStream())); |
| | | } |
| | | |
| | | @Override |
| | | public ServletInputStream getInputStream() throws IOException { |
| | | if (super.getHeader(HttpHeaders.CONTENT_TYPE) == null) { |
| | | return super.getInputStream(); |
| | | } |
| | | |
| | | if (super.getHeader(HttpHeaders.CONTENT_TYPE).startsWith(MediaType.MULTIPART_FORM_DATA_VALUE)) { |
| | | return super.getInputStream(); |
| | | } |
| | | |
| | | if (body == null) { |
| | | body = WebUtil.getRequestBody(super.getInputStream()).getBytes(); |
| | | } |
| | | |
| | | final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body); |
| | | |
| | | return new ServletInputStream() { |
| | | |
| | | @Override |
| | | public int read() { |
| | | return byteArrayInputStream.read(); |
| | | } |
| | | |
| | | @Override |
| | | public boolean isFinished() { |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public boolean isReady() { |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public void setReadListener(ReadListener readListener) { |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * è·ååå§request |
| | | * |
| | | * @return HttpServletRequest |
| | | */ |
| | | public HttpServletRequest getOrgRequest() { |
| | | return orgRequest; |
| | | } |
| | | |
| | | /** |
| | | * è·ååå§request |
| | | * |
| | | * @param request request |
| | | * @return HttpServletRequest |
| | | */ |
| | | public static HttpServletRequest getOrgRequest(HttpServletRequest request) { |
| | | if (request instanceof BladeHttpServletRequestWrapper) { |
| | | return ((BladeHttpServletRequestWrapper) request).getOrgRequest(); |
| | | } |
| | | return request; |
| | | } |
| | | |
| | | } |