diff options
author | Vitaly Takmazov | 2016-07-14 12:10:17 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-07-14 12:10:17 +0300 |
commit | d8af0bae55e6d32440cd9faa65674a0ae239968e (patch) | |
tree | db95a4541f1524f498281a1dc54b0873403e1616 /juick-api/src/main/java/com | |
parent | f04b1c21e42525377d01b5d102006ae4efe5d4b1 (diff) |
JSON: read X-Scheme header to set urls scheme, SQL queries refactoring
Diffstat (limited to 'juick-api/src/main/java/com')
-rw-r--r-- | juick-api/src/main/java/com/juick/api/Messages.java | 45 |
1 files changed, 23 insertions, 22 deletions
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<Integer> mids) throws IOException { - if (mids != null && !mids.isEmpty()) { - List<com.juick.Message> 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<com.juick.Message> 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<Integer> 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<Integer> 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<com.juick.Message> 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); |