Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
GlowrootAuthUserMiddleware.java
Go to the documentation of this file.
1package dev.rafex.ether.glowroot.jetty12;
2
3import java.util.Objects;
4import java.util.function.Function;
5
6import org.glowroot.agent.api.Glowroot;
7
8/*-
9 * #%L
10 * ether-glowroot-jetty12
11 * %%
12 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
13 * %%
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 * THE SOFTWARE.
31 * #L%
32 */
33
34import dev.rafex.ether.http.core.HttpExchange;
35import dev.rafex.ether.http.core.HttpHandler;
36import dev.rafex.ether.http.core.Middleware;
37
38/**
39 * Middleware that sets the authenticated user on the Glowroot transaction.
40 *
41 * <p>
42 * Calls {@link Glowroot#setTransactionUser(String)} with the value returned by
43 * a configurable {@code userExtractor} function. If the extractor returns
44 * {@code null} or blank, the user is not set.
45 * </p>
46 *
47 * <p>
48 * For Jetty-specific extraction from JWT auth context use
49 * {@link GlowrootJettyExtractors#authUser()} to obtain the extractor:
50 * </p>
51 *
52 * <pre>{@code
53 * middlewareRegistry.add(new GlowrootAuthUserMiddleware(GlowrootJettyExtractors.authUser()));
54 * }</pre>
55 *
56 * <p>
57 * Setting the user enables Glowroot's <em>"User recording"</em> — you can
58 * search all slow traces for a specific user, invaluable for debugging
59 * user-specific issues.
60 * </p>
61 */
62public final class GlowrootAuthUserMiddleware implements Middleware {
63
64 private final Function<HttpExchange, String> userExtractor;
65
66 /**
67 * Creates a middleware with the given user-extractor function.
68 *
69 * @param userExtractor function that derives the user identifier from an
70 * {@link HttpExchange}; must not be {@code null}
71 */
72 public GlowrootAuthUserMiddleware(final Function<HttpExchange, String> userExtractor) {
73 this.userExtractor = Objects.requireNonNull(userExtractor, "userExtractor must not be null");
74 }
75
76 @Override
77 public HttpHandler wrap(final HttpHandler next) {
78 return exchange -> {
79 try {
80 final var user = userExtractor.apply(exchange);
81 if (user != null && !user.isBlank()) {
82 Glowroot.setTransactionUser(user);
83 Glowroot.addTransactionAttribute("auth.user", user);
84 }
85 } catch (final Throwable ignore) {
86 // Extractor or Glowroot failure must never affect the request
87 }
88 return next.handle(exchange);
89 };
90 }
91}
GlowrootAuthUserMiddleware(final Function< HttpExchange, String > userExtractor)
Creates a middleware with the given user-extractor function.
boolean handle(HttpExchange exchange)