下面列出了怎么用org.apache.commons.lang3.text.StrLookup的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean incomingRequestPostProcessed(RequestDetails theRequestDetails, HttpServletRequest theRequest, HttpServletResponse theResponse) throws AuthenticationException {
log.trace("incomingRequestPostProcessed "+theRequest.getMethod());
Enumeration<String> headers = theRequest.getHeaderNames();
while (headers.hasMoreElements()) {
String header = headers.nextElement();
log.debug("Header = "+ header + "="+ theRequest.getHeader(header));
}
// Perform any string substitutions from the message format
StrLookup<?> lookup = new MyLookup(theRequest, theRequestDetails);
StrSubstitutor subs = new StrSubstitutor(lookup, "${", "}", '\\');
// Actually log the line
String myMessageFormat = "httpVerb[${requestVerb}] Source[${remoteAddr}] Operation[${operationType} ${idOrResourceName}] UA[${requestHeader.user-agent}] Params[${requestParameters}] RequestId[${requestHeader.x-request-id}] ForwardedFor[${requestHeader.x-forwarded-for}] ForwardedHost[${requestHeader.x-forwarded-host}] CorrelationId[] ProcessingTime[] ResponseCode[]";
String line = subs.replace(myMessageFormat);
log.info(line);
return true;
}
@Override
public void processingCompletedNormally(ServletRequestDetails theRequestDetails) {
// Perform any string substitutions from the message format
StrLookup<?> lookup = new MyLookup(theRequestDetails.getServletRequest(), theRequestDetails);
StrSubstitutor subs = new StrSubstitutor(lookup, "${", "}", '\\');
for (String header : theRequestDetails.getServletResponse().getHeaderNames()) {
log.debug("Header = " + header + "=" + theRequestDetails.getServletResponse().getHeader(header));
}
String myMessageFormat = "httpVerb[${requestVerb}] Source[${remoteAddr}] Operation[${operationType} ${idOrResourceName}] UA[${requestHeader.user-agent}] Params[${requestParameters}] RequestId[${requestHeader.x-request-id}] ForwardedFor[${requestHeader.x-forwarded-for}] ForwardedHost[${requestHeader.x-forwarded-host}] CorrelationId[${requestHeader.x-request-id}] ProcessingTime[${processingTimeMillis}]";
String line = subs.replace(myMessageFormat);
log.info(line+" ResponseCode["+theRequestDetails.getServletResponse().getStatus()+"]");
}
public static <T> T loadConfigFromEnv(Class<T> configurationClass, final String path)
throws IOException
{
LOGGER.info("Parsing configuration file from {} ", path);
logProcessEnv();
final Path configPath = Paths.get(path);
final File file = configPath.toAbsolutePath().toFile();
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
final StrSubstitutor sub = new StrSubstitutor(new StrLookup<Object>() {
@Override
public String lookup(String key) {
return System.getenv(key);
}
});
sub.setEnableSubstitutionInVariables(true);
final String conf = sub.replace(FileUtils.readFileToString(file));
return mapper.readValue(conf, configurationClass);
}
String substituteRelationships(String query, final Multimap<String, Object> valueMap) {
StrSubstitutor substitutor = new StrSubstitutor(new StrLookup<String>() {
@Override
public String lookup(String key) {
Collection<String> resolvedRelationshipTypes =
transform(valueMap.get(key), new Function<Object, String>() {
@Override
public String apply(Object input) {
if (input.toString().matches(".*(\\s).*")) {
throw new IllegalArgumentException(
"Cypher relationship templates must not contain spaces");
}
return curieUtil.getIri(input.toString()).orElse(input.toString());
}
});
return on("|").join(resolvedRelationshipTypes);
}
});
return substitutor.replace(query);
}
public static String replaceVariableReferences(final Evaluator evaluator, final String body,
final ResultRecorder resultRecorder) {
if (body == null) {
return null;
}
StrSubstitutor sub = new StrSubstitutor(new StrLookup<Object>() {
@Override
public String lookup(String name) {
try {
Object value = evaluator.evaluate(name);
if (value == null) {
return "";
} else {
return value.toString();
}
} catch (Exception e) {
resultRecorder.record(Result.FAILURE);
return "<span class=\"failure\">" + e.toString() + "</span>";
}
}
}, "$(", ")", '\\');
return sub.replace(body);
}
/**
* Adapter from the {@link EnvironmentAccessor}'s system-property resolution to the {@code StrLookup} interface.
*
* @param env the {@code EnvironmentAccessor} to use for the lookups
* @return a {@code StrLookup} view of the accessor's system properties
*/
private StrLookup<String> systemPropertiesLookup(final EnvironmentAccessor env) {
return new StrLookup<String>() {
@Override
public String lookup(String key) {
return env.getSystemProperty(key);
}
};
}
public static <V> StrSubstitutor newStrSubstitutor(String start, String end, char escape, final Map<String, V> valueMap){
StrSubstitutor substitutor = new StrSubstitutor(StrLookup.mapLookup(valueMap), StrMatcher.stringMatcher(start), StrMatcher.stringMatcher(end), escape);
return substitutor;
}
public StrSubstitutorVisitor(Map<String, String> variables) {
Map<String, String> escapedVars = new HashMap<>(variables.size());
variables.forEach((key, value) -> escapedVars.put(key, StringEscapeUtils.escapeXml10(value)));
strSubstitutor = new StrSubstitutor(StrLookup.mapLookup(escapedVars), PREFIX, StrSubstitutor.DEFAULT_SUFFIX,
StrSubstitutor.DEFAULT_ESCAPE);
}