From d8af0bae55e6d32440cd9faa65674a0ae239968e Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 14 Jul 2016 12:10:17 +0300 Subject: JSON: read X-Scheme header to set urls scheme, SQL queries refactoring --- .../src/main/java/com/juick/api/Messages.java | 45 +++++++++++----------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'juick-api') diff --git a/juick-api/src/main/java/com/juick/api/Messages.java b/juick-api/src/main/java/com/juick/api/Messages.java index ad8d82e0..e461ac07 100644 --- a/juick-api/src/main/java/com/juick/api/Messages.java +++ b/juick-api/src/main/java/com/juick/api/Messages.java @@ -2,6 +2,7 @@ package com.juick.api; import com.juick.Tag; import com.juick.User; +import com.juick.json.JSONSerializer; import com.juick.json.MessageSerializer; import com.juick.server.MessagesQueries; import com.juick.server.TagQueries; @@ -17,9 +18,12 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; /** * @@ -37,27 +41,23 @@ public class Messages { this.sql = sql; } - void feedMessages(HttpServletRequest request, HttpServletResponse response, List mids) throws IOException { - if (mids != null && !mids.isEmpty()) { - List msgs = MessagesQueries.getMessages(sql, mids); - if (msgs != null && !msgs.isEmpty()) { - String json = messageSerializer.serializeList(msgs); - Main.replyJSON(request, response, json); - } else { - response.sendError(404); + void feedMessages(HttpServletRequest request, HttpServletResponse response, List msgs) throws IOException { + String requestScheme = request.getHeader("X-Scheme"); + if (requestScheme != null) { + if (Objects.equals(requestScheme, "https")) { + messageSerializer.setUriScheme(JSONSerializer.URIScheme.Secure); } - } else { - response.sendError(404); } + String json = messageSerializer.serializeList(msgs); + Main.replyJSON(request, response, json); } public void doGetHome(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); - - feedMessages(request, response, MessagesQueries.getMyFeed(sql, vuid, before_mid)); - + List mids = MessagesQueries.getMyFeed(sql, vuid, before_mid); + feedMessages(request, response, MessagesQueries.getMessages(sql, mids)); } public void doGet(HttpServletRequest request, @@ -68,40 +68,42 @@ public class Messages { String popular = request.getParameter("popular"); String media = request.getParameter("media"); String tag = request.getParameter("tag"); + List mids = new ArrayList<>(); if (!StringUtils.isEmpty(uname)) { User user = UserQueries.getUserByName(sql, uname); if (user != null) { if (!StringUtils.isEmpty(media)) { - feedMessages(request, response, MessagesQueries.getUserPhotos(sql, user.getUID(), 0, before_mid)); + mids = MessagesQueries.getUserPhotos(sql, user.getUID(), 0, before_mid); } else if (!StringUtils.isEmpty(tag)) { Tag tagObject = TagQueries.getTag(sql, tag, false); if (tagObject != null) { - feedMessages(request, response, MessagesQueries.getUserTag(sql, user.getUID(), tagObject.TID, 0, before_mid)); + mids = MessagesQueries.getUserTag(sql, user.getUID(), tagObject.TID, 0, before_mid); } else { response.sendError(404); } } else { - feedMessages(request, response, MessagesQueries.getUserBlog(sql, user.getUID(), 0, before_mid)); + mids = MessagesQueries.getUserBlog(sql, user.getUID(), 0, before_mid); } } else { response.sendError(404); } } else { if (!StringUtils.isEmpty(popular)) { - feedMessages(request, response, MessagesQueries.getPopular(sql, vuid, before_mid)); + mids = MessagesQueries.getPopular(sql, vuid, before_mid); } else if (!StringUtils.isEmpty(media)) { - feedMessages(request, response, MessagesQueries.getPhotos(sql, vuid, before_mid)); + mids = MessagesQueries.getPhotos(sql, vuid, before_mid); } else if (!StringUtils.isEmpty(tag)) { Tag tagObject = TagQueries.getTag(sql, tag, false); if (tagObject != null) { - feedMessages(request, response, MessagesQueries.getTag(sql, tagObject.TID, vuid, before_mid, 20)); + mids = MessagesQueries.getTag(sql, tagObject.TID, vuid, before_mid, 20); } else { response.sendError(404); } } else { - feedMessages(request, response, MessagesQueries.getAll(sql, vuid, before_mid)); + mids = MessagesQueries.getAll(sql, vuid, before_mid); } } + feedMessages(request, response, MessagesQueries.getMessages(sql, mids)); } public void doThreadGet(HttpServletRequest request, HttpServletResponse response, int vuid) throws IOException { @@ -113,8 +115,7 @@ public class Messages { } else { List replies = MessagesQueries.getReplies(sql, mid); replies.add(0, msg); - String json = messageSerializer.serializeList(replies); - Main.replyJSON(request, response, json); + feedMessages(request, response, replies); } } else { response.sendError(404); -- cgit v1.2.3