From 47d7e0e533687b204e6c781f0dce57ca54e01348 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 7 Apr 2017 11:01:29 +0300 Subject: juick-www: refactoring, drop userref shit --- .../java/com/juick/www/controllers/NewMessage.java | 64 ++++++++++------------ 1 file changed, 28 insertions(+), 36 deletions(-) (limited to 'juick-www/src/main/java/com/juick/www/controllers/NewMessage.java') diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java index 2c92f9d8..f7279fb8 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java @@ -17,9 +17,12 @@ */ package com.juick.www.controllers; +import com.juick.Status; import com.juick.Tag; import com.juick.server.helpers.TagStats; import com.juick.server.util.HttpBadRequestException; +import com.juick.server.util.HttpForbiddenException; +import com.juick.server.util.HttpNotFoundException; import com.juick.server.util.HttpUtils; import com.juick.service.*; import com.juick.util.UserUtils; @@ -29,13 +32,15 @@ import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.math.NumberUtils; import org.imgscalr.Scalr; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.env.Environment; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.stanza.model.Message; @@ -313,47 +318,41 @@ public class NewMessage { } @PostMapping("/comment") - public void doPostComment(HttpServletRequest request, HttpServletResponse response, - @RequestParam(required = false) String img, - @RequestParam(required = false) MultipartFile attach) throws IOException { + public String doPostComment( + @RequestParam(required = false, defaultValue = "0") Integer mid, + @RequestParam(required = false, defaultValue = "0") Integer rid, + @RequestParam String body, + @RequestParam(required = false) String img, + @RequestParam(required = false) MultipartFile attach) throws IOException { com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.getUid() == 0) { - response.sendError(HttpServletResponse.SC_FORBIDDEN); - return; + throw new HttpForbiddenException(); } - int mid = NumberUtils.toInt(request.getParameter("mid"), 0); if (mid == 0) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; + throw new HttpBadRequestException(); } com.juick.Message msg = messagesService.getMessage(mid); if (msg == null) { - response.sendError(HttpServletResponse.SC_NOT_FOUND); - return; + throw new HttpNotFoundException(); } - int rid = NumberUtils.toInt(request.getParameter("rid"), 0); com.juick.Message reply = null; if (rid > 0) { reply = messagesService.getReply(mid, rid); if (reply == null) { - response.sendError(HttpServletResponse.SC_NOT_FOUND); - return; + throw new HttpNotFoundException(); } } - String body = request.getParameter("body"); - if (body == null || body.length() < 1 || body.length() > 4096) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; + if (body.length() < 1 || body.length() > 4096) { + throw new HttpBadRequestException(); } body = body.replace("\r", StringUtils.EMPTY); if ((msg.ReadOnly && msg.getUser().getUid() != visitor.getUid()) || userService.isInBLAny(msg.getUser().getUid(), visitor.getUid()) || (reply != null && userService.isInBLAny(reply.getUser().getUid(), visitor.getUid()))) { - response.sendError(HttpServletResponse.SC_FORBIDDEN); - return; + throw new HttpForbiddenException(); } String attachmentFName = HttpUtils.receiveMultiPartFile(attach, webApp.getTmpDir()); @@ -427,29 +426,22 @@ public class NewMessage { logger.warn("XMPP unavailable"); } - Utils.sendTemporaryRedirect(response, "/" + msg.getUser().getName() + "/" + mid + "#" + ridnew); + return "redirect:/" + msg.getUser().getName() + "/" + mid + "#" + ridnew; } @PostMapping("/like") - public void doPostRecomm(HttpServletRequest request, HttpServletResponse response) throws IOException { + @ResponseBody + public Status doPostRecomm(@RequestParam Integer mid) throws IOException { com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.getUid() == 0) { - response.sendError(HttpServletResponse.SC_FORBIDDEN); - return; - } - int mid = NumberUtils.toInt(request.getParameter("mid"), 0); - if (mid == 0) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; + throw new HttpForbiddenException(); } com.juick.Message msg = messagesService.getMessage(mid); if (msg == null) { - response.sendError(HttpServletResponse.SC_NOT_FOUND); - return; + throw new HttpNotFoundException(); } if (msg.getUser().getUid() == visitor.getUid()) { - response.sendError(HttpServletResponse.SC_FORBIDDEN); - return; + throw new HttpForbiddenException(); } boolean res = messagesService.recommendMessage(mid, visitor.getUid()); @@ -468,9 +460,9 @@ public class NewMessage { logger.warn("XMPP unavailable"); } - Utils.replyJSON(request, response, "{\"status\":\"ok\"}"); + return Status.OK; } else { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + throw new HttpBadRequestException(); } } } -- cgit v1.2.3