Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Spark error pages when there are websocket routes #11

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.slf4j.Logger;
Expand All @@ -36,6 +37,7 @@
import spark.embeddedserver.EmbeddedServer;
import spark.embeddedserver.jetty.websocket.WebSocketHandlerWrapper;
import spark.embeddedserver.jetty.websocket.WebSocketServletContextHandlerFactory;
import spark.http.matching.MatcherFilter;
import spark.ssl.SslStores;

/**
Expand Down Expand Up @@ -135,8 +137,10 @@ public int ignite(String host,
handlersInList.add(handler);

// WebSocket handler must be the last one
if (webSocketServletContextHandler != null) {
handlersInList.add(webSocketServletContextHandler);
handlersInList.add(webSocketServletContextHandler);

if (handler instanceof JettyHandler jh && jh.getFilter() instanceof MatcherFilter matcherFilter) {
webSocketServletContextHandler.setErrorHandler(new JettyErrorHandler(matcherFilter));
}

HandlerList handlers = new HandlerList();
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/spark/embeddedserver/jetty/JettyErrorHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package spark.embeddedserver.jetty;

import org.eclipse.jetty.server.handler.ErrorHandler;

import java.io.IOException;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import spark.CustomErrorPages;
import spark.Request;
import spark.RequestResponseFactory;
import spark.Response;
import spark.http.matching.Body;
import spark.http.matching.MatcherFilter;
import spark.serialization.SerializerChain;

public class JettyErrorHandler extends ErrorHandler {
private final MatcherFilter matcherFilter;

public JettyErrorHandler(MatcherFilter matcherFilter) {
this.matcherFilter=matcherFilter;
}

@Override
protected void generateAcceptableResponse(org.eclipse.jetty.server.Request baseRequest, HttpServletRequest servletRequest,
HttpServletResponse servletResponse, int code, String message, String contentType) throws IOException {
if (CustomErrorPages.existsFor(code)) {
Request request = RequestResponseFactory.create(servletRequest);
Response response = RequestResponseFactory.create(servletResponse);
SerializerChain serializer = matcherFilter.getSerializerChain();
Object page = CustomErrorPages.getFor(code, request, response);
Body body = Body.create();
body.set(page);
body.serializeTo(servletResponse, serializer, servletRequest, request, response);
} else {
super.generateAcceptableResponse(baseRequest, servletRequest, servletResponse, code, message, contentType);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/spark/http/matching/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/**
* Represents the 'body'
*/
final class Body {
public final class Body {

private Object content;

Expand Down