1 package net.avcompris.commons3.client; 2 3 import javax.annotation.Nullable; 4 5 import org.apache.commons.logging.Log; 6 import org.springframework.stereotype.Component; 7 8 import net.avcompris.commons3.utils.LogFactory; 9 10 @Component 11 public final class SessionPropagator { 12 13 private static final ThreadLocal<String> USER_SESSION_ID = new ThreadLocal<>(); 14 private static final ThreadLocal<String> AUTHORIZATION_HEADER = new ThreadLocal<>(); 15 16 private static final Log logger = LogFactory.getLog(SessionPropagator.class); 17 18 public SessionPropagator setUserSessionId(@Nullable final String userSessionId) { 19 20 if (logger.isDebugEnabled()) { 21 logger.debug("setUserSessionId(): " + userSessionId); 22 } 23 24 if (userSessionId == null) { 25 26 USER_SESSION_ID.remove(); 27 28 } else { 29 30 USER_SESSION_ID.set(userSessionId); 31 } 32 33 return this; 34 } 35 36 public SessionPropagator setAuthorizationHeader(@Nullable final String authorizationHeader) { 37 38 if (logger.isDebugEnabled()) { 39 logger.debug("setAuthorizationHeader(): " + authorizationHeader); 40 } 41 42 if (authorizationHeader == null) { 43 44 AUTHORIZATION_HEADER.remove(); 45 46 } else { 47 48 AUTHORIZATION_HEADER.set(authorizationHeader); 49 } 50 51 return this; 52 } 53 54 @Nullable 55 public String getUserSessionId() { 56 57 return USER_SESSION_ID.get(); 58 } 59 60 @Nullable 61 public String getAuthorizationHeader() { 62 63 return AUTHORIZATION_HEADER.get(); 64 } 65 }