java110
2021-05-01 2e25cc13082decf083b010028813247c07f86947
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.java110.service.configuration;
 
 
import brave.internal.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestHeader;
 
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Set;
 
public class StringDecoderForHeaderConverter implements GenericConverter {
    private Logger logger = LoggerFactory.getLogger(StringDecoderForHeaderConverter.class);
 
    private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    private static final String NO_NAME = "NO_NAME";
 
    private Charset charset;
 
    public StringDecoderForHeaderConverter(@Nullable Charset charset) {
        this.charset = charset;
        if (this.charset == null) {
            this.charset = DEFAULT_CHARSET;
        }
    }
 
    /**
     * +返回编码值
     * @return charset
     */
    public Charset getCharset() {
        return charset;
    }
 
    /**
     * +设置编码值
     * @param charset 编码值
     */
    public void setCharset(Charset charset) {
        this.charset = charset;
 
        if (this.charset == null) {
            this.charset = DEFAULT_CHARSET;
        }
    }
 
    @Override
    public Set<ConvertiblePair> getConvertibleTypes() {
        return Collections.singleton(new ConvertiblePair(String.class, String.class));
    }
 
    @Override
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
        if (ObjectUtils.isEmpty(source)) {
            return source;
        }
 
        String name = needDecoder(source, targetType);
        if (name != null) {
            return convert(source.toString(), name);
        }
 
        return source;
    }
 
    /**
     * +是否需要解码
     * @param source 待处理的值
     * @param targetType 类型
     * @return 非null:需要解码;null:无需解码
     */
    private String needDecoder(Object source, TypeDescriptor targetType) {
        RequestHeader requestHeader = targetType.getAnnotation(RequestHeader.class);
        Class<?> type = targetType.getType();
        if (requestHeader != null && type == String.class) {
            if (source.toString().indexOf("%") >= 0) {
                String name = requestHeader.name();
                if (name == null || name.equals("")) {
                    name = requestHeader.value();
                }
                if (name == null || name.equals("")) {
                    name = NO_NAME;
                }
 
                return name;
            }
        }
 
        return null;
    }
 
    /**
     * +结果解码
     * @param source 待解码的结果
     * @param name 参数名称
     * @return 解码后的结果
     */
    private String convert(final String source, final String name) {
        if (logger.isDebugEnabled()) {
            logger.debug("Begin convert[" + source + "] for RequestHeader[" + name + "].");
        }
        String _result = null;
        try {
            _result = URLDecoder.decode(source, this.charset.name());
            if (logger.isDebugEnabled()) {
                logger.debug("Success convert[" + source + ", " + _result + "] for RequestHeader[" + name + "].");
            }
 
            return _result;
        } catch(Exception e) {
            logger.warn("Fail convert[" + source + "] for RequestHeader[" + name + "]!", e);
        }
 
        return source;
    }
}