haungxing
4 months ago
16 changed files with 175 additions and 85 deletions
@ -0,0 +1,22 @@
|
||||
package com.hnac.hzinfo.inspect.offline.filter; |
||||
|
||||
import javax.servlet.*; |
||||
import javax.servlet.annotation.WebFilter; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import java.io.IOException; |
||||
|
||||
/** |
||||
* @author ysj |
||||
* @version 4.0.0 |
||||
* @create 2024-07-24-19:07 |
||||
*/ |
||||
@WebFilter(filterName = "ContentTypeFilter", urlPatterns = "/offline/syncOfflineDataToOnline") |
||||
public class ContentTypeFilter implements Filter { |
||||
|
||||
@Override |
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { |
||||
HttpServletRequest httpRequest = (HttpServletRequest) request; |
||||
ContentTypeRequestWrapper wrappedRequest = new ContentTypeRequestWrapper(httpRequest); |
||||
chain.doFilter(wrappedRequest, response); |
||||
} |
||||
} |
@ -0,0 +1,53 @@
|
||||
package com.hnac.hzinfo.inspect.offline.filter; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletRequestWrapper; |
||||
import java.util.Collections; |
||||
import java.util.Enumeration; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author ysj |
||||
* @version 4.0.0 |
||||
* @create 2024-07-24-19:08 |
||||
*/ |
||||
public class ContentTypeRequestWrapper extends HttpServletRequestWrapper { |
||||
|
||||
private final String newContentType; |
||||
|
||||
public ContentTypeRequestWrapper(HttpServletRequest request) { |
||||
super(request); |
||||
// 你想要设置的新Content-Type
|
||||
this.newContentType = "form-data"; |
||||
} |
||||
|
||||
@Override |
||||
public String getHeader(String name) { |
||||
if ("Content-Type".equalsIgnoreCase(name)) { |
||||
return newContentType; |
||||
} |
||||
return super.getHeader(name); |
||||
} |
||||
|
||||
@Override |
||||
public Enumeration<String> getHeaders(String name) { |
||||
if ("Content-Type".equalsIgnoreCase(name)) { |
||||
return Collections.enumeration(Collections.singleton(newContentType)); |
||||
} |
||||
return super.getHeaders(name); |
||||
} |
||||
|
||||
@Override |
||||
public Enumeration<String> getHeaderNames() { |
||||
Map<String, String> headers = new HashMap<>(); |
||||
HttpServletRequest originalRequest = (HttpServletRequest) getRequest(); |
||||
Enumeration<String> headerNames = originalRequest.getHeaderNames(); |
||||
while (headerNames.hasMoreElements()) { |
||||
String headerName = headerNames.nextElement(); |
||||
headers.put(headerName, headerName); |
||||
} |
||||
headers.put("Content-Type", "Content-Type"); |
||||
return Collections.enumeration(headers.keySet()); |
||||
} |
||||
} |
Loading…
Reference in new issue