yangys
2024-04-01 14f1953b1944b3e53d8312e151902c4695faa2e1
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
package com.qianwen.core.context;
 
import java.util.function.Function;
import com.qianwen.core.context.props.BladeContextProperties;
import com.qianwen.core.tool.utils.StringUtil;
import com.qianwen.core.tool.utils.ThreadLocalUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.http.HttpHeaders;
import org.springframework.lang.Nullable;
 
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
/* loaded from: blade-core-context-9.3.0.0-SNAPSHOT.jar:org/springblade/core/context/BladeServletContext.class */
public class BladeServletContext implements BladeContext {
    private final BladeContextProperties contextProperties;
    private final BladeHttpHeadersGetter httpHeadersGetter;
 
    public BladeServletContext(final BladeContextProperties contextProperties, final BladeHttpHeadersGetter httpHeadersGetter) {
        this.contextProperties = contextProperties;
        this.httpHeadersGetter = httpHeadersGetter;
    }
 
    @Override // org.springblade.core.context.BladeContext
    @Nullable
    public String getRequestId() {
        return get(this.contextProperties.getHeaders().getRequestId());
    }
 
    @Override // org.springblade.core.context.BladeContext
    @Nullable
    public String getAccountId() {
        return get(this.contextProperties.getHeaders().getAccountId());
    }
 
    @Override // org.springblade.core.context.BladeContext
    @Nullable
    public String getTenantId() {
        return get(this.contextProperties.getHeaders().getTenantId());
    }
 
    @Override // org.springblade.core.context.BladeContext
    @Nullable
    public String get(String ctxKey) {
        HttpHeaders headers = (HttpHeaders)ThreadLocalUtil.getIfAbsent("bladeContext", this.httpHeadersGetter::get);
        if (headers == null || headers.isEmpty()) {
          return null;
        }
        return headers.getFirst(ctxKey);
    }
 
    @Override // org.springblade.core.context.BladeContext
    @Nullable
    public <T> T get(String ctxKey, Function<String, T> function) {
        String ctxValue = get(ctxKey);
        if (StringUtil.isBlank(ctxValue)) {
            return null;
        }
        return function.apply(ctxKey);
    }
}