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 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; } }