From abf30da750d3ce5ca0b364d19c97324f9f2e1e53 Mon Sep 17 00:00:00 2001 From: daniel Date: Mon, 14 Jul 2014 00:22:08 +0200 Subject: [PATCH 1/2] Refactored sessionID into value object having its own class. Refactored rest of code to use it. Modified AuthorizeHandler to check for sessionID in handshake data - AuthorizationListener is able to extract data from Cookie and put it back to HandshakeData. --- .../socketio/Configuration.java | 201 +++++++++++------- .../socketio/DefaultSessionID.java | 45 ++++ .../socketio/HandshakeData.java | 9 + .../corundumstudio/socketio/SessionID.java | 6 + .../socketio/SocketIOChannelInitializer.java | 13 +- .../socketio/SocketIOClient.java | 5 +- .../socketio/SocketIONamespace.java | 3 +- .../socketio/SocketIOServer.java | 3 +- .../socketio/ack/AckManager.java | 16 +- .../socketio/ack/AckSchedulerKey.java | 5 +- .../socketio/handler/AuthorizeHandler.java | 77 ++++--- .../socketio/handler/ClientHead.java | 9 +- .../socketio/handler/ClientsBox.java | 10 +- .../handler/DefaultSessionIDFactory.java | 20 ++ .../socketio/handler/SessionIDFactory.java | 11 + .../socketio/messages/HttpMessage.java | 8 +- .../socketio/messages/XHROptionsMessage.java | 4 +- .../socketio/messages/XHRPostMessage.java | 4 +- .../socketio/namespace/Namespace.java | 22 +- .../socketio/protocol/AuthPacket.java | 8 +- .../socketio/protocol/PacketDecoder.java | 8 +- .../socketio/scheduler/SchedulerKey.java | 6 +- .../socketio/store/HazelcastStore.java | 5 +- .../socketio/store/HazelcastStoreFactory.java | 4 +- .../socketio/store/MemoryStoreFactory.java | 4 +- .../socketio/store/RedissonStore.java | 5 +- .../socketio/store/RedissonStoreFactory.java | 4 +- .../socketio/store/StoreFactory.java | 4 +- .../socketio/store/pubsub/ConnectMessage.java | 8 +- .../store/pubsub/DisconnectMessage.java | 8 +- .../store/pubsub/JoinLeaveMessage.java | 8 +- .../socketio/transport/NamespaceClient.java | 4 +- .../socketio/transport/PollingTransport.java | 16 +- .../transport/WebSocketTransport.java | 14 +- .../handler/AuthorizeHandlerTest.java | 102 +++++++++ .../socketio/parser/DecoderAckPacketTest.java | 8 +- .../socketio/parser/EncoderAckPacketTest.java | 8 +- 37 files changed, 479 insertions(+), 216 deletions(-) create mode 100644 src/main/java/com/corundumstudio/socketio/DefaultSessionID.java create mode 100644 src/main/java/com/corundumstudio/socketio/SessionID.java create mode 100644 src/main/java/com/corundumstudio/socketio/handler/DefaultSessionIDFactory.java create mode 100644 src/main/java/com/corundumstudio/socketio/handler/SessionIDFactory.java create mode 100644 src/test/java/com/corundumstudio/socketio/handler/AuthorizeHandlerTest.java diff --git a/src/main/java/com/corundumstudio/socketio/Configuration.java b/src/main/java/com/corundumstudio/socketio/Configuration.java index 72c646b8..836d4b86 100644 --- a/src/main/java/com/corundumstudio/socketio/Configuration.java +++ b/src/main/java/com/corundumstudio/socketio/Configuration.java @@ -19,6 +19,7 @@ import java.util.Arrays; import java.util.List; +import com.corundumstudio.socketio.handler.SessionIDFactory; import com.corundumstudio.socketio.handler.SuccessAuthorizationListener; import com.corundumstudio.socketio.listener.DefaultExceptionListener; import com.corundumstudio.socketio.listener.ExceptionListener; @@ -33,7 +34,8 @@ public class Configuration { private String context = "/socket.io"; - private List transports = Arrays.asList(Transport.WEBSOCKET, Transport.POLLING); + private List transports = Arrays.asList(Transport.WEBSOCKET, + Transport.POLLING); private int bossThreads = 0; // 0 = current_processors_amount * 2 private int workerThreads = 0; // 0 = current_processors_amount * 2 @@ -74,14 +76,17 @@ public class Configuration { private boolean addVersionHeader = true; private String origin; - + + private SessionIDFactory sessionIDFactory; + public Configuration() { } /** * Defend from further modifications by cloning - * - * @param configuration - Configuration object to clone + * + * @param configuration + * - Configuration object to clone */ Configuration(Configuration conf) { setBossThreads(conf.getBossThreads()); @@ -104,7 +109,8 @@ public Configuration() { setTrustStoreFormat(conf.getTrustStoreFormat()); setTrustStorePassword(conf.getTrustStorePassword()); - setTransports(conf.getTransports().toArray(new Transport[conf.getTransports().size()])); + setTransports(conf.getTransports().toArray( + new Transport[conf.getTransports().size()])); setMaxHttpContentLength(conf.getMaxHttpContentLength()); setPackagePrefix(conf.getPackagePrefix()); @@ -116,7 +122,7 @@ public Configuration() { setAckMode(conf.getAckMode()); setMaxFramePayloadLength(conf.getMaxFramePayloadLength()); setUpgradeTimeout(conf.getUpgradeTimeout()); - + setAddVersionHeader(conf.isAddVersionHeader()); setOrigin(conf.getOrigin()); } @@ -126,11 +132,11 @@ public JsonSupport getJsonSupport() { } /** - * Allows to setup custom implementation of - * JSON serialization/deserialization - * + * Allows to setup custom implementation of JSON + * serialization/deserialization + * * @param jsonSupport - * + * * @see JsonSupport */ public void setJsonSupport(JsonSupport jsonSupport) { @@ -142,9 +148,8 @@ public String getHostname() { } /** - * Optional parameter. If not set then bind address - * will be 0.0.0.0 or ::0 - * + * Optional parameter. If not set then bind address will be 0.0.0.0 or ::0 + * * @param hostname */ public void setHostname(String hostname) { @@ -154,6 +159,7 @@ public void setHostname(String hostname) { public int getPort() { return port; } + public void setPort(int port) { this.port = port; } @@ -161,6 +167,7 @@ public void setPort(int port) { public int getBossThreads() { return bossThreads; } + public void setBossThreads(int bossThreads) { this.bossThreads = bossThreads; } @@ -168,36 +175,39 @@ public void setBossThreads(int bossThreads) { public int getWorkerThreads() { return workerThreads; } + public void setWorkerThreads(int workerThreads) { this.workerThreads = workerThreads; } /** * Ping interval - * + * * @param value * - time in seconds */ public void setPingInterval(int heartbeatIntervalSecs) { this.pingInterval = heartbeatIntervalSecs; } + public int getPingInterval() { return pingInterval; } /** - * Ping timeout - * Use 0 to disable it - * + * Ping timeout Use 0 to disable it + * * @param value * - time in seconds */ public void setPingTimeout(int heartbeatTimeoutSecs) { this.pingTimeout = heartbeatTimeoutSecs; } + public int getPingTimeout() { return pingTimeout; } + public boolean isHeartbeatsEnabled() { return pingTimeout > 0; } @@ -205,6 +215,7 @@ public boolean isHeartbeatsEnabled() { public String getContext() { return context; } + public void setContext(String context) { this.context = context; } @@ -214,12 +225,12 @@ public boolean isAllowCustomRequests() { } /** - * Allow to service custom requests differs from socket.io protocol. - * In this case it's necessary to add own handler which handle them - * to avoid hang connections. - * Default is {@code false} - * - * @param allowCustomRequests - {@code true} to allow + * Allow to service custom requests differs from socket.io protocol. In this + * case it's necessary to add own handler which handle them to avoid hang + * connections. Default is {@code false} + * + * @param allowCustomRequests + * - {@code true} to allow */ public void setAllowCustomRequests(boolean allowCustomRequests) { this.allowCustomRequests = allowCustomRequests; @@ -227,107 +238,118 @@ public void setAllowCustomRequests(boolean allowCustomRequests) { /** * SSL key store password - * + * * @param keyStorePassword */ public void setKeyStorePassword(String keyStorePassword) { this.keyStorePassword = keyStorePassword; } + public String getKeyStorePassword() { return keyStorePassword; } /** * SSL key store stream, maybe appointed to any source - * + * * @param keyStore */ public void setKeyStore(InputStream keyStore) { this.keyStore = keyStore; } + public InputStream getKeyStore() { return keyStore; } /** * Key store format - * + * * @param keyStoreFormat */ public void setKeyStoreFormat(String keyStoreFormat) { this.keyStoreFormat = keyStoreFormat; } + public String getKeyStoreFormat() { return keyStoreFormat; } /** * Set maximum http content length limit - * + * * @param maxContentLength - * the maximum length of the aggregated http content. + * the maximum length of the aggregated http content. */ public void setMaxHttpContentLength(int value) { this.maxHttpContentLength = value; } + public int getMaxHttpContentLength() { return maxHttpContentLength; } /** * Transports supported by server - * - * @param transports - list of transports + * + * @param transports + * - list of transports */ - public void setTransports(Transport ... transports) { + public void setTransports(Transport... transports) { if (transports.length == 0) { throw new IllegalArgumentException("Transports list can't be empty"); } this.transports = Arrays.asList(transports); } + public List getTransports() { return transports; } /** - * Package prefix for sending json-object from client - * without full class name. - * - * With defined package prefix socket.io client - * just need to define '@class: 'SomeType'' in json object - * instead of '@class: 'com.full.package.name.SomeType'' - * - * @param packagePrefix - prefix string - * + * Package prefix for sending json-object from client without full class + * name. + * + * With defined package prefix socket.io client just need to define '@class: + * 'SomeType'' in json object instead of '@class: + * 'com.full.package.name.SomeType'' + * + * @param packagePrefix + * - prefix string + * */ public void setPackagePrefix(String packagePrefix) { this.packagePrefix = packagePrefix; } + public String getPackagePrefix() { return packagePrefix; } /** - * Buffer allocation method used during packet encoding. - * Default is {@code true} - * - * @param preferDirectBuffer {@code true} if a direct buffer should be tried to be used as target for - * the encoded messages. If {@code false} is used it will allocate a heap - * buffer, which is backed by an byte array. + * Buffer allocation method used during packet encoding. Default is + * {@code true} + * + * @param preferDirectBuffer + * {@code true} if a direct buffer should be tried to be used as + * target for the encoded messages. If {@code false} is used it + * will allocate a heap buffer, which is backed by an byte array. */ public void setPreferDirectBuffer(boolean preferDirectBuffer) { this.preferDirectBuffer = preferDirectBuffer; } + public boolean isPreferDirectBuffer() { return preferDirectBuffer; } /** - * Data store - used to store session data and implements distributed pubsub. - * Default is {@code MemoryStoreFactory} - * - * @param storeFactory - implements StoreFactory - * + * Data store - used to store session data and implements distributed + * pubsub. Default is {@code MemoryStoreFactory} + * + * @param storeFactory + * - implements StoreFactory + * * @see com.corundumstudio.socketio.store.MemoryStoreFactory * @see com.corundumstudio.socketio.store.RedissonStoreFactory * @see com.corundumstudio.socketio.store.HazelcastStoreFactory @@ -335,37 +357,41 @@ public boolean isPreferDirectBuffer() { public void setStoreFactory(StoreFactory clientStoreFactory) { this.storeFactory = clientStoreFactory; } + public StoreFactory getStoreFactory() { return storeFactory; } /** - * Authorization listener invoked on every handshake. - * Accepts or denies a client by {@code AuthorizationListener.isAuthorized} method. + * Authorization listener invoked on every handshake. Accepts or denies a + * client by {@code AuthorizationListener.isAuthorized} method. * Accepts all clients by default. - * - * @param authorizationListener - authorization listener itself - * + * + * @param authorizationListener + * - authorization listener itself + * * @see com.corundumstudio.socketio.AuthorizationListener */ - public void setAuthorizationListener(AuthorizationListener authorizationListener) { + public void setAuthorizationListener( + AuthorizationListener authorizationListener) { this.authorizationListener = authorizationListener; } + public AuthorizationListener getAuthorizationListener() { return authorizationListener; } /** - * Exception listener invoked on any exception in - * SocketIO listener - * + * Exception listener invoked on any exception in SocketIO listener + * * @param exceptionListener - * + * * @see com.corundumstudio.socketio.listener.ExceptionListener */ public void setExceptionListener(ExceptionListener exceptionListener) { this.exceptionListener = exceptionListener; } + public ExceptionListener getExceptionListener() { return exceptionListener; } @@ -373,9 +399,10 @@ public ExceptionListener getExceptionListener() { public SocketConfig getSocketConfig() { return socketConfig; } + /** * TCP socket configuration - * + * * @param socketConfig */ public void setSocketConfig(SocketConfig socketConfig) { @@ -383,24 +410,24 @@ public void setSocketConfig(SocketConfig socketConfig) { } /** - * Auto ack-response mode - * Default is {@code AckMode.AUTO_SUCCESS_ONLY} - * + * Auto ack-response mode Default is {@code AckMode.AUTO_SUCCESS_ONLY} + * * @see AckMode - * + * * @param ackMode */ public void setAckMode(AckMode ackMode) { this.ackMode = ackMode; } + public AckMode getAckMode() { return ackMode; } - public String getTrustStoreFormat() { return trustStoreFormat; } + public void setTrustStoreFormat(String trustStoreFormat) { this.trustStoreFormat = trustStoreFormat; } @@ -408,6 +435,7 @@ public void setTrustStoreFormat(String trustStoreFormat) { public InputStream getTrustStore() { return trustStore; } + public void setTrustStore(InputStream trustStore) { this.trustStore = trustStore; } @@ -415,18 +443,20 @@ public void setTrustStore(InputStream trustStore) { public String getTrustStorePassword() { return trustStorePassword; } + public void setTrustStorePassword(String trustStorePassword) { this.trustStorePassword = trustStorePassword; } /** * Set maximum websocket frame content length limit - * + * * @param maxContentLength */ public void setMaxFramePayloadLength(int maxFramePayloadLength) { this.maxFramePayloadLength = maxFramePayloadLength; } + public int getMaxFramePayloadLength() { return maxFramePayloadLength; } @@ -439,37 +469,54 @@ public int getMaxFramePayloadLength() { public void setUpgradeTimeout(int upgradeTimeout) { this.upgradeTimeout = upgradeTimeout; } + public int getUpgradeTimeout() { return upgradeTimeout; } /** - * Adds Server header with lib version to http response. - * Default is true + * Adds Server header with lib version to http response. Default is + * true * * @param addVersionHeader */ public void setAddVersionHeader(boolean addVersionHeader) { this.addVersionHeader = addVersionHeader; } + public boolean isAddVersionHeader() { return addVersionHeader; } /** * Set Access-Control-Allow-Origin header value for http each - * response. - * Default is null - * - * If value is null then request ORIGIN header value used. - * + * response. Default is null + * + * If value is null then request ORIGIN header value + * used. + * * @param origin */ public void setOrigin(String origin) { this.origin = origin; } + public String getOrigin() { return origin; - } - + } + + /** + * Session ID factory used to generated a new ID or parse a one from URL, + * or cookie. + * + * @param sessionIDFactory + */ + public void setSessionIDFactory(SessionIDFactory sessionIDFactory) { + this.sessionIDFactory = sessionIDFactory; + } + + public SessionIDFactory getSessionIDFactory() { + return this.sessionIDFactory; + } + } diff --git a/src/main/java/com/corundumstudio/socketio/DefaultSessionID.java b/src/main/java/com/corundumstudio/socketio/DefaultSessionID.java new file mode 100644 index 00000000..acab77ec --- /dev/null +++ b/src/main/java/com/corundumstudio/socketio/DefaultSessionID.java @@ -0,0 +1,45 @@ +package com.corundumstudio.socketio; + +import java.util.UUID; + +/** + * Default SessionID implementation based on UUID class. + */ +public class DefaultSessionID implements SessionID { + + private UUID uuid; + + public DefaultSessionID(UUID id) { + this.uuid = id; + } + + @Override + public String toString() { + return this.uuid.toString(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((uuid == null) ? 0 : uuid.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DefaultSessionID other = (DefaultSessionID) obj; + if (uuid == null) { + if (other.uuid != null) + return false; + } else if (!uuid.equals(other.uuid)) + return false; + return true; + } +} diff --git a/src/main/java/com/corundumstudio/socketio/HandshakeData.java b/src/main/java/com/corundumstudio/socketio/HandshakeData.java index 6b475c50..487c9eaf 100644 --- a/src/main/java/com/corundumstudio/socketio/HandshakeData.java +++ b/src/main/java/com/corundumstudio/socketio/HandshakeData.java @@ -31,6 +31,7 @@ public class HandshakeData implements Serializable { private String url; private Map> urlParams; private boolean xdomain; + private SessionID sessionId; public HandshakeData() { } @@ -84,4 +85,12 @@ public String getSingleUrlParam(String name) { return null; } + public void setSessionId(SessionID sessionId) { + this.sessionId = sessionId; + } + + public SessionID getSessionId() { + return this.sessionId; + } + } diff --git a/src/main/java/com/corundumstudio/socketio/SessionID.java b/src/main/java/com/corundumstudio/socketio/SessionID.java new file mode 100644 index 00000000..ea8ab137 --- /dev/null +++ b/src/main/java/com/corundumstudio/socketio/SessionID.java @@ -0,0 +1,6 @@ +package com.corundumstudio.socketio; + +public interface SessionID { + + String toString(); +} diff --git a/src/main/java/com/corundumstudio/socketio/SocketIOChannelInitializer.java b/src/main/java/com/corundumstudio/socketio/SocketIOChannelInitializer.java index a4ac2539..76e2d247 100644 --- a/src/main/java/com/corundumstudio/socketio/SocketIOChannelInitializer.java +++ b/src/main/java/com/corundumstudio/socketio/SocketIOChannelInitializer.java @@ -42,18 +42,19 @@ import com.corundumstudio.socketio.handler.EncoderHandler; import com.corundumstudio.socketio.handler.InPacketHandler; import com.corundumstudio.socketio.handler.PacketListener; +import com.corundumstudio.socketio.handler.SessionIDFactory; import com.corundumstudio.socketio.handler.WrongUrlHandler; import com.corundumstudio.socketio.namespace.NamespacesHub; +import com.corundumstudio.socketio.protocol.JsonSupport; import com.corundumstudio.socketio.protocol.PacketDecoder; import com.corundumstudio.socketio.protocol.PacketEncoder; -import com.corundumstudio.socketio.protocol.JsonSupport; import com.corundumstudio.socketio.scheduler.CancelableScheduler; import com.corundumstudio.socketio.scheduler.HashedWheelScheduler; import com.corundumstudio.socketio.store.StoreFactory; import com.corundumstudio.socketio.store.pubsub.DisconnectMessage; import com.corundumstudio.socketio.store.pubsub.PubSubStore; -import com.corundumstudio.socketio.transport.WebSocketTransport; import com.corundumstudio.socketio.transport.PollingTransport; +import com.corundumstudio.socketio.transport.WebSocketTransport; public class SocketIOChannelInitializer extends ChannelInitializer implements DisconnectableHub { @@ -94,7 +95,6 @@ public void handlerAdded(ChannelHandlerContext ctx) { public void start(Configuration configuration, NamespacesHub namespacesHub) { this.configuration = configuration; - ackManager = new AckManager(scheduler); JsonSupport jsonSupport = configuration.getJsonSupport(); @@ -114,9 +114,10 @@ public void start(Configuration configuration, NamespacesHub namespacesHub) { StoreFactory factory = configuration.getStoreFactory(); factory.init(namespacesHub, authorizeHandler, jsonSupport); - authorizeHandler = new AuthorizeHandler(connectPath, scheduler, configuration, namespacesHub, factory, this, ackManager, clientsBox); - xhrPollingTransport = new PollingTransport(decoder, authorizeHandler, clientsBox); - webSocketTransport = new WebSocketTransport(isSsl, authorizeHandler, configuration, scheduler, clientsBox); + SessionIDFactory sessionIDFactory = configuration.getSessionIDFactory(); + authorizeHandler = new AuthorizeHandler(connectPath, scheduler, configuration, namespacesHub, factory, this, ackManager, clientsBox, sessionIDFactory); + xhrPollingTransport = new PollingTransport(decoder, authorizeHandler, clientsBox, sessionIDFactory); + webSocketTransport = new WebSocketTransport(isSsl, authorizeHandler, configuration, scheduler, clientsBox, sessionIDFactory); PacketListener packetListener = new PacketListener(ackManager, namespacesHub, xhrPollingTransport, scheduler); diff --git a/src/main/java/com/corundumstudio/socketio/SocketIOClient.java b/src/main/java/com/corundumstudio/socketio/SocketIOClient.java index 45453a93..0680aea0 100644 --- a/src/main/java/com/corundumstudio/socketio/SocketIOClient.java +++ b/src/main/java/com/corundumstudio/socketio/SocketIOClient.java @@ -17,7 +17,6 @@ import java.net.SocketAddress; import java.util.Set; -import java.util.UUID; import com.corundumstudio.socketio.protocol.Packet; import com.corundumstudio.socketio.store.Store; @@ -68,11 +67,11 @@ public interface SocketIOClient extends ClientOperations, Store { SocketIONamespace getNamespace(); /** - * Client session id, uses {@link UUID} object + * Client session id, uses {@link SessionID} object * * @return - session id */ - UUID getSessionId(); + SessionID getSessionId(); /** * Get client remote address diff --git a/src/main/java/com/corundumstudio/socketio/SocketIONamespace.java b/src/main/java/com/corundumstudio/socketio/SocketIONamespace.java index a8a60a1a..42b5589a 100644 --- a/src/main/java/com/corundumstudio/socketio/SocketIONamespace.java +++ b/src/main/java/com/corundumstudio/socketio/SocketIONamespace.java @@ -16,7 +16,6 @@ package com.corundumstudio.socketio; import java.util.Collection; -import java.util.UUID; import com.corundumstudio.socketio.listener.ClientListeners; @@ -41,6 +40,6 @@ public interface SocketIONamespace extends ClientListeners { * @param uuid * @return */ - SocketIOClient getClient(UUID uuid); + SocketIOClient getClient(SessionID uuid); } diff --git a/src/main/java/com/corundumstudio/socketio/SocketIOServer.java b/src/main/java/com/corundumstudio/socketio/SocketIOServer.java index c209d976..362a6c50 100644 --- a/src/main/java/com/corundumstudio/socketio/SocketIOServer.java +++ b/src/main/java/com/corundumstudio/socketio/SocketIOServer.java @@ -24,7 +24,6 @@ import java.net.InetSocketAddress; import java.util.Collection; -import java.util.UUID; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -82,7 +81,7 @@ public Collection getAllClients() { * @param uuid * @return */ - public SocketIOClient getClient(UUID uuid) { + public SocketIOClient getClient(SessionID uuid) { return namespacesHub.get(Namespace.DEFAULT_NAME).getClient(uuid); } diff --git a/src/main/java/com/corundumstudio/socketio/ack/AckManager.java b/src/main/java/com/corundumstudio/socketio/ack/AckManager.java index c0bd612f..98634c8d 100644 --- a/src/main/java/com/corundumstudio/socketio/ack/AckManager.java +++ b/src/main/java/com/corundumstudio/socketio/ack/AckManager.java @@ -18,7 +18,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; @@ -30,6 +29,7 @@ import com.corundumstudio.socketio.Disconnectable; import com.corundumstudio.socketio.MultiTypeAckCallback; import com.corundumstudio.socketio.MultiTypeArgs; +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.SocketIOClient; import com.corundumstudio.socketio.handler.ClientHead; import com.corundumstudio.socketio.protocol.Packet; @@ -70,7 +70,7 @@ public void initAckIndex(long index) { private final Logger log = LoggerFactory.getLogger(getClass()); - private final Map ackEntries = new ConcurrentHashMap(); + private final Map ackEntries = new ConcurrentHashMap(); private final CancelableScheduler scheduler; @@ -79,12 +79,12 @@ public AckManager(CancelableScheduler scheduler) { this.scheduler = scheduler; } - public void initAckIndex(UUID sessionId, long index) { + public void initAckIndex(SessionID sessionId, long index) { AckEntry ackEntry = getAckEntry(sessionId); ackEntry.initAckIndex(index); } - private AckEntry getAckEntry(UUID sessionId) { + private AckEntry getAckEntry(SessionID sessionId) { AckEntry ackEntry = ackEntries.get(sessionId); if (ackEntry == null) { ackEntry = new AckEntry(); @@ -120,7 +120,7 @@ public void onAck(SocketIOClient client, Packet packet) { } } - private AckCallback removeCallback(UUID sessionId, long index) { + private AckCallback removeCallback(SessionID sessionId, long index) { AckEntry ackEntry = ackEntries.get(sessionId); // may be null if client disconnected // before timeout occurs @@ -130,12 +130,12 @@ private AckCallback removeCallback(UUID sessionId, long index) { return null; } - public AckCallback getCallback(UUID sessionId, long index) { + public AckCallback getCallback(SessionID sessionId, long index) { AckEntry ackEntry = getAckEntry(sessionId); return ackEntry.getAckCallback(index); } - public long registerAck(UUID sessionId, AckCallback callback) { + public long registerAck(SessionID sessionId, AckCallback callback) { AckEntry ackEntry = getAckEntry(sessionId); ackEntry.initAckIndex(0); long index = ackEntry.addAckCallback(callback); @@ -149,7 +149,7 @@ public long registerAck(UUID sessionId, AckCallback callback) { return index; } - private void scheduleTimeout(final long index, final UUID sessionId, AckCallback callback) { + private void scheduleTimeout(final long index, final SessionID sessionId, AckCallback callback) { if (callback.getTimeout() == -1) { return; } diff --git a/src/main/java/com/corundumstudio/socketio/ack/AckSchedulerKey.java b/src/main/java/com/corundumstudio/socketio/ack/AckSchedulerKey.java index 9a11fc9b..ab44efb2 100644 --- a/src/main/java/com/corundumstudio/socketio/ack/AckSchedulerKey.java +++ b/src/main/java/com/corundumstudio/socketio/ack/AckSchedulerKey.java @@ -15,15 +15,14 @@ */ package com.corundumstudio.socketio.ack; -import java.util.UUID; - +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.scheduler.SchedulerKey; public class AckSchedulerKey extends SchedulerKey { private final long index; - public AckSchedulerKey(Type type, UUID sessionId, long index) { + public AckSchedulerKey(Type type, SessionID sessionId, long index) { super(type, sessionId); this.index = index; } diff --git a/src/main/java/com/corundumstudio/socketio/handler/AuthorizeHandler.java b/src/main/java/com/corundumstudio/socketio/handler/AuthorizeHandler.java index 352ea995..7100f56a 100644 --- a/src/main/java/com/corundumstudio/socketio/handler/AuthorizeHandler.java +++ b/src/main/java/com/corundumstudio/socketio/handler/AuthorizeHandler.java @@ -33,7 +33,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.UUID; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,6 +41,7 @@ import com.corundumstudio.socketio.Disconnectable; import com.corundumstudio.socketio.DisconnectableHub; import com.corundumstudio.socketio.HandshakeData; +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.SocketIOClient; import com.corundumstudio.socketio.Transport; import com.corundumstudio.socketio.ack.AckManager; @@ -58,7 +58,8 @@ import com.corundumstudio.socketio.store.pubsub.PubSubStore; @Sharable -public class AuthorizeHandler extends ChannelInboundHandlerAdapter implements Disconnectable { +public class AuthorizeHandler extends ChannelInboundHandlerAdapter implements + Disconnectable { private final Logger log = LoggerFactory.getLogger(getClass()); @@ -72,8 +73,12 @@ public class AuthorizeHandler extends ChannelInboundHandlerAdapter implements Di private final AckManager ackManager; private final ClientsBox clientsBox; - public AuthorizeHandler(String connectPath, CancelableScheduler scheduler, Configuration configuration, NamespacesHub namespacesHub, StoreFactory storeFactory, - DisconnectableHub disconnectable, AckManager ackManager, ClientsBox clientsBox) { + private SessionIDFactory sessionIDFactory; + + public AuthorizeHandler(String connectPath, CancelableScheduler scheduler, + Configuration configuration, NamespacesHub namespacesHub, + StoreFactory storeFactory, DisconnectableHub disconnectable, + AckManager ackManager, ClientsBox clientsBox, SessionIDFactory sessionIDFactory) { super(); this.connectPath = connectPath; this.configuration = configuration; @@ -83,82 +88,92 @@ public AuthorizeHandler(String connectPath, CancelableScheduler scheduler, Confi this.disconnectable = disconnectable; this.ackManager = ackManager; this.clientsBox = clientsBox; + this.sessionIDFactory = sessionIDFactory; } @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + public void channelRead(ChannelHandlerContext ctx, Object msg) + throws Exception { if (msg instanceof FullHttpRequest) { FullHttpRequest req = (FullHttpRequest) msg; Channel channel = ctx.channel(); - QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri()); + QueryStringDecoder queryDecoder = new QueryStringDecoder( + req.getUri()); if (!configuration.isAllowCustomRequests() && !queryDecoder.path().startsWith(connectPath)) { - HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST); + HttpResponse res = new DefaultHttpResponse(HTTP_1_1, + HttpResponseStatus.BAD_REQUEST); channel.write(res).addListener(ChannelFutureListener.CLOSE); req.release(); - log.warn("Blocked wrong request! url: {}, ip: {}", queryDecoder.path(), channel.remoteAddress()); + log.warn("Blocked wrong request! url: {}, ip: {}", + queryDecoder.path(), channel.remoteAddress()); return; } List sid = queryDecoder.parameters().get("sid"); - if (queryDecoder.path().equals(connectPath) - && sid == null) { + if (queryDecoder.path().equals(connectPath) && sid == null) { String origin = req.headers().get(HttpHeaders.Names.ORIGIN); - if (!authorize(ctx, channel, origin, queryDecoder.parameters(), req)) { + if (!authorize(ctx, channel, origin, queryDecoder.parameters(), + req)) { req.release(); return; } - // forward message to polling or websocket handler to bind channel + // forward message to polling or websocket handler to bind + // channel } } ctx.fireChannelRead(msg); } - private boolean authorize(ChannelHandlerContext ctx, Channel channel, String origin, Map> params, FullHttpRequest req) + private boolean authorize(ChannelHandlerContext ctx, Channel channel, + String origin, Map> params, FullHttpRequest req) throws IOException { - Map> headers = new HashMap>(req.headers().names().size()); + Map> headers = new HashMap>( + req.headers().names().size()); for (String name : req.headers().names()) { List values = req.headers().getAll(name); headers.put(name, values); } HandshakeData data = new HandshakeData(headers, params, - (InetSocketAddress)channel.remoteAddress(), - req.getUri(), origin != null && !origin.equalsIgnoreCase("null")); + (InetSocketAddress) channel.remoteAddress(), req.getUri(), + origin != null && !origin.equalsIgnoreCase("null")); boolean result = false; try { - result = configuration.getAuthorizationListener().isAuthorized(data); + 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); + HttpResponse res = new DefaultHttpResponse(HTTP_1_1, + HttpResponseStatus.UNAUTHORIZED); + channel.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE); log.debug("Handshake unauthorized"); return false; } - // TODO try to get sessionId from cookie - UUID sessionId = UUID.randomUUID(); + SessionID sessionId = data.getSessionId() != null ? data.getSessionId() + : sessionIDFactory.generateNew(); List transportValue = params.get("transport"); Transport transport = Transport.byName(transportValue.get(0)); - ClientHead client = new ClientHead(sessionId, ackManager, disconnectable, storeFactory, data, clientsBox, transport, disconnectScheduler, configuration); + 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"}; + transports = new String[] { "websocket" }; } - AuthPacket authPacket = new AuthPacket(sessionId, transports, configuration.getPingInterval(), - configuration.getPingTimeout()); + AuthPacket authPacket = new AuthPacket(sessionId, transports, + configuration.getPingInterval(), configuration.getPingTimeout()); Packet packet = new Packet(PacketType.OPEN); packet.setData(authPacket); client.send(packet); @@ -168,7 +183,7 @@ private boolean authorize(ChannelHandlerContext ctx, Channel channel, String ori return true; } - public void connect(UUID sessionId) { + public void connect(SessionID sessionId) { SchedulerKey key = new SchedulerKey(Type.PING_TIMEOUT, sessionId); disconnectScheduler.cancel(key); } @@ -183,7 +198,11 @@ public void connect(ClientHead client) { packet.setSubType(PacketType.CONNECT); client.send(packet); - configuration.getStoreFactory().pubSubStore().publish(PubSubStore.CONNECT, new ConnectMessage(client.getSessionId())); + configuration + .getStoreFactory() + .pubSubStore() + .publish(PubSubStore.CONNECT, + new ConnectMessage(client.getSessionId())); SocketIOClient nsClient = client.addNamespaceClient(ns); ns.onConnect(nsClient); diff --git a/src/main/java/com/corundumstudio/socketio/handler/ClientHead.java b/src/main/java/com/corundumstudio/socketio/handler/ClientHead.java index e3888638..a4eaac07 100644 --- a/src/main/java/com/corundumstudio/socketio/handler/ClientHead.java +++ b/src/main/java/com/corundumstudio/socketio/handler/ClientHead.java @@ -27,9 +27,7 @@ import java.util.Map.Entry; import java.util.Queue; import java.util.Set; -import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -39,6 +37,7 @@ import com.corundumstudio.socketio.Configuration; import com.corundumstudio.socketio.DisconnectableHub; import com.corundumstudio.socketio.HandshakeData; +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.Transport; import com.corundumstudio.socketio.ack.AckManager; import com.corundumstudio.socketio.messages.OutPacketMessage; @@ -62,7 +61,7 @@ public class ClientHead { private final Map namespaceClients = new ConcurrentHashMap(); private final Map channels = new HashMap(); private final HandshakeData handshakeData; - private final UUID sessionId; + private final SessionID sessionId; private final Store store; private final DisconnectableHub disconnectableHub; @@ -74,7 +73,7 @@ public class ClientHead { // TODO use lazy set private volatile Transport currentTransport; - public ClientHead(UUID sessionId, AckManager ackManager, DisconnectableHub disconnectable, + public ClientHead(SessionID sessionId, AckManager ackManager, DisconnectableHub disconnectable, StoreFactory storeFactory, HandshakeData handshakeData, ClientsBox clientsBox, Transport transport, CancelableScheduler disconnectScheduler, Configuration configuration) { this.sessionId = sessionId; @@ -196,7 +195,7 @@ public AckManager getAckManager() { return ackManager; } - public UUID getSessionId() { + public SessionID getSessionId() { return sessionId; } diff --git a/src/main/java/com/corundumstudio/socketio/handler/ClientsBox.java b/src/main/java/com/corundumstudio/socketio/handler/ClientsBox.java index fc3725a4..bf98459b 100644 --- a/src/main/java/com/corundumstudio/socketio/handler/ClientsBox.java +++ b/src/main/java/com/corundumstudio/socketio/handler/ClientsBox.java @@ -18,18 +18,18 @@ import io.netty.channel.Channel; import java.util.Map; -import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import com.corundumstudio.socketio.HandshakeData; +import com.corundumstudio.socketio.SessionID; public class ClientsBox { - private final Map uuid2clients = new ConcurrentHashMap(); + private final Map uuid2clients = new ConcurrentHashMap(); private final Map channel2clients = new ConcurrentHashMap(); // TODO use storeFactory - public HandshakeData getHandshakeData(UUID sessionId) { + public HandshakeData getHandshakeData(SessionID sessionId) { ClientHead client = uuid2clients.get(sessionId); if (client == null) { return null; @@ -42,11 +42,11 @@ public void addClient(ClientHead clientHead) { uuid2clients.put(clientHead.getSessionId(), clientHead); } - public void removeClient(UUID sessionId) { + public void removeClient(SessionID sessionId) { uuid2clients.remove(sessionId); } - public ClientHead get(UUID sessionId) { + public ClientHead get(SessionID sessionId) { return uuid2clients.get(sessionId); } diff --git a/src/main/java/com/corundumstudio/socketio/handler/DefaultSessionIDFactory.java b/src/main/java/com/corundumstudio/socketio/handler/DefaultSessionIDFactory.java new file mode 100644 index 00000000..ef6f7179 --- /dev/null +++ b/src/main/java/com/corundumstudio/socketio/handler/DefaultSessionIDFactory.java @@ -0,0 +1,20 @@ +package com.corundumstudio.socketio.handler; + +import java.util.UUID; + +import com.corundumstudio.socketio.DefaultSessionID; +import com.corundumstudio.socketio.SessionID; + +public class DefaultSessionIDFactory implements SessionIDFactory { + + @Override + public SessionID generateNew() { + return new DefaultSessionID(UUID.randomUUID()); + } + + @Override + public SessionID fromString(String string) { + return new DefaultSessionID(UUID.fromString(string)); + } + +} \ No newline at end of file diff --git a/src/main/java/com/corundumstudio/socketio/handler/SessionIDFactory.java b/src/main/java/com/corundumstudio/socketio/handler/SessionIDFactory.java new file mode 100644 index 00000000..bfb21716 --- /dev/null +++ b/src/main/java/com/corundumstudio/socketio/handler/SessionIDFactory.java @@ -0,0 +1,11 @@ +package com.corundumstudio.socketio.handler; + +import com.corundumstudio.socketio.SessionID; + +public interface SessionIDFactory { + + SessionID generateNew(); + + SessionID fromString(String string); + +} diff --git a/src/main/java/com/corundumstudio/socketio/messages/HttpMessage.java b/src/main/java/com/corundumstudio/socketio/messages/HttpMessage.java index 9f349413..e586fc56 100644 --- a/src/main/java/com/corundumstudio/socketio/messages/HttpMessage.java +++ b/src/main/java/com/corundumstudio/socketio/messages/HttpMessage.java @@ -15,14 +15,14 @@ */ package com.corundumstudio.socketio.messages; -import java.util.UUID; +import com.corundumstudio.socketio.SessionID; public class HttpMessage extends BaseMessage { private final String origin; - private final UUID sessionId; + private final SessionID sessionId; - public HttpMessage(String origin, UUID sessionId) { + public HttpMessage(String origin, SessionID sessionId) { this.origin = origin; this.sessionId = sessionId; } @@ -31,7 +31,7 @@ public String getOrigin() { return origin; } - public UUID getSessionId() { + public SessionID getSessionId() { return sessionId; } diff --git a/src/main/java/com/corundumstudio/socketio/messages/XHROptionsMessage.java b/src/main/java/com/corundumstudio/socketio/messages/XHROptionsMessage.java index 9d753f3c..84e6532d 100644 --- a/src/main/java/com/corundumstudio/socketio/messages/XHROptionsMessage.java +++ b/src/main/java/com/corundumstudio/socketio/messages/XHROptionsMessage.java @@ -15,11 +15,11 @@ */ package com.corundumstudio.socketio.messages; -import java.util.UUID; +import com.corundumstudio.socketio.SessionID; public class XHROptionsMessage extends XHRPostMessage { - public XHROptionsMessage(String origin, UUID sessionId) { + public XHROptionsMessage(String origin, SessionID sessionId) { super(origin, sessionId); } diff --git a/src/main/java/com/corundumstudio/socketio/messages/XHRPostMessage.java b/src/main/java/com/corundumstudio/socketio/messages/XHRPostMessage.java index b137bfab..63d385df 100644 --- a/src/main/java/com/corundumstudio/socketio/messages/XHRPostMessage.java +++ b/src/main/java/com/corundumstudio/socketio/messages/XHRPostMessage.java @@ -15,11 +15,11 @@ */ package com.corundumstudio.socketio.messages; -import java.util.UUID; +import com.corundumstudio.socketio.SessionID; public class XHRPostMessage extends HttpMessage { - public XHRPostMessage(String origin, UUID sessionId) { + public XHRPostMessage(String origin, SessionID sessionId) { super(origin, sessionId); } diff --git a/src/main/java/com/corundumstudio/socketio/namespace/Namespace.java b/src/main/java/com/corundumstudio/socketio/namespace/Namespace.java index 3f763e41..b8ae0065 100644 --- a/src/main/java/com/corundumstudio/socketio/namespace/Namespace.java +++ b/src/main/java/com/corundumstudio/socketio/namespace/Namespace.java @@ -22,7 +22,6 @@ import java.util.Map; import java.util.Queue; import java.util.Set; -import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentMap; @@ -32,6 +31,7 @@ import com.corundumstudio.socketio.BroadcastOperations; import com.corundumstudio.socketio.Configuration; import com.corundumstudio.socketio.MultiTypeArgs; +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.SocketIOClient; import com.corundumstudio.socketio.SocketIONamespace; import com.corundumstudio.socketio.annotation.ScannerEngine; @@ -63,9 +63,9 @@ public class Namespace implements SocketIONamespace { private final Queue connectListeners = new ConcurrentLinkedQueue(); private final Queue disconnectListeners = new ConcurrentLinkedQueue(); - private final Map allClients = new ConcurrentHashMap(); - private final ConcurrentMap> roomClients = new ConcurrentHashMap>(); - private final ConcurrentMap> clientRooms = new ConcurrentHashMap>(); + private final Map allClients = new ConcurrentHashMap(); + private final ConcurrentMap> roomClients = new ConcurrentHashMap>(); + private final ConcurrentMap> clientRooms = new ConcurrentHashMap>(); private final String name; private final AckMode ackMode; @@ -240,7 +240,7 @@ public void addListeners(Object listeners, Class listenersClass) { engine.scan(this, listeners, listenersClass); } - public void joinRoom(String room, UUID sessionId) { + public void joinRoom(String room, SessionID sessionId) { join(room, sessionId); storeFactory.pubSubStore().publish(PubSubStore.JOIN, new JoinLeaveMessage(sessionId, room, getName())); } @@ -270,12 +270,12 @@ private void join(ConcurrentMap> map, K key, V value) { } } - public void join(String room, UUID sessionId) { + public void join(String room, SessionID sessionId) { join(roomClients, room, sessionId); join(clientRooms, sessionId, room); } - public void leaveRoom(String room, UUID sessionId) { + public void leaveRoom(String room, SessionID sessionId) { leave(room, sessionId); storeFactory.pubSubStore().publish(PubSubStore.LEAVE, new JoinLeaveMessage(sessionId, room, getName())); } @@ -292,7 +292,7 @@ private void leave(ConcurrentMap> map, K room, V sessionId) { } } - public void leave(String room, UUID sessionId) { + public void leave(String room, SessionID sessionId) { leave(roomClients, room, sessionId); leave(clientRooms, sessionId, room); } @@ -306,14 +306,14 @@ public Set getRooms(SocketIOClient client) { } public Iterable getRoomClients(String room) { - Set sessionIds = roomClients.get(room); + Set sessionIds = roomClients.get(room); if (sessionIds == null) { return Collections.emptyList(); } List result = new ArrayList(); - for (UUID sessionId : sessionIds) { + for (SessionID sessionId : sessionIds) { SocketIOClient client = allClients.get(sessionId); if(client != null) { result.add(client); @@ -326,7 +326,7 @@ public Collection getAllClients() { return Collections.unmodifiableCollection(allClients.values()); } - public SocketIOClient getClient(UUID uuid) { + public SocketIOClient getClient(SessionID uuid) { return allClients.get(uuid); } diff --git a/src/main/java/com/corundumstudio/socketio/protocol/AuthPacket.java b/src/main/java/com/corundumstudio/socketio/protocol/AuthPacket.java index ebb1156d..7162395c 100644 --- a/src/main/java/com/corundumstudio/socketio/protocol/AuthPacket.java +++ b/src/main/java/com/corundumstudio/socketio/protocol/AuthPacket.java @@ -15,17 +15,17 @@ */ package com.corundumstudio.socketio.protocol; -import java.util.UUID; +import com.corundumstudio.socketio.SessionID; public class AuthPacket { - private final UUID sid; + private final SessionID sid; private final String[] upgrades; private final int pingInterval; private final int pingTimeout; - public AuthPacket(UUID sid, String[] upgrades, int pingInterval, int pingTimeout) { + public AuthPacket(SessionID sid, String[] upgrades, int pingInterval, int pingTimeout) { super(); this.sid = sid; this.upgrades = upgrades; @@ -41,7 +41,7 @@ public int getPingTimeout() { return pingTimeout; } - public UUID getSid() { + public SessionID getSid() { return sid; } diff --git a/src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java b/src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java index 342eae51..9c82ee55 100644 --- a/src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java +++ b/src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java @@ -23,9 +23,9 @@ import java.io.IOException; import java.net.URLDecoder; -import java.util.UUID; import com.corundumstudio.socketio.AckCallback; +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.ack.AckManager; public class PacketDecoder { @@ -81,7 +81,7 @@ private PacketType readInnerType(ByteBuf buffer) { } @Deprecated - public Packet decodePacket(String string, UUID uuid) throws IOException { + public Packet decodePacket(String string, SessionID uuid) throws IOException { ByteBuf buf = Unpooled.copiedBuffer(string, CharsetUtil.UTF_8); try { return null; @@ -102,7 +102,7 @@ public boolean process(byte value) throws Exception { } - public Packet decodePackets(ByteBuf buffer, UUID uuid) throws IOException { + public Packet decodePackets(ByteBuf buffer, SessionID uuid) throws IOException { boolean isString = buffer.getByte(0) == 0x0; if (isString) { int headEndIndex = buffer.forEachByte(new ByteBufProcessor() { @@ -133,7 +133,7 @@ private String readString(ByteBuf frame, int size) { return new String(bytes, CharsetUtil.UTF_8); } - private Packet decode(UUID uuid, ByteBuf frame) throws IOException { + private Packet decode(SessionID uuid, ByteBuf frame) throws IOException { PacketType type = readType(frame); Packet packet = new Packet(type); diff --git a/src/main/java/com/corundumstudio/socketio/scheduler/SchedulerKey.java b/src/main/java/com/corundumstudio/socketio/scheduler/SchedulerKey.java index 2bfb5bb1..329fb9cb 100644 --- a/src/main/java/com/corundumstudio/socketio/scheduler/SchedulerKey.java +++ b/src/main/java/com/corundumstudio/socketio/scheduler/SchedulerKey.java @@ -15,16 +15,16 @@ */ package com.corundumstudio.socketio.scheduler; -import java.util.UUID; +import com.corundumstudio.socketio.SessionID; public class SchedulerKey { public enum Type {POLLING, HEARBEAT_TIMEOUT, PING_TIMEOUT, ACK_TIMEOUT, UPGRADE_TIMEOUT}; private final Type type; - private final UUID sessionId; + private final SessionID sessionId; - public SchedulerKey(Type type, UUID sessionId) { + public SchedulerKey(Type type, SessionID sessionId) { this.type = type; this.sessionId = sessionId; } diff --git a/src/main/java/com/corundumstudio/socketio/store/HazelcastStore.java b/src/main/java/com/corundumstudio/socketio/store/HazelcastStore.java index 04e2e092..97c76269 100644 --- a/src/main/java/com/corundumstudio/socketio/store/HazelcastStore.java +++ b/src/main/java/com/corundumstudio/socketio/store/HazelcastStore.java @@ -15,8 +15,7 @@ */ package com.corundumstudio.socketio.store; -import java.util.UUID; - +import com.corundumstudio.socketio.SessionID; import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.IMap; @@ -25,7 +24,7 @@ public class HazelcastStore implements Store { private final IMap map; - public HazelcastStore(UUID sessionId, HazelcastInstance hazelcastInstance) { + public HazelcastStore(SessionID sessionId, HazelcastInstance hazelcastInstance) { map = hazelcastInstance.getMap(sessionId.toString()); } diff --git a/src/main/java/com/corundumstudio/socketio/store/HazelcastStoreFactory.java b/src/main/java/com/corundumstudio/socketio/store/HazelcastStoreFactory.java index 91e8e0ac..89730729 100644 --- a/src/main/java/com/corundumstudio/socketio/store/HazelcastStoreFactory.java +++ b/src/main/java/com/corundumstudio/socketio/store/HazelcastStoreFactory.java @@ -16,8 +16,8 @@ package com.corundumstudio.socketio.store; import java.util.Map; -import java.util.UUID; +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.store.pubsub.BaseStoreFactory; import com.corundumstudio.socketio.store.pubsub.PubSubStore; import com.hazelcast.client.HazelcastClient; @@ -56,7 +56,7 @@ public HazelcastStoreFactory(HazelcastInstance hazelcastClient, HazelcastInstanc } @Override - public Store createStore(UUID sessionId) { + public Store createStore(SessionID sessionId) { return new HazelcastStore(sessionId, hazelcastClient); } diff --git a/src/main/java/com/corundumstudio/socketio/store/MemoryStoreFactory.java b/src/main/java/com/corundumstudio/socketio/store/MemoryStoreFactory.java index a5c4dc65..75539645 100644 --- a/src/main/java/com/corundumstudio/socketio/store/MemoryStoreFactory.java +++ b/src/main/java/com/corundumstudio/socketio/store/MemoryStoreFactory.java @@ -16,9 +16,9 @@ package com.corundumstudio.socketio.store; import java.util.Map; -import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.store.pubsub.BaseStoreFactory; import com.corundumstudio.socketio.store.pubsub.PubSubStore; @@ -27,7 +27,7 @@ public class MemoryStoreFactory extends BaseStoreFactory { private final MemoryPubSubStore pubSubMemoryStore = new MemoryPubSubStore(); @Override - public Store createStore(UUID sessionId) { + public Store createStore(SessionID sessionId) { return new MemoryStore(); } diff --git a/src/main/java/com/corundumstudio/socketio/store/RedissonStore.java b/src/main/java/com/corundumstudio/socketio/store/RedissonStore.java index 5c9ead4b..6ba7a283 100644 --- a/src/main/java/com/corundumstudio/socketio/store/RedissonStore.java +++ b/src/main/java/com/corundumstudio/socketio/store/RedissonStore.java @@ -16,15 +16,16 @@ package com.corundumstudio.socketio.store; import java.util.Map; -import java.util.UUID; import org.redisson.Redisson; +import com.corundumstudio.socketio.SessionID; + public class RedissonStore implements Store { private final Map map; - public RedissonStore(UUID sessionId, Redisson redisson) { + public RedissonStore(SessionID sessionId, Redisson redisson) { this.map = redisson.getMap(sessionId.toString()); } diff --git a/src/main/java/com/corundumstudio/socketio/store/RedissonStoreFactory.java b/src/main/java/com/corundumstudio/socketio/store/RedissonStoreFactory.java index ab7f0924..f3e6e7c3 100644 --- a/src/main/java/com/corundumstudio/socketio/store/RedissonStoreFactory.java +++ b/src/main/java/com/corundumstudio/socketio/store/RedissonStoreFactory.java @@ -16,10 +16,10 @@ package com.corundumstudio.socketio.store; import java.util.Map; -import java.util.UUID; import org.redisson.Redisson; +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.store.pubsub.BaseStoreFactory; import com.corundumstudio.socketio.store.pubsub.PubSubStore; @@ -52,7 +52,7 @@ public RedissonStoreFactory(Redisson redisClient, Redisson redisPub, Redisson re } @Override - public Store createStore(UUID sessionId) { + public Store createStore(SessionID sessionId) { return new RedissonStore(sessionId, redisClient); } diff --git a/src/main/java/com/corundumstudio/socketio/store/StoreFactory.java b/src/main/java/com/corundumstudio/socketio/store/StoreFactory.java index a3f5186c..dd45c400 100644 --- a/src/main/java/com/corundumstudio/socketio/store/StoreFactory.java +++ b/src/main/java/com/corundumstudio/socketio/store/StoreFactory.java @@ -16,9 +16,9 @@ package com.corundumstudio.socketio.store; import java.util.Map; -import java.util.UUID; import com.corundumstudio.socketio.Disconnectable; +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.handler.AuthorizeHandler; import com.corundumstudio.socketio.namespace.NamespacesHub; import com.corundumstudio.socketio.protocol.JsonSupport; @@ -35,7 +35,7 @@ public interface StoreFactory extends Disconnectable { Map createMap(String name); - Store createStore(UUID sessionId); + Store createStore(SessionID sessionId); void init(NamespacesHub namespacesHub, AuthorizeHandler authorizeHandler, JsonSupport jsonSupport); diff --git a/src/main/java/com/corundumstudio/socketio/store/pubsub/ConnectMessage.java b/src/main/java/com/corundumstudio/socketio/store/pubsub/ConnectMessage.java index 04d716b9..402c3066 100644 --- a/src/main/java/com/corundumstudio/socketio/store/pubsub/ConnectMessage.java +++ b/src/main/java/com/corundumstudio/socketio/store/pubsub/ConnectMessage.java @@ -15,23 +15,23 @@ */ package com.corundumstudio.socketio.store.pubsub; -import java.util.UUID; +import com.corundumstudio.socketio.SessionID; public class ConnectMessage extends PubSubMessage { private static final long serialVersionUID = 3108918714495865101L; - private UUID sessionId; + private SessionID sessionId; public ConnectMessage() { } - public ConnectMessage(UUID sessionId) { + public ConnectMessage(SessionID sessionId) { super(); this.sessionId = sessionId; } - public UUID getSessionId() { + public SessionID getSessionId() { return sessionId; } diff --git a/src/main/java/com/corundumstudio/socketio/store/pubsub/DisconnectMessage.java b/src/main/java/com/corundumstudio/socketio/store/pubsub/DisconnectMessage.java index 3067a30c..4b48d057 100644 --- a/src/main/java/com/corundumstudio/socketio/store/pubsub/DisconnectMessage.java +++ b/src/main/java/com/corundumstudio/socketio/store/pubsub/DisconnectMessage.java @@ -15,23 +15,23 @@ */ package com.corundumstudio.socketio.store.pubsub; -import java.util.UUID; +import com.corundumstudio.socketio.SessionID; public class DisconnectMessage extends PubSubMessage { private static final long serialVersionUID = -2763553673397520368L; - private UUID sessionId; + private SessionID sessionId; public DisconnectMessage() { } - public DisconnectMessage(UUID sessionId) { + public DisconnectMessage(SessionID sessionId) { super(); this.sessionId = sessionId; } - public UUID getSessionId() { + public SessionID getSessionId() { return sessionId; } diff --git a/src/main/java/com/corundumstudio/socketio/store/pubsub/JoinLeaveMessage.java b/src/main/java/com/corundumstudio/socketio/store/pubsub/JoinLeaveMessage.java index 20ce881f..00f85818 100644 --- a/src/main/java/com/corundumstudio/socketio/store/pubsub/JoinLeaveMessage.java +++ b/src/main/java/com/corundumstudio/socketio/store/pubsub/JoinLeaveMessage.java @@ -15,20 +15,20 @@ */ package com.corundumstudio.socketio.store.pubsub; -import java.util.UUID; +import com.corundumstudio.socketio.SessionID; public class JoinLeaveMessage extends PubSubMessage { private static final long serialVersionUID = -944515928988033174L; - private UUID sessionId; + private SessionID sessionId; private String namespace; private String room; public JoinLeaveMessage() { } - public JoinLeaveMessage(UUID id, String room, String namespace) { + public JoinLeaveMessage(SessionID id, String room, String namespace) { super(); this.sessionId = id; this.room = room; @@ -39,7 +39,7 @@ public String getNamespace() { return namespace; } - public UUID getSessionId() { + public SessionID getSessionId() { return sessionId; } diff --git a/src/main/java/com/corundumstudio/socketio/transport/NamespaceClient.java b/src/main/java/com/corundumstudio/socketio/transport/NamespaceClient.java index 96cddef4..cf4b0d3e 100644 --- a/src/main/java/com/corundumstudio/socketio/transport/NamespaceClient.java +++ b/src/main/java/com/corundumstudio/socketio/transport/NamespaceClient.java @@ -18,7 +18,6 @@ import java.net.SocketAddress; import java.util.Arrays; import java.util.Set; -import java.util.UUID; import java.util.concurrent.atomic.AtomicBoolean; import org.slf4j.Logger; @@ -26,6 +25,7 @@ import com.corundumstudio.socketio.AckCallback; import com.corundumstudio.socketio.HandshakeData; +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.SocketIOClient; import com.corundumstudio.socketio.Transport; import com.corundumstudio.socketio.handler.ClientHead; @@ -124,7 +124,7 @@ public void disconnect() { } @Override - public UUID getSessionId() { + public SessionID getSessionId() { return baseClient.getSessionId(); } diff --git a/src/main/java/com/corundumstudio/socketio/transport/PollingTransport.java b/src/main/java/com/corundumstudio/socketio/transport/PollingTransport.java index 2c9aec05..88f6c02b 100644 --- a/src/main/java/com/corundumstudio/socketio/transport/PollingTransport.java +++ b/src/main/java/com/corundumstudio/socketio/transport/PollingTransport.java @@ -36,11 +36,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.corundumstudio.socketio.DefaultSessionID; +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.Transport; import com.corundumstudio.socketio.handler.AuthorizeHandler; import com.corundumstudio.socketio.handler.ClientHead; import com.corundumstudio.socketio.handler.ClientsBox; import com.corundumstudio.socketio.handler.EncoderHandler; +import com.corundumstudio.socketio.handler.SessionIDFactory; import com.corundumstudio.socketio.messages.PacketsMessage; import com.corundumstudio.socketio.messages.XHROptionsMessage; import com.corundumstudio.socketio.messages.XHRPostMessage; @@ -57,7 +60,7 @@ public class PollingTransport extends ChannelInboundHandlerAdapter { private final ClientsBox clientsBox; private final AuthorizeHandler authorizeHandler; - public PollingTransport(PacketDecoder decoder, AuthorizeHandler authorizeHandler, ClientsBox clientsBox) { + public PollingTransport(PacketDecoder decoder, AuthorizeHandler authorizeHandler, ClientsBox clientsBox, SessionIDFactory sessionIDFactory) { this.decoder = decoder; this.authorizeHandler = authorizeHandler; this.clientsBox = clientsBox; @@ -93,7 +96,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception try { if (sid != null && sid.get(0) != null) { - final UUID sessionId = UUID.fromString(sid.get(0)); + // TODO: abstract factory for sessionID + final SessionID sessionId = new DefaultSessionID(UUID.fromString(sid.get(0))); handleMessage(req, sessionId, queryDecoder, ctx); } else { // first connection @@ -109,7 +113,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception ctx.fireChannelRead(msg); } - private void handleMessage(FullHttpRequest req, UUID sessionId, QueryStringDecoder queryDecoder, ChannelHandlerContext ctx) + private void handleMessage(FullHttpRequest req, SessionID sessionId, QueryStringDecoder queryDecoder, ChannelHandlerContext ctx) throws IOException { String origin = req.headers().get(HttpHeaders.Names.ORIGIN); if (queryDecoder.parameters().containsKey("disconnect")) { @@ -128,7 +132,7 @@ private void handleMessage(FullHttpRequest req, UUID sessionId, QueryStringDecod } } - private void onOptions(UUID sessionId, ChannelHandlerContext ctx, String origin) { + private void onOptions(SessionID sessionId, ChannelHandlerContext ctx, String origin) { ClientHead client = clientsBox.get(sessionId); if (client == null) { log.error("{} is not registered. Closing connection", sessionId); @@ -139,7 +143,7 @@ private void onOptions(UUID sessionId, ChannelHandlerContext ctx, String origin) ctx.channel().writeAndFlush(new XHROptionsMessage(origin, sessionId)); } - private void onPost(UUID sessionId, ChannelHandlerContext ctx, String origin, ByteBuf content) + private void onPost(SessionID sessionId, ChannelHandlerContext ctx, String origin, ByteBuf content) throws IOException { ClientHead client = clientsBox.get(sessionId); if (client == null) { @@ -161,7 +165,7 @@ private void onPost(UUID sessionId, ChannelHandlerContext ctx, String origin, By ctx.pipeline().fireChannelRead(new PacketsMessage(client, content, Transport.POLLING)); } - protected void onGet(UUID sessionId, ChannelHandlerContext ctx, String origin) { + protected void onGet(SessionID sessionId, ChannelHandlerContext ctx, String origin) { ClientHead client = clientsBox.get(sessionId); if (client == null) { log.error("{} is not registered. Closing connection", sessionId); diff --git a/src/main/java/com/corundumstudio/socketio/transport/WebSocketTransport.java b/src/main/java/com/corundumstudio/socketio/transport/WebSocketTransport.java index 8b75b5e2..8d772b2b 100644 --- a/src/main/java/com/corundumstudio/socketio/transport/WebSocketTransport.java +++ b/src/main/java/com/corundumstudio/socketio/transport/WebSocketTransport.java @@ -32,17 +32,18 @@ import io.netty.util.ReferenceCountUtil; import java.util.List; -import java.util.UUID; import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.corundumstudio.socketio.Configuration; +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.Transport; import com.corundumstudio.socketio.handler.AuthorizeHandler; import com.corundumstudio.socketio.handler.ClientHead; import com.corundumstudio.socketio.handler.ClientsBox; +import com.corundumstudio.socketio.handler.SessionIDFactory; import com.corundumstudio.socketio.messages.PacketsMessage; import com.corundumstudio.socketio.scheduler.CancelableScheduler; import com.corundumstudio.socketio.scheduler.SchedulerKey; @@ -61,14 +62,17 @@ public class WebSocketTransport extends ChannelInboundHandlerAdapter { private final boolean isSsl; + private SessionIDFactory sessionIDFactory; + public WebSocketTransport(boolean isSsl, AuthorizeHandler authorizeHandler, Configuration configuration, - CancelableScheduler scheduler, ClientsBox clientsBox) { + CancelableScheduler scheduler, ClientsBox clientsBox, SessionIDFactory sessionIDFactory) { this.isSsl = isSsl; this.authorizeHandler = authorizeHandler; this.configuration = configuration; this.scheduler = scheduler; this.clientsBox = clientsBox; + this.sessionIDFactory = sessionIDFactory; } @Override @@ -103,7 +107,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception return; } if (sid != null && sid.get(0) != null) { - final UUID sessionId = UUID.fromString(sid.get(0)); + final SessionID sessionId = sessionIDFactory.fromString(sid.get(0)); handshake(ctx, sessionId, path, req); } else { ClientHead client = ctx.channel().attr(ClientHead.CLIENT).get(); @@ -141,7 +145,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); } - private void handshake(ChannelHandlerContext ctx, final UUID sessionId, String path, FullHttpRequest req) { + private void handshake(ChannelHandlerContext ctx, final SessionID sessionId, String path, FullHttpRequest req) { final Channel channel = ctx.channel(); WebSocketServerHandshakerFactory factory = @@ -164,7 +168,7 @@ public void operationComplete(ChannelFuture future) throws Exception { } } - private void connectClient(final Channel channel, final UUID sessionId) { + private void connectClient(final Channel channel, final SessionID sessionId) { ClientHead client = clientsBox.get(sessionId); if (client == null) { log.warn("Unauthorized client with sessionId: {} with ip: {}. Channel closed!", diff --git a/src/test/java/com/corundumstudio/socketio/handler/AuthorizeHandlerTest.java b/src/test/java/com/corundumstudio/socketio/handler/AuthorizeHandlerTest.java new file mode 100644 index 00000000..ef798590 --- /dev/null +++ b/src/test/java/com/corundumstudio/socketio/handler/AuthorizeHandlerTest.java @@ -0,0 +1,102 @@ +package com.corundumstudio.socketio.handler; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import io.netty.channel.embedded.EmbeddedChannel; +import io.netty.handler.codec.http.DefaultFullHttpRequest; +import io.netty.handler.codec.http.DefaultHttpResponse; +import io.netty.handler.codec.http.HttpMethod; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.HttpVersion; + +import java.net.InetSocketAddress; + +import mockit.Mocked; +import mockit.NonStrictExpectations; + +import org.junit.Before; +import org.junit.Test; + +import com.corundumstudio.socketio.AuthorizationListener; +import com.corundumstudio.socketio.Configuration; +import com.corundumstudio.socketio.DisconnectableHub; +import com.corundumstudio.socketio.HandshakeData; +import com.corundumstudio.socketio.SessionID; +import com.corundumstudio.socketio.ack.AckManager; +import com.corundumstudio.socketio.namespace.NamespacesHub; +import com.corundumstudio.socketio.scheduler.HashedWheelScheduler; +import com.corundumstudio.socketio.store.MemoryStoreFactory; +import com.corundumstudio.socketio.transport.WebSocketTransport; + +public class AuthorizeHandlerTest { + + private static final String TEST_SESSION_ID = "666"; + + private static final String TEST_URI = "/?transport=" + WebSocketTransport.NAME; + + @Mocked + private DisconnectableHub disconnectableHub; + + private EmbeddedChannel channel; + + private Configuration configuration; + + private ClientsBox clientsBox; + + @Before + public void setUp() { + configuration = new Configuration(); + HashedWheelScheduler scheduler = new HashedWheelScheduler(); + clientsBox = new ClientsBox(); + channel = new EmbeddedChannel(new AuthorizeHandler("/", scheduler, + configuration, new NamespacesHub(configuration), + new MemoryStoreFactory(), disconnectableHub, new AckManager( + scheduler), clientsBox, new DefaultSessionIDFactory())); + + new NonStrictExpectations(channel) { + { + channel.remoteAddress(); + result = new InetSocketAddress(8080); + } + }; + } + + @Test + public void shouldAuthorizeRequest() throws Exception { + prepareAuthorizationListener(true, null); + + channel.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, + HttpMethod.GET, TEST_URI)); + // then + Object in = channel.readInbound(); + assertTrue(in instanceof DefaultFullHttpRequest); + DefaultFullHttpRequest req = (DefaultFullHttpRequest) in; + assertEquals(1, req.refCnt()); + + } + + @Test + public void shouldNotAuthorizeRequest() throws Exception { + prepareAuthorizationListener(false, null); + + channel.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, + HttpMethod.GET, TEST_URI)); + // then + Object out = channel.readOutbound(); + assertTrue(out instanceof DefaultHttpResponse); + DefaultHttpResponse res = (DefaultHttpResponse) out; + assertEquals(HttpResponseStatus.UNAUTHORIZED, res.getStatus()); + } + + private void prepareAuthorizationListener(final boolean authorizeResponse, final SessionID sessionId) { + configuration.setAuthorizationListener(new AuthorizationListener() { + @Override + public boolean isAuthorized(HandshakeData data) { + if (sessionId != null) { + data.setSessionId(sessionId); + } + return authorizeResponse; + } + }); + } +} diff --git a/src/test/java/com/corundumstudio/socketio/parser/DecoderAckPacketTest.java b/src/test/java/com/corundumstudio/socketio/parser/DecoderAckPacketTest.java index e7c11a59..8b8710f4 100644 --- a/src/test/java/com/corundumstudio/socketio/parser/DecoderAckPacketTest.java +++ b/src/test/java/com/corundumstudio/socketio/parser/DecoderAckPacketTest.java @@ -16,19 +16,17 @@ package com.corundumstudio.socketio.parser; import java.io.IOException; -import java.util.Arrays; -import java.util.UUID; import mockit.Expectations; -import com.fasterxml.jackson.core.JsonParseException; - import org.junit.Assert; import org.junit.Test; import com.corundumstudio.socketio.AckCallback; +import com.corundumstudio.socketio.SessionID; import com.corundumstudio.socketio.protocol.Packet; import com.corundumstudio.socketio.protocol.PacketType; +import com.fasterxml.jackson.core.JsonParseException; public class DecoderAckPacketTest extends DecoderBaseTest { @@ -52,7 +50,7 @@ public void testDecodeWithArgs() throws IOException { private void initExpectations() { new Expectations() {{ - ackManager.getCallback((UUID)any, anyInt); + ackManager.getCallback((SessionID)any, anyInt); result = new AckCallback(String.class) { @Override public void onSuccess(String result) { diff --git a/src/test/java/com/corundumstudio/socketio/parser/EncoderAckPacketTest.java b/src/test/java/com/corundumstudio/socketio/parser/EncoderAckPacketTest.java index f867ea38..5e9c6b41 100644 --- a/src/test/java/com/corundumstudio/socketio/parser/EncoderAckPacketTest.java +++ b/src/test/java/com/corundumstudio/socketio/parser/EncoderAckPacketTest.java @@ -16,7 +16,9 @@ package com.corundumstudio.socketio.parser; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.Unpooled; +import io.netty.buffer.UnpooledByteBufAllocator; import io.netty.util.CharsetUtil; import java.io.IOException; @@ -35,7 +37,7 @@ public void testEncode() throws IOException { Packet packet = new Packet(PacketType.ACK); packet.setAckId(140L); ByteBuf result = Unpooled.buffer(); -// encoder.encodePacket(packet, result); + encoder.encodePacket(packet, result, ByteBufAllocator.DEFAULT, true, false); Assert.assertEquals("6:::140", result.toString(CharsetUtil.UTF_8)); } @@ -43,9 +45,9 @@ public void testEncode() throws IOException { public void testEncodeWithArgs() throws IOException { Packet packet = new Packet(PacketType.ACK); packet.setAckId(12L); -// packet.setArgs(Arrays.asList("woot", "wa")); + //packet.setArgs(Arrays.asList("woot", "wa")); ByteBuf result = Unpooled.buffer(); -// encoder.encodePacket(packet, result); + //encoder.encodePacket(packet, result); Assert.assertEquals("6:::12+[\"woot\",\"wa\"]", result.toString(CharsetUtil.UTF_8)); } From 920a134cdc1c7e001358257927b7a95792867ba4 Mon Sep 17 00:00:00 2001 From: daniel Date: Wed, 16 Jul 2014 00:02:46 +0200 Subject: [PATCH 2/2] Fixed missing default SessionIDFactory initialization and added missing file headers. --- .../corundumstudio/socketio/Configuration.java | 3 ++- .../corundumstudio/socketio/DefaultSessionID.java | 15 +++++++++++++++ .../com/corundumstudio/socketio/SessionID.java | 15 +++++++++++++++ .../socketio/handler/DefaultSessionIDFactory.java | 15 +++++++++++++++ .../socketio/handler/SessionIDFactory.java | 15 +++++++++++++++ .../socketio/handler/AuthorizeHandlerTest.java | 15 +++++++++++++++ 6 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/corundumstudio/socketio/Configuration.java b/src/main/java/com/corundumstudio/socketio/Configuration.java index 836d4b86..ec16eb5e 100644 --- a/src/main/java/com/corundumstudio/socketio/Configuration.java +++ b/src/main/java/com/corundumstudio/socketio/Configuration.java @@ -19,6 +19,7 @@ import java.util.Arrays; import java.util.List; +import com.corundumstudio.socketio.handler.DefaultSessionIDFactory; import com.corundumstudio.socketio.handler.SessionIDFactory; import com.corundumstudio.socketio.handler.SuccessAuthorizationListener; import com.corundumstudio.socketio.listener.DefaultExceptionListener; @@ -77,7 +78,7 @@ public class Configuration { private String origin; - private SessionIDFactory sessionIDFactory; + private SessionIDFactory sessionIDFactory = new DefaultSessionIDFactory(); public Configuration() { } diff --git a/src/main/java/com/corundumstudio/socketio/DefaultSessionID.java b/src/main/java/com/corundumstudio/socketio/DefaultSessionID.java index acab77ec..0fe3bc75 100644 --- a/src/main/java/com/corundumstudio/socketio/DefaultSessionID.java +++ b/src/main/java/com/corundumstudio/socketio/DefaultSessionID.java @@ -1,3 +1,18 @@ +/** + * Copyright 2012 Nikita Koksharov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.corundumstudio.socketio; import java.util.UUID; diff --git a/src/main/java/com/corundumstudio/socketio/SessionID.java b/src/main/java/com/corundumstudio/socketio/SessionID.java index ea8ab137..6847b63d 100644 --- a/src/main/java/com/corundumstudio/socketio/SessionID.java +++ b/src/main/java/com/corundumstudio/socketio/SessionID.java @@ -1,3 +1,18 @@ +/** + * Copyright 2012 Nikita Koksharov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.corundumstudio.socketio; public interface SessionID { diff --git a/src/main/java/com/corundumstudio/socketio/handler/DefaultSessionIDFactory.java b/src/main/java/com/corundumstudio/socketio/handler/DefaultSessionIDFactory.java index ef6f7179..0b482084 100644 --- a/src/main/java/com/corundumstudio/socketio/handler/DefaultSessionIDFactory.java +++ b/src/main/java/com/corundumstudio/socketio/handler/DefaultSessionIDFactory.java @@ -1,3 +1,18 @@ +/** + * Copyright 2012 Nikita Koksharov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.corundumstudio.socketio.handler; import java.util.UUID; diff --git a/src/main/java/com/corundumstudio/socketio/handler/SessionIDFactory.java b/src/main/java/com/corundumstudio/socketio/handler/SessionIDFactory.java index bfb21716..35c11607 100644 --- a/src/main/java/com/corundumstudio/socketio/handler/SessionIDFactory.java +++ b/src/main/java/com/corundumstudio/socketio/handler/SessionIDFactory.java @@ -1,3 +1,18 @@ +/** + * Copyright 2012 Nikita Koksharov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.corundumstudio.socketio.handler; import com.corundumstudio.socketio.SessionID; diff --git a/src/test/java/com/corundumstudio/socketio/handler/AuthorizeHandlerTest.java b/src/test/java/com/corundumstudio/socketio/handler/AuthorizeHandlerTest.java index ef798590..cb128b6a 100644 --- a/src/test/java/com/corundumstudio/socketio/handler/AuthorizeHandlerTest.java +++ b/src/test/java/com/corundumstudio/socketio/handler/AuthorizeHandlerTest.java @@ -1,3 +1,18 @@ +/** + * Copyright 2012 Nikita Koksharov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.corundumstudio.socketio.handler; import static org.junit.Assert.assertEquals;