Skip to content

Commit b7cdbef

Browse files
committed
Refactored SMSServiceImpl for #1008
1 parent b3f6c73 commit b7cdbef

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

myconext-server/src/main/java/myconext/sms/SMSServiceImpl.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414

1515
import java.net.URI;
1616
import java.nio.charset.Charset;
17-
import java.util.List;
18-
import java.util.Locale;
19-
import java.util.Map;
17+
import java.nio.charset.StandardCharsets;
18+
import java.util.*;
2019

2120
@SuppressWarnings("unchecked")
2221
public class SMSServiceImpl implements SMSService {
@@ -28,7 +27,7 @@ public class SMSServiceImpl implements SMSService {
2827
private final String templateNl;
2928
private final String templateEn;
3029
private final RestTemplate restTemplate = new RestTemplate();
31-
private final MultiValueMap<String, String> headers = new HttpHeaders();
30+
private final String bearer;
3231

3332

3433
@SneakyThrows
@@ -37,32 +36,41 @@ public SMSServiceImpl(String url, String bearer, String route) {
3736
this.templateNl = IOUtils.toString(new ClassPathResource("sms/template_nl.txt").getInputStream(), Charset.defaultCharset());
3837
this.templateEn = IOUtils.toString(new ClassPathResource("sms/template_en.txt").getInputStream(), Charset.defaultCharset());
3938
this.route = route;
40-
headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
41-
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
42-
headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + bearer);
39+
this.bearer = bearer;
40+
41+
LOG.info("Converters: " + restTemplate.getMessageConverters());
42+
43+
restTemplate.getInterceptors().add((request, body, execution) -> {
44+
LOG.info(String.format("Sending SMS request to %s with headers %s and body:\n%s",
45+
request.getURI(),
46+
request.getHeaders(),
47+
new String(body, StandardCharsets.UTF_8)));
48+
return execution.execute(request, body);
49+
});
4350
}
4451

4552
protected String formatMessage(String code, Locale locale) {
46-
String template = locale != null && locale.getLanguage().equalsIgnoreCase("nl") ? templateNl : templateEn;
53+
String template = locale != null && "nl".equalsIgnoreCase(locale.getLanguage()) ? templateNl : templateEn;
4754
return String.format(template, code);
4855
}
4956

5057
@Override
5158
public String send(String mobile, String code, Locale locale) {
5259
String format = formatMessage(code, locale);
53-
Map<String, Object> body = Map.of(
54-
"encoding", "auto",
55-
"body", format,
56-
"route", route,
57-
"originator", "eduID",
58-
"recipients", List.of(mobile)
59-
);
60+
61+
Map<String, Object> body = new HashMap<>();
62+
body.put("encoding", "auto");
63+
body.put("body", format);
64+
body.put("route", route);
65+
body.put("originator", "eduID");
66+
body.put("recipients", Collections.singletonList(mobile));
67+
68+
HttpHeaders headers = new HttpHeaders();
69+
headers.setContentType(MediaType.APPLICATION_JSON);
70+
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
71+
headers.setBearerAuth(this.bearer);
6072

6173
RequestEntity<?> requestEntity = new RequestEntity<>(body, headers, HttpMethod.POST, URI.create(url));
62-
LOG.info(String.format("Sending SMS with url : %s, route : %s body: %s",
63-
url,
64-
route,
65-
body));
6674
restTemplate.exchange(requestEntity, Void.class);
6775
return format;
6876
}

0 commit comments

Comments
 (0)