Reputation: 649
This is my method.
public boolean authenticateAndPollCallbackResult(BankIdAuthRequest bankIdAuthRequest) {
ResponseEntity<BankIdAuthResponse> authResponse = bankIdAuthentication(bankIdAuthRequest);
AbstractApplicationForm applicationForm = applicationFormRepository.findByToken(bankIdAuthRequest.getRefID());
try {
//Add new bankId authentication to database.
BankIdAuthenticationEntity bankIdAuthenticationEntity = new BankIdAuthenticationEntity();
bankIdAuthenticationEntity.setAbstractApplicationForm(applicationForm);
bankIdAuthenticationEntity.setAuthStatus(STATUS_PROGRESS);
bankIdAuthenticationEntity.setOrderReference(authResponse.getBody().getOrderRef());
bankIdAuthenticationEntity.setAutoStartToken(authResponse.getBody().getAutoStartToken());
Long bankIdAuthenticationId = bankIdAuthenticationRepository.save(bankIdAuthenticationEntity).getId();
BankIdAuthenticationEntity.AuthStatus authStatus;
do {
TimeUnit.MILLISECONDS.sleep(1500);
authStatus = getAuthStatus(bankIdAuthenticationId);
if (authStatus == BankIdAuthenticationEntity.AuthStatus.COMPLETED)
return true;
if (authStatus == BankIdAuthenticationEntity.AuthStatus.FAILED || authStatus == BankIdAuthenticationEntity.AuthStatus.NOT_ASSIGNED)
return false;
} while (authStatus == BankIdAuthenticationEntity.AuthStatus.PROGRESS);
} catch (InterruptedException e) {
log.error("InterruptedException: ", e);
Thread.currentThread().interrupt();
} catch (NullPointerException e) {
log.error("Either BankId API not responding correctly. Check server connection", e);
} catch (Exception e) {
log.error("Exception: Polling collect endpoint method failed", e);
}
return false;
}
Now SonarQube warns that these two lines can return null (and they can):
bankIdAuthenticationEntity.setOrderReference(authResponse.getBody().getOrderRef());
bankIdAuthenticationEntity.setAutoStartToken(authResponse.getBody().getAutoStartToken();
But i don't know what the best way to check for null is. I tried using Objects.requireNonNull which throws a null and the i figured the null check would catch it but it just feel ugly and not correct.
Any suggestions or absolute correct ways of doing this that i might have missed?
Upvotes: 0
Views: 414
Reputation: 3841
The problem is that authResponse.getBody()
can be null. Right?
In this cas you should check it before an either throw an exception or not execute the two lines:
if(authResponse.getBody() != null {
bankIdAuthenticationEntity.setOrderReference(authResponse.getBody().getOrderRef());
bankIdAuthenticationEntity.setAutoStartToken(authResponse.getBody().getAutoStartToken();
})
or
if(authResponse.getBody() == null {
throw new ....Exception();
}
And if the problem is that getOrderRef()
or getAutoStartToken()
could return null, you should check these values before and handle the cases when they are null.
Upvotes: 1