use of org.apache.commons.text.StringSubstitutor in project dhis2-core by dhis2.
the class SimplisticHttpGetGateWay method getRequestEntity.
// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private HttpEntity<String> getRequestEntity(GenericHttpGatewayConfig config, String text, Set<String> recipients) {
// Matches
final StringSubstitutor substitutor = new StringSubstitutor(getRequestData(config, text, recipients));
// on
// ${...}
String data = substitutor.replace(config.getConfigurationTemplate());
return new HttpEntity<>(data, getRequestHeaderParameters(config));
}
use of org.apache.commons.text.StringSubstitutor in project dhis2-core by dhis2.
the class GenericSmsGatewayTest method testSendSms_Url.
@Test
void testSendSms_Url() {
username.setHeader(false);
password.setHeader(false);
password.setConfidential(true);
valueStore.put(username.getKey(), username.getValue());
valueStore.put(password.getKey(), password.getValue());
strSubstitutor = new StringSubstitutor(valueStore);
gatewayConfig.getParameters().clear();
gatewayConfig.setParameters(Arrays.asList(username, password));
gatewayConfig.setContentType(ContentType.FORM_URL_ENCODED);
gatewayConfig.setConfigurationTemplate(CONFIG_TEMPLATE_URL_ENCODED);
body = strSubstitutor.replace(CONFIG_TEMPLATE_URL_ENCODED);
ResponseEntity<String> responseEntity = new ResponseEntity<>("success", HttpStatus.OK);
when(pbeStringEncryptor.decrypt(anyString())).thenReturn(password.getValue());
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());
}
use of org.apache.commons.text.StringSubstitutor 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.text.StringSubstitutor in project dhis2-core by dhis2.
the class JdbcCompleteDataSetRegistrationExchangeStore method createQuery.
private static String createQuery(ExportParams params) {
IdSchemes idSchemes = params.getOutputIdSchemes() != null ? params.getOutputIdSchemes() : new IdSchemes();
ImmutableMap.Builder<String, String> namedParamsBuilder = ImmutableMap.<String, String>builder().put(DATA_SET_SCHEME, idSchemes.getDataSetIdScheme().getIdentifiableString().toLowerCase()).put(ORG_UNIT_SCHEME, idSchemes.getOrgUnitIdScheme().getIdentifiableString().toLowerCase()).put(ATTR_OPT_COMBO_SCHEME, idSchemes.getAttributeOptionComboIdScheme().getIdentifiableString().toLowerCase());
String sql = "SELECT ds.${dsScheme} AS dsid, pe.startdate AS pe_start, pt.name AS ptname, ou.${ouScheme} AS ouid, " + "aoc.${aocScheme} AS aocid, cdsr.storedby AS storedby, cdsr.date AS created, cdsr.completed AS completed " + "FROM completedatasetregistration cdsr " + "INNER JOIN dataset ds ON ( cdsr.datasetid=ds.datasetid ) " + "INNER JOIN period pe ON ( cdsr.periodid=pe.periodid ) " + "INNER JOIN periodtype pt ON ( pe.periodtypeid=pt.periodtypeid ) " + "INNER JOIN organisationunit ou ON ( cdsr.sourceid=ou.organisationunitid ) " + "INNER JOIN categoryoptioncombo aoc ON ( cdsr.attributeoptioncomboid = aoc.categoryoptioncomboid ) ";
sql += createOrgUnitGroupJoin(params);
sql += createDataSetClause(params, namedParamsBuilder);
sql += createOrgUnitClause(params, namedParamsBuilder);
sql += createPeriodClause(params, namedParamsBuilder);
sql += createCreatedClause(params, namedParamsBuilder);
sql += createLimitClause(params, namedParamsBuilder);
sql = new StringSubstitutor(namedParamsBuilder.build(), "${", "}").replace(sql);
log.debug("CompleteDataSetRegistrations query: " + sql);
return sql;
}
use of org.apache.commons.text.StringSubstitutor in project dhis2-core by dhis2.
the class AttributeOptionComboLoader method getAttributeOptionCombo.
private CategoryOptionCombo getAttributeOptionCombo(IdScheme idScheme, String categoryComboId, Set<CategoryOption> categoryOptions) {
final String key = "categorycomboid";
final String categoryComboKey = resolveId(idScheme, key, categoryComboId);
final String optionsId = categoryOptions.stream().map(co -> Long.toString(co.getId())).map(s -> "'" + s + "'").collect(Collectors.joining(","));
StringSubstitutor sub = new StringSubstitutor(ImmutableMap.<String, String>builder().put("resolvedScheme", Objects.requireNonNull(categoryComboKey)).put("option_ids", optionsId).build());
// TODO use cache
List<CategoryOptionCombo> categoryOptionCombos = jdbcTemplate.query(sub.replace(SQL_GET_CATEGORYOPTIONCOMBO_BY_CATEGORYIDS), (rs, i) -> bind("categoryoptioncomboid", rs));
if (categoryOptionCombos.size() == 1) {
return categoryOptionCombos.get(0);
} else {
// TODO throw an error??
return null;
}
}
Aggregations