From 3e1ec05d5aeb0a02e6051c0f35607e6a91995092 Mon Sep 17 00:00:00 2001 From: Christian Wagner Date: Wed, 14 Jan 2015 17:48:21 +0100 Subject: avoid trying to iterate over a collection that is null (untested) --- .../moa/id/commons/db/NewConfigurationDBRead.java | 79 ++++++++++++++-------- 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/NewConfigurationDBRead.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/NewConfigurationDBRead.java index 470d4d293..8e2ae7e46 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/NewConfigurationDBRead.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/NewConfigurationDBRead.java @@ -1,6 +1,7 @@ package at.gv.egovernment.moa.id.commons.db; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; @@ -21,6 +22,15 @@ public class NewConfigurationDBRead { @Autowired private static Configuration conf; + @SuppressWarnings("unchecked") + public static > T nullGuard(T item) { + if (item == null) { + return (T) Collections.emptyList(); + } else { + return item; + } + } + /** * * @param key @@ -28,10 +38,9 @@ public class NewConfigurationDBRead { * @return */ private static List getAllObjects(String key, Class clazz) { - List result = null; + List result = null; result = conf.getList("getAllUsers", clazz); - return result; } @@ -40,10 +49,15 @@ public class NewConfigurationDBRead { * @return */ public static List getAllUsers() { - Logger.trace("Get All Users from database."); - return getAllObjects("getAllUsers", UserDatabase.class); + List result = getAllObjects("getAllUsers", UserDatabase.class); + if (result.size() == 0) { + Logger.trace("No entries found."); + return null; + } + + return result; } /** @@ -51,7 +65,6 @@ public class NewConfigurationDBRead { * @return */ public static List getAllOnlineApplications() { - Logger.trace("Get All OnlineApplications from database."); return getAllObjects("getAllOnlineApplications", OnlineApplication.class); @@ -62,18 +75,22 @@ public class NewConfigurationDBRead { * @return */ public static List getAllNewOnlineApplications() { - Logger.trace("Get All New OnlineApplications from database."); List result = new ArrayList(); List allOAs = getAllOnlineApplications(); - for (OnlineApplication oa : allOAs) { + for (OnlineApplication oa : nullGuard(allOAs)) { if (!oa.isIsActive() && oa.isIsAdminRequired()) { result.add(oa); } } + if (result.size() == 0) { + Logger.trace("No entries found."); + return null; + } + return result; } @@ -82,7 +99,6 @@ public class NewConfigurationDBRead { * @return */ public static MOAIDConfiguration getMOAIDConfiguration() { - Logger.trace("Load MOAID Configuration from database."); MOAIDConfiguration result = null; @@ -97,18 +113,22 @@ public class NewConfigurationDBRead { * @return */ public static List getAllActiveOnlineApplications() { - Logger.trace("Get All New OnlineApplications from database."); List result = new ArrayList(); List allOAs = getAllOnlineApplications(); - for (OnlineApplication oa : allOAs) { + for (OnlineApplication oa : nullGuard(allOAs)) { if (oa.isIsActive()) { result.add(oa); } } + if (result.size() == 0) { + Logger.trace("No entries found."); + return null; + } + return result; } @@ -118,13 +138,12 @@ public class NewConfigurationDBRead { * @return */ public static OnlineApplication getActiveOnlineApplication(String id) { - Logger.trace("Getting Active OnlineApplication with ID " + id + " from database."); OnlineApplication result = null; List allActiveOAs = getAllActiveOnlineApplications(); - for (OnlineApplication oa : allActiveOAs) { + for (OnlineApplication oa : nullGuard(allActiveOAs)) { String publicUrlPrefix = oa.getPublicURLPrefix(); if (publicUrlPrefix != null && publicUrlPrefix.length() <= id.length()) { if ((id.substring(1, publicUrlPrefix.length()).equals(publicUrlPrefix))) { @@ -152,7 +171,7 @@ public class NewConfigurationDBRead { OnlineApplication result = null; List allOAs = getAllOnlineApplications(); - for (OnlineApplication oa : allOAs) { + for (OnlineApplication oa : nullGuard(allOAs)) { if (oa.getHjid() == dbid) { result = oa; break; @@ -168,13 +187,12 @@ public class NewConfigurationDBRead { * @return */ public static OnlineApplication getOnlineApplication(String id) { - Logger.trace("Getting OnlineApplication with ID " + id + " from database."); OnlineApplication result = null; List allOAs = getAllOnlineApplications(); - for (OnlineApplication oa : allOAs) { + for (OnlineApplication oa : nullGuard(allOAs)) { String publicUrlPrefix = oa.getPublicURLPrefix(); if (publicUrlPrefix != null && publicUrlPrefix.length() <= id.length()) { if (id.substring(1, publicUrlPrefix.length()).equals(publicUrlPrefix)) { @@ -200,10 +218,9 @@ public class NewConfigurationDBRead { Logger.trace("Getting OnlineApplication with ID " + id + " from database."); List result = new ArrayList(); - List allOAs = getAllOnlineApplications(); - for (OnlineApplication oa : allOAs) { + for (OnlineApplication oa : nullGuard(allOAs)) { if (id.equals(oa.getFriendlyName())) { result.add(oa); } @@ -212,10 +229,9 @@ public class NewConfigurationDBRead { if (result.size() == 0) { Logger.trace("No entries found."); return null; - } else { - Logger.trace("Found entries: " + result.size()); - return result; } + + return result; } /** @@ -228,13 +244,19 @@ public class NewConfigurationDBRead { List result = new ArrayList(); List allUsers = getAllUsers(); - for (UserDatabase user : allUsers) { + for (UserDatabase user : nullGuard(allUsers)) { // TODO check result of query "... userdatabase.userRequestTokken is not null" if Tokken is null -> (null, "NULL", "", ... ?) if ((user.getUserRequestTokken() != null || !user.getUserRequestTokken().equals("") || !user.getUserRequestTokken().equals("NULL")) && (user.isIsAdminRequest()) && (!user.isIsMailAddressVerified())) { result.add(user); } } + + if (result.size() == 0) { + Logger.trace("No entries found."); + return null; + } + return result; } @@ -244,13 +266,12 @@ public class NewConfigurationDBRead { * @return */ public static UserDatabase getNewUserWithTokken(String tokken) { - Logger.trace("Getting Userinformation with Tokken " + tokken + " from database."); UserDatabase result = null; List allUsers = getAllUsers(); - for (UserDatabase user : allUsers) { + for (UserDatabase user : nullGuard(allUsers)) { if (user.getUserRequestTokken().equals(tokken)) { result = user; break; @@ -266,12 +287,13 @@ public class NewConfigurationDBRead { * @return */ public static UserDatabase getUsersWithOADBID(long id) { + Logger.trace("Getting Userinformation with OADBID " + id + " from database."); UserDatabase result = null; List allUsers = getAllUsers(); boolean quit = false; - for (UserDatabase user : allUsers) { + for (UserDatabase user : nullGuard(allUsers)) { for (OnlineApplication oa : user.getOnlineApplication()) { @@ -296,12 +318,11 @@ public class NewConfigurationDBRead { * @return */ public static UserDatabase getUserWithID(long id) { - Logger.trace("Getting Userinformation with ID " + id + " from database."); UserDatabase result = null; List allUsers = getAllUsers(); - for (UserDatabase user : allUsers) { + for (UserDatabase user : nullGuard(allUsers)) { if (user.getHjid() == id) { result = user; } @@ -316,13 +337,12 @@ public class NewConfigurationDBRead { * @return */ public static UserDatabase getUserWithUserName(String username) { - Logger.trace("Getting Userinformation with ID " + username + " from database."); UserDatabase result = null; List allUsers = getAllUsers(); - for (UserDatabase user : allUsers) { + for (UserDatabase user : nullGuard(allUsers)) { if (user.getUsername().equals(username)) { result = user; } @@ -337,13 +357,12 @@ public class NewConfigurationDBRead { * @return */ public static UserDatabase getUserWithUserBPKWBPK(String bpkwbpk) { - Logger.trace("Getting Userinformation with ID " + bpkwbpk + " from database."); UserDatabase result = null; List allUsers = getAllUsers(); - for (UserDatabase user : allUsers) { + for (UserDatabase user : nullGuard(allUsers)) { if (user.getBpk().equals(bpkwbpk)) { result = user; } -- cgit v1.2.3