下面列出了org.apache.commons.lang3.StringUtils#substringsBetween ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static String evaluateExpressions(String template, Map<String, Object> scenarioVariables) {
log.debug("evaluate expressions {}, {}", template, scenarioVariables);
String result = template;
if (result != null && !Base64.isBase64(result)) {
String[] foundExpressions = StringUtils.substringsBetween(result,"<f>", "</f>");
if (foundExpressions != null) {
for (String expression : foundExpressions) {
ScriptEngine engine = new JSScriptEngine();
ScriptEngineFunctionResult evalResult = engine.executeFunction(expression, scenarioVariables);
result = result.replace(
"<f>" + expression + "</f>",
Matcher.quoteReplacement(evalResult.getResult())
);
log.debug("evaluating result {}", result);
}
}
}
log.debug("evaluate expressions result {}", result);
return result;
}
public static String formatName(String pattern, String firstName, String lastName, String middleName) throws ParseException {
if (pattern == null || pattern.length() == 0)
throw new ParseException("Pattern error", 0);
if (firstName == null || firstName.equals("null"))
firstName = "";
if (lastName == null || lastName.equals("null"))
lastName = "";
if (middleName == null || middleName.equals("null"))
middleName = "";
String[] params = StringUtils.substringsBetween(pattern, "{", "}");
int i;
for (i = 0; i < params.length; i++) {
pattern = StringUtils.replace(pattern, "{" + params[i] + "}", "{" + i + "}", 1);
params[i] = parseParam(params[i], firstName, lastName, middleName);
}
for (i = 0; i < params.length; i++) {
pattern = StringUtils.replace(pattern, "{" + i + "}", params[i], 1);
}
return pattern;
}
/**
* 如果在str中,"where"的下一个单词如果是"or"或者"and",那么就删除(忽略大小写)
*
* @param str 待处理的字符串
* @return 处理之后的字符串
*/
public static String parWhere(String str) { // 不可能传递null进来
// <where> 不存在大小写问题,因为在初始化阶段已经严格要求<where>,</where> 只能是小写
final String openWhere = "<where>";
final String closeWhere = "</where>";
String[] wheres = StringUtils.substringsBetween(str, openWhere, closeWhere);
if (wheres != null) {
for (String where : wheres) {
// sorce 不会受 where 的变化的变化
String sorce = where; // 把值copy一份
where = where.trim().replaceFirst("(?i)^where\\b", "");
// 如果第一个单词是"or"或者and,则去掉
where = where.trim().replaceFirst("(?i)^or\\b", "");
where = where.trim().replaceFirst("(?i)^and\\b", "");
where = where.trim();
// 注意: 这里用quote是因为 sorce 很可能会包含有正则符号
if ("".equals(where)) {
str = str.replaceFirst(Pattern.quote(openWhere + sorce + closeWhere), "");
} else {
str = str.replaceFirst(Pattern.quote(openWhere + sorce + closeWhere), Matcher.quoteReplacement("where " + where));
}
}
}
return str;
}
@Test
public void shouldLogRestClientEndEventWithRequestIdAndElapsedTime() {
String requestId = UUID.randomUUID().toString();
URI requestUrl = URI.create("/publicapi-request");
String requestMethod = "GET";
when(clientRequestContext.getUri()).thenReturn(requestUrl);
when(clientRequestContext.getMethod()).thenReturn(requestMethod);
MultivaluedMap<String, Object> mockHeaders = new MultivaluedHashMap<>();
MultivaluedMap<String, String> mockHeaders2 = new MultivaluedHashMap<>();
when(clientRequestContext.getHeaders()).thenReturn(mockHeaders);
when(clientResponseContext.getHeaders()).thenReturn(mockHeaders2);
MDC.put(LoggingKeys.MDC_REQUEST_ID_KEY, requestId);
loggingFilter.filter(clientRequestContext);
loggingFilter.filter(clientRequestContext, clientResponseContext);
verify(mockAppender, times(2)).doAppend(loggingEventArgumentCaptor.capture());
List<LoggingEvent> loggingEvents = loggingEventArgumentCaptor.getAllValues();
assertThat(loggingEvents.get(0).getFormattedMessage(), is(format("[%s] - %s to %s began", requestId, requestMethod, requestUrl)));
String endLogMessage = loggingEvents.get(1).getFormattedMessage();
assertThat(endLogMessage, containsString(format("[%s] - %s to %s ended - total time ", requestId, requestMethod, requestUrl)));
String[] timeTaken = StringUtils.substringsBetween(endLogMessage, "total time ", "ms");
assertTrue(NumberUtils.isCreatable(timeTaken[0]));
}