use of com.corundumstudio.socketio.protocol.Packet in project netty-socketio by mrniko.
the class AuthorizeHandler method authorize.
private boolean authorize(ChannelHandlerContext ctx, Channel channel, String origin, Map<String, List<String>> params, FullHttpRequest req) throws IOException {
Map<String, List<String>> headers = new HashMap<String, List<String>>(req.headers().names().size());
for (String name : req.headers().names()) {
List<String> values = req.headers().getAll(name);
headers.put(name, values);
}
HandshakeData data = new HandshakeData(req.headers(), params, (InetSocketAddress) channel.remoteAddress(), req.uri(), origin != null && !origin.equalsIgnoreCase("null"));
boolean result = false;
try {
result = configuration.getAuthorizationListener().isAuthorized(data);
} catch (Exception e) {
log.error("Authorization error", e);
}
if (!result) {
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
channel.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
log.debug("Handshake unauthorized, query params: {} headers: {}", params, headers);
return false;
}
UUID sessionId = this.generateOrGetSessionIdFromRequest(req.headers());
List<String> transportValue = params.get("transport");
if (transportValue == null) {
log.error("Got no transports for request {}", req.uri());
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
channel.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
return false;
}
Transport transport = Transport.byName(transportValue.get(0));
if (!configuration.getTransports().contains(transport)) {
Map<String, Object> errorData = new HashMap<String, Object>();
errorData.put("code", 0);
errorData.put("message", "Transport unknown");
channel.attr(EncoderHandler.ORIGIN).set(origin);
channel.writeAndFlush(new HttpErrorMessage(errorData));
return false;
}
ClientHead client = new ClientHead(sessionId, ackManager, disconnectable, storeFactory, data, clientsBox, transport, disconnectScheduler, configuration);
channel.attr(ClientHead.CLIENT).set(client);
clientsBox.addClient(client);
String[] transports = {};
if (configuration.getTransports().contains(Transport.WEBSOCKET)) {
transports = new String[] { "websocket" };
}
AuthPacket authPacket = new AuthPacket(sessionId, transports, configuration.getPingInterval(), configuration.getPingTimeout());
Packet packet = new Packet(PacketType.OPEN);
packet.setData(authPacket);
client.send(packet);
client.schedulePingTimeout();
log.debug("Handshake authorized for sessionId: {}, query params: {} headers: {}", sessionId, params, headers);
return true;
}
use of com.corundumstudio.socketio.protocol.Packet in project netty-socketio by mrniko.
the class NamespaceClient method disconnect.
@Override
public void disconnect() {
Packet packet = new Packet(PacketType.MESSAGE);
packet.setSubType(PacketType.DISCONNECT);
send(packet);
// onDisconnect();
}
use of com.corundumstudio.socketio.protocol.Packet in project netty-socketio by mrniko.
the class AckRequest method sendAckData.
/**
* Send ack data to client.
* Can be invoked only once during {@link DataListener#onData}
* method invocation.
*
* @param objs - ack data object list
*/
public void sendAckData(List<Object> objs) {
if (!isAckRequested() || !sended.compareAndSet(false, true)) {
return;
}
Packet ackPacket = new Packet(PacketType.MESSAGE);
ackPacket.setSubType(PacketType.ACK);
ackPacket.setAckId(originalPacket.getAckId());
ackPacket.setData(objs);
client.send(ackPacket);
}
use of com.corundumstudio.socketio.protocol.Packet in project netty-socketio by mrniko.
the class BroadcastOperations method sendEvent.
@Override
public void sendEvent(String name, Object... data) {
Packet packet = new Packet(PacketType.MESSAGE);
packet.setSubType(PacketType.EVENT);
packet.setName(name);
packet.setData(Arrays.asList(data));
send(packet);
}
Aggregations