Add user log context to user handler (#97)

Co-authored-by: pramilaniroshan <pramila@entgra.io>
Reviewed-on: community/device-mgt-core#97
Co-authored-by: Pramila Niroshan <pramila@entgra.io>
Co-committed-by: Pramila Niroshan <pramila@entgra.io>
store-certificate-device-id
Pramila Niroshan 2 years ago committed by Pahansith Gunathilake
parent 7f5137712c
commit b4d5babd93

@ -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);
}

@ -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());
}
}
}

@ -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);

Loading…
Cancel
Save