Skip to content

Commit 3cf3362

Browse files
committed
refactor: 替换过时API
1 parent da6f7aa commit 3cf3362

File tree

10 files changed

+48
-55
lines changed

10 files changed

+48
-55
lines changed

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/ReactiveTokenAuthenticationSupplier.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import lombok.AllArgsConstructor;
44
import org.hswebframework.web.authorization.Authentication;
55
import org.hswebframework.web.authorization.ReactiveAuthenticationSupplier;
6-
import org.hswebframework.web.context.ContextKey;
7-
import org.hswebframework.web.context.ContextUtils;
8-
import org.hswebframework.web.logger.ReactiveLogger;
96
import reactor.core.publisher.Mono;
107

118
@AllArgsConstructor

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/UserTokenAuthenticationSupplier.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.hswebframework.web.authorization.token;
22

33
import org.hswebframework.web.authorization.*;
4-
import org.hswebframework.web.context.ContextKey;
5-
import org.hswebframework.web.context.ContextUtils;
64
import org.springframework.beans.factory.annotation.Autowired;
75
import reactor.core.publisher.Mono;
86

@@ -65,13 +63,14 @@ protected Optional<Authentication> get(AuthenticationManager authenticationManag
6563
@Override
6664
public Optional<Authentication> get() {
6765

68-
return ContextUtils.currentContext()
69-
.get(ContextKey.of(ParsedToken.class))
70-
.map(t -> userTokenManager.getByToken(t.getToken()))
71-
.map(tokenMono -> tokenMono
72-
.map(token -> get(thirdPartAuthenticationManager.get(token.getType()), token.getUserId()))
73-
.flatMap(Mono::justOrEmpty))
74-
.flatMap(Mono::blockOptional);
66+
67+
return Optional
68+
.ofNullable(UserTokenHolder.currentToken())
69+
.map(t -> userTokenManager.getByToken(t.getToken()))
70+
.map(tokenMono -> tokenMono
71+
.map(token -> get(thirdPartAuthenticationManager.get(token.getType()), token.getUserId()))
72+
.flatMap(Mono::justOrEmpty))
73+
.flatMap(Mono::blockOptional);
7574

7675
}
7776
}

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/UserTokenReactiveAuthenticationSupplier.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
import org.hswebframework.web.authorization.Authentication;
44
import org.hswebframework.web.authorization.ReactiveAuthenticationManager;
55
import org.hswebframework.web.authorization.ReactiveAuthenticationSupplier;
6-
import org.hswebframework.web.authorization.exception.UnAuthorizedException;
7-
import org.hswebframework.web.context.ContextKey;
8-
import org.hswebframework.web.context.ContextUtils;
9-
import org.hswebframework.web.logger.ReactiveLogger;
106
import org.springframework.beans.factory.annotation.Autowired;
117
import reactor.core.publisher.Mono;
128

hsweb-authorization/hsweb-authorization-api/src/test/java/org/hswebframework/web/authorization/AuthenticationTests.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,15 @@
44
import org.hswebframework.web.authorization.simple.builder.SimpleAuthenticationBuilder;
55
import org.hswebframework.web.authorization.simple.builder.SimpleDataAccessConfigBuilderFactory;
66
import org.hswebframework.web.authorization.token.*;
7-
import org.hswebframework.web.context.ContextKey;
87
import org.hswebframework.web.logger.ReactiveLogger;
98
import org.junit.Assert;
109
import org.junit.Before;
1110
import org.junit.Test;
1211
import org.springframework.context.support.StaticApplicationContext;
1312
import reactor.core.publisher.Mono;
14-
import reactor.core.publisher.SignalType;
1513
import reactor.test.StepVerifier;
1614
import reactor.util.context.Context;
1715

18-
import java.util.Collections;
19-
import java.util.Set;
20-
21-
import static org.hswebframework.web.context.ContextUtils.*;
2216
import static org.junit.Assert.*;
2317

2418
public class AuthenticationTests {

hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/web/ReactiveUserTokenController.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88
import org.hswebframework.web.authorization.annotation.QueryAction;
99
import org.hswebframework.web.authorization.annotation.Resource;
1010
import org.hswebframework.web.authorization.annotation.SaveAction;
11-
import org.hswebframework.web.authorization.exception.UnAuthorizedException;
1211
import org.hswebframework.web.authorization.token.ParsedToken;
1312
import org.hswebframework.web.authorization.token.TokenState;
1413
import org.hswebframework.web.authorization.token.UserToken;
1514
import org.hswebframework.web.authorization.token.UserTokenManager;
16-
import org.hswebframework.web.context.ContextKey;
17-
import org.hswebframework.web.context.ContextUtils;
1815
import org.springframework.beans.factory.annotation.Autowired;
1916
import org.springframework.context.annotation.Lazy;
2017
import org.springframework.web.bind.annotation.*;

hsweb-core/src/main/java/org/hswebframework/web/context/ContextKey.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
@AllArgsConstructor
77
@Getter
8+
@Deprecated
89
public final class ContextKey<T> {
910

1011
private final String key;

hsweb-core/src/main/java/org/hswebframework/web/context/ContextUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/**
1010
* @since 4.0.0
1111
*/
12+
@Deprecated
1213
public class ContextUtils {
1314

1415
private static final ThreadLocal<Context> contextThreadLocal = ThreadLocal.withInitial(MapContext::new);

hsweb-datasource/hsweb-datasource-api/src/main/java/org/hswebframework/web/datasource/switcher/DefaultReactiveSwitcher.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package org.hswebframework.web.datasource.switcher;
22

33
import lombok.extern.slf4j.Slf4j;
4-
import org.hswebframework.web.context.ContextKey;
5-
import org.hswebframework.web.context.ContextUtils;
64
import org.reactivestreams.Publisher;
75
import reactor.core.publisher.Flux;
86
import reactor.core.publisher.Mono;
7+
import reactor.util.context.Context;
98

109
import java.util.Collection;
1110
import java.util.Deque;
@@ -16,44 +15,52 @@
1615
@Slf4j
1716
public class DefaultReactiveSwitcher implements ReactiveSwitcher {
1817

19-
private String name;
18+
private final String name;
2019

21-
private String defaultId;
20+
private final String defaultId;
2221

23-
private String type;
22+
private final String type;
2423

25-
public DefaultReactiveSwitcher(String name,String type) {
24+
public DefaultReactiveSwitcher(String name, String type) {
2625
this.name = "ReactiveSwitcher.".concat(name);
2726
this.defaultId = name.concat(".").concat("_default");
28-
this.type=type;
27+
this.type = type;
2928
}
3029

3130
@Deprecated
3231
private <R> Mono<R> doInContext(Function<Deque<String>, Mono<R>> function) {
33-
return ContextUtils.reactiveContext()
34-
.map(ctx -> ctx.getOrDefault(ContextKey.<Deque<String>>of(this.name), LinkedList::new))
35-
.flatMap(function);
32+
33+
return Mono
34+
.deferContextual(ctx -> function.apply(ctx
35+
.<Deque<String>>getOrEmpty(this.name)
36+
.orElseGet(LinkedList::new)));
3637
}
3738

3839
@SuppressWarnings("all")
39-
private <R extends Publisher<?>> R doInContext(R publisher, Consumer<Deque<String>> consumer) {
40+
private <R extends Publisher<?>> R doInContext(R publisher, Consumer<Deque<String>> consumer) {
4041
if (publisher instanceof Mono) {
41-
return (R)((Mono<?>) publisher)
42-
.contextWrite(ContextUtils.acceptContext(ctx -> {
43-
consumer.accept(ctx.getOrDefault(ContextKey.<Deque<String>>of(this.name), LinkedList::new));
44-
}));
42+
return (R) Mono
43+
.deferContextual(ctx -> {
44+
Deque<String> deque = ctx.<Deque<String>>getOrEmpty(this.name).orElseGet(LinkedList::new);
45+
consumer.accept(deque);
46+
return ((Mono<R>) publisher)
47+
.contextWrite(Context.of(name, deque));
48+
});
4549
} else if (publisher instanceof Flux) {
46-
return (R)((Flux<?>) publisher)
47-
.contextWrite(ContextUtils.acceptContext(ctx -> {
48-
consumer.accept(ctx.getOrDefault(ContextKey.<Deque<String>>of(this.name), LinkedList::new));
49-
}));
50+
return (R) Flux
51+
.deferContextual(ctx -> {
52+
Deque<String> deque = ctx.<Deque<String>>getOrEmpty(this.name).orElseGet(LinkedList::new);
53+
consumer.accept(deque);
54+
return ((Flux<R>) publisher)
55+
.contextWrite(Context.of(name, deque));
56+
});
5057
}
5158
return publisher;
5259
}
5360

5461
@Override
55-
public <P extends Publisher<?>> P useLast(P publisher) {
56-
return doInContext(publisher,queue -> {
62+
public <P extends Publisher<?>> P useLast(P publisher) {
63+
return doInContext(publisher, queue -> {
5764
// 没有上一次了
5865
if (queue.isEmpty()) {
5966
return;
@@ -66,16 +73,16 @@ public <P extends Publisher<?>> P useLast(P publisher) {
6673

6774
@Override
6875
public <P extends Publisher<?>> P use(P publisher, String id) {
69-
return doInContext(publisher,queue-> queue.addLast(id));
76+
return doInContext(publisher, queue -> queue.addLast(id));
7077
}
7178

7279
@Override
73-
public <P extends Publisher<?>> P useDefault(P publisher) {
74-
return use(publisher,defaultId);
80+
public <P extends Publisher<?>> P useDefault(P publisher) {
81+
return use(publisher, defaultId);
7582
}
7683

7784
@Override
78-
public <P extends Publisher<?>> P reset(P publisher) {
85+
public <P extends Publisher<?>> P reset(P publisher) {
7986
return doInContext(publisher, Collection::clear);
8087
}
8188

hsweb-datasource/hsweb-datasource-api/src/main/java/org/hswebframework/web/datasource/switcher/DefaultSwitcher.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.hswebframework.web.datasource.switcher;
22

33
import lombok.extern.slf4j.Slf4j;
4-
import org.hswebframework.web.context.ContextKey;
5-
import org.hswebframework.web.context.ContextUtils;
4+
import org.hswebframework.web.context.ContextHolder;
65

76

87
import java.util.Deque;
@@ -26,8 +25,10 @@ public DefaultSwitcher(String name, String type) {
2625

2726
protected Deque<String> getUsedHistoryQueue() {
2827
// 从ThreadLocal中获取一个使用记录
29-
return ContextUtils.currentContext()
30-
.<Deque<String>>getOrDefault(ContextKey.of(name), LinkedList::new);
28+
return ContextHolder
29+
.current()
30+
.<Deque<String>>getOrEmpty(name)
31+
.orElseGet(LinkedList::new);
3132
}
3233

3334
@Override

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@
235235
<plugin>
236236
<groupId>org.apache.maven.plugins</groupId>
237237
<artifactId>maven-source-plugin</artifactId>
238-
<version>2.4</version>
238+
<version>3.3.1</version>
239239
<executions>
240240
<execution>
241241
<id>attach-sources</id>

0 commit comments

Comments
 (0)