当前位置:网站首页>Application of decorator mode, packaging ServletRequest and adding addparameter method
Application of decorator mode, packaging ServletRequest and adding addparameter method
2022-06-29 09:51:00 【milugloomy】
What is the decorator mode
Decorator mode (Decorator Pattern) Allow a to Add new functions to existing objects , Without changing its structure , It is a wrapper for existing classes . This pattern creates a Decoration , Used to wrap the original class , And under the premise of maintaining the integrity of class method signature , Provides additional functionality .
key word : Existing objects , Add new features .
The method of subclassing is to add new functions to the parent class , For class . The decorator pattern is for an existing object , Rather than class .
explain
See the following example :
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequestWrapper servletRequest = (HttpServletRequestWrapper) req;
AuthServletRequest request = new AuthServletRequest(servletRequest);
//...
chain.doFilter(request, req);
}there servletRequest Is an existing object , Here we want to dynamically extend servletRequest Methods , The method of subclass inheritance is not easy to implement , But it can be easily realized through decorator mode .
AuthServletRequest It's decoration , Packed the original servletRequest, Guarantee servletRequest Complete case , Implemented additional functions .
Increase method
The gateway is used in the project , The gateway resolves jwt after , take userId and role adopt header Transfer to business item , Structure diagram is as follows :

Business items are obtained userId and role after , I hope it can be stuffed into HttpRequest Of parameter, In this way spring Project Controller Can be directly defined in the method , Simplify the development of business projects . Like this :
RequestMapping("/sysUser")
public MyResEntity sysUser(Integer userId){
SysUser sysUser = sysUserService.sysUser(userId);
return new MyResEntity(sysUser);
}however HttpServletRequest No parameter is added to parameter This method , It is impossible to realize the above requirements .
Here comes the decorator mode . newly added AuthServletRequest class , as follows :
public class AuthServletRequest extends HttpServletRequestWrapper {
// Storage request Data Map
private Map<String, String[]> params = new HashMap<String, String[]>();
public AuthServletRequest(HttpServletRequestWrapper request) {
super(request);
// Put the existing parameter Pass to params
this.params.putAll(request.getParameterMap());
}
// rewrite getParameter, Represents the parameters from the current class map obtain
@Override
public String getParameter(String name) {
String[] values = params.get(name);
if (values == null || values.length == 0) {
return null;
}
return values[0];
}
// rewrite getParameterValues, Represents the parameters from the current class map obtain
@Override
public String[] getParameterValues(String name) {
return params.get(name);
}
// The core approach , Add parameter Methods
public void addParameter(String name, Object value) {
if (value != null) {
System.out.println(value);
if (value instanceof String[]) {
params.put(name, (String[]) value);
} else if (value instanceof String) {
params.put(name, new String[]{(String) value});
} else {
params.put(name, new String[]{String.valueOf(value)});
}
}
}
}Then add a new filter AuthFilter, The code is as follows :
public class AuthFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequestWrapper servletRequest = (HttpServletRequestWrapper) req;
AuthServletRequest request = new AuthServletRequest(servletRequest);
HttpServletResponse response = (HttpServletResponse) res;
// header The value in is gateway analysis jwt The resulting value
String userId = request.getHeader("userId");
if (userId != null && request.getParameter("userId") == null) {
request.addParameter("userId", userId);
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
Conclusion
We use decorator mode , Dynamically give... When the project is running ServletRequest Added addParameter Method , Extends the original object .
Decorator pattern is an alternative pattern of inheritance , Its advantage is that it can dynamically extend the functions of an implementation class . The decorator mode is jdk And various frameworks , for example :
stay Java in ,InputStream,FileInputStream,BufferedInputStream etc. IO Stream operation uses decorator mode .
stay spring Use redis Realization session Shared functions ,spring Also use decorator mode to wrap HttpServletRequest, Decoration is SessionRepositoryRequestWrapper, It rewrites getSession Method , from redis Create and get session object .
边栏推荐
- 云管理平台:OpenStack架构设计及详细解读
- The principle of session and cookie
- UE4 材质UV纹理不随模型缩放拉伸
- Could not open JDBC connection for transaction
- Five heart charity matchmaker team
- CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
- Data visualization: the significance of data visualization
- 监控数据源连接池使用情况
- Factory mode
- In the era of data processing, data quality construction is the way for enterprises to survive
猜你喜欢

基于stm32标准库独立按键的多按键状态机的实现

Fabrication d'une calculatrice d'addition simple basée sur pyqt5 et Qt Designer

Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images

UE4 动画重定向

A 2.5D Cancer Segmentation for MRI Images Based on U-Net

用户级线程和内核级线程

Implementation of multi key state machine based on STM32 standard library

ORA-01950 对表空间无权限

UE4 蓝图修改Array 中Get a copy 为 reference

Pytorch summary learning series - operation
随机推荐
linux下centos7中mysql5.7安装教程
Segmentation of Head and Neck Tumours Using Modified U-net
Data governance: Metadata Management (Part 2)
General part: cognition, design and best practice of prototype design
官方stm32芯片包下载地址 stm32f10x stm32f40x下载
Slider validation code
Es error nonodeavailableexception[none of the configured nodes are available:[.127.0.0.1}{127.0.0.1:9300]
c#判断数组是否包含另一个数组的任何项
UE4 blueprint modify get a copy in array to reference
基于PyQt5和Qt Designer的简易加法计算器的制作
Visual assist plug-in settings for UE4 vs
转载 :判断对象是否具有属性的5种方法
CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
Iso16000-9 testing of volatile organic compounds in building products and furniture
A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
LeetCode刷题——泰波那契数列
Western Polytechnic University, one of the "seven national defense schools", was attacked by overseas networks
数据治理:数据治理在数据中台下的解决方案
Simplicity studio does not recognize the new JLINK V9 solution
数据治理:数据标准管理(第三篇)