Interface HandlerDecorator
- All Known Subinterfaces:
HandlerInterceptor
- All Known Implementing Classes:
AuthenticatingInterceptor, ContentFilterInterceptor, DataProtectionInterceptor, DisableMetrics, DocumentHandlerDecorator, ErrorReportingInterceptor, HandlerDecorator.MergedDecorator, HandlerMonitor, SchedulingInterceptor, ValidatingInterceptor, WebsocketHandlerDecorator
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Functional interface for decorating
Handler instances that process DeserializingMessage objects.
A HandlerDecorator is used to wrap a handler with additional behavior. This provides a flexible mechanism for
implementing cross-cutting concerns such as authorization, metrics, context propagation, or logging.
Unlike HandlerInterceptor, which focuses on message-level interception, a HandlerDecorator wraps the
entire handler object and can be applied at a higher level—for example, before handler resolution occurs.
Composition
Decorators can be chained using andThen(HandlerDecorator), allowing multiple layers of behavior to be
composed in a defined order.
Example:
public class MetricsDecorator implements HandlerDecorator {
@Override
public Handler<DeserializingMessage> wrap(Handler<DeserializingMessage> handler) {
return new Handler<>() {
@Override
public Optional<HandlerInvoker> getInvoker(DeserializingMessage message) {
return handler.getInvoker(message);
}
@Override
public Class<?> getTargetClass() {
return handler.getTargetClass();
}
@Override
public Object invoke(BiFunction<Object, Object, Object> combiner) {
long start = System.nanoTime();
Object result = handler.getInvoker(message).orElseThrow().invoke(combiner);
metrics.recordLatency(System.nanoTime() - start);
return result;
}
};
}
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classA composite decorator that merges two decorators into one. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final HandlerDecoratorA no-op decorator that returns the original handler unmodified. -
Method Summary
Modifier and TypeMethodDescriptiondefault HandlerDecoratorandThen(HandlerDecorator next) Chains this decorator with another, producing a composite decorator.wrap(Handler<DeserializingMessage> handler) Wraps the given handler with additional behavior.
-
Field Details
-
noOp
A no-op decorator that returns the original handler unmodified.
-
-
Method Details
-
wrap
Wraps the given handler with additional behavior.- Parameters:
handler- the original handler to be wrapped- Returns:
- a decorated handler
-
andThen
Chains this decorator with another, producing a composite decorator.The
nextdecorator is applied first, followed by this decorator.- Parameters:
next- the decorator to apply before this one- Returns:
- a combined
HandlerDecorator
-