package com.datacloudsec.alarm.process;
import com.datacloudsec.alarm.bean.AlarmBean;
import com.datacloudsec.alarm.repo.entity.EventAlarmLog;
import com.datacloudsec.alarm.util.CamelCaseUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* @Author xlj
* @Date 2018/7/30 14:44
*/
@Component
public class PlaceHolderProcess {
private static Logger logger = LoggerFactory.getLogger(PlaceHolderProcess.class);
private String placeholderPrefix = "${";
private CharSequence placeholderSuffix = "}";
private String valueSeparator = ":";
public String parseStringValue(EventAlarmLog log, AlarmBean alarmBean) {
String value = log.getAlarmName();
StringBuilder result = new StringBuilder(value);
int startIndex = value.indexOf(this.placeholderPrefix);
while (startIndex != -1) {
int endIndex = this.findPlaceholderEndIndex(result, startIndex);
if (endIndex != -1) {
String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
String propVal = resolvePlaceholder(placeholder, alarmBean);
if (propVal == null && this.valueSeparator != null) {
int separatorIndex = placeholder.indexOf(this.valueSeparator);
if (separatorIndex != -1) {
String actualPlaceholder = placeholder.substring(0, separatorIndex);
String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
propVal = resolvePlaceholder(actualPlaceholder, alarmBean);
if (propVal == null) {
propVal = defaultValue;
}
}
}
if (propVal != null) {
result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length());
} else {
startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
}
} else {
startIndex = -1;
}
}
return result.toString();
}
private String resolvePlaceholder(String placeholder, AlarmBean alarmBean) {
try {
String underlineName = CamelCaseUtils.toUnderlineName(placeholder);
return BeanUtils.getProperty(alarmBean, underlineName);
} catch (Exception e) {
return null;
}
}
private int findPlaceholderEndIndex(CharSequence buf, int startIndex) {
int index = startIndex + this.placeholderPrefix.length();
int withinNestedPlaceholder = 0;
while (index < buf.length()) {
if (substringMatch(buf, index, this.placeholderSuffix)) {
if (withinNestedPlaceholder <= 0) {
return index;
}
--withinNestedPlaceholder;
index += this.placeholderSuffix.length();
} else {
++index;
}
}
return -1;
}
public static boolean substringMatch(CharSequence str, int index, CharSequence substring) {
if (index + substring.length() > str.length()) {
return false;
} else {
for (int i = 0; i < substring.length(); ++i) {
if (str.charAt(index + i) != substring.charAt(i)) {
return false;
}
}
return true;
}
}
}
@Test public void placeHolderTest(){ PlaceHolderProcess placeHolderProcess = new PlaceHolderProcess(); EventAlarmLog log = new EventAlarmLog(); log.setAlarmName("告警名称:${eventName:异常告警},告警时间:${occurTime},告警内容:${eventContent:默认告警内容}"); AlarmBean bean = new AlarmBean(); bean.setEvent_name("SQL注入"); bean.setOccur_time(String.valueOf(new Date().getTime())); String value = placeHolderProcess.parseStringValue(log, bean); System.out.println(value); }