forked from perwendel/spark
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for perwendel#1273, perwendel#375 (Merged PR#980)
- Loading branch information
A.Lepe
committed
May 16, 2023
1 parent
17ac17c
commit 22ac89e
Showing
20 changed files
with
471 additions
and
6 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
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
20 changes: 20 additions & 0 deletions
20
src/main/java/spark/embeddedserver/jetty/eventsource/EventSourceHandlerClassWrapper.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.eventsource; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class EventSourceHandlerClassWrapper implements EventSourceHandlerWrapper { | ||
private final Class<?> handlerClass; | ||
public EventSourceHandlerClassWrapper(Class<?> handlerClass) { | ||
requireNonNull(handlerClass, "EventSource handler class cannot be null"); | ||
EventSourceHandlerWrapper.validateHandlerClass(handlerClass); | ||
this.handlerClass = handlerClass; | ||
} | ||
@Override | ||
public Object getHandler() { | ||
try { | ||
return handlerClass.newInstance(); | ||
} catch (InstantiationException | IllegalAccessException ex) { | ||
throw new RuntimeException("Could not instantiate event source handler", ex); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/spark/embeddedserver/jetty/eventsource/EventSourceHandlerInstanceWrapper.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,18 @@ | ||
package spark.embeddedserver.jetty.eventsource; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class EventSourceHandlerInstanceWrapper implements EventSourceHandlerWrapper { | ||
final Object handler; | ||
|
||
public EventSourceHandlerInstanceWrapper(Object handler) { | ||
requireNonNull(handler, "EventSource handler cannot be null"); | ||
EventSourceHandlerWrapper.validateHandlerClass(handler.getClass()); | ||
this.handler = handler; | ||
} | ||
|
||
@Override | ||
public Object getHandler() { | ||
return handler; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/spark/embeddedserver/jetty/eventsource/EventSourceHandlerWrapper.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.eventsource; | ||
|
||
import org.eclipse.jetty.servlets.EventSourceServlet; | ||
|
||
/** | ||
* A wrapper for event source handler classes/instances. | ||
*/ | ||
public interface EventSourceHandlerWrapper { | ||
/** | ||
* Gets the actual handler - if necessary, instantiating an object. | ||
* | ||
* @return The handler instance. | ||
*/ | ||
Object getHandler(); | ||
|
||
static void validateHandlerClass(Class<?> handlerClass) { | ||
boolean valid = EventSourceServlet.class.isAssignableFrom(handlerClass); | ||
if (!valid) { | ||
throw new IllegalArgumentException( | ||
"EventSource handler must extend 'EventSourceServlet'"); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../java/spark/embeddedserver/jetty/eventsource/EventSourceServletContextHandlerFactory.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,40 @@ | ||
package spark.embeddedserver.jetty.eventsource; | ||
|
||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
import org.eclipse.jetty.servlet.ServletHolder; | ||
import org.eclipse.jetty.servlets.EventSourceServlet; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Map; | ||
|
||
public class EventSourceServletContextHandlerFactory { | ||
private static final Logger logger = LoggerFactory.getLogger(EventSourceServletContextHandlerFactory.class); | ||
|
||
/** | ||
* Creates a new eventSource servlet context handler. | ||
* | ||
* @param eventSourceHandlers eventSourceHandlers | ||
* @return a new eventSource servlet context handler or 'null' if creation failed. | ||
*/ | ||
public static ServletContextHandler create(Map<String, EventSourceHandlerWrapper> eventSourceHandlers) { | ||
ServletContextHandler eventSourceServletContextHandler = null; | ||
if (eventSourceHandlers != null) { | ||
try { | ||
eventSourceServletContextHandler = new ServletContextHandler(null, "/", true, false); | ||
addToExistingContext(eventSourceServletContextHandler, eventSourceHandlers); | ||
} catch (Exception ex) { | ||
logger.error("creation of event source context handler failed.", ex); | ||
eventSourceServletContextHandler = null; | ||
} | ||
} | ||
return eventSourceServletContextHandler; | ||
} | ||
|
||
public static void addToExistingContext(ServletContextHandler contextHandler, Map<String, EventSourceHandlerWrapper> eventSourceHandlers){ | ||
if (eventSourceHandlers == null) | ||
return; | ||
eventSourceHandlers.forEach((path, servletWrapper)-> | ||
contextHandler.addServlet(new ServletHolder((EventSourceServlet)servletWrapper.getHandler()), path)); | ||
} | ||
} |
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
Oops, something went wrong.