This file contains hidden or 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
protected final Function<UserNotPresentError, ResponseEntity<?>> userFacadeErrorHandler = | |
userNotPresentError -> ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); | |
protected final Function<Either<ItemsError, ?>, ResponseEntity> resultHandler = serviceResult -> serviceResult.fold( | |
itemsError -> handleItemsError(itemsError), | |
successValue -> Match(successValue).of( | |
Case($(), () -> ResponseEntity.ok(successValue).build()) | |
) | |
); |
This file contains hidden or 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
protected Either<UserNotPresentError, UserId> userOrFail(RequestContext requestContext) { | |
return requestContext.userId().toEither(UserNotPresentError::new); | |
} |
This file contains hidden or 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
protected <T> Either<ItemsDbError, T> tryDbCall(Supplier<T> dbCall) { | |
return Try.of(dbCall::get) | |
.fold( | |
dbError -> { | |
logger.warn("Problem executing db operation", dbError); | |
return Left(new ItemsDbError(dbError.getMessage())); | |
}, | |
Either::right | |
); | |
} |
This file contains hidden or 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
@Bean | |
@Scope(scopeName = ScopeConfig.THREAD_INHERITED, | |
proxyMode = ScopedProxyMode.TARGET_CLASS) | |
public TokenString tokenString() { | |
HttpServletRequest request = ((ServletRequestAttributes) | |
RequestContextHolder | |
.currentRequestAttributes()) | |
.getRequest(); | |
String value = request.getHeader("Authorization").split(" ")[1]; | |
return new TokenString(value); |
This file contains hidden or 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
@Bean | |
public CustomScopeConfigurer customScopeConfigurer() { | |
CustomScopeConfigurer configurer = new CustomScopeConfigurer(); | |
Map<String, Object> scopes = new HashMap<>(); | |
scopes.put(THREAD_INHERITED, new InheritedThreadScope()); | |
configurer.setScopes(scopes); | |
return configurer; | |
} |
This file contains hidden or 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
public class InheritedThreadScope implements Scope { | |
private static final Logger logger = LoggerFactory.getLogger(InheritedThreadScope.class); | |
private final ThreadLocal<Map<String, Object>> threadScope = | |
new InheritableThreadLocal() { | |
@Override | |
protected Map<String, Object> initialValue() { | |
return new HashMap<>(); | |
} |
This file contains hidden or 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
@GetMapping | |
public CompletableFuture<ResponseEntity> get() { | |
logger.info("GET request"); | |
return myService.callBAsync().thenApply(status -> | |
status.equals("ok") ? | |
ResponseEntity.ok().build() : | |
ResponseEntity.badRequest().build()); | |
} |
This file contains hidden or 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
@Autowired | |
TokenString tokenString; | |
public CompletableFuture<String> callBAsync() { | |
return supplyAsync(() -> { | |
logger.info("calling B"); | |
RestTemplate restTemplate = new RestTemplate(); | |
HttpHeaders headers = new HttpHeaders(); | |
headers.set("Authorization", "bearer "+tokenString.getValue()); | |
HttpEntity entity = new HttpEntity(headers); | |
ResponseEntity re = restTemplate.exchange( |
This file contains hidden or 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
@GetMapping | |
public CompletableFuture<ResponseEntity> get() { | |
return supplyAsync(() -> ResponseEntity.ok().build()) | |
} |
This file contains hidden or 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
public class TokenString { | |
private final String value; | |
public TokenString(String value) { | |
this.value = value; | |
} | |
public String getValue() { | |
return this.value; | |
} |
NewerOlder