3030import java .util .concurrent .Callable ;
3131
3232@ NoArgsConstructor (access = AccessLevel .PRIVATE )
33- public class MDCHandler {
33+ public class MdcHandler {
3434
3535 public static String MDC_KEY_TENANT = "tenant" ;
3636 public static String MDC_KEY_USER = "user" ;
3737
38- private static final MDCHandler SINGLETON = new MDCHandler ();
38+ private static final MdcHandler SINGLETON = new MdcHandler ();
3939
4040 @ Value ("${hawkbit.logging.mdchandler.enabled:true}" )
4141 private boolean mdcEnabled ;
4242 @ Autowired (required = false )
4343 private SpringSecurityAuditorAware springSecurityAuditorAware = new SpringSecurityAuditorAware ();
44- @ Autowired (required = false )
45- private SystemSecurityContext securityContext ;
4644
4745 /**
4846 * @return The holder singleton instance.
4947 */
50- public static MDCHandler getInstance () {
48+ public static MdcHandler getInstance () {
5149 return SINGLETON ;
5250 }
5351
5452 /**
55- * Executes callable and returns the result. If MDC is enabled, it sets the tenant and / or user in the MDC context.
53+ * Executes callable and returns the result. If MDC is enabled, it sets the tenant and / or user from the authentication in the MDC context.
5654 *
5755 * @param <T> the return type
5856 * @param callable the callable to execute
5957 * @return the result
6058 * @throws Exception if thrown by the callable
6159 */
62- public <T > T withLogging (final Callable <T > callable ) throws Exception {
60+ public <T > T callWithAuth (final Callable <T > callable ) throws Exception {
6361 if (!mdcEnabled ) {
6462 return callable .call ();
6563 }
@@ -76,43 +74,84 @@ public <T> T withLogging(final Callable<T> callable) throws Exception {
7674 tenant = null ;
7775 }
7876
79- final String currentTenant = MDC .get (MDC_KEY_TENANT );
80- if (Objects .equals (currentTenant , tenant )) {
81- return putUserAndCall (callable );
82- } else {
83- put (MDC_KEY_TENANT , tenant );
84- try {
85- return putUserAndCall (callable );
86- } finally {
87- put (MDC_KEY_TENANT , currentTenant );
88- }
77+ final String user = springSecurityAuditorAware
78+ .getCurrentAuditor ()
79+ .filter (username -> !username .equals ("system" )) // null and system are the same - system user
80+ .orElse (null );
81+
82+ return callWithTenantAndUser0 (callable , tenant , user );
83+ }
84+
85+ /**
86+ * Executes callable and returns the result. If MDC is enabled, it sets the tenant and / or user from the authentication in the MDC context.
87+ * Calls the {@link #callWithAuth(Callable)} method and wraps any catchable exception into a {@link RuntimeException}.
88+ *
89+ * @param <T> the return type
90+ * @param callable the callable to execute
91+ * @return the result
92+ */
93+ public <T > T callWithAuthRE (final Callable <T > callable ) {
94+ try {
95+ return callWithAuth (callable );
96+ } catch (final RuntimeException re ) {
97+ throw re ;
98+ } catch (final Exception e ) {
99+ throw new RuntimeException (e );
89100 }
90101 }
91102
92103 /**
93- * With logging throwing Runtime Exception (withLoggingRE). Calls the {@link #withLogging(Callable)} method and
94- * wraps any catchable exception into a {@link RuntimeException}.
104+ * Executes callable and returns the result. If MDC is enabled, it sets the tenant and / or user in the MDC context.
105+ *
106+ * @param <T> the return type
107+ * @param callable the callable to execute
108+ * @param tenant the tenant to set in the MDC context
109+ * @param user the user to set in the MDC context
110+ * @return the result
111+ */
112+ public <T > T callWithTenantAndUser (final Callable <T > callable , final String tenant , final String user ) throws Exception {
113+ if (!mdcEnabled ) {
114+ return callable .call ();
115+ }
116+
117+ return callWithTenantAndUser0 (callable , tenant , user );
118+ }
119+
120+ /**
121+ * Executes callable and returns the result. If MDC is enabled, it sets the tenant and / or user from the authentication in the MDC context.
122+ * Calls the {@link #callWithTenantAndUser(Callable, String, String)} method and wraps any catchable exception into a {@link RuntimeException}.
95123 *
96124 * @param <T> the return type
97125 * @param callable the callable to execute
126+ * @param tenant the tenant to set in the MDC context
127+ * @param user the user to set in the MDC context
98128 * @return the result
99129 */
100- public <T > T withLoggingRE (final Callable <T > callable ) {
130+ public <T > T callWithTenantAndUserRE (final Callable <T > callable , final String tenant , final String user ) {
101131 try {
102- return withLogging (callable );
132+ return callWithTenantAndUser (callable , tenant , user );
103133 } catch (final RuntimeException re ) {
104134 throw re ;
105135 } catch (final Exception e ) {
106136 throw new RuntimeException (e );
107137 }
108138 }
109139
110- private <T > T putUserAndCall (final Callable <T > callable ) throws Exception {
111- final String user = springSecurityAuditorAware
112- .getCurrentAuditor ()
113- .filter (username -> !username .equals ("system" )) // null and system are the same - system user
114- .orElse (null );
140+ private static <T > T callWithTenantAndUser0 (final Callable <T > callable , final String tenant , final String user ) throws Exception {
141+ final String currentTenant = MDC .get (MDC_KEY_TENANT );
142+ if (Objects .equals (currentTenant , tenant )) {
143+ return callWithUser (callable , user );
144+ } else {
145+ put (MDC_KEY_TENANT , tenant );
146+ try {
147+ return callWithUser (callable , user );
148+ } finally {
149+ put (MDC_KEY_TENANT , currentTenant );
150+ }
151+ }
152+ }
115153
154+ private static <T > T callWithUser (final Callable <T > callable , final String user ) throws Exception {
116155 final String currentUser = MDC .get (MDC_KEY_USER );
117156 if (Objects .equals (currentUser , user )) {
118157 return callable .call ();
@@ -137,17 +176,17 @@ private static void put(final String key, final String value) {
137176 @ NoArgsConstructor (access = AccessLevel .PRIVATE )
138177 public static class Filter {
139178
140- public static void addLoggingFilter (final HttpSecurity httpSecurity ) {
179+ public static void addMdcFilter (final HttpSecurity httpSecurity ) {
141180 httpSecurity .addFilterBefore (new OncePerRequestFilter () {
142181
143- private final MDCHandler mdcFilter = MDCHandler .getInstance ();
182+ private final MdcHandler mdcFilter = MdcHandler .getInstance ();
144183
145184 @ Override
146185 protected void doFilterInternal (
147186 final HttpServletRequest request , final HttpServletResponse response , final FilterChain filterChain )
148187 throws ServletException , IOException {
149188 try {
150- mdcFilter .withLogging (() -> {
189+ mdcFilter .callWithAuth (() -> {
151190 filterChain .doFilter (request , response );
152191 return null ;
153192 });
0 commit comments