Skip to content

Commit

Permalink
fix: LazyInitializationException thrown by handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
D-D-H committed Mar 9, 2024
1 parent f4be7f0 commit e03e14b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down Expand Up @@ -36,4 +36,6 @@ default JifaAuthenticationToken register(String name, String username, String pa
String getCurrentUserJwtTokenOrNull();

UserEntity getCurrentUser();

UserEntity getCurrentUserRef();
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public long handleTransferRequest(FileTransferRequest request) {
Validate.isFalse(config.getDisabledFileTransferMethods().contains(request.getMethod()), FILE_TRANSFER_METHOD_DISABLED);

if (isMaster()) {
FileLocation location = workerService.decideLocationForNewFile(userService.getCurrentUser(), request.getType());
FileLocation location = workerService.decideLocationForNewFile(userService.getCurrentUserRef(), request.getType());
assert location.valid();
if (!location.useSharedStorage()) {
return workerService.syncRequest(location.staticWorker(),
Expand All @@ -194,7 +194,7 @@ public long handleTransferRequest(FileTransferRequest request) {

TransferringFileEntity transferringFile = new TransferringFileEntity();
transferringFile.setUniqueName(generateFileUniqueName());
transferringFile.setUser(userService.getCurrentUser());
transferringFile.setUser(userService.getCurrentUserRef());
transferringFile.setOriginalName(FileTransferUtil.extractOriginalName(request));
transferringFile.setType(request.getType());
transferringFile.setTransferState(FileTransferState.IN_PROGRESS);
Expand Down Expand Up @@ -228,7 +228,7 @@ public long handleUploadRequest(FileType type, MultipartFile file) throws Throwa
Validate.isFalse(config.getDisabledFileTransferMethods().contains(FileTransferMethod.UPLOAD), FILE_TRANSFER_METHOD_DISABLED);

if (isMaster()) {
FileLocation location = workerService.decideLocationForNewFile(userService.getCurrentUser(), type);
FileLocation location = workerService.decideLocationForNewFile(userService.getCurrentUserRef(), type);
if (!location.useSharedStorage()) {
return workerService.forwardUploadRequestToStaticWorker(location.staticWorker(), type, file);
}
Expand All @@ -246,7 +246,7 @@ public long handleUploadRequest(FileType type, MultipartFile file) throws Throwa
return transactionTemplate.execute(status -> {
FileEntity newFile = new FileEntity();
newFile.setUniqueName(uniqueName);
newFile.setUser(userService.getCurrentUser());
newFile.setUser(userService.getCurrentUserRef());
newFile.setOriginalName(originalName);
newFile.setType(type);
newFile.setSize(size);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down Expand Up @@ -38,7 +38,9 @@

import java.util.Optional;

import static org.eclipse.jifa.common.domain.exception.CommonException.CE;
import static org.eclipse.jifa.server.enums.ServerErrorCode.INCORRECT_PASSWORD;
import static org.eclipse.jifa.server.enums.ServerErrorCode.UNSUPPORTED_NAMESPACE;
import static org.eclipse.jifa.server.enums.ServerErrorCode.USERNAME_EXISTS;
import static org.eclipse.jifa.server.enums.ServerErrorCode.USER_NOT_FOUND;

Expand Down Expand Up @@ -188,6 +190,20 @@ public String getCurrentUserJwtTokenOrNull() {

@Override
public UserEntity getCurrentUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication instanceof JifaAuthenticationToken token) {
return userRepo.findById(token.getUserId()).orElseThrow(() -> CE(USER_NOT_FOUND));
}

if (authentication instanceof AnonymousAuthenticationToken) {
return null;
}

throw new ShouldNotReachHereException();
}

@Override
public UserEntity getCurrentUserRef() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication instanceof JifaAuthenticationToken token) {
return userRepo.getReferenceById(token.getUserId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down

0 comments on commit e03e14b

Please sign in to comment.