hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
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
package chat.server.moquette.message;
 
import java.util.Base64;
 
import io.moquette.spi.impl.security.AES;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.util.AttributeKey;
 
public class ClientID {
    
    private static final AttributeKey<Object> Attr_Key_ClientId;
    private String value;
    
    static {
        Attr_Key_ClientId = AttributeKey.valueOf("clientId");
    }
    
    public ClientID() {
        
    }
 
    public static ClientID valueOf(FullHttpRequest httpRequest) {
        ClientID result = new ClientID();
        
        try {
            String cid = httpRequest.headers().get("cid");
            
            if (cid == null) {
                return result;
            }
            
            byte[] bytes = Base64.getDecoder().decode(cid);
            bytes = AES.AESDecrypt(bytes, "", true);
            
            result.value = new String(bytes);
        }
        catch (Exception e) {
        }
        
        return result;
    }
 
    public static ClientID valueOf(MqttConnectPayload payload) {
        ClientID result = new ClientID();
        String value = payload.clientIdentifier();
        result.value = value;
        
        return result;
    }
    
    public static ClientID valueOf(ChannelHandlerContext ctx) {
        Channel channel = ctx.channel();
        return valueOf(channel);
    }
 
    public static ClientID valueOf(Channel channel) {
        ClientID result = new ClientID();
        
        if (channel == null) {
            return result;
        }
        
        String value = String.valueOf(channel.attr(Attr_Key_ClientId).get());
        result.value = value;
        
        return result;
    }
 
    public void setValueTo(Channel channel) {
        channel.attr(Attr_Key_ClientId).set(value);
    }
    
    public boolean isEmpty() {
        if (value == null) {
            return true;
        }
        
        if ("".equals(value)) {
            return true;
        }
        
        return false;
    }
 
    public String getValue() {
        return value;
    }
 
    @Override
    public String toString() {
        //return "<" + value + ">";
        return value;
    }
    
}