forked from TencentBlueKing/bk-job
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: 处理Esb请求时,请求结束后加上出口日志,web过滤器只处理request,esb和service过滤器处理request和r…
…esponse TencentBlueKing#3378
- Loading branch information
Showing
4 changed files
with
103 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...main/java/com/tencent/bk/job/common/web/filter/WebRepeatableReadServletRequestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.tencent.bk.job.common.web.filter; | ||
|
||
import com.tencent.bk.job.common.web.filter.utils.ServletUtil; | ||
import com.tencent.bk.job.common.web.model.RepeatableReadWriteHttpServletRequest; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import java.io.IOException; | ||
|
||
public class WebRepeatableReadServletRequestFilter implements Filter { | ||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
// do nothing | ||
} | ||
|
||
/** | ||
* 仅包装ServletRequest,给/web使用 | ||
* | ||
*/ | ||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, | ||
FilterChain chain) throws IOException, ServletException { | ||
ServletRequest servletRequest = request; | ||
|
||
if (ServletUtil.isJsonRequest(request)) { | ||
// 仅处理 ContentType: application/json 请求 | ||
servletRequest = new RepeatableReadWriteHttpServletRequest((HttpServletRequest) request); | ||
} | ||
|
||
chain.doFilter(servletRequest, response); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
// do nothing | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...mons/common-web/src/main/java/com/tencent/bk/job/common/web/filter/utils/ServletUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.tencent.bk.job.common.web.filter.utils; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import javax.servlet.ServletRequest; | ||
|
||
|
||
/** | ||
* 处理servlet请求和响应的工具类 | ||
*/ | ||
public class ServletUtil { | ||
|
||
/** | ||
* 判断 servlet 请求的 Content-Type 是否是 application/json | ||
* @param request 请求 | ||
* @return 请求的 Content-Type 是否是 application/json | ||
*/ | ||
public static boolean isJsonRequest(ServletRequest request) { | ||
return isJsonContentType(request.getContentType()); | ||
} | ||
|
||
private static boolean isJsonContentType(String contentType) { | ||
if (StringUtils.isBlank(contentType)) { | ||
return false; | ||
} | ||
contentType = contentType.trim().toLowerCase(); | ||
return contentType.startsWith("application/json"); | ||
} | ||
} |