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 ++++++------- .../main/java/com/juick/json/JSONSerializer.java | 19 ++++++ .../java/com/juick/json/MessageSerializer.java | 7 +- .../java/com/juick/server/MessagesQueries.java | 76 ++++++++++------------ 4 files changed, 81 insertions(+), 66 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 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); diff --git a/juick-core/src/main/java/com/juick/json/JSONSerializer.java b/juick-core/src/main/java/com/juick/json/JSONSerializer.java index 142cacf0..3dc9e04e 100644 --- a/juick-core/src/main/java/com/juick/json/JSONSerializer.java +++ b/juick-core/src/main/java/com/juick/json/JSONSerializer.java @@ -17,6 +17,25 @@ import org.json.JSONObject; */ public abstract class JSONSerializer { + public enum URIScheme { + Plain, + Secure + } + + private URIScheme uriScheme; + + public URIScheme getUriScheme() { + return uriScheme; + } + + public void setUriScheme(URIScheme uriScheme) { + this.uriScheme = uriScheme; + } + + public JSONSerializer() { + this.uriScheme = URIScheme.Plain; + } + /** * * @param json diff --git a/juick-core/src/main/java/com/juick/json/MessageSerializer.java b/juick-core/src/main/java/com/juick/json/MessageSerializer.java index d5adfa6e..9da828c9 100644 --- a/juick-core/src/main/java/com/juick/json/MessageSerializer.java +++ b/juick-core/src/main/java/com/juick/json/MessageSerializer.java @@ -126,9 +126,10 @@ public class MessageSerializer extends JSONSerializer { if (msg.AttachmentType != null) { String fname = msg.getMID() + (msg.getRID() > 0 ? "-" + msg.getRID() : "") + "." + msg.AttachmentType; JSONObject photo = new JSONObject(); - photo.put("thumbnail", "http://i.juick.com/ps/" + fname); - photo.put("small", "http://i.juick.com/photos-512/" + fname); - photo.put("medium", "http://i.juick.com/photos-1024/" + fname); + String protocol = getUriScheme() == URIScheme.Plain ? "http:" : "https:"; + photo.put("thumbnail", protocol + "//i.juick.com/ps/" + fname); + photo.put("small", protocol + "//i.juick.com/photos-512/" + fname); + photo.put("medium", protocol + "//i.juick.com/photos-1024/" + fname); json.put("photo", photo); } } catch (JSONException e) { diff --git a/juick-core/src/main/java/com/juick/server/MessagesQueries.java b/juick-core/src/main/java/com/juick/server/MessagesQueries.java index 3c848f54..b39de0ed 100644 --- a/juick-core/src/main/java/com/juick/server/MessagesQueries.java +++ b/juick-core/src/main/java/com/juick/server/MessagesQueries.java @@ -46,34 +46,34 @@ public class MessagesQueries { public Message mapRow(ResultSet rs, int rowNum) throws SQLException { Message msg = new Message(); msg.setMID(rs.getInt(1)); - msg.setUser(new User()); - msg.getUser().setUID(rs.getInt(2)); - msg.getUser().setUName(rs.getString(3)); - msg.TimeAgo = rs.getInt(4); - msg.setDate(rs.getTimestamp(5)); - msg.ReadOnly = rs.getBoolean(6); - msg.Privacy = rs.getInt(7); + msg.setRID(rs.getInt(2)); + msg.ReplyTo = rs.getInt(3); + User user = new User(); + user.setUID(rs.getInt(4)); + user.setUName(rs.getString(5)); + user.Banned = rs.getBoolean(6); + msg.setUser(user); + msg.TimeAgo = rs.getInt(7); + msg.setDate(rs.getTimestamp(8)); + msg.ReadOnly = rs.getBoolean(9); + msg.Privacy = rs.getInt(10); msg.FriendsOnly = msg.Privacy < 0; - msg.Replies = rs.getInt(8); - msg.AttachmentType = rs.getString(9); - //if (rs.getInt(10) > 0) { - // msg.Place = PlacesQueries.getPlace(sql, rs.getInt(9)); - //} - if (rs.getDouble(10) != 0) { + msg.Replies = rs.getInt(11); + msg.AttachmentType = rs.getString(12); + if (rs.getDouble(13) != 0) { msg.Place = new com.juick.Place(); - msg.Place.lat = rs.getDouble(11); - msg.Place.lon = rs.getDouble(12); + msg.Place.lat = rs.getDouble(14); + msg.Place.lon = rs.getDouble(15); } - msg.Likes = rs.getInt(13); - msg.Hidden = rs.getBoolean(14); + msg.Likes = rs.getInt(16); + msg.Hidden = rs.getBoolean(17); // parse tags string - String tagsStr = rs.getString(15); + String tagsStr = rs.getString(18); if (tagsStr != null) { - Arrays.asList(tagsStr.split(" ")) - .stream().forEach(t -> msg.Tags.add(new Tag(t))); + Arrays.stream(tagsStr.split(" ")).forEach(t -> msg.Tags.add(new Tag(t))); } - msg.RepliesBy = rs.getString(16); - msg.setText(rs.getString(17)); + msg.RepliesBy = rs.getString(19); + msg.setText(rs.getString(20)); return msg; } } @@ -234,7 +234,8 @@ public class MessagesQueries { public static com.juick.Message getMessage(JdbcTemplate sql, int mid) { try { - return sql.queryForObject("SELECT messages.message_id,messages.user_id,users.nick," + return sql.queryForObject("SELECT messages.message_id, 0 as rid, 0 as replyto, " + + "messages.user_id,users.nick, 0 as banned, " + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW())," + "messages.ts," + "messages.readonly,messages.privacy,messages.replies," @@ -570,7 +571,8 @@ public class MessagesQueries { public static List getMessages(JdbcTemplate sql, List mids) { if (!mids.isEmpty()) { - return sql.query("SELECT messages.message_id,messages.user_id,users.nick," + return sql.query("SELECT messages.message_id, 0 as rid, 0 as replyto, " + + "messages.user_id,users.nick, 0 as banned, " + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW())," + "messages.ts," + "messages.readonly,messages.privacy,messages.replies," @@ -588,24 +590,16 @@ public class MessagesQueries { } public static List getReplies(JdbcTemplate sql, int mid) { - return sql.query("SELECT replies.reply_id,replies.replyto,replies.user_id,users.nick,users.banned,replies.txt," + - "TIMESTAMPDIFF(MINUTE,replies.ts,NOW()),replies.ts,replies.attach FROM replies INNER JOIN users " + + return sql.query("SELECT 0 as mid, replies.reply_id,replies.replyto, " + + "replies.user_id,users.nick,users.banned, " + + "TIMESTAMPDIFF(MINUTE,replies.ts,NOW()),replies.ts," + + "0 as readonly, 0 as privacy, 0 as replies," + + "replies.attach, 0 as place_id, 0 as lat," + + "0 as lon, 0 as likes, 0 as hidden," + + "NULL as tags,NULL as repliesby, replies.txt " + + "FROM replies INNER JOIN users " + "ON replies.user_id=users.id WHERE replies.message_id=? ORDER BY replies.reply_id ASC", - new Object[]{mid}, (rs, num) -> { - com.juick.Message msg = new com.juick.Message(); - msg.setMID(mid); - msg.setRID(rs.getInt(1)); - msg.ReplyTo = rs.getInt(2); - msg.setUser(new User()); - msg.getUser().setUID(rs.getInt(3)); - msg.getUser().setUName(rs.getString(4)); - msg.getUser().Banned = rs.getBoolean(5); - msg.setText(rs.getString(6)); - msg.TimeAgo = rs.getInt(7); - msg.setDate(rs.getTimestamp(8)); - msg.AttachmentType = rs.getString(9); - return msg; - }); + new MessageMapper(), mid); } public static boolean setMessagePopular(JdbcTemplate sql, int mid, int popular) { -- cgit v1.2.3