Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable passing WebSocket handler instances.
- Loading branch information
Showing
12 changed files
with
142 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/spark/embeddedserver/jetty/websocket/WebSocketHandlerClassWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package spark.embeddedserver.jetty.websocket; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class WebSocketHandlerClassWrapper implements WebSocketHandlerWrapper { | ||
|
||
private final Class<?> handlerClass; | ||
|
||
public WebSocketHandlerClassWrapper(Class<?> handlerClass) { | ||
requireNonNull(handlerClass, "WebSocket handler class cannot be null"); | ||
WebSocketHandlerWrapper.validateHandlerClass(handlerClass); | ||
this.handlerClass = handlerClass; | ||
} | ||
@Override | ||
public Object getHandler() { | ||
try { | ||
return handlerClass.newInstance(); | ||
} catch (InstantiationException | IllegalAccessException ex) { | ||
throw new RuntimeException("Could not instantiate websocket handler", ex); | ||
} | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/spark/embeddedserver/jetty/websocket/WebSocketHandlerInstanceWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package spark.embeddedserver.jetty.websocket; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class WebSocketHandlerInstanceWrapper implements WebSocketHandlerWrapper { | ||
|
||
private final Object handler; | ||
|
||
public WebSocketHandlerInstanceWrapper(Object handler) { | ||
requireNonNull(handler, "WebSocket handler cannot be null"); | ||
WebSocketHandlerWrapper.validateHandlerClass(handler.getClass()); | ||
this.handler = handler; | ||
} | ||
|
||
@Override | ||
public Object getHandler() { | ||
return handler; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/spark/embeddedserver/jetty/websocket/WebSocketHandlerWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package spark.embeddedserver.jetty.websocket; | ||
|
||
import org.eclipse.jetty.websocket.api.WebSocketListener; | ||
import org.eclipse.jetty.websocket.api.annotations.WebSocket; | ||
|
||
/** | ||
* A wrapper for web socket handler classes/instances. | ||
*/ | ||
public interface WebSocketHandlerWrapper { | ||
|
||
/** | ||
* Gets the actual handler - if necessary, instantiating an object. | ||
* | ||
* @return The handler instance. | ||
*/ | ||
Object getHandler(); | ||
|
||
static void validateHandlerClass(Class<?> handlerClass) { | ||
boolean valid = WebSocketListener.class.isAssignableFrom(handlerClass) | ||
|| handlerClass.isAnnotationPresent(WebSocket.class); | ||
if (!valid) { | ||
throw new IllegalArgumentException( | ||
"WebSocket handler must implement 'WebSocketListener' or be annotated as '@WebSocket'"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters