Alejandro Acuña
2024-08-12 831c7bd85763b5eb5ef46664c65f0181824f9e13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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<String, AddressLogin> maddressBlocked;
    private static ConcurrentHashMap<String, UserIdentification> 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<ConcurrentHashMap<String, AddressLogin>>(){});
        } 
        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<ConcurrentHashMap<String, UserIdentification>>(){});
        } 
        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<AddressLogin> 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<UserIdentification> 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();
    }
}