Revision 60105
Added by Konstantina Galouni over 3 years ago
modules/uoa-admin-tools-library/trunk/src/main/java/eu/dnetlib/uoaadmintoolslibrary/handlers/ForbiddenException.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaadmintoolslibrary.handlers; |
|
2 |
|
|
3 |
import org.springframework.http.HttpStatus; |
|
4 |
import org.springframework.web.bind.annotation.ResponseStatus; |
|
5 |
|
|
6 |
@ResponseStatus(HttpStatus.FORBIDDEN) |
|
7 |
public class ForbiddenException extends RuntimeException { |
|
8 |
public ForbiddenException(String message){ |
|
9 |
super(message); |
|
10 |
} |
|
11 |
} |
modules/uoa-admin-tools-library/trunk/src/main/java/eu/dnetlib/uoaadmintoolslibrary/handlers/AdminToolsLibraryExceptionsHandler.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import eu.dnetlib.uoaadmintoolslibrary.responses.ExceptionResponse; |
4 | 4 |
import org.apache.log4j.Logger; |
5 |
import org.springframework.dao.DuplicateKeyException; |
|
6 |
import org.springframework.data.crossstore.ChangeSetPersister; |
|
5 | 7 |
import org.springframework.http.HttpStatus; |
6 | 8 |
import org.springframework.http.ResponseEntity; |
7 | 9 |
import org.springframework.web.bind.MissingServletRequestParameterException; |
... | ... | |
62 | 64 |
log.debug("invalidReCaptchaException exception"); |
63 | 65 |
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST); |
64 | 66 |
} |
67 |
|
|
68 |
|
|
69 |
@ExceptionHandler(ChangeSetPersister.NotFoundException.class) |
|
70 |
public ResponseEntity<ExceptionResponse> notFoundException(Exception ex) { |
|
71 |
ExceptionResponse response = new ExceptionResponse(); |
|
72 |
response.setErrorCode("Not found Exception"); |
|
73 |
response.setErrorMessage("Not found Exception"); |
|
74 |
response.setErrors(ex.getMessage()); |
|
75 |
response.setStatus(HttpStatus.NOT_FOUND); |
|
76 |
log.error("notFoundException exception : "+ ex.getMessage()); |
|
77 |
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND); |
|
78 |
} |
|
79 |
|
|
80 |
@ExceptionHandler(ForbiddenException.class) |
|
81 |
public ResponseEntity<ExceptionResponse> forbiddenException(Exception ex) { |
|
82 |
ExceptionResponse response = new ExceptionResponse(); |
|
83 |
response.setErrorCode("Forbidden Exception"); |
|
84 |
response.setErrorMessage("Forbidden Exception"); |
|
85 |
response.setErrors(ex.getMessage()); |
|
86 |
response.setStatus(HttpStatus.FORBIDDEN); |
|
87 |
log.error("forbiddenException exception : "+ ex.getMessage()); |
|
88 |
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.FORBIDDEN); |
|
89 |
} |
|
90 |
|
|
91 |
@ExceptionHandler(DuplicateKeyException.class) |
|
92 |
public ResponseEntity<ExceptionResponse> duplicateKeyException(Exception ex) { |
|
93 |
ExceptionResponse response = new ExceptionResponse(); |
|
94 |
response.setErrorCode("DuplicateKey Exception"); |
|
95 |
response.setErrorMessage("DuplicateKey Exception"); |
|
96 |
response.setErrors(ex.getMessage()); |
|
97 |
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR); |
|
98 |
log.error("duplicateKeyException exception : "+ ex.getMessage()); |
|
99 |
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.INTERNAL_SERVER_ERROR); |
|
100 |
} |
|
65 | 101 |
} |
modules/uoa-admin-tools-library/trunk/src/main/java/eu/dnetlib/uoaadmintoolslibrary/responses/ExceptionResponse.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.uoaadmintoolslibrary.responses; |
2 | 2 |
|
3 |
import java.util.List;
|
|
3 |
import org.springframework.http.HttpStatus;
|
|
4 | 4 |
|
5 | 5 |
public class ExceptionResponse { |
6 |
|
|
6 |
private HttpStatus status; |
|
7 | 7 |
private String errorCode; |
8 | 8 |
private String errorMessage; |
9 | 9 |
private String errors; |
10 | 10 |
|
11 |
public ExceptionResponse() { |
|
12 |
} |
|
11 |
public ExceptionResponse() {} |
|
13 | 12 |
|
13 |
public HttpStatus getStatus() { return status; } |
|
14 |
|
|
15 |
public void setStatus(HttpStatus status) { this.status = status; } |
|
16 |
|
|
14 | 17 |
public String getErrorCode() { |
15 | 18 |
return errorCode; |
16 | 19 |
} |
modules/uoa-admin-tools-library/trunk/src/main/java/eu/dnetlib/uoaadmintoolslibrary/responses/SingleValueWrapperResponse.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaadmintoolslibrary.responses; |
|
2 |
|
|
3 |
public class SingleValueWrapperResponse<T> { |
|
4 |
private T value = null; |
|
5 |
|
|
6 |
public SingleValueWrapperResponse() { } |
|
7 |
public SingleValueWrapperResponse(T value) { |
|
8 |
this.value = value; |
|
9 |
} |
|
10 |
|
|
11 |
public T getValue() { |
|
12 |
return value; |
|
13 |
} |
|
14 |
|
|
15 |
public void setValue(T value) { |
|
16 |
this.value = value; |
|
17 |
} |
|
18 |
} |
modules/uoa-admin-tools-library/trunk/src/main/java/eu/dnetlib/uoaadmintoolslibrary/controllers/GenericPortalController.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaadmintoolslibrary.controllers; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal; |
|
4 |
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PortalResponse; |
|
5 |
import eu.dnetlib.uoaadmintoolslibrary.services.PortalService; |
|
6 |
import org.apache.log4j.Logger; |
|
7 |
import org.springframework.beans.factory.annotation.Autowired; |
|
8 |
import org.springframework.web.bind.annotation.*; |
|
9 |
|
|
10 |
import java.util.List; |
|
11 |
|
|
12 |
// not used by portals |
|
13 |
@RestController |
|
14 |
@RequestMapping("/portal") |
|
15 |
@CrossOrigin(origins = "*") |
|
16 |
public class GenericPortalController { |
|
17 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
18 |
|
|
19 |
@Autowired |
|
20 |
private PortalService portalService; |
|
21 |
|
|
22 |
@RequestMapping(value = "/{pid}/type", method = RequestMethod.GET) |
|
23 |
public String getPortalType(@PathVariable String pid) { |
|
24 |
Portal portal = portalService.getPortal(pid); |
|
25 |
if (portal == null) { |
|
26 |
return null; |
|
27 |
} else { |
|
28 |
return portal.getType(); |
|
29 |
} |
|
30 |
} |
|
31 |
|
|
32 |
@RequestMapping(value = {""}, method = RequestMethod.GET) |
|
33 |
public List<Portal> getAllPortals() { |
|
34 |
List<Portal> portals = portalService.getAllPortals(); |
|
35 |
|
|
36 |
return portals; |
|
37 |
} |
|
38 |
|
|
39 |
@RequestMapping(value = {"/full"}, method = RequestMethod.GET) |
|
40 |
public List<PortalResponse> getAllPortalsFull() { |
|
41 |
return portalService.getAllPortalsFull(); |
|
42 |
} |
|
43 |
|
|
44 |
// @PreAuthorize("isAuthenticated()") |
|
45 |
// @RequestMapping(value = {"/my-portals"}, method = RequestMethod.GET) |
|
46 |
// public List<Portal> getMyPortals( |
|
47 |
// //@RequestHeader("X-XSRF-TOKEN") String token |
|
48 |
// |
|
49 |
// ) { |
|
50 |
//// log.debug(token); |
|
51 |
//// UserInfo userInfo = utils.getUserInfo(token); |
|
52 |
//// log.debug(userInfo); |
|
53 |
//// if(userInfo != null) { |
|
54 |
//// List<String> roles = userInfo.getRoles(); |
|
55 |
//// for (String role : roles) { |
|
56 |
//// log.debug(role); |
|
57 |
//// } |
|
58 |
//// } |
|
59 |
// |
|
60 |
// List<GrantedAuthority> authorities = null; |
|
61 |
// Authentication authentication = (Authentication) SecurityContextHolder.getContext().getAuthentication(); |
|
62 |
// if(authentication != null) { |
|
63 |
// authorities = (List<GrantedAuthority>) authentication.getAuthorities(); |
|
64 |
// } |
|
65 |
// log.debug(authorities); |
|
66 |
// |
|
67 |
//// for(GrantedAuthority authority : authorities) { |
|
68 |
//// authorizationService. |
|
69 |
//// } |
|
70 |
// List<Portal> portals = portalService.getAllPortals(); |
|
71 |
// for(Portal portal : portals) { |
|
72 |
// if(authorizationService..manager(portal.getType(), portal.getPid())) { |
|
73 |
// |
|
74 |
// } |
|
75 |
// } |
|
76 |
// |
|
77 |
// List<Portal> myPortals = new ArrayList<>(); |
|
78 |
// // Get roles and for every role, find portalType and add portals |
|
79 |
// List<String> portalTypes = new ArrayList<>(); |
|
80 |
// portalTypes.add("connect"); |
|
81 |
// portalTypes.add("community"); |
|
82 |
// portalTypes.add("explore"); |
|
83 |
// for(String portalType : portalTypes) { |
|
84 |
// myPortals.addAll(portalService.getAllPortalsByType(portalType)); |
|
85 |
// log.debug("Num of portals now: "+myPortals.size()); |
|
86 |
// } |
|
87 |
// |
|
88 |
// return myPortals; |
|
89 |
// } |
|
90 |
} |
modules/uoa-admin-tools-library/trunk/src/main/java/eu/dnetlib/uoaadmintoolslibrary/controllers/SimpleErrorController.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaadmintoolslibrary.controllers; |
|
2 |
import org.springframework.beans.factory.annotation.Autowired; |
|
3 |
import org.springframework.boot.autoconfigure.web.ErrorAttributes; |
|
4 |
import org.springframework.boot.autoconfigure.web.ErrorController; |
|
5 |
import org.springframework.util.Assert; |
|
6 |
import org.springframework.web.bind.annotation.CrossOrigin; |
|
7 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
8 |
import org.springframework.web.bind.annotation.RestController; |
|
9 |
import org.springframework.web.context.request.RequestAttributes; |
|
10 |
import org.springframework.web.context.request.ServletRequestAttributes; |
|
11 |
|
|
12 |
import javax.servlet.http.HttpServletRequest; |
|
13 |
import java.util.Map; |
|
14 |
|
|
15 |
/** |
|
16 |
* Created by argirok on 8/3/2018. |
|
17 |
*/ |
|
18 |
|
|
19 |
@RestController |
|
20 |
@CrossOrigin(origins = "*") |
|
21 |
@RequestMapping("/error") |
|
22 |
public class SimpleErrorController implements ErrorController { |
|
23 |
|
|
24 |
private final ErrorAttributes errorAttributes; |
|
25 |
|
|
26 |
@Autowired |
|
27 |
public SimpleErrorController(ErrorAttributes errorAttributes) { |
|
28 |
Assert.notNull(errorAttributes, "ErrorAttributes must not be null"); |
|
29 |
this.errorAttributes = errorAttributes; |
|
30 |
} |
|
31 |
|
|
32 |
@Override |
|
33 |
public String getErrorPath() { |
|
34 |
return "/error"; |
|
35 |
} |
|
36 |
|
|
37 |
@RequestMapping |
|
38 |
public Map<String, Object> error(HttpServletRequest aRequest){ |
|
39 |
Map<String, Object> body = getErrorAttributes(aRequest,getTraceParameter(aRequest)); |
|
40 |
String trace = (String) body.get("trace"); |
|
41 |
if(trace != null){ |
|
42 |
String[] lines = trace.split("\n\t"); |
|
43 |
body.put("trace", lines); |
|
44 |
} |
|
45 |
return body; |
|
46 |
} |
|
47 |
|
|
48 |
private boolean getTraceParameter(HttpServletRequest request) { |
|
49 |
String parameter = request.getParameter("trace"); |
|
50 |
if (parameter == null) { |
|
51 |
return false; |
|
52 |
} |
|
53 |
return !"false".equals(parameter.toLowerCase()); |
|
54 |
} |
|
55 |
|
|
56 |
private Map<String, Object> getErrorAttributes(HttpServletRequest aRequest, boolean includeStackTrace) { |
|
57 |
RequestAttributes requestAttributes = new ServletRequestAttributes(aRequest); |
|
58 |
return errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace); |
|
59 |
} |
|
60 |
} |
Also available in: Unified diff
[Trunk | Admin Tools Library]:
1. GenericPortalController.java: Added Generic portal controller for handling all portals without type limitation.
2. SimpleErrorController.java: Added /error request mapping, to format all error that are not explicitly handled.
3. AdminToolsLibraryExceptionsHandler.java: Added methods
"notFoundException()" (ChangeSetPersister.NotFoundException),
"forbiddenException()" (ForbiddenException),
"duplicateKeyException()" (DuplicateKeyException).
4. ForbiddenException.java: [NEW] Added class ForbiddenException extends RuntimeException.
5. ExceptionResponse.java: Added field HttpStatus status.
6. SingleValueWrapperResponse.java: [Moved from Admin Tools Service to Admin Tools Library]
Generic class SingleValueWrapperResponse created, with field "value" of type defined when instance is created (used for returning single value from API methods).