-
Notifications
You must be signed in to change notification settings - Fork 120
/
ConfigServiceImpl.java
168 lines (142 loc) · 5.15 KB
/
ConfigServiceImpl.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.flowci.core.config.service;
import com.flowci.core.common.config.AppProperties;
import com.flowci.core.common.domain.Mongoable;
import com.flowci.core.common.manager.SpringEventManager;
import com.flowci.core.config.dao.ConfigDao;
import com.flowci.core.config.domain.*;
import com.flowci.core.config.event.GetConfigEvent;
import com.flowci.core.secret.domain.Secret;
import com.flowci.core.secret.event.GetSecretEvent;
import com.flowci.domain.SimpleAuthPair;
import com.flowci.exception.ArgumentException;
import com.flowci.exception.DuplicateException;
import com.flowci.exception.NotFoundException;
import com.flowci.exception.StatusException;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.EventListener;
import org.springframework.core.io.Resource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
@Log4j2
@Service
public class ConfigServiceImpl implements ConfigService {
@Value("classpath:default/smtp-demo-config.yml")
private Resource defaultSmtpConfigYml;
@Autowired
private AppProperties appProperties;
@Autowired
private ConfigDao configDao;
@Autowired
private SpringEventManager eventManager;
@PostConstruct
public void onInit() {
try {
Config config = ConfigParser.parse(defaultSmtpConfigYml.getInputStream());
Optional<Config> optional = configDao.findByName(config.getName());
// delete if default smtp config is disabled
if (!appProperties.isDefaultSmtpConfig()) {
optional.ifPresent(value -> configDao.delete(value));
return;
}
if (optional.isPresent()) {
return;
}
configDao.save(config);
log.info("Config {} has been created", config.getName());
} catch (IOException e) {
log.warn(e);
}
}
@EventListener
public void onGetConfigEvent(GetConfigEvent event) {
try {
Config config = get(event.getName());
event.setFetched(config);
} catch (NotFoundException e) {
event.setError(e);
}
}
@Override
public Config get(String name) {
Optional<Config> optional = configDao.findByName(name);
if (!optional.isPresent()) {
throw new NotFoundException("Configuration name {0} is not found", name);
}
return optional.get();
}
@Override
public List<Config> list() {
return configDao.findAll(Mongoable.SortByCreatedAtASC);
}
@Override
public List<Config> list(Config.Category category) {
return configDao.findAllByCategoryOrderByCreatedAtAsc(category);
}
@Override
public Config save(String name, SmtpOption option) {
try {
SmtpConfig config = load(name, SmtpConfig.class);
config.setServer(option.getServer());
config.setPort(option.getPort());
config.setSecure(option.getSecure());
config.setAuth(option.getAuth());
if (option.hasSecret()) {
config.setAuth(getAuthPairFromSecret(option));
}
return save(config);
} catch (ReflectiveOperationException e) {
throw new StatusException(e.getMessage());
}
}
@Override
public Config save(String name, String text) {
try {
TextConfig config = load(name, TextConfig.class);
config.setText(text);
return save(config);
} catch (ReflectiveOperationException e) {
throw new StatusException(e.getMessage());
}
}
private <T extends Config> T load(String name, Class<T> tClass) throws ReflectiveOperationException {
Optional<Config> optional = configDao.findByName(name);
T config;
if (optional.isPresent()) {
config = (T) optional.get();
} else {
config = tClass.newInstance();
config.setName(name);
}
return config;
}
private <T extends Config> T save(T config) {
try {
return configDao.save(config);
} catch (DuplicateKeyException e) {
throw new DuplicateException("Config name {0} is already defined", config.getName());
}
}
private SimpleAuthPair getAuthPairFromSecret(SmtpOption option) {
GetSecretEvent event = eventManager.publish(new GetSecretEvent(this, option.getSecret()));
if (event.hasError()) {
throw event.getError();
}
Secret secret = event.getFetched();
if (secret.getCategory() != Secret.Category.AUTH) {
throw new ArgumentException("Invalid secret type");
}
return (SimpleAuthPair) secret.toSimpleSecret();
}
@Override
public Config delete(String name) {
Config config = get(name);
configDao.deleteById(config.getId());
return config;
}
}