Java Code Examples for org.apache.commons.lang3.StringUtils#substringsBetween()

The following examples show how to use org.apache.commons.lang3.StringUtils#substringsBetween() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExecutorUtils.java    From AuTe-Framework with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: UserUtils.java    From cuba with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: TypeUtil.java    From fastquery with Apache License 2.0 5 votes vote down vote up
/**
 * 如果在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;
}
 
Example 4
Source File: RestClientLoggingFilterTest.java    From pay-publicapi with MIT License 5 votes vote down vote up
@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]));

}