Skip to content

Commit

Permalink
Merge pull request #35 from aiven/npe
Browse files Browse the repository at this point in the history
Flip equals or use Objects#equals to avoid possible NPE
  • Loading branch information
jlprat authored Sep 26, 2022
2 parents a02f4ab + 8b3f7e3 commit 7253d06
Show file tree
Hide file tree
Showing 31 changed files with 588 additions and 448 deletions.
15 changes: 9 additions & 6 deletions src/main/java/io/aiven/klaw/auth/KwAuthenticationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,18 @@ Authentication searchUserAttributes(HttpServletRequest request, HttpServletRespo

// User found in AD and not in KW db

if (userAttributesObject.get("userFound").equals(Boolean.TRUE)) {
if (Boolean.TRUE.equals(userAttributesObject.get("userFound"))) {
try {
log.info("User found in AD and not in Klaw db :{}", userName);
String existingRegistrationId =
manageDatabase.getHandleDbRequests().getRegistrationId(userName);

if (existingRegistrationId != null) {
if (existingRegistrationId.equals("PENDING_ACTIVATION"))
if ("PENDING_ACTIVATION".equals(existingRegistrationId)) {
response.sendRedirect("registrationReview");
else response.sendRedirect("register?userRegistrationId=" + existingRegistrationId);
} else {
response.sendRedirect("register?userRegistrationId=" + existingRegistrationId);
}
} else {
String randomId = UUID.randomUUID().toString();

Expand All @@ -198,11 +200,12 @@ Authentication searchUserAttributes(HttpServletRequest request, HttpServletRespo
registerUserInfoModel.setPwd("");

Attributes attributes = (Attributes) userAttributesObject.get("attributes");
if (attributes.get("mail") != null)
if (attributes.get("mail") != null) {
registerUserInfoModel.setMailid((String) attributes.get("mail").get());
if (attributes.get("displayname") != null)
}
if (attributes.get("displayname") != null) {
registerUserInfoModel.setFullname((String) attributes.get("displayname").get());

}
RegisterUserInfo registerUserInfo = new RegisterUserInfo();
copyProperties(registerUserInfoModel, registerUserInfo);
manageDatabase.getHandleDbRequests().registerUserForAD(registerUserInfo);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/aiven/klaw/auth/KwRequestFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ public void setAuthenticationManager(AuthenticationManager authenticationManager
public Authentication attemptAuthentication(
HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {

if (kwInstallationType.equals("saas")) {
if ("saas".equals(kwInstallationType)) {
String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
boolean captchaResponse = validateCaptchaService.validateCaptcha(gRecaptchaResponse);
if (!captchaResponse) throw new AuthenticationServiceException("Invalid Captcha.");
}

if (authenticationType.equals("ad")) {
// Check if useer exists in kw database
if ("ad".equals(authenticationType)) {
// Check if user exists in kw database
if (manageDatabase.getHandleDbRequests().getUsersInfo(request.getParameter("username"))
== null) {
return kwAuthenticationService.searchUserAttributes(request, response);
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/io/aiven/klaw/config/ManageDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private void loadStaticDataToDb() {
String productName = "Klaw";
Optional<ProductDetails> productDetails = handleDbRequests.selectProductDetails(productName);
if (productDetails.isPresent()) {
if (!productDetails.get().getVersion().equals(kwVersion))
if (!Objects.equals(productDetails.get().getVersion(), kwVersion))
handleDbRequests.insertProductDetails(
defaultDataService.getProductDetails(productName, kwVersion));
} else
Expand All @@ -208,7 +208,7 @@ private void loadStaticDataToDb() {
// }

private void checkSSOAuthentication() {
if (authenticationType.equals("db") && ssoEnabled.equals("true")) {
if ("db".equals(authenticationType) && "true".equals(ssoEnabled)) {
log.error(
"Error : Please configure authentication type to ad, if SSO is enabled. Shutting down..");
shutdownApp();
Expand Down Expand Up @@ -280,7 +280,7 @@ public boolean getIsTrialLicense() {

private Integer getTenantIdFromName(String tenantName) {
return tenantMap.entrySet().stream()
.filter(obj -> obj.getValue().equals(tenantName))
.filter(obj -> Objects.equals(obj.getValue(), tenantName))
.findFirst()
.get()
.getKey();
Expand All @@ -304,7 +304,7 @@ public Integer getTeamIdFromTeamName(int tenantId, String teamName) {
if (teamName != null)
optionalTeam =
teamIdAndNamePerTenant.get(tenantId).entrySet().stream()
.filter(a -> a.getValue().equals(teamName))
.filter(a -> Objects.equals(a.getValue(), teamName))
.findFirst();
else return null;

Expand Down Expand Up @@ -363,15 +363,15 @@ private void loadEnvsForAllTenants() {
public void loadEnvsForOneTenant(Integer tenantId) {
List<Env> kafkaEnvList =
handleDbRequests.selectAllKafkaEnvs(tenantId).stream()
.filter(env -> env.getEnvExists().equals("true"))
.filter(env -> "true".equals(env.getEnvExists()))
.collect(Collectors.toList());
List<Env> schemaEnvList =
handleDbRequests.selectAllSchemaRegEnvs(tenantId).stream()
.filter(env -> env.getEnvExists().equals("true"))
.filter(env -> "true".equals(env.getEnvExists()))
.collect(Collectors.toList());
List<Env> kafkaConnectEnvList =
handleDbRequests.selectAllKafkaConnectEnvs(tenantId).stream()
.filter(env -> env.getEnvExists().equals("true"))
.filter(env -> "true".equals(env.getEnvExists()))
.collect(Collectors.toList());

List<String> envList1 = kafkaEnvList.stream().map(Env::getId).collect(Collectors.toList());
Expand Down Expand Up @@ -403,7 +403,7 @@ public void loadTenantTeamsForOneTenant(List<Team> allTeams, Integer tenantId) {

List<Team> teamList =
allTeams.stream()
.filter(team -> team.getTenantId().equals(tenantId))
.filter(team -> Objects.equals(team.getTenantId(), tenantId))
.collect(Collectors.toList());

for (Team team : teamList) {
Expand Down Expand Up @@ -579,15 +579,15 @@ private void loadEnvironmentsMapForAllTenants() {
public void loadEnvMapForOneTenant(Integer tenantId) {
List<Env> kafkaEnvList =
handleDbRequests.selectAllKafkaEnvs(tenantId).stream()
.filter(env -> env.getEnvExists().equals("true"))
.filter(env -> "true".equals(env.getEnvExists()))
.collect(Collectors.toList());
List<Env> schemaEnvList =
handleDbRequests.selectAllSchemaRegEnvs(tenantId).stream()
.filter(env -> env.getEnvExists().equals("true"))
.filter(env -> "true".equals(env.getEnvExists()))
.collect(Collectors.toList());
List<Env> kafkaConnectEnvList =
handleDbRequests.selectAllKafkaConnectEnvs(tenantId).stream()
.filter(env -> env.getEnvExists().equals("true"))
.filter(env -> "true".equals(env.getEnvExists()))
.collect(Collectors.toList());
List<Env> allEnvList = new ArrayList<>();
allEnvList.addAll(kafkaEnvList);
Expand All @@ -601,7 +601,7 @@ public void loadEnvMapForOneTenant(Integer tenantId) {

List<Env> kafkaEnvTenantList =
kafkaEnvList.stream()
.filter(kafkaEnv -> kafkaEnv.getTenantId().equals(tenantId))
.filter(kafkaEnv -> Objects.equals(kafkaEnv.getTenantId(), tenantId))
.collect(Collectors.toList());
Map<String, Map<String, List<String>>> envParamsMap = new HashMap<>();

Expand Down
45 changes: 25 additions & 20 deletions src/main/java/io/aiven/klaw/helpers/db/rdbms/InsertDataJdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -102,8 +103,11 @@ public synchronized Map<String, String> insertIntoRequestTopic(TopicRequest topi
activityLog.setEnv(topicRequest.getEnvironment());
activityLog.setTenantId(topicRequest.getTenantId());

if (insertIntoActivityLog(activityLog).equals("success")) hashMap.put("result", "success");
else hashMap.put("result", "failure");
if ("success".equals(insertIntoActivityLog(activityLog))) {
hashMap.put("result", "success");
} else {
hashMap.put("result", "failure");
}

return hashMap;
}
Expand Down Expand Up @@ -136,8 +140,11 @@ public synchronized Map<String, String> insertIntoRequestConnector(
activityLog.setEnv(connectorRequest.getEnvironment());
activityLog.setTenantId(connectorRequest.getTenantId());

if (insertIntoActivityLog(activityLog).equals("success")) hashMap.put("result", "success");
else hashMap.put("result", "failure");
if ("success".equals(insertIntoActivityLog(activityLog))) {
hashMap.put("result", "success");
} else {
hashMap.put("result", "failure");
}

return hashMap;
}
Expand Down Expand Up @@ -304,7 +311,7 @@ public String insertIntoTeams(Team team) {

// making unique teamnames per tenant
if (teamRepo.findAllByTenantId(team.getTenantId()).stream()
.anyMatch(team1 -> team1.getTeamname().equals(team.getTeamname()))) {
.anyMatch(team1 -> Objects.equals(team1.getTeamname(), team.getTeamname()))) {
return "Failure. Team already exists";
}

Expand Down Expand Up @@ -347,10 +354,10 @@ public String insertIntoRegisterUsers(RegisterUserInfo userInfo) {

// STAGING status comes from AD users
if (userExists.isPresent()) {
if (userExists.get().getStatus().equals("APPROVED")) {
if ("APPROVED".equals(userExists.get().getStatus())) {
// do nothing -- user is deleted
} else if (!userExists.get().getStatus().equals("STAGING")
&& !userExists.get().getStatus().equals("PENDING"))
} else if (!"STAGING".equals(userExists.get().getStatus())
&& !"PENDING".equals(userExists.get().getStatus()))
return "Failure. Registration already exists";
}

Expand Down Expand Up @@ -384,13 +391,11 @@ public synchronized Integer getNextActivityLogRequestId(int tenantId) {
}

public Integer getNextTopicRequestId(String idType, int tenantId) {
Integer topicId;
if (idType.equals("TOPIC_REQ_ID")) topicId = topicRequestsRepo.getNextTopicRequestId(tenantId);
else if (idType.equals("TOPIC_ID")) topicId = topicRepo.getNextTopicRequestId(tenantId);
else topicId = null;
Integer topicId = null;
if ("TOPIC_REQ_ID".equals(idType)) topicId = topicRequestsRepo.getNextTopicRequestId(tenantId);
else if ("TOPIC_ID".equals(idType)) topicId = topicRepo.getNextTopicRequestId(tenantId);

if (topicId == null) return 1001;
else return topicId + 1;
return topicId == null ? 1001 : topicId + 1;
}

public Integer getNextConnectorRequestId(String idType, int tenantId) {
Expand All @@ -406,14 +411,14 @@ else if (idType.equals("CONNECTOR_ID"))
}

public Integer getNextSchemaRequestId(String idType, int tenantId) {
Integer schemaReqId;
if (idType.equals("SCHEMA_REQ_ID"))
Integer schemaReqId = null;
if ("SCHEMA_REQ_ID".equals(idType)) {
schemaReqId = schemaRequestRepo.getNextSchemaRequestId(tenantId);
else if (idType.equals("SCHEMA_ID")) schemaReqId = messageSchemaRepo.getNextSchemaId(tenantId);
else schemaReqId = null;
} else if ("SCHEMA_ID".equals(idType)) {
schemaReqId = messageSchemaRepo.getNextSchemaId(tenantId);
}

if (schemaReqId == null) return 1001;
else return schemaReqId + 1;
return schemaReqId == null ? 1001 : schemaReqId + 1;
}

public String addNewTenant(KwTenants kwTenants) {
Expand Down
Loading

0 comments on commit 7253d06

Please sign in to comment.