package com.mgm.mcrm.common.constants; import com.alibaba.fastjson.JSONObject; import com.auth0.jwt.JWT; import com.auth0.jwt.interfaces.DecodedJWT; import com.mgm.mcrm.appsetting.models.SysSetting; import com.mgm.mcrm.authorization.dtos.LoginUserInfo; import com.mgm.mcrm.authorization.models.SysUser; import com.mgm.mcrm.booking.services.BookingService; import com.mgm.mcrm.common.dtos.config.APIConfig; import com.mgm.mcrm.common.dtos.msg.AppSettingMsgData; import com.mgm.mcrm.common.dtos.msg.UpdateCfgItem; import com.mgm.mcrm.common.dtos.system.AppServerState; import com.mgm.mcrm.common.utils.LogUtil; import com.mgm.mcrm.common.utils.NetworkUtil; import com.mgm.mcrm.logging.models.SysLogLogin; import com.mgm.mcrm.logging.models.SysLogOperation; import jakarta.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Strings; import org.springframework.boot.system.ApplicationHome; import org.springframework.context.MessageSource; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.web.servlet.support.RequestContextUtils; import java.io.*; import java.lang.reflect.Field; import java.text.SimpleDateFormat; import java.util.*; public class SysCommon { public static String runtime_path = ""; public static String serverIp=""; public static String serverName=""; public static boolean dbDebugLog = false; public static boolean apiDebugLog = false; public static boolean appDebugLog = false; public static boolean mgmApiMockup =true; public static boolean jmsRunning=false; public static String defaultRouter = "welcome"; public static String runtimeName="crm_mobile_runtime"; public static int postTimeOut = 20; //for stress test public static int tokenExpMin = 5; public static int refreshTokenExpDay = 30; public static String tokenSecret = "SecretKeyWhichShouldBeLongEnoughToBeSecure67848563"; public static APIConfig apiConfig = new APIConfig(); private static Map notifyTemplateMap = new HashMap(); private static Map appServerStates = new HashMap(); private static List allowOriginSource = new ArrayList(); private static List soapServers = new ArrayList(); private static List httpApiServers = new ArrayList(); private static List allowIPSource = new ArrayList(); // @Autowired private static MessageSource messageSource; public static String getSystemPath() { ApplicationHome h = new ApplicationHome(SysCommon.class); File f = h.getSource(); return f.getParentFile().toString(); } public static void initRuntimePath() { runtime_path = getSystemPath(); } public static boolean loadSystemConfig() { boolean isLoaded = true; Properties props = new Properties(); try { initRuntimePath(); // load config from appconfig.properties String configFile = runtime_path + "/crm_mobile_runtime/appconfig.properties"; InputStream inStream = new BufferedInputStream(new FileInputStream(configFile)); props.load(inStream); //--Server Info serverIp = NetworkUtil.getInternalIp(); serverName = NetworkUtil.getServerName(); loadAPIConfig(props); } catch (Exception e) { isLoaded = false; LogUtil.error(SysCommon.class, "Load configuration error:" + e.getMessage()); } finally { props.clear(); props = null; } return isLoaded; } public static void loadSettingsFromDB(List sysSettings) { System.out.println("[SysCommon][loadSettingsFromDB] Load settings from DB, setting size=" + sysSettings.size()); for(int i=0; i0) { contentBuffer.append("\n"); } contentBuffer.append(lineData); lineCnt++; } bufferedReader.close(); fileContent = contentBuffer.toString(); } catch (Exception e) { //e.printStackTrace(); //LogUtil.error(SysCommon.class, "Get Data File failed:"+e.getMessage()); } finally { try { if (bufferedReader != null) { bufferedReader.close(); } if(fileReader!=null) { fileReader.close(); } } catch (IOException e) { e.printStackTrace(); } } //-- return fileContent; } public static AppServerState refreshAppServerState() { AppServerState currentState = new AppServerState(); currentState.setServerIp(SysCommon.serverIp); currentState.setHostName(SysCommon.serverName); currentState.setServerType("MOBILE"); currentState.setApiDebugLog(SysCommon.apiDebugLog); currentState.setDbDebugLog(SysCommon.dbDebugLog); currentState.setAppDebugLog(SysCommon.appDebugLog); appServerStates.put(currentState.getServerType()+"-"+SysCommon.serverIp, currentState); return currentState; } public static void registerServerInfo(AppServerState serverInfo) { if(serverInfo!=null && (!Strings.CS.equals(serverIp, serverInfo.getServerIp()) || !Strings.CS.equals("MOBILE", serverInfo.getServerType()) )) { appServerStates.put(serverInfo.getServerType()+"-"+serverInfo.getServerIp(), serverInfo); } } public static AppServerState updateCfgByRequest(AppSettingMsgData updateRequest) { if(updateRequest!=null) { List cfgItems = updateRequest.getConfigItems(); if(cfgItems!=null) { for(UpdateCfgItem cfgItem:cfgItems) { if(Strings.CS.equals("dbDebugLog", cfgItem.getCfgItemKey())) { SysCommon.dbDebugLog= Boolean.parseBoolean(cfgItem.getCfgItemValue()); }else if(Strings.CS.equals("apiDebugLog", cfgItem.getCfgItemKey())) { SysCommon.apiDebugLog= Boolean.parseBoolean(cfgItem.getCfgItemValue()); }else if(Strings.CS.equals("appDebugLog", cfgItem.getCfgItemKey())) { SysCommon.appDebugLog= Boolean.parseBoolean(cfgItem.getCfgItemValue()); } } } } return refreshAppServerState(); } public static LoginUserInfo parseUserFromToken(HttpServletRequest request) { LoginUserInfo loginUser = null; String token = request.getHeader("Authorization"); if(StringUtils.isNotBlank(token)) { try { // token = token.substring(6); loginUser = parseUserFromToken(token); }catch(Exception err) { LogUtil.error(SysCommon.class, "Parse Token failed:"+err.getMessage()); loginUser =null; } } return loginUser; } public static LoginUserInfo parseUserFromToken(String tokenContent) { LoginUserInfo loginUser = null; if(StringUtils.isNotBlank(tokenContent)) { try { tokenContent = tokenContent.replace("Bearer ", ""); DecodedJWT decodeData = JWT.decode(tokenContent); loginUser = new LoginUserInfo(); String userId = decodeData.getSubject(); if(StringUtils.isNotBlank(userId)) { loginUser.setId(Long.parseLong(userId)); }else { loginUser.setId(-1L); } loginUser.setLoginId(decodeData.getClaim("login_id").asString()); loginUser.setGroupName(decodeData.getClaim("user_group").asString()); }catch(Exception err) { LogUtil.error(SysCommon.class, "Parse Token failed:"+err.getMessage()); loginUser =null; } } return loginUser; } public static void loadSecurityConfig(String allowOrigins) { if(allowOrigins!=null) { String[] serverLists = allowOrigins.split(","); if(serverLists!=null) { for(String item:serverLists) { if(StringUtils.isNotBlank(item)) { addAllowOriginSource(item.trim()); } } } } } public static void addAllowOriginSource(String srcServer) { allowOriginSource.add(srcServer); } public static boolean isAllowOriginSource(String srcServer) { boolean isAllow = false; if(StringUtils.isNotBlank(srcServer)) { for(String allowItem:allowOriginSource) { if(Strings.CI.startsWith(srcServer, allowItem)) { isAllow=true; break; } } } return isAllow; } public static void loadSoapServerConfig(String soapConfig) { if(soapConfig!=null) { String[] serverLists = soapConfig.split(","); if(serverLists!=null) { for(String item:serverLists) { if(StringUtils.isNotBlank(item)) { addSoapServer(item.trim()); } } } } } public static void addSoapServer(String srcServer) { soapServers.add(srcServer); } public static boolean isAllowSoapServer(String srcServer) { boolean isAllow = false; if(StringUtils.isNotBlank(srcServer)) { for(String allowItem:soapServers) { if(Strings.CI.startsWith(srcServer, allowItem)) { isAllow=true; break; } } } return isAllow; } public static void loadApiServerConfig(String apiServerConfig) { if(apiServerConfig!=null) { String[] serverLists = apiServerConfig.split(","); if(serverLists!=null) { for(String item:serverLists) { if(StringUtils.isNotBlank(item)) { addApiServer(item.trim()); } } } } } public static void addApiServer(String srcServer) { httpApiServers.add(srcServer); } public static boolean isAllowApiServer(String srcServer) { boolean isAllow = false; if(StringUtils.isNotBlank(srcServer)) { for(String allowItem:httpApiServers) { if(Strings.CI.startsWith(srcServer, allowItem)) { isAllow=true; break; } } } return isAllow; } public static void loadAllowIPConfig(String allowIPConfig) { if(allowIPConfig!=null) { String[] ipLists = allowIPConfig.split(","); if(ipLists!=null) { for(String item:ipLists) { if(StringUtils.isNotBlank(item)) { addAllowIP(item.trim()); } } } } } public static void addAllowIP(String allowIP) { allowIPSource.add(allowIP); } public static boolean isAllowIPSource(String ip) { boolean isAllow = false; if(StringUtils.isNotBlank(ip)) { for(String allowItem:allowIPSource) { LogUtil.info(BookingService.class,"[isAllowIPSource]allow item:"+allowItem); if(Strings.CI.startsWith(ip, allowItem)) { isAllow=true; break; } } } return isAllow; } }