e1305e8c
sfeutrie
projet Spring boo...
|
1
2
3
|
package com.PFE.ServerManager;
import org.springframework.beans.factory.annotation.Autowired;
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
4
|
import org.springframework.http.HttpStatus;
|
e1305e8c
sfeutrie
projet Spring boo...
|
5
|
import org.springframework.stereotype.Controller;
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
6
|
import org.springframework.web.bind.annotation.*;
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
7
|
import org.springframework.web.multipart.MultipartFile;
|
d0a03db7
sfeutrie
restructuration d...
|
8
|
import org.springframework.web.servlet.ModelAndView;
|
57e157a4
sfeutrie
optimisation de l...
|
9
|
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
10
11
|
import java.io.*;
import java.sql.Timestamp;
|
115e3f68
sfeutrie
amélioration du f...
|
12
13
|
import java.util.*;
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
14
15
16
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.Authentication;
|
cb678ccc
Unknown
Changement pour l...
|
17
18
|
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
|
e1305e8c
sfeutrie
projet Spring boo...
|
19
|
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
20
|
import javax.servlet.annotation.MultipartConfig;
|
57e157a4
sfeutrie
optimisation de l...
|
21
|
|
d0a03db7
sfeutrie
restructuration d...
|
22
|
@Controller
|
2df8b522
Unknown
Choix du fichier ...
|
23
24
|
@MultipartConfig(fileSizeThreshold = 20971520) //à changer, taille max des fichiers
|
e1305e8c
sfeutrie
projet Spring boo...
|
25
|
public class MainController {
|
d0a03db7
sfeutrie
restructuration d...
|
26
27
|
@Autowired
|
a13d6d0f
Unknown
Fichier de config...
|
28
29
30
|
NodeRepository nodeRepository;
@Autowired
|
e1305e8c
sfeutrie
projet Spring boo...
|
31
32
|
CustomerRepository customerRepository;
|
d0a03db7
sfeutrie
restructuration d...
|
33
34
35
|
@Autowired
RoleRepository roleRepository;
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
36
|
@Autowired
|
d1d16f06
sfeutrie
implementation de...
|
37
38
39
|
MajRepository majRepository;
@Autowired
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
40
41
42
|
BCryptPasswordEncoder bCryptPasswordEncoder;
@GetMapping(value="/")
|
f1c88101
sfeutrie
Ergonomie du site
|
43
|
public String uploadRedirection(){
|
cb678ccc
Unknown
Changement pour l...
|
44
|
return "redirect:home";
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
45
46
47
48
49
50
|
}
@GetMapping(value="/home")
public ModelAndView home() {
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
51
52
|
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
|
16791f79
sfeutrie
routage du site
|
53
|
modelAndView.addObject("customerRole", customer.getRole());
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
54
55
|
modelAndView.setViewName("home");
return modelAndView;
|
e1305e8c
sfeutrie
projet Spring boo...
|
56
57
|
}
|
f1c88101
sfeutrie
Ergonomie du site
|
58
59
60
61
62
63
64
65
66
67
68
|
@GetMapping(value="/upload")
public ModelAndView upload() {
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
modelAndView.addObject("customerRole", customer.getRole());
modelAndView.setViewName("upload");
return modelAndView;
}
|
deabfae9
sfeutrie
ajout d'une class...
|
69
70
71
72
73
74
75
|
@GetMapping(value="/session")
public ModelAndView session() {
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
modelAndView.addObject("customerRole", customer.getRole());
|
f6a02b6c
Unknown
Mise à jour front...
|
76
|
modelAndView.addObject("customerMaj", customer.getMaj());
|
a13d6d0f
Unknown
Fichier de config...
|
77
78
|
List<Node> nodes = nodeRepository.findAll();
modelAndView.addObject("nodes", nodes);
|
2df8b522
Unknown
Choix du fichier ...
|
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
File file = new File("files");
File[] dirs = file.listFiles();
List<String> filesName = new ArrayList<String>();
if (dirs != null) {
for (int i = 0; i < dirs.length; i++) {
if (dirs[i].isDirectory() == true) {
if ((dirs[i].getName().split("_")[0]).equals(customer.getEmail().split("@")[0])) {
File dir = new File(dirs[i].getAbsolutePath());
File[] f = dir.listFiles();
filesName.add(f[0].getName());
}
}
}
}
modelAndView.addObject("customerFiles", filesName);
|
deabfae9
sfeutrie
ajout d'une class...
|
97
|
modelAndView.setViewName("session");
|
f1c88101
sfeutrie
Ergonomie du site
|
98
|
|
deabfae9
sfeutrie
ajout d'une class...
|
99
100
101
|
return modelAndView;
}
|
d0a03db7
sfeutrie
restructuration d...
|
102
|
@GetMapping(path="/registration")
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
103
104
105
|
public ModelAndView registration() {
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
106
107
|
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
|
115e3f68
sfeutrie
amélioration du f...
|
108
|
modelAndView.addObject("customerRole", customer.getRole());
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
109
110
|
modelAndView.setViewName("registration");
return modelAndView;
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
111
112
|
}
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
113
114
115
116
117
|
@GetMapping(path="/denied")
public String denied() {
return "denied";
}
|
d0a03db7
sfeutrie
restructuration d...
|
118
|
@PostMapping(path="/registration")
|
072e7fc2
Unknown
Passage de pseudo...
|
119
|
public ModelAndView addNewUser(@RequestParam String email, @RequestParam String password, @RequestParam String role) {
|
d0a03db7
sfeutrie
restructuration d...
|
120
121
|
//Model map, ModelAndView ou l'utilisation direct comme dans la méthode précédente sont 3 méthodes qui permettent d'envoyer des informations et donc de changer l'apparence d'une page
ModelAndView modelAndView = new ModelAndView(); // il n'est peut être pas utile d'utiliser ModelAndView
|
e1305e8c
sfeutrie
projet Spring boo...
|
122
|
Customer n = new Customer();
|
072e7fc2
Unknown
Passage de pseudo...
|
123
|
n.setEmail(email);
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
124
|
n.setPassword(bCryptPasswordEncoder.encode(password));
|
072e7fc2
Unknown
Passage de pseudo...
|
125
|
n.setId((int)(customerRepository.count() + 1));
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
126
|
n.setActive(1);
|
072e7fc2
Unknown
Passage de pseudo...
|
127
|
Customer temp = customerRepository.findByEmail(email);
|
16791f79
sfeutrie
routage du site
|
128
|
Role userRole = roleRepository.findByRole(role);
|
57e157a4
sfeutrie
optimisation de l...
|
129
|
n.setRoles(new HashSet<Role>(Arrays.asList(userRole)));
|
f1c88101
sfeutrie
Ergonomie du site
|
130
|
|
115e3f68
sfeutrie
amélioration du f...
|
131
132
|
//utilisé uniquement pour continuer à afficher l'utilisateur connecté//
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
133
134
|
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
|
115e3f68
sfeutrie
amélioration du f...
|
135
136
|
modelAndView.addObject("customerRole", customer.getRole());
modelAndView.setViewName("registration");
|
072e7fc2
Unknown
Passage de pseudo...
|
137
|
|
f1c88101
sfeutrie
Ergonomie du site
|
138
139
140
|
List<Customer> list = customerRepository.findAll(); // attention : la méthode findAll() de JpaRepository retourne une liste alors que celle de CrudRepository retourne un itérable
modelAndView.addObject("list", list);
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
141
|
if(temp != null) {
|
e23a6c00
Antoine Duquenoy
UI : modification...
|
142
143
|
modelAndView.addObject("message", "L'utilisateur existe déjà !");
modelAndView.addObject("fail", true);
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
144
|
}
|
d0a03db7
sfeutrie
restructuration d...
|
145
|
else {
|
e23a6c00
Antoine Duquenoy
UI : modification...
|
146
147
|
modelAndView.addObject("message", "L'utilisateur a bien été ajouté !");
modelAndView.addObject("ok", true);
|
d0a03db7
sfeutrie
restructuration d...
|
148
|
customerRepository.save(n);
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
149
|
}
|
d0a03db7
sfeutrie
restructuration d...
|
150
151
152
|
modelAndView.setViewName("registration");
return modelAndView;
}
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
153
|
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
154
155
|
@RequestMapping(value = "/file", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
|
3e6ff772
Unknown
Page home restruc...
|
156
|
public void submit(@RequestParam MultipartFile file) {
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
157
158
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
159
|
Customer customer = customerRepository.findByEmail(auth.getName());
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
160
|
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
072e7fc2
Unknown
Passage de pseudo...
|
161
|
File dirs = new File("files/" + customer.getEmail().split("@")[0] + "_" + timestamp.getTime());
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
162
163
164
|
dirs.mkdirs();
OutputStream outputStream = null;
InputStream inputStream = null;
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
try {
inputStream = file.getInputStream();
File newFile = new File(dirs.getPath() + "/" + file.getOriginalFilename());
if (!newFile.exists()) {
newFile.createNewFile();
}
outputStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
}
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
179
180
181
|
catch (IOException e) {
e.printStackTrace();
}
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
182
183
184
185
186
|
finally {
try {
outputStream.close();
}
catch(IOException e) {
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
187
188
|
}
}
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
189
190
|
}
|
cb678ccc
Unknown
Changement pour l...
|
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
|
@RequestMapping(value = "/config", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void submitConfig(@RequestParam MultipartFile file) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = customerRepository.findByEmail(auth.getName());
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
}
catch (IOException e) {
e.printStackTrace();
}
nodeRepository.deleteAll();
Yaml yaml = new Yaml(new Constructor(ConfigNodes.class));
ConfigNodes conf = yaml.load(inputStream);
Iterator<NodeYAML> iter = conf.getNodes().iterator();
while (iter.hasNext()) {
NodeYAML element = iter.next();
Node node = new Node();
node.setName(element.getName());
node.setArch(element.getArch());
node.setIp(element.getIp());
node.setNode_id((int)(nodeRepository.count() + 1));
nodeRepository.save(node);
}
}
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
220
|
|
d0a03db7
sfeutrie
restructuration d...
|
221
|
@GetMapping(path="/login")
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
222
|
public ModelAndView login() {
|
d0a03db7
sfeutrie
restructuration d...
|
223
224
225
|
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login");
return modelAndView;
|
e1305e8c
sfeutrie
projet Spring boo...
|
226
|
}
|
e1305e8c
sfeutrie
projet Spring boo...
|
227
|
|
115e3f68
sfeutrie
amélioration du f...
|
228
229
230
231
232
233
|
@GetMapping(path="/all")
public ModelAndView getAllUsers() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("all");
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
234
235
|
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
|
115e3f68
sfeutrie
amélioration du f...
|
236
237
|
modelAndView.addObject("customerRole", customer.getRole());
|
115e3f68
sfeutrie
amélioration du f...
|
238
|
List<Customer> list = customerRepository.findAll(); // attention : la méthode findAll() de JpaRepository retourne une liste alors que celle de CrudRepository retourne un itérable
|
072e7fc2
Unknown
Passage de pseudo...
|
239
|
modelAndView.addObject("list", list);
|
115e3f68
sfeutrie
amélioration du f...
|
240
|
return modelAndView;
|
e1305e8c
sfeutrie
projet Spring boo...
|
241
|
}
|
d0a03db7
sfeutrie
restructuration d...
|
242
|
|
f6a02b6c
Unknown
Mise à jour front...
|
243
|
@PostMapping(path="/savemaj")
|
2df8b522
Unknown
Choix du fichier ...
|
244
|
public String saveMaj(@RequestParam String name, @RequestParam String date, @RequestParam String nodes, @RequestParam String file){
|
d1d16f06
sfeutrie
implementation de...
|
245
246
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = customerRepository.findByEmail(auth.getName());
|
f1c88101
sfeutrie
Ergonomie du site
|
247
|
|
d1d16f06
sfeutrie
implementation de...
|
248
|
Maj maj_c = new Maj();
|
f6a02b6c
Unknown
Mise à jour front...
|
249
|
maj_c.setMaj(name);
|
d1d16f06
sfeutrie
implementation de...
|
250
|
maj_c.setDate(date);
|
d1d16f06
sfeutrie
implementation de...
|
251
|
maj_c.setNodes(nodes);
|
2df8b522
Unknown
Choix du fichier ...
|
252
|
maj_c.setFile(file);
|
d1d16f06
sfeutrie
implementation de...
|
253
|
maj_c.setMaj_id((int)(majRepository.count() + 1));
|
f1c88101
sfeutrie
Ergonomie du site
|
254
|
|
d1d16f06
sfeutrie
implementation de...
|
255
|
majRepository.save(maj_c); // ajouter la mise a jour dans la table
|
f6a02b6c
Unknown
Mise à jour front...
|
256
257
258
|
HashSet<Maj> majs = new HashSet<Maj>(Arrays.asList(maj_c));
majs.addAll(customer.getMaj());
customer.setMaj(majs);
|
d1d16f06
sfeutrie
implementation de...
|
259
|
customerRepository.save(customer); // permet de rendre effective la jointure entre customer et maj
|
f1c88101
sfeutrie
Ergonomie du site
|
260
|
|
f6a02b6c
Unknown
Mise à jour front...
|
261
|
return "redirect:/session";
|
deabfae9
sfeutrie
ajout d'une class...
|
262
|
}
|
e1305e8c
sfeutrie
projet Spring boo...
|
263
|
}
|