package art.servers.transactionsserver.model.access.login; import art.library.interop.serialization.Serialization; import art.library.model.devices.user.UserIdentification; import art.library.model.transactions.useraccess.AddressLogin; import art.servers.transactionsserver.Shared; import com.fasterxml.jackson.core.type.TypeReference; import java.io.File; import java.util.ArrayList; import java.util.concurrent.ConcurrentHashMap; public class LoginBlockedRepository { private static ConcurrentHashMap maddressBlocked; private static ConcurrentHashMap musersBlocked; static { readBlockedAddressFile(); readBlockedUsersFile(); } public static void readBlockedAddressFile() { try { maddressBlocked = Serialization.convertValue(Serialization.deserialize(Object.class, new File(Shared.getApplicationName() + "-blocked-addresses.json")),new TypeReference>(){}); } catch (Exception e) { maddressBlocked = new ConcurrentHashMap<>(16, 0.9f, 1); } } public static void writeBlockedAddressFile() throws Exception { try { Serialization.serialize(maddressBlocked, new File(Shared.getApplicationName() + "-blocked-addresses.json")); } catch (Exception ex) { throw new Exception(Shared.getApplicationName() + "-blocked-addresses.json"); } } public static void readBlockedUsersFile() { try { musersBlocked = Serialization.convertValue(Serialization.deserialize(Object.class, new File(Shared.getApplicationName() + "-blocked-users.json")),new TypeReference>(){}); } catch (Exception e) { musersBlocked = new ConcurrentHashMap<>(16, 0.9f, 1); } } public static void writeUserIdentificationLoginBlocked() throws Exception { try { Serialization.serialize(musersBlocked, new File(Shared.getApplicationName() + "-blocked-users.json")); } catch (Exception ex) { throw new Exception(Shared.getApplicationName() + "-blocked-users.json"); } } //ADDRESS public static ArrayList getAddressLogins() { return new ArrayList<>(maddressBlocked.values()); } public static AddressLogin getAddressLogin(String addressLoginKey) throws Exception { return maddressBlocked.get(addressLoginKey); } public static void setAddressLogin(AddressLogin addresslogin) throws Exception { if (maddressBlocked == null) throw new Exception("Adderss identification key not found"); maddressBlocked.put(addresslogin.loginAddress, addresslogin); writeBlockedAddressFile(); } public static void removeAddress(String ipAddress) throws Exception { maddressBlocked.remove(ipAddress); writeBlockedAddressFile(); } //USER public static ArrayList getUserIdentifications() { return new ArrayList<>(musersBlocked.values()); } public static UserIdentification getUserIdentification(String userIdentificationKey) throws Exception { if (musersBlocked == null) throw new Exception("User identification key not found"); return musersBlocked.get(userIdentificationKey); } public static void setUserIdentification(UserIdentification userIdentification) throws Exception { if (musersBlocked == null) throw new Exception("Repository blocked users not found"); musersBlocked.put(userIdentification.username, userIdentification); writeUserIdentificationLoginBlocked(); } public static void removeUserIdentification(String username) throws Exception { musersBlocked.remove(username); writeUserIdentificationLoginBlocked(); } }