From 2f136229df32ab204c1b1226dfdd24c347c32561 Mon Sep 17 00:00:00 2001 From: Daniel Gerhardt <code@dgerhardt.net> Date: Wed, 26 Apr 2017 17:45:36 +0200 Subject: [PATCH] Add support messages and rethrowing to custom exceptions Add messages to exceptions and slightly refactor exception handling. --- .../controller/FeedbackController.java | 11 +++----- .../arsnova/controller/WelcomeController.java | 4 +-- .../exceptions/BadRequestException.java | 16 ++++++++++++ .../exceptions/ForbiddenException.java | 16 ++++++++++++ .../exceptions/NoContentException.java | 16 ++++++++++++ .../arsnova/exceptions/NotFoundException.java | 16 ++++++++++++ .../exceptions/NotImplementedException.java | 16 ++++++++++++ .../exceptions/PayloadTooLargeException.java | 16 ++++++++++++ .../PreconditionFailedException.java | 16 ++++++++++++ .../exceptions/UnauthorizedException.java | 16 ++++++++++++ .../thm/arsnova/services/SessionService.java | 25 +++++++++---------- 11 files changed, 145 insertions(+), 23 deletions(-) diff --git a/src/main/java/de/thm/arsnova/controller/FeedbackController.java b/src/main/java/de/thm/arsnova/controller/FeedbackController.java index 4e8b4f79..2f3e3b16 100644 --- a/src/main/java/de/thm/arsnova/controller/FeedbackController.java +++ b/src/main/java/de/thm/arsnova/controller/FeedbackController.java @@ -94,14 +94,9 @@ public class FeedbackController extends AbstractController { @RequestBody final int value ) { User user = userService.getCurrentUser(); - if (feedbackService.saveFeedback(sessionkey, value, user)) { - Feedback feedback = feedbackService.getFeedback(sessionkey); - if (feedback != null) { - return feedback; - } - throw new RuntimeException(); - } + feedbackService.saveFeedback(sessionkey, value, user); + Feedback feedback = feedbackService.getFeedback(sessionkey); - throw new NotFoundException(); + return feedback; } } diff --git a/src/main/java/de/thm/arsnova/controller/WelcomeController.java b/src/main/java/de/thm/arsnova/controller/WelcomeController.java index 9313f5ef..c036abda 100644 --- a/src/main/java/de/thm/arsnova/controller/WelcomeController.java +++ b/src/main/java/de/thm/arsnova/controller/WelcomeController.java @@ -86,13 +86,13 @@ public class WelcomeController extends AbstractController { ) { /* Block requests from the server itself to prevent DoS attacks caused by request loops */ if ("127.0.0.1".equals(request.getRemoteAddr())) { - throw new BadRequestException(); + throw new BadRequestException("Access to localhost not allowed."); } /* Block requests to servers in private networks */ try { final InetAddress addr = InetAddress.getByName(new URL(url).getHost()); if (addr.isSiteLocalAddress()) { - throw new BadRequestException(); + throw new BadRequestException("Access to site-local addresses not allowed."); } } catch (UnknownHostException | MalformedURLException e) { throw new BadRequestException(); diff --git a/src/main/java/de/thm/arsnova/exceptions/BadRequestException.java b/src/main/java/de/thm/arsnova/exceptions/BadRequestException.java index c9b72874..b7e80b83 100644 --- a/src/main/java/de/thm/arsnova/exceptions/BadRequestException.java +++ b/src/main/java/de/thm/arsnova/exceptions/BadRequestException.java @@ -5,4 +5,20 @@ package de.thm.arsnova.exceptions; */ public class BadRequestException extends RuntimeException { private static final long serialVersionUID = 1L; + + public BadRequestException() { + super(); + } + + public BadRequestException(String message) { + super(message); + } + + public BadRequestException(Throwable e) { + super(e); + } + + public BadRequestException(String message, Throwable e) { + super(message, e); + } } diff --git a/src/main/java/de/thm/arsnova/exceptions/ForbiddenException.java b/src/main/java/de/thm/arsnova/exceptions/ForbiddenException.java index 05ed5bf3..05b21dab 100644 --- a/src/main/java/de/thm/arsnova/exceptions/ForbiddenException.java +++ b/src/main/java/de/thm/arsnova/exceptions/ForbiddenException.java @@ -5,4 +5,20 @@ package de.thm.arsnova.exceptions; */ public class ForbiddenException extends RuntimeException { private static final long serialVersionUID = 1L; + + public ForbiddenException() { + super(); + } + + public ForbiddenException(String message) { + super(message); + } + + public ForbiddenException(Throwable e) { + super(e); + } + + public ForbiddenException(String message, Throwable e) { + super(message, e); + } } diff --git a/src/main/java/de/thm/arsnova/exceptions/NoContentException.java b/src/main/java/de/thm/arsnova/exceptions/NoContentException.java index f0de3c46..f3f7e816 100644 --- a/src/main/java/de/thm/arsnova/exceptions/NoContentException.java +++ b/src/main/java/de/thm/arsnova/exceptions/NoContentException.java @@ -5,4 +5,20 @@ package de.thm.arsnova.exceptions; */ public class NoContentException extends RuntimeException { private static final long serialVersionUID = 1L; + + public NoContentException() { + super(); + } + + public NoContentException(String message) { + super(message); + } + + public NoContentException(Throwable e) { + super(e); + } + + public NoContentException(String message, Throwable e) { + super(message, e); + } } diff --git a/src/main/java/de/thm/arsnova/exceptions/NotFoundException.java b/src/main/java/de/thm/arsnova/exceptions/NotFoundException.java index 0937ebde..a0a12b25 100644 --- a/src/main/java/de/thm/arsnova/exceptions/NotFoundException.java +++ b/src/main/java/de/thm/arsnova/exceptions/NotFoundException.java @@ -5,4 +5,20 @@ package de.thm.arsnova.exceptions; */ public class NotFoundException extends RuntimeException { private static final long serialVersionUID = 1L; + + public NotFoundException() { + super(); + } + + public NotFoundException(String message) { + super(message); + } + + public NotFoundException(Throwable e) { + super(e); + } + + public NotFoundException(String message, Throwable e) { + super(message, e); + } } diff --git a/src/main/java/de/thm/arsnova/exceptions/NotImplementedException.java b/src/main/java/de/thm/arsnova/exceptions/NotImplementedException.java index ed5e30f6..93f7ed79 100644 --- a/src/main/java/de/thm/arsnova/exceptions/NotImplementedException.java +++ b/src/main/java/de/thm/arsnova/exceptions/NotImplementedException.java @@ -5,4 +5,20 @@ package de.thm.arsnova.exceptions; */ public class NotImplementedException extends RuntimeException { private static final long serialVersionUID = 1L; + + public NotImplementedException() { + super(); + } + + public NotImplementedException(String message) { + super(message); + } + + public NotImplementedException(Throwable e) { + super(e); + } + + public NotImplementedException(String message, Throwable e) { + super(message, e); + } } diff --git a/src/main/java/de/thm/arsnova/exceptions/PayloadTooLargeException.java b/src/main/java/de/thm/arsnova/exceptions/PayloadTooLargeException.java index 6562a149..9233b58f 100644 --- a/src/main/java/de/thm/arsnova/exceptions/PayloadTooLargeException.java +++ b/src/main/java/de/thm/arsnova/exceptions/PayloadTooLargeException.java @@ -5,4 +5,20 @@ package de.thm.arsnova.exceptions; */ public class PayloadTooLargeException extends RuntimeException { private static final long serialVersionUID = 1L; + + public PayloadTooLargeException() { + super(); + } + + public PayloadTooLargeException(String message) { + super(message); + } + + public PayloadTooLargeException(Throwable e) { + super(e); + } + + public PayloadTooLargeException(String message, Throwable e) { + super(message, e); + } } diff --git a/src/main/java/de/thm/arsnova/exceptions/PreconditionFailedException.java b/src/main/java/de/thm/arsnova/exceptions/PreconditionFailedException.java index 423b3a4f..e14d30c7 100644 --- a/src/main/java/de/thm/arsnova/exceptions/PreconditionFailedException.java +++ b/src/main/java/de/thm/arsnova/exceptions/PreconditionFailedException.java @@ -5,4 +5,20 @@ package de.thm.arsnova.exceptions; */ public class PreconditionFailedException extends RuntimeException { private static final long serialVersionUID = 1L; + + public PreconditionFailedException() { + super(); + } + + public PreconditionFailedException(String message) { + super(message); + } + + public PreconditionFailedException(Throwable e) { + super(e); + } + + public PreconditionFailedException(String message, Throwable e) { + super(message, e); + } } diff --git a/src/main/java/de/thm/arsnova/exceptions/UnauthorizedException.java b/src/main/java/de/thm/arsnova/exceptions/UnauthorizedException.java index 7a4c3dc8..f6a91249 100644 --- a/src/main/java/de/thm/arsnova/exceptions/UnauthorizedException.java +++ b/src/main/java/de/thm/arsnova/exceptions/UnauthorizedException.java @@ -5,4 +5,20 @@ package de.thm.arsnova.exceptions; */ public class UnauthorizedException extends RuntimeException { private static final long serialVersionUID = 1L; + + public UnauthorizedException() { + super(); + } + + public UnauthorizedException(String message) { + super(message); + } + + public UnauthorizedException(Throwable e) { + super(e); + } + + public UnauthorizedException(String message, Throwable e) { + super(message, e); + } } diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java index 5da25855..77a22e5b 100644 --- a/src/main/java/de/thm/arsnova/services/SessionService.java +++ b/src/main/java/de/thm/arsnova/services/SessionService.java @@ -174,7 +174,7 @@ public class SessionService implements ISessionService, ApplicationEventPublishe if (connectorClient != null && session.isCourseSession()) { final String courseid = session.getCourseId(); if (!connectorClient.getMembership(user.getUsername(), courseid).isMember()) { - throw new ForbiddenException(); + throw new ForbiddenException("User is no course member."); } } @@ -205,15 +205,15 @@ public class SessionService implements ISessionService, ApplicationEventPublishe } if (!session.isActive()) { if (user.hasRole(UserSessionService.Role.STUDENT)) { - throw new ForbiddenException(); + throw new ForbiddenException("User is not session creator."); } else if (user.hasRole(UserSessionService.Role.SPEAKER) && !session.isCreator(user)) { - throw new ForbiddenException(); + throw new ForbiddenException("User is not session creator."); } } if (connectorClient != null && session.isCourseSession()) { final String courseid = session.getCourseId(); if (!connectorClient.getMembership(user.getUsername(), courseid).isMember()) { - throw new ForbiddenException(); + throw new ForbiddenException("User is no course member."); } } return session; @@ -336,7 +336,7 @@ public class SessionService implements ISessionService, ApplicationEventPublishe final Session session = databaseDao.getSessionFromKeyword(sessionkey); final User user = userService.getCurrentUser(); if (!session.isCreator(user)) { - throw new ForbiddenException(); + throw new ForbiddenException("User is not session creator."); } session.setActive(lock); this.publisher.publishEvent(new StatusSessionEvent(this, session)); @@ -374,7 +374,7 @@ public class SessionService implements ISessionService, ApplicationEventPublishe public Session changeSessionCreator(String sessionkey, String newCreator) { final Session existingSession = databaseDao.getSessionFromKeyword(sessionkey); if (existingSession == null) { - throw new RuntimeException("Could not load session " + sessionkey + "."); + throw new NullPointerException("Could not load session " + sessionkey + "."); } return databaseDao.changeSessionCreator(existingSession, newCreator); } @@ -424,7 +424,7 @@ public class SessionService implements ISessionService, ApplicationEventPublishe final User user = userService.getCurrentUser(); final SessionInfo info = databaseDao.importSession(user, importSession); if (info == null) { - throw new RuntimeException("Could not import session."); + throw new NullPointerException("Could not import session."); } return info; } @@ -460,7 +460,7 @@ public class SessionService implements ISessionService, ApplicationEventPublishe final Session session = databaseDao.getSessionFromKeyword(sessionkey); final User user = userService.getCurrentUser(); if (!session.isCreator(user)) { - throw new UnauthorizedException(); + throw new UnauthorizedException("User is not session creator."); } session.setFeatures(features); this.publisher.publishEvent(new FeatureChangeEvent(this, session)); @@ -472,7 +472,7 @@ public class SessionService implements ISessionService, ApplicationEventPublishe final Session session = databaseDao.getSessionFromKeyword(sessionkey); final User user = userService.getCurrentUser(); if (!session.isCreator(user)) { - throw new UnauthorizedException(); + throw new UnauthorizedException("User is not session creator."); } if (!lock) { feedbackService.cleanFeedbackVotesInSession(sessionkey, 0); @@ -488,7 +488,7 @@ public class SessionService implements ISessionService, ApplicationEventPublishe final Session session = databaseDao.getSessionFromKeyword(sessionkey); final User user = userService.getCurrentUser(); if (!session.isCreator(user)) { - throw new UnauthorizedException(); + throw new UnauthorizedException("User is not session creator."); } session.setFlipFlashcards(flip); this.publisher.publishEvent(new FlipFlashcardsEvent(this, session)); @@ -500,7 +500,7 @@ public class SessionService implements ISessionService, ApplicationEventPublishe if (session.getPpLogo().startsWith("http")) { final String base64ImageString = imageUtils.encodeImageToString(session.getPpLogo()); if (base64ImageString == null) { - throw new BadRequestException(); + throw new BadRequestException("Could not encode image."); } session.setPpLogo(base64ImageString); } @@ -508,8 +508,7 @@ public class SessionService implements ISessionService, ApplicationEventPublishe // base64 adds offset to filesize, formula taken from: http://en.wikipedia.org/wiki/Base64#MIME final int fileSize = (int) ((session.getPpLogo().length() - 814) / 1.37); if (fileSize > uploadFileSizeByte) { - logger.error("Could not save file. File is too large with {} Byte.", fileSize); - throw new PayloadTooLargeException(); + throw new PayloadTooLargeException("Could not save file. File is too large with " + fileSize + " Byte."); } } } -- GitLab