Skip to content

Commit e84e39a

Browse files
committed
Fixes #13931 - Introduce ConditionalHandler.NextElseReject.
Signed-off-by: Simone Bordet <[email protected]>
1 parent a783e13 commit e84e39a

File tree

2 files changed

+63
-2
lines changed
  • documentation/jetty/modules/programming-guide/pages/server
  • jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler

2 files changed

+63
-2
lines changed

documentation/jetty/modules/programming-guide/pages/server/http.adoc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,14 +874,17 @@ Alternatively, you can use the following `ConditionalHandler` subclasses that im
874874
** When conditions are met, you have to write the implementation.
875875
** When conditions are not met, the handling of the request is forwarded to the next `Handler`.
876876
* `ConditionalHandler.Reject`:
877-
** When conditions are met, a response is written with a configurable, non-2xx, HTTP status code.
877+
** When conditions are met, a response is written with a configurable HTTP status code.
878878
** When conditions are not met, the handling of the request is forwarded to the next `Handler`.
879879
* `ConditionalHandler.SkipNext`:
880880
** When conditions are met, the handling of the request is forwarded to the next-next `Handler`, skipping the next `Handler`.
881881
** When conditions are not met, the handling of the request is forwarded to the next `Handler`.
882882
* `ConditionalHandler.DontHandle`:
883883
** When conditions are met, the request is not handled.
884884
** When conditions are not met, the handling of the request is forwarded to the next `Handler`.
885+
* `ConditionalHandler.NextElseReject`:
886+
** When conditions are met, the handling of the request is forwarded to the next `Handler`.
887+
** When conditions are not met, a response is written with a configurable HTTP status code.
885888

886889
Conditions can be specified using an include/exclude mechanism, where a condition match if it is not excluded, and either it is included or the include set is empty.
887890

@@ -894,7 +897,7 @@ For example, you can specify to match only the specific HTTP request method `DEL
894897
* The request remote address.
895898
* Custom predicates that receive the request to match a custom condition.
896899

897-
Notable subclasses of `ConditionalHandler` are, for example, <<handler-use-qos,`QoSHandler`>> and <<handler-use-thread-limit,`ThreadLimitHandler`>>.
900+
Notable subclasses of `ConditionalHandler` are, for example, <<handler-use-qos,`QoSHandler`>>, <<handler-use-thread-limit,`ThreadLimitHandler`>> and <<handler-use-dos,`DoSHandler`>>.
898901

899902
[[handler-use-compression]]
900903
==== CompressionHandler

jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ConditionalHandler.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,4 +851,62 @@ protected boolean onConditionsMet(Request request, Response response, Callback c
851851
return nextNext != null && nextNext.handle(request, response, callback);
852852
}
853853
}
854+
855+
/**
856+
* <p>An implementation of {@link ConditionalHandler} that, if conditions are met,
857+
* calls {@link #nextHandler(Request, Response, Callback)}, otherwise sends an
858+
* error response with a configurable HTTP status code, by default {@code 403}
859+
* (Forbidden).</p>
860+
* <p>Websites that want to allow only certain HTTP methods, can configure an
861+
* instance of this class allowing only {@code GET}, {@code HEAD} and {@code POST},
862+
* so that other HTTP methods such as {@code DELETE}, {@code TRACE}, etc. are
863+
* not allowed.</p>
864+
* <p>Similarly, websites can configure HTTP URI path patterns to allow or
865+
* disallow specific request paths.</p>
866+
*/
867+
public static class NextElseReject extends ConditionalHandler
868+
{
869+
private final int _status;
870+
871+
public NextElseReject()
872+
{
873+
this(HttpStatus.FORBIDDEN_403);
874+
}
875+
876+
public NextElseReject(int status)
877+
{
878+
this(null, status);
879+
}
880+
881+
public NextElseReject(Handler nextHandler, int status)
882+
{
883+
this(false, nextHandler, status);
884+
}
885+
886+
public NextElseReject(boolean dynamic, Handler nextHandler, int status)
887+
{
888+
super(dynamic, nextHandler);
889+
if (status < 200 || status > 999)
890+
throw new IllegalArgumentException("bad status");
891+
_status = status;
892+
}
893+
894+
public int getStatusCode()
895+
{
896+
return _status;
897+
}
898+
899+
@Override
900+
protected boolean onConditionsMet(Request request, Response response, Callback callback) throws Exception
901+
{
902+
return nextHandler(request, response, callback);
903+
}
904+
905+
@Override
906+
protected boolean onConditionsNotMet(Request request, Response response, Callback callback)
907+
{
908+
Response.writeError(request, response, callback, getStatusCode());
909+
return true;
910+
}
911+
}
854912
}

0 commit comments

Comments
 (0)