From b4d5babd93278e937255174d500035ad43903f91 Mon Sep 17 00:00:00 2001 From: Pramila Niroshan Date: Mon, 10 Apr 2023 05:37:19 +0000 Subject: [PATCH] Add user log context to user handler (#97) Co-authored-by: pramilaniroshan Reviewed-on: https://repository.entgra.net/community/device-mgt-core/pulls/97 Co-authored-by: Pramila Niroshan Co-committed-by: Pramila Niroshan --- .../notification/logger/UserLogContext.java | 47 +++++++++++++++++++ .../logger/util/MDCContextUtil.java | 10 ++++ .../ui/request/interceptor/UserHandler.java | 9 ++-- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/components/logger/io.entgra.notification.logger/src/main/java/io/entgra/notification/logger/UserLogContext.java b/components/logger/io.entgra.notification.logger/src/main/java/io/entgra/notification/logger/UserLogContext.java index 13702d570b..49f8d0f189 100644 --- a/components/logger/io.entgra.notification.logger/src/main/java/io/entgra/notification/logger/UserLogContext.java +++ b/components/logger/io.entgra.notification.logger/src/main/java/io/entgra/notification/logger/UserLogContext.java @@ -25,12 +25,18 @@ public class UserLogContext extends LogContext { private final String userEmail; private final String metaInfo; private final String tenantID; + private final boolean isUserRegistered; + private final boolean isDeviceRegisterged; + private final String tenantDomain; private UserLogContext(Builder builder) { this.userEmail = builder.userEmail; this.userName = builder.userName; this.metaInfo = builder.metaInfo; this.tenantID = builder.tenantID; + this.isUserRegistered = builder.isUserRegistered; + this.isDeviceRegisterged = builder.isDeviceRegisterged; + this.tenantDomain = builder.tenantDomain; } public String getTenantID() { @@ -49,12 +55,26 @@ public class UserLogContext extends LogContext { return metaInfo; } + public boolean isUserRegistered() { + return isUserRegistered; + } + + public boolean isDeviceRegisterged() { + return isDeviceRegisterged; + } + + public String getTenantDomain() { + return tenantDomain; + } public static class Builder { private String userName; private String userEmail; private String metaInfo; private String tenantID; + private boolean isUserRegistered; + private boolean isDeviceRegisterged; + private String tenantDomain; public Builder() { } @@ -95,6 +115,33 @@ public class UserLogContext extends LogContext { return this; } + public boolean getIsUserRegistered() { + return isUserRegistered; + } + + public Builder setUserRegistered(boolean userRegistered) { + isUserRegistered = userRegistered; + return this; + } + + public boolean getIsDeviceRegisterged() { + return isDeviceRegisterged; + } + + public Builder setDeviceRegisterged(boolean deviceRegisterged) { + isDeviceRegisterged = deviceRegisterged; + return this; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public Builder setTenantDomain(String tenantDomain) { + this.tenantDomain = tenantDomain; + return this; + } + public UserLogContext build() { return new UserLogContext(this); } diff --git a/components/logger/io.entgra.notification.logger/src/main/java/io/entgra/notification/logger/util/MDCContextUtil.java b/components/logger/io.entgra.notification.logger/src/main/java/io/entgra/notification/logger/util/MDCContextUtil.java index fd78c26dba..7db6830a1a 100644 --- a/components/logger/io.entgra.notification.logger/src/main/java/io/entgra/notification/logger/util/MDCContextUtil.java +++ b/components/logger/io.entgra.notification.logger/src/main/java/io/entgra/notification/logger/util/MDCContextUtil.java @@ -51,6 +51,16 @@ public final class MDCContextUtil { if (mdcContext.getTenantID() != null) { MDC.put("TenantId", mdcContext.getTenantID()); } + if (mdcContext.isUserRegistered()) { + MDC.put("IsUserRegistered", "Registered"); + } + if (mdcContext.isDeviceRegisterged()) { + MDC.put("IsDeviceRegistered", mdcContext.isDeviceRegisterged()); + } + if (mdcContext.getTenantDomain() != null) { + MDC.put("TenantDomain", mdcContext.getTenantDomain()); + } + } } diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java index 997015ce8f..53be11141e 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java @@ -22,11 +22,12 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; +import io.entgra.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.notification.logger.UserLogContext; +import io.entgra.notification.logger.impl.EntgraUserLoggerImpl; import io.entgra.ui.request.interceptor.beans.AuthData; import io.entgra.ui.request.interceptor.util.HandlerConstants; import io.entgra.ui.request.interceptor.util.HandlerUtil; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.apache.http.HttpHeaders; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpPost; @@ -48,7 +49,8 @@ import java.util.Base64; @MultipartConfig @WebServlet("/user") public class UserHandler extends HttpServlet { - private static final Log log = LogFactory.getLog(UserHandler.class); + private static final EntgraLogger log = new EntgraUserLoggerImpl(UserHandler.class); + UserLogContext.Builder userLogContextBuilder = new UserLogContext.Builder(); private static final long serialVersionUID = 9050048549140517002L; @Override @@ -118,6 +120,7 @@ public class UserHandler extends HttpServlet { proxyResponse.setData( jTokenResultAsJsonObject.get("username").getAsString().replaceAll("@carbon.super", "")); HandlerUtil.handleSuccess(resp, proxyResponse); + log.info("Customer login", userLogContextBuilder.setUserName(proxyResponse.getData()).setUserRegistered(true).build()); } } catch (IOException e) { log.error("Error occurred while sending the response into the socket. ", e);