use of org.apache.commons.lang3.text.StrSubstitutor in project dhis2-core by dhis2.
the class GenericSmsGatewayTest method testSendSms_Json.
@Test
void testSendSms_Json() {
strSubstitutor = new StringSubstitutor(valueStore);
body = strSubstitutor.replace(CONFIG_TEMPLATE_JSON);
gatewayConfig.getParameters().clear();
gatewayConfig.setParameters(Arrays.asList(username, password));
gatewayConfig.setContentType(ContentType.APPLICATION_JSON);
gatewayConfig.setConfigurationTemplate(CONFIG_TEMPLATE_JSON);
ResponseEntity<String> responseEntity = new ResponseEntity<>("success", HttpStatus.OK);
when(restTemplate.exchange(any(URI.class), any(HttpMethod.class), any(HttpEntity.class), eq(String.class))).thenReturn(responseEntity);
assertThat(subject.send(SUBJECT, TEXT, RECIPIENTS, gatewayConfig).isOk(), is(true));
verify(restTemplate).exchange(any(URI.class), httpMethodArgumentCaptor.capture(), httpEntityArgumentCaptor.capture(), eq(String.class));
assertNotNull(httpEntityArgumentCaptor.getValue());
assertNotNull(httpMethodArgumentCaptor.getValue());
HttpMethod httpMethod = httpMethodArgumentCaptor.getValue();
assertEquals(HttpMethod.POST, httpMethod);
HttpEntity<String> requestEntity = httpEntityArgumentCaptor.getValue();
assertEquals(body, requestEntity.getBody());
HttpHeaders httpHeaders = requestEntity.getHeaders();
assertTrue(httpHeaders.get("Content-type").contains(gatewayConfig.getContentType().getValue()));
List<GenericGatewayParameter> parameters = gatewayConfig.getParameters();
parameters.stream().filter(p -> p.isEncode() && p.isConfidential() && p.isHeader()).forEach(p -> {
assertTrue(httpHeaders.containsKey(p.getKey()));
assertEquals(" Basic ZGVjcnlwdGVkVGV4dA==", httpHeaders.get(p.getKey()).get(0));
});
}
use of org.apache.commons.lang3.text.StrSubstitutor in project dhis2-core by dhis2.
the class AttributeOptionComboLoaderTest method replace.
private String replace(String sql, String... keyVal) {
Map<String, String> vals = new HashMap<>();
for (int i = 0; i < keyVal.length - 1; i++) {
vals.put(keyVal[i], keyVal[i + 1]);
}
StrSubstitutor sub = new StrSubstitutor(vals);
return sub.replace(sql);
}
use of org.apache.commons.lang3.text.StrSubstitutor in project eclipse.jdt.ls by eclipse.
the class ResourceUtils method expandPath.
/**
* Expand paths starting with ~/ if necessary; replaces all the occurrences of
* variables as ${variabeName} in the given string with their matching values
* from the environment variables and system properties.
*
* @param path
* @return expanded or original path
*/
public static String expandPath(String path) {
if (path != null) {
if (path.startsWith("~" + File.separator)) {
path = System.getProperty("user.home") + path.substring(1);
}
StrLookup<String> variableResolver = new StrLookup<>() {
@Override
public String lookup(String key) {
if (key.length() > 0) {
try {
String prop = System.getProperty(key);
if (prop != null) {
return prop;
}
return System.getenv(key);
} catch (final SecurityException scex) {
return null;
}
}
return null;
}
};
StrSubstitutor strSubstitutor = new StrSubstitutor(variableResolver);
return strSubstitutor.replace(path);
}
return path;
}
use of org.apache.commons.lang3.text.StrSubstitutor in project thingsboard by thingsboard.
the class BasicMapperUtils method getOAuth2User.
public static OAuth2User getOAuth2User(String email, Map<String, Object> attributes, OAuth2MapperConfig config) {
OAuth2User oauth2User = new OAuth2User();
oauth2User.setEmail(email);
oauth2User.setTenantName(getTenantName(email, attributes, config));
if (!StringUtils.isEmpty(config.getBasic().getLastNameAttributeKey())) {
String lastName = getStringAttributeByKey(attributes, config.getBasic().getLastNameAttributeKey());
oauth2User.setLastName(lastName);
}
if (!StringUtils.isEmpty(config.getBasic().getFirstNameAttributeKey())) {
String firstName = getStringAttributeByKey(attributes, config.getBasic().getFirstNameAttributeKey());
oauth2User.setFirstName(firstName);
}
if (!StringUtils.isEmpty(config.getBasic().getCustomerNamePattern())) {
StrSubstitutor sub = new StrSubstitutor(attributes, START_PLACEHOLDER_PREFIX, END_PLACEHOLDER_PREFIX);
String customerName = sub.replace(config.getBasic().getCustomerNamePattern());
oauth2User.setCustomerName(customerName);
}
oauth2User.setAlwaysFullScreen(config.getBasic().isAlwaysFullScreen());
if (!StringUtils.isEmpty(config.getBasic().getDefaultDashboardName())) {
oauth2User.setDefaultDashboardName(config.getBasic().getDefaultDashboardName());
}
return oauth2User;
}
use of org.apache.commons.lang3.text.StrSubstitutor in project dcos-commons by mesosphere.
the class YAMLConfigurationLoader method loadConfigFromEnv.
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);
}
Aggregations