BaseController.java
4.31 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
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
package etunicorn;
import com.fasterxml.jackson.annotation.JsonProperty;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* etunicorn-server
* Copyright © 2017 Le Club Info Polytech Lille
* Tous droits réservés
*/
@RestController
public class BaseController {
// Permettent la vérification de permissions dans les méthodes de controlleur
@Autowired
private HttpServletRequest request;
@Autowired
private SessionService sessionService;
@Autowired
private PermissionRepository permissionRepository;
protected boolean hasPermission(Permission permission) {
Session session = sessionService.getSession(request);
if (session == null || permission == null) {
return false;
}
return session.hasPermission(permission);
}
protected boolean hasPermission(String nomPermission) {
Permission permission = permissionRepository.findByNom(nomPermission);
return hasPermission(permission);
}
// Utilités pour générer des erreurs
ResponseEntity generateError(HttpStatus status, String message) {
JSONObject json = new JSONObject();
json.put("status", status.value());
json.put("message", message);
return new ResponseEntity(json, status);
}
ResponseEntity generateError(HttpStatus status) {
return generateError(status, String.format("Erreur de type %d", status.value()));
}
ResponseEntity generateError(HttpStatus status, Exception exception, String message) {
JSONObject json = new JSONObject();
json.put("status", status.value());
json.put("message", message);
json.put("errorMessage", exception.getLocalizedMessage());
json.put("stacktrace", exception.getStackTrace().toString());
return new ResponseEntity(json, status);
}
ResponseEntity generateError(Exception exception) {
return generateError(HttpStatus.INTERNAL_SERVER_ERROR, exception, exception.getLocalizedMessage());
}
// Utilités pour merger un request dans une entity
void mergeRequestInEntity(Object request, Object entity) throws EntityRequestMismatchException {
for (Method getMethode : request.getClass().getMethods()) {
String getMethodName = getMethode.getName();
JsonProperty annotation = getMethode.getAnnotation(JsonProperty.class);
if (getMethodName.startsWith("get") && annotation != null) {
String setMethodName = "s" + getMethodName.substring(1);
Method setMethode;
Class fieldClass;
try {
fieldClass = entity.getClass().getMethod(getMethodName).getReturnType();
setMethode = entity.getClass().getMethod(setMethodName, fieldClass);
} catch (NoSuchMethodException e) {
throw new EntityRequestMismatchException();
}
try {
if (getMethode.invoke(request) != null) {
Object data = getMethode.invoke(request);
if (data.getClass() != fieldClass) {
Constructor constructor = fieldClass.getConstructor(data.getClass());
if (constructor != null) {
data = constructor.newInstance(data);
}
}
setMethode.invoke(entity, data);
}
} catch (IllegalAccessException e) {
throw new EntityRequestMismatchException();
} catch (InvocationTargetException e) {
throw new EntityRequestMismatchException();
} catch (NoSuchMethodException e) {
throw new EntityRequestMismatchException();
} catch (InstantiationException e) {
throw new EntityRequestMismatchException();
}
}
}
}
}