Newer
Older
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* _____________________________________________________________________________
*
* INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
* Trieste INAF - IA2 Italian Center for Astronomical Archives
* _____________________________________________________________________________
*
* Copyright (C) 2016 Istituto Nazionale di Astrofisica
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License Version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package it.inaf.ia2.tsm.webapp;
import it.inaf.ia2.tsm.webapp.xmlconfig.TapCredentials;
import it.inaf.ia2.tsm.webapp.xmlconfig.UCDConfiguration;
import it.inaf.ia2.tsm.webapp.xmlconfig.UCDListConfiguration;
import it.inaf.ia2.tsm.webapp.xmlconfig.UsersConfiguration;
import it.inaf.ia2.tsm.webapp.xmlconfig.UserConfiguration;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.xml.bind.JAXB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
@Named("config")
@ApplicationScoped
public class ConfigurationManager {
private static final Logger LOG = LoggerFactory.getLogger(ConfigurationManager.class);
private static final String USERS_CONFIG_FILE_NAME = "users.xml";
private static final String UCD_CONFIG_FILE_NAME = "ucd.xml";
private File configDirectory;
private File usersConfigFile;
private File ucdConfigFile;
private UsersConfiguration usersConfig;
private UCDListConfiguration ucdListConfig;
@PostConstruct
public void init() {
try {
Properties prop = new Properties();
try (InputStream in = getClass().getClassLoader().getResourceAsStream("webapp.properties")) {
prop.load(in);
}
configDirectory = new File(prop.getProperty("config_directory"));
usersConfigFile = configDirectory.toPath().resolve(USERS_CONFIG_FILE_NAME).toFile();
ucdConfigFile = configDirectory.toPath().resolve(UCD_CONFIG_FILE_NAME).toFile();
if (!configDirectory.exists()) {
configDirectory.mkdirs();
}
if (!usersConfigFile.exists()) {
usersConfigFile.createNewFile();
usersConfig = new UsersConfiguration();
updateUsersConfigurationFile();
} else {
usersConfig = JAXB.unmarshal(usersConfigFile, UsersConfiguration.class);
}
if (!ucdConfigFile.exists()) {
ucdConfigFile.createNewFile();
ucdListConfig = new UCDListConfiguration();
updateUCDConfigurationFile();
} else {
ucdListConfig = JAXB.unmarshal(ucdConfigFile, UCDListConfiguration.class);
}
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
}
public List<UserConfiguration> getUsersConfiguration() {
return Collections.unmodifiableList(usersConfig.getUsers());
}
public List<UCDConfiguration> getUCDConfiguration() {
return Collections.unmodifiableList(ucdListConfig.getUCDList());
}
private void updateUsersConfigurationFile() {
JAXB.marshal(usersConfig, usersConfigFile);
}
private void updateUCDConfigurationFile() {
JAXB.marshal(ucdListConfig, ucdConfigFile);
}
/**
* Add a new user to the list, but only if none of the existing users have
* the same username.
*
* @return true if the user has been added to the list, false otherwise.
*/
public synchronized boolean addUser(UserConfiguration user) {
if (user.getUsername() == null) {
return false;
}
for (UserConfiguration u : usersConfig.getUsers()) {
if (u.getUsername().equals(user.getUsername())) {
return false;
}
}
usersConfig.getUsers().add(user);
updateUsersConfigurationFile();
return true;
}
public synchronized boolean deleteUser(String username) {
Iterator<UserConfiguration> ite = usersConfig.getUsers().iterator();
while (ite.hasNext()) {
UserConfiguration userConfig = ite.next();
if (userConfig.getUsername().equals(username)) {
ite.remove();
updateUsersConfigurationFile();
return true;
}
}
return false;
}
/**
* Add a new UCD to the list, but only if the word has not already been
* entered.
*
* @return true if the UCD has been added to the list, false otherwise.
*/
public synchronized boolean addUCD(UCDConfiguration ucdConfig) {
for (UCDConfiguration ucd : ucdListConfig.getUCDList()) {
if (ucd.getWord().equals(ucdConfig.getWord())) {
return false;
}
}
ucdListConfig.getUCDList().add(ucdConfig);
updateUCDConfigurationFile();
return true;
}
/**
* @return true if the UCD has been deleted from the list, false otherwise.
*/
public synchronized boolean deleteUCD(UCDConfiguration ucdConfig) {
Iterator<UCDConfiguration> ite = ucdListConfig.getUCDList().iterator();
while (ite.hasNext()) {
UCDConfiguration ucd = ite.next();
if (ucd.getWord().equals(ucdConfig.getWord())) {
ite.remove();
updateUCDConfigurationFile();
return true;
}
}
return false;
}
public List<TapCredentials> getCredentials(String username) {
for (UserConfiguration user : usersConfig.getUsers()) {
if (user.getUsername().equals(username)) {
return Collections.unmodifiableList(user.getCredentialsInfo());
}
}
return null;
}
/**
* Add a new credentials to the list, but only if the credentials has not
* already been inserted.
*
* @return true if the credentials has been added to the list, false
* otherwise.
*/
public synchronized boolean addCredentials(String username, TapCredentials credentials) {
for (UserConfiguration user : usersConfig.getUsers()) {
if (user.getUsername().equals(username)) {
for (TapCredentials tapCredentials : user.getCredentialsInfo()) {
if (tapCredentials.equals(credentials)) {
return false;
}
}
user.getCredentialsInfo().add(credentials);
updateUsersConfigurationFile();
return false;
}
}
return false;
}
/**
* @return true if the credentials has been deleted from the list, false
* otherwise.
*/
public synchronized boolean deleteCredentials(String username, TapCredentials credentials) {
for (UserConfiguration user : usersConfig.getUsers()) {
if (user.getUsername().equals(username)) {
Iterator<TapCredentials> ite = user.getCredentialsInfo().iterator();
while (ite.hasNext()) {
TapCredentials tapCredentials = ite.next();
if (tapCredentials.equals(credentials)) {
ite.remove();
updateUsersConfigurationFile();
return true;
}
}
}
}
return false;
}
public String getRestPath() {
return FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/faces/rest";
}
public String getVersion() {
return Version.NUMBER;
}
}