diff --git a/src/main/java/de/thm/arsnova/ImageUtils.java b/src/main/java/de/thm/arsnova/ImageUtils.java index 0af116547c229c335ee88ca292528365c927cf7c..29c55c70cf8155f759b082a846a7b69a607ebe45 100644 --- a/src/main/java/de/thm/arsnova/ImageUtils.java +++ b/src/main/java/de/thm/arsnova/ImageUtils.java @@ -34,41 +34,44 @@ import org.slf4j.LoggerFactory; /** * Util class for image operations. - * + * * @author Daniel Vogel (daniel.vogel@mni.thm.de) * */ public class ImageUtils { + + private ImageUtils() {} + public static final Logger LOGGER = LoggerFactory.getLogger(ImageUtils.class); /** * Converts an image to an Base64 String. - * + * * @param imageUrl The image url as a {@link String} * @return The Base64 {@link String} of the image on success, otherwise <code>null</code>. */ public static String encodeImageToString(String imageUrl) { - + String[] urlParts = imageUrl.split("\\."); StringBuilder result = new StringBuilder(); - + // get format - // - // The format is read dynamically. We have to take control - // in the frontend that no unsupported formats are transmitted! - if ( urlParts.length > 0 ) { + // + // The format is read dynamically. We have to take control + // in the frontend that no unsupported formats are transmitted! + if ( urlParts.length > 0 ) { String extension = urlParts[urlParts.length-1]; - result.append("data:image/" + extension + ";base64,"); - result.append(Base64.encodeBase64String(convertFileToByteArray(imageUrl))); - + result.append("data:image/" + extension + ";base64,"); + result.append(Base64.encodeBase64String(convertFileToByteArray(imageUrl))); + return result.toString(); - } - + } + return null; } - + /** * Gets the bytestream of an image url. * s @@ -80,23 +83,23 @@ public class ImageUtils { try { URL url = new URL(imageUrl); BufferedImage image = ImageIO.read(url); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - ImageIO.write(image, extension, baos); - - baos.flush(); - baos.close(); - return baos.toByteArray(); - + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + ImageIO.write(image, extension, baos); + + baos.flush(); + baos.close(); + return baos.toByteArray(); + } catch (MalformedURLException e) { LOGGER.error(e.getLocalizedMessage()); } catch (IOException e) { LOGGER.error(e.getLocalizedMessage()); } - return null; + return null; } - + /** * Gets the bytestream of an image url. * s @@ -107,28 +110,28 @@ public class ImageUtils { try { URL url = new URL(imageUrl); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - InputStream is = url.openStream(); - byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time. - int n; - - while ( (n = is.read(byteChunk)) > 0 ) { - baos.write(byteChunk, 0, n); - } - - baos.flush(); - baos.close(); - - return baos.toByteArray(); - + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + InputStream is = url.openStream(); + byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time. + int n; + + while ( (n = is.read(byteChunk)) > 0 ) { + baos.write(byteChunk, 0, n); + } + + baos.flush(); + baos.close(); + + return baos.toByteArray(); + } catch (MalformedURLException e) { LOGGER.error(e.getLocalizedMessage()); } catch (IOException e) { LOGGER.error(e.getLocalizedMessage()); } - return null; + return null; } } diff --git a/src/main/java/de/thm/arsnova/controller/AbstractController.java b/src/main/java/de/thm/arsnova/controller/AbstractController.java index a0dc60afcea4f4093fb73b2b43328e646d8c0b34..983a998ce5b76151836096d4d81047a7f61a35cd 100644 --- a/src/main/java/de/thm/arsnova/controller/AbstractController.java +++ b/src/main/java/de/thm/arsnova/controller/AbstractController.java @@ -16,6 +16,9 @@ import de.thm.arsnova.exceptions.PreconditionFailedException; import de.thm.arsnova.exceptions.UnauthorizedException; public class AbstractController { + protected static final String X_DEPRECATED_API = "X-Deprecated-API"; + protected static final String X_FORWARDED = "X-Forwarded"; + @ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler(NotFoundException.class) public void handleNotFoundException(final Exception e, final HttpServletRequest request) { diff --git a/src/main/java/de/thm/arsnova/controller/FeedbackController.java b/src/main/java/de/thm/arsnova/controller/FeedbackController.java index e5c47d71b7f5b615255e208735e97edc6fab2b30..e1e8b29db2b0549aa32b26fa975d76fd72b69f8f 100644 --- a/src/main/java/de/thm/arsnova/controller/FeedbackController.java +++ b/src/main/java/de/thm/arsnova/controller/FeedbackController.java @@ -50,7 +50,7 @@ public class FeedbackController extends AbstractController { @RequestMapping(value = "/session/{sessionkey}/feedback", method = RequestMethod.GET) public final Feedback getFeedback(@PathVariable final String sessionkey, final HttpServletResponse response) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); return feedbackService.getFeedback(sessionkey); } @@ -59,7 +59,7 @@ public class FeedbackController extends AbstractController { public final Integer getMyFeedback(@PathVariable final String sessionkey, final HttpServletResponse response) { Integer value = feedbackService.getMyFeedback(sessionkey, userService.getCurrentUser()); - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); if (value != null && value >= Feedback.MIN_FEEDBACK_TYPE && value <= Feedback.MAX_FEEDBACK_TYPE) { return value; @@ -69,7 +69,7 @@ public class FeedbackController extends AbstractController { @RequestMapping(value = "/session/{sessionkey}/feedbackcount", method = RequestMethod.GET) public final int getFeedbackCount(@PathVariable final String sessionkey, final HttpServletResponse response) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); return feedbackService.getFeedbackCount(sessionkey); } @@ -79,14 +79,14 @@ public class FeedbackController extends AbstractController { @PathVariable final String sessionkey, final HttpServletResponse response ) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); return feedbackService.getAverageFeedbackRounded(sessionkey); } @RequestMapping(value = "/session/{sessionkey}/averagefeedback", method = RequestMethod.GET) public final double getAverageFeedback(@PathVariable final String sessionkey, final HttpServletResponse response) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); return feedbackService.getAverageFeedback(sessionkey); } @@ -98,7 +98,7 @@ public class FeedbackController extends AbstractController { @RequestBody final int value, final HttpServletResponse response ) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); User user = userService.getCurrentUser(); if (feedbackService.saveFeedback(sessionkey, value, user)) { diff --git a/src/main/java/de/thm/arsnova/controller/LecturerQuestionController.java b/src/main/java/de/thm/arsnova/controller/LecturerQuestionController.java index ab0dac16a00018c6beae16d8e15b7a566a06f96c..ce5f97cac1095bb068d325dd6a1cd6333bdceda5 100644 --- a/src/main/java/de/thm/arsnova/controller/LecturerQuestionController.java +++ b/src/main/java/de/thm/arsnova/controller/LecturerQuestionController.java @@ -80,7 +80,10 @@ public class LecturerQuestionController extends AbstractController { @RequestBody final Question question, final HttpServletResponse response ) { - return this.questionService.update(question); + try { + return this.questionService.update(question); + } catch (Exception e) {} + throw new BadRequestException(); } @RequestMapping(value = "/{questionId}/publish", method = RequestMethod.POST) @@ -186,7 +189,7 @@ public class LecturerQuestionController extends AbstractController { @RequestParam(value = "flashcardsonly", defaultValue = "false") final boolean flashcardsOnly, @RequestParam(value = "preparationquestionsonly", defaultValue = "false") final boolean preparationQuestionsOnly, final HttpServletResponse response) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); if (lectureQuestionsOnly) { return questionService.getLectureQuestionCount(sessionkey); @@ -225,7 +228,7 @@ public class LecturerQuestionController extends AbstractController { if (answers == null || answers.isEmpty()) { throw new NoContentException(); } - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); return answers; } @@ -256,7 +259,7 @@ public class LecturerQuestionController extends AbstractController { return null; } - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); return answer; } @@ -363,7 +366,7 @@ public class LecturerQuestionController extends AbstractController { @PathVariable final String questionId, final HttpServletResponse response ) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); return questionService.getAnswerCount(questionId); } @@ -381,7 +384,7 @@ public class LecturerQuestionController extends AbstractController { @RequestParam final String sessionkey, final HttpServletResponse response ) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); return questionService.getMyAnswers(sessionkey); } @@ -393,7 +396,7 @@ public class LecturerQuestionController extends AbstractController { @RequestParam(value = "preparationquestionsonly", defaultValue = "false") final boolean preparationQuestionsOnly, final HttpServletResponse response ) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); if (lectureQuestionsOnly) { return questionService.countLectureQuestionAnswers(sessionkey); diff --git a/src/main/java/de/thm/arsnova/controller/LegacyController.java b/src/main/java/de/thm/arsnova/controller/LegacyController.java index 83c049daddd8b5df8e8e246a291148a0c066fcab..acd93535000cda5d6dd889ba2532fb1f2d98bb27 100644 --- a/src/main/java/de/thm/arsnova/controller/LegacyController.java +++ b/src/main/java/de/thm/arsnova/controller/LegacyController.java @@ -44,8 +44,8 @@ public class LegacyController extends AbstractController { @RequestMapping(value = "/session/mysessions", method = RequestMethod.GET) public final String redirectSessionMy( final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return "forward:/session/?ownedonly=true"; } @@ -53,8 +53,8 @@ public class LegacyController extends AbstractController { @RequestMapping(value = "/session/visitedsessions", method = RequestMethod.GET) public final String redirectSessionVisited( final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return "forward:/session/?visitedonly=true"; } @@ -63,8 +63,8 @@ public class LegacyController extends AbstractController { public final String redirectQuestionByLecturer( @PathVariable final String sessionKey, final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return String.format("forward:/lecturerquestion/?sessionkey=%s", sessionKey); } @@ -72,8 +72,8 @@ public class LegacyController extends AbstractController { public final String redirectQuestionByLecturerList( @PathVariable final String sessionKey, final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return String.format("forward:/lecturerquestion/?sessionkey=%s", sessionKey); } @@ -81,8 +81,8 @@ public class LegacyController extends AbstractController { public final String redirectQuestionByLecturerCount( @PathVariable final String sessionKey, final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return String.format("forward:/lecturerquestion/count?sessionkey=%s", sessionKey); } @@ -90,8 +90,8 @@ public class LegacyController extends AbstractController { public final String redirectQuestionByLecturerAnswerCount( @PathVariable final String sessionKey, final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return String.format("forward:/lecturerquestion/answercount?sessionkey=%s", sessionKey); } @@ -99,8 +99,8 @@ public class LegacyController extends AbstractController { public final String redirectQuestionByLecturerUnnsweredCount( @PathVariable final String sessionKey, final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return String.format("forward:/lecturerquestion/answercount?sessionkey=%s", sessionKey); } @@ -108,8 +108,8 @@ public class LegacyController extends AbstractController { public final String redirectQuestionByLecturerMyAnswers( @PathVariable final String sessionKey, final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return String.format("forward:/lecturerquestion/myanswers?sessionkey=%s", sessionKey); } @@ -117,8 +117,8 @@ public class LegacyController extends AbstractController { public final String redirectQuestionByAudience( @PathVariable final String sessionKey, final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return String.format("forward:/audiencequestion/?sessionkey=%s", sessionKey); } @@ -127,7 +127,7 @@ public class LegacyController extends AbstractController { public final void deleteAllInterposedQuestions( @PathVariable final String sessionKey, final HttpServletResponse response - ) { + ) { questionService.deleteAllInterposedQuestions(sessionKey); } @@ -135,8 +135,8 @@ public class LegacyController extends AbstractController { public final String redirectQuestionByAudienceCount( @PathVariable final String sessionKey, final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return String.format("forward:/audiencequestion/count?sessionkey=%s", sessionKey); } @@ -144,8 +144,8 @@ public class LegacyController extends AbstractController { public final String redirectQuestionByAudienceReadCount( @PathVariable final String sessionKey, final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return String.format("forward:/audiencequestion/readcount?sessionkey=%s", sessionKey); } @@ -156,21 +156,21 @@ public class LegacyController extends AbstractController { @PathVariable final String sessionKey, @PathVariable final String arg1, final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return String.format("forward:/lecturerquestion/%s/?sessionkey=%s", arg1, sessionKey); } @RequestMapping( value = { "/session/{sessionKey}/question/{arg1}/{arg2}", "/session/{sessionKey}/questions/{arg1}/{arg2}" } - ) + ) public final String redirectQuestionByLecturerWithTwoArguments( @PathVariable final String sessionKey, @PathVariable final String arg1, @PathVariable final String arg2, final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return String.format("forward:/lecturerquestion/%s/%s/?sessionkey=%s", arg1, arg2, sessionKey); } @@ -179,8 +179,8 @@ public class LegacyController extends AbstractController { @PathVariable final String sessionKey, @PathVariable final String arg1, final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return String.format("forward:/audiencequestion/%s/?sessionkey=%s", arg1, sessionKey); } @@ -190,8 +190,8 @@ public class LegacyController extends AbstractController { @PathVariable final String arg1, @PathVariable final String arg2, final HttpServletResponse response - ) { - response.addHeader("X-Deprecated-API", "1"); + ) { + response.addHeader(X_DEPRECATED_API, "1"); return String.format("forward:/audiencequestion/%s/%s/?sessionkey=%s", arg1, arg2, sessionKey); } diff --git a/src/main/java/de/thm/arsnova/controller/SessionController.java b/src/main/java/de/thm/arsnova/controller/SessionController.java index 4654a06da266c55cb0a2bf52726e83b3d7bfe0aa..47da7bbbe202009cfcb4a96eb5df0045adad8f31 100644 --- a/src/main/java/de/thm/arsnova/controller/SessionController.java +++ b/src/main/java/de/thm/arsnova/controller/SessionController.java @@ -74,7 +74,7 @@ public class SessionController extends AbstractController { @PathVariable final String sessionkey, final HttpServletResponse response ) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); User user = userService.getCurrentUser(); LoggedIn loggedIn = sessionService.registerAsOnlineUser(user, sessionkey); @@ -90,7 +90,7 @@ public class SessionController extends AbstractController { @PathVariable final String sessionkey, final HttpServletResponse response ) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); return userService.getUsersInSessionCount(sessionkey); } @@ -198,7 +198,7 @@ public class SessionController extends AbstractController { @PathVariable final String sessionKey, final HttpServletResponse response ) { - response.addHeader("X-Forwarded", "1"); + response.addHeader(X_FORWARDED, "1"); return String.format("forward:/lecturerquestion/?sessionkey=%s", sessionKey); } @@ -209,7 +209,7 @@ public class SessionController extends AbstractController { @PathVariable final String arg1, final HttpServletResponse response ) { - response.addHeader("X-Forwarded", "1"); + response.addHeader(X_FORWARDED, "1"); return String.format("forward:/lecturerquestion/%s/?sessionkey=%s", arg1, sessionKey); } @@ -221,7 +221,7 @@ public class SessionController extends AbstractController { @PathVariable final String arg2, final HttpServletResponse response ) { - response.addHeader("X-Forwarded", "1"); + response.addHeader(X_FORWARDED, "1"); return String.format("forward:/lecturerquestion/%s/%s/?sessionkey=%s", arg1, arg2, sessionKey); } @@ -234,7 +234,7 @@ public class SessionController extends AbstractController { @PathVariable final String arg3, final HttpServletResponse response ) { - response.addHeader("X-Forwarded", "1"); + response.addHeader(X_FORWARDED, "1"); return String.format("forward:/lecturerquestion/%s/%s/%s/?sessionkey=%s", arg1, arg2, arg3, sessionKey); } @@ -244,7 +244,7 @@ public class SessionController extends AbstractController { @PathVariable final String sessionKey, final HttpServletResponse response ) { - response.addHeader("X-Forwarded", "1"); + response.addHeader(X_FORWARDED, "1"); return String.format("forward:/audiencequestion/?sessionkey=%s", sessionKey); } @@ -255,7 +255,7 @@ public class SessionController extends AbstractController { @PathVariable final String arg1, final HttpServletResponse response ) { - response.addHeader("X-Forwarded", "1"); + response.addHeader(X_FORWARDED, "1"); return String.format("forward:/audiencequestion/%s/?sessionkey=%s", arg1, sessionKey); } @@ -267,7 +267,7 @@ public class SessionController extends AbstractController { @PathVariable final String arg2, final HttpServletResponse response ) { - response.addHeader("X-Forwarded", "1"); + response.addHeader(X_FORWARDED, "1"); return String.format("forward:/audiencequestion/%s/%s/?sessionkey=%s", arg1, arg2, sessionKey); } @@ -280,7 +280,7 @@ public class SessionController extends AbstractController { @PathVariable final String arg3, final HttpServletResponse response ) { - response.addHeader("X-Forwarded", "1"); + response.addHeader(X_FORWARDED, "1"); return String.format("forward:/audiencequestion/%s/%s/%s/?sessionkey=%s", arg1, arg2, arg3, sessionKey); } diff --git a/src/main/java/de/thm/arsnova/controller/StatisticsController.java b/src/main/java/de/thm/arsnova/controller/StatisticsController.java index f0d2941770068dcb34ec7abe8d9a02295223f05d..df5db1bb46e64def8efe9b961c2f0c0e3c774023 100644 --- a/src/main/java/de/thm/arsnova/controller/StatisticsController.java +++ b/src/main/java/de/thm/arsnova/controller/StatisticsController.java @@ -13,7 +13,7 @@ import de.thm.arsnova.entities.Statistics; import de.thm.arsnova.services.IStatisticsService; @RestController -public class StatisticsController { +public class StatisticsController extends AbstractController { @Autowired private IStatisticsService statisticsService; @@ -36,21 +36,21 @@ public class StatisticsController { @RequestMapping(method = RequestMethod.GET, value = "/statistics/activeusercount", produces = "text/plain") public final String countActiveUsers(HttpServletResponse response) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); return Integer.toString(statisticsService.countActiveUsers()); } @RequestMapping(method = RequestMethod.GET, value = "/statistics/loggedinusercount", produces = "text/plain") public final String countLoggedInUsers(HttpServletResponse response) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); return Integer.toString(statisticsService.countLoggedInUsers()); } @RequestMapping(method = RequestMethod.GET, value = "/statistics/sessioncount", produces = "text/plain") public final String countSessions(HttpServletResponse response) { - response.addHeader("X-Deprecated-API", "1"); + response.addHeader(X_DEPRECATED_API, "1"); return Integer.toString(statisticsService.getStatistics().getOpenSessions() + statisticsService.getStatistics().getClosedSessions()); diff --git a/src/main/java/de/thm/arsnova/events/ARSnovaEvent.java b/src/main/java/de/thm/arsnova/events/ARSnovaEvent.java index 9db6f6eb3791165b943991c2162b5a0954ea329d..5701edc6e5ce3088fb1073c601fc3707ca0c87b6 100644 --- a/src/main/java/de/thm/arsnova/events/ARSnovaEvent.java +++ b/src/main/java/de/thm/arsnova/events/ARSnovaEvent.java @@ -10,9 +10,9 @@ public class ARSnovaEvent extends ApplicationEvent { private String sessionKey; private User user; - private String eventName; - private Object data; - private Destination destination; + private final String eventName; + private final Object data; + private final Destination destination; public enum Destination { USER, @@ -50,7 +50,6 @@ public class ARSnovaEvent extends ApplicationEvent { public User getRecipient() { return user; } - public Destination getDestinationType() { return destination;