-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSMSServiceImpl.java
More file actions
58 lines (50 loc) · 2.23 KB
/
Copy pathSMSServiceImpl.java
File metadata and controls
58 lines (50 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package myconext.sms;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@SuppressWarnings("unchecked")
public class SMSServiceImpl implements SMSService {
private final String url;
private final String templateNl;
private final String templateEn;
private final RestTemplate restTemplate = new RestTemplate();
private final MultiValueMap<String, String> headers = new HttpHeaders();
@SneakyThrows
public SMSServiceImpl(String url, String bearer) {
this.url = url;
this.templateNl = IOUtils.toString(new ClassPathResource("sms/template_nl.txt").getInputStream(), Charset.defaultCharset());
this.templateEn = IOUtils.toString(new ClassPathResource("sms/template_en.txt").getInputStream(), Charset.defaultCharset());
headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + bearer);
}
protected String formatMessage(String code, Locale locale) {
String template = locale != null && locale.getLanguage().equalsIgnoreCase("nl") ? templateNl : templateEn;
return String.format(template, code);
}
@Override
public String send(String mobile, String code, Locale locale) {
String format = formatMessage(code, locale);
Map<String, Object> body = Map.of(
"encoding", "auto",
"body", format,
"route", "1984",
"originator", "eduID",
"recipients", List.of(mobile)
);
RequestEntity<?> requestEntity = new RequestEntity(body, headers, HttpMethod.POST, URI.create(url));
restTemplate.exchange(requestEntity, Void.class);
return format;
}
}