-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSMSServiceImpl.java
More file actions
70 lines (60 loc) · 2.64 KB
/
Copy pathSMSServiceImpl.java
File metadata and controls
70 lines (60 loc) · 2.64 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
59
60
61
62
63
64
65
66
67
68
69
70
package myconext.sms;
import lombok.SneakyThrows;
import myconext.tiqr.TiqrController;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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 static final Log LOG = LogFactory.getLog(SMSServiceImpl.class);
private final String url;
public final String route;
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, String route) {
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());
this.route = route;
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", route,
"originator", "eduID",
"recipients", List.of(mobile)
);
RequestEntity<?> requestEntity = new RequestEntity<>(body, headers, HttpMethod.POST, URI.create(url));
LOG.info(String.format("Sending SMS with url : %s, route : %s body: %s",
url,
route,
body));
restTemplate.exchange(requestEntity, Void.class);
return format;
}
}