Skip to content
Snippets Groups Projects
Commit 2f136229 authored by Daniel Gerhardt's avatar Daniel Gerhardt
Browse files

Add support messages and rethrowing to custom exceptions

Add messages to exceptions and slightly refactor exception handling.
parent 531f6906
Branches
Tags
No related merge requests found
Showing
with 145 additions and 23 deletions
......@@ -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;
}
}
......@@ -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();
......
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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.");
}
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment