From bdbf65e0a50758bfcc88e1cc9c501eeed4e7e45d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 12 Jul 2016 17:39:33 +0300 Subject: jackson-databind WIP --- build.gradle | 1 + juick-core/build.gradle | 1 + juick-core/src/main/java/com/juick/Message.java | 29 +++-- juick-core/src/main/java/com/juick/Tag.java | 18 ++- juick-core/src/main/java/com/juick/User.java | 16 ++- .../java/com/juick/json/MessageSerializer.java | 6 +- .../java/com/juick/server/MessagesQueries.java | 142 +++++++++------------ .../src/main/java/com/juick/server/PMQueries.java | 2 +- .../src/main/java/com/juick/server/TagQueries.java | 14 +- .../com/juick/server/protocol/JuickProtocol.java | 4 +- .../com/juick/xmpp/extensions/JuickMessage.java | 9 +- juick-rss/src/main/java/com/juick/rss/Main.java | 2 +- .../src/main/java/com/juick/www/Discover.java | 10 +- .../src/main/java/com/juick/www/NewMessage.java | 4 +- .../src/main/java/com/juick/www/PageTemplates.java | 4 +- juick-www/src/main/java/com/juick/www/User.java | 19 +-- src/test/java/com/juick/tests/ApiTests.java | 4 +- src/test/java/com/juick/tests/JsonTests.java | 25 +++- src/test/java/com/juick/tests/MessageTests.java | 4 +- 19 files changed, 179 insertions(+), 135 deletions(-) diff --git a/build.gradle b/build.gradle index a3741657..ec5774b6 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,7 @@ repositories { } dependencies { testCompile project(':juick-core') + testCompile 'com.fasterxml.jackson.core:jackson-databind:2.8.0' testCompile 'org.springframework:spring-jdbc:4.3.1.RELEASE' testCompile 'ch.vorburger.mariaDB4j:mariaDB4j:2.1.3' testCompile 'org.slf4j:slf4j-jdk14:1.7.13' diff --git a/juick-core/build.gradle b/juick-core/build.gradle index bb10c52b..ac70cb9a 100644 --- a/juick-core/build.gradle +++ b/juick-core/build.gradle @@ -6,6 +6,7 @@ repositories { dependencies { compile project(':deps:com.juick.xmpp') + compile 'com.fasterxml.jackson.core:jackson-annotations:2.8.0' compile 'org.apache.httpcomponents:httpclient:4.5.1' compile 'org.apache.commons:commons-lang3:3.4' compile "org.springframework:spring-jdbc:4.3.1.RELEASE" diff --git a/juick-core/src/main/java/com/juick/Message.java b/juick-core/src/main/java/com/juick/Message.java index c128b4f2..2b66e667 100644 --- a/juick-core/src/main/java/com/juick/Message.java +++ b/juick-core/src/main/java/com/juick/Message.java @@ -17,7 +17,10 @@ */ package com.juick; +import com.fasterxml.jackson.annotation.JsonProperty; + import java.util.*; +import java.util.stream.Collectors; /** * @@ -28,11 +31,12 @@ public class Message implements Comparable { private int MID = 0; private int RID = 0; - + + @JsonProperty("replyto") public int ReplyTo = 0; private String Text = null; private User User = null; - public List Tags = new ArrayList<>(); + public List Tags = new ArrayList<>(); private Date date; public int TimeAgo = 0; public int Privacy = 1; @@ -49,8 +53,8 @@ public class Message implements Comparable { public int Likes = 0; private boolean liked = false; public List childs = new ArrayList<>(); - private Optional PM = Optional.empty(); - private Optional Recommendation = Optional.empty(); + private PM PM; + private Recommendation Recommendation; public Message() { } @@ -82,7 +86,8 @@ public class Message implements Comparable { public void parseTags(String strTags) { if (strTags != null) { - Tags.addAll(Arrays.asList(strTags.split(" "))); + List tags = Arrays.asList(strTags.split(" ")); + Tags.addAll(tags.stream().map(Tag::new).collect(Collectors.toList())); } } @@ -156,8 +161,8 @@ public class Message implements Comparable { public String getTagsString() { String ret = ""; if (!Tags.isEmpty()) { - for (String Tag : Tags) { - ret += " *" + Tag; + for (Tag Tag : Tags) { + ret += " *" + Tag.getName(); } if (FriendsOnly) { ret += " *friends"; @@ -178,6 +183,7 @@ public class Message implements Comparable { return ret; } + @JsonProperty("mid") public int getMID() { return MID; } @@ -186,6 +192,7 @@ public class Message implements Comparable { this.MID = MID; } + @JsonProperty("rid") public int getRID() { return RID; } @@ -219,19 +226,19 @@ public class Message implements Comparable { this.date = date; } - public Optional getPM() { + public com.juick.PM getPM() { return PM; } public void setPM(com.juick.PM PM) { - this.PM = Optional.ofNullable(PM); + this.PM = PM; } - public Optional getRecommendation() { + public Recommendation getRecommendation() { return Recommendation; } public void setRecommendation(Recommendation recommendation) { - this.Recommendation = Optional.ofNullable(recommendation); + this.Recommendation = recommendation; } public boolean isLiked() { diff --git a/juick-core/src/main/java/com/juick/Tag.java b/juick-core/src/main/java/com/juick/Tag.java index 119aad99..8b790377 100644 --- a/juick-core/src/main/java/com/juick/Tag.java +++ b/juick-core/src/main/java/com/juick/Tag.java @@ -17,6 +17,9 @@ */ package com.juick; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonValue; + import java.util.Objects; /** @@ -25,11 +28,15 @@ import java.util.Objects; */ public class Tag { - public String Name = null; + private String Name; public int TID = 0; public int SynonymID = 0; public int UsageCnt = 0; + public Tag(String name) { + this.Name = name; + } + @Override public boolean equals(Object o) { boolean equal = false; @@ -38,4 +45,13 @@ public class Tag { } return equal; } + @JsonIgnore + public String getName() { + return Name; + } + @Override + @JsonValue + public String toString() { + return Name; + } } diff --git a/juick-core/src/main/java/com/juick/User.java b/juick-core/src/main/java/com/juick/User.java index b9111d8f..a8109e50 100644 --- a/juick-core/src/main/java/com/juick/User.java +++ b/juick-core/src/main/java/com/juick/User.java @@ -17,6 +17,9 @@ */ package com.juick; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; + /** * * @author Ugnich Anton @@ -25,11 +28,13 @@ public class User { private int UID = 0; private String UName = null; + @JsonIgnore public Object Avatar = null; private String FullName = null; private String JID = null; - public int MessagesCount = 0; + private int MessagesCount = 0; private String AuthHash = null; + @JsonIgnore public boolean Banned = false; public User() { @@ -51,6 +56,7 @@ public class User { return (obj instanceof User && ((User) obj).getUID() == this.getUID()); } + @JsonProperty("uid") public int getUID() { return UID; } @@ -59,6 +65,7 @@ public class User { this.UID = UID; } + @JsonProperty("uname") public String getUName() { return UName; } @@ -67,6 +74,7 @@ public class User { this.UName = UName; } + @JsonProperty("fullname") public String getFullName() { return FullName; } @@ -75,6 +83,7 @@ public class User { FullName = fullName; } + @JsonProperty("jid") public String getJID() { return JID; } @@ -83,6 +92,7 @@ public class User { this.JID = JID; } + @JsonIgnore public String getAuthHash() { return AuthHash; } @@ -90,7 +100,11 @@ public class User { public void setAuthHash(String authHash) { AuthHash = authHash; } + @JsonProperty("unreadCount") public Integer getUnreadCount() { return MessagesCount; } + public void setUnreadCount(Integer count) { + MessagesCount = count; + } } 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 4238df60..d5adfa6e 100644 --- a/juick-core/src/main/java/com/juick/json/MessageSerializer.java +++ b/juick-core/src/main/java/com/juick/json/MessageSerializer.java @@ -18,6 +18,7 @@ package com.juick.json; import com.juick.Message; +import com.juick.Tag; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -27,6 +28,7 @@ import java.text.SimpleDateFormat; import java.util.TimeZone; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; /** * @@ -68,7 +70,7 @@ public class MessageSerializer extends JSONSerializer { if (json.has("tags")) { JSONArray tags = json.getJSONArray("tags"); for (int n = 0; n < tags.length(); n++) { - jmsg.Tags.add(tags.getString(n).replace(""", "\"")); + jmsg.Tags.add(new Tag(tags.getString(n).replace(""", "\""))); } } @@ -113,7 +115,7 @@ public class MessageSerializer extends JSONSerializer { json.put("user", userSerializer.serialize(msg.getUser())); } if (msg.Tags != null && msg.Tags.size() > 0) { - json.put("tags", new JSONArray(msg.Tags)); + json.put("tags", new JSONArray(msg.Tags.stream().map(Tag::getName).collect(Collectors.toList()))); } if (msg.Replies > 0) { json.put("replies", msg.Replies); 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 3d0d734d..3c848f54 100644 --- a/juick-core/src/main/java/com/juick/server/MessagesQueries.java +++ b/juick-core/src/main/java/com/juick/server/MessagesQueries.java @@ -18,24 +18,22 @@ package com.juick.server; import com.juick.Message; +import com.juick.Tag; import com.juick.User; import com.juick.server.helpers.PrivacyOpts; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.jdbc.core.ConnectionCallback; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import org.springframework.util.StringUtils; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.Statement; -import java.sql.Types; -import java.util.ArrayList; +import java.sql.*; +import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.logging.Logger; /** * @@ -43,7 +41,42 @@ import java.util.logging.Logger; */ public class MessagesQueries { - private static final Logger LOGGER = Logger.getLogger(MessagesQueries.class.getName()); + public static class MessageMapper implements RowMapper { + @Override + 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.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.Place = new com.juick.Place(); + msg.Place.lat = rs.getDouble(11); + msg.Place.lon = rs.getDouble(12); + } + msg.Likes = rs.getInt(13); + msg.Hidden = rs.getBoolean(14); + // parse tags string + String tagsStr = rs.getString(15); + if (tagsStr != null) { + Arrays.asList(tagsStr.split(" ")) + .stream().forEach(t -> msg.Tags.add(new Tag(t))); + } + msg.RepliesBy = rs.getString(16); + msg.setText(rs.getString(17)); + return msg; + } + } public static int createMessage(JdbcTemplate sql, int uid, String txt, String attachment, List tags) { KeyHolder holder = new GeneratedKeyHolder(); @@ -72,7 +105,7 @@ public class MessagesQueries { tagsNames += " "; tagsIDs += ","; } - tagsNames += tags.get(i).Name; + tagsNames += tags.get(i).getName(); tagsIDs += "(" + mid + "," + tags.get(i).TID + ")"; } if (tags.size() > 0) { @@ -201,7 +234,7 @@ public class MessagesQueries { public static com.juick.Message getMessage(JdbcTemplate sql, int mid) { try { - return sql.queryForObject("SELECT messages.user_id,users.nick," + return sql.queryForObject("SELECT messages.message_id,messages.user_id,users.nick," + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW())," + "messages.ts," + "messages.readonly,messages.privacy,messages.replies," @@ -211,35 +244,8 @@ public class MessagesQueries { + "INNER JOIN users ON messages.user_id=users.id " + "INNER JOIN messages_txt AS txt " + "ON messages.message_id=txt.message_id " - + "WHERE messages.message_id=?", new Object[]{mid}, - (rs, rowNum) -> { - Message msg = new Message(); - msg.setMID(mid); - msg.setUser(new User()); - msg.getUser().setUID(rs.getInt(1)); - msg.getUser().setUName(rs.getString(2)); - msg.TimeAgo = rs.getInt(3); - msg.setDate(rs.getTimestamp(4)); - msg.ReadOnly = rs.getBoolean(5); - msg.Privacy = rs.getInt(6); - msg.FriendsOnly = msg.Privacy < 0; - msg.Replies = rs.getInt(7); - msg.AttachmentType = rs.getString(8); - //if (rs.getInt(9) > 0) { - // msg.Place = PlacesQueries.getPlace(sql, rs.getInt(9)); - //} - if (rs.getDouble(10) != 0) { - msg.Place = new com.juick.Place(); - msg.Place.lat = rs.getDouble(10); - msg.Place.lon = rs.getDouble(11); - } - msg.Likes = rs.getInt(12); - msg.Hidden = rs.getBoolean(13); - msg.parseTags(rs.getString(14)); - msg.RepliesBy = rs.getString(15); - msg.setText(rs.getString(16)); - return msg; - }); + + "WHERE messages.message_id=?", + new MessageMapper(), mid); } catch (EmptyResultDataAccessException e) { return null; } @@ -252,7 +258,7 @@ public class MessagesQueries { + "replies.attach,replies.txt FROM replies INNER JOIN users " + "ON replies.user_id=users.id " + "WHERE replies.message_id=? AND replies.reply_id=?", - new Object[]{mid, rid}, (rs, num) -> { + (rs, num) -> { Message msg = new Message(); msg.setMID(mid); msg.setRID(rid); @@ -264,7 +270,7 @@ public class MessagesQueries { msg.AttachmentType = rs.getString(5); msg.setText(rs.getString(6)); return msg; - }); + }, mid, rid); } catch (EmptyResultDataAccessException e) { return null; } @@ -290,10 +296,9 @@ public class MessagesQueries { return sql.query("SELECT tags.tag_id,synonym_id,name,stat_messages FROM tags " + "INNER JOIN messages_tags ON (messages_tags.message_id=? AND messages_tags.tag_id=tags.tag_id)", new Object[]{mid}, (rs, num) -> { - com.juick.Tag t = new com.juick.Tag(); + com.juick.Tag t = new com.juick.Tag(rs.getString(3)); t.TID = rs.getInt(1); t.SynonymID = rs.getInt(2); - t.Name = rs.getString(3); t.UsageCnt = rs.getInt(4); return t; }); @@ -479,10 +484,10 @@ public class MessagesQueries { if (before > 0) { mids = sqlSearch.queryForList("SELECT id AS message_id FROM messages WHERE MATCH(?) AND id 0) { return sql.queryForList("SELECT message_id FROM messages WHERE message_id " + @@ -495,10 +500,10 @@ public class MessagesQueries { public static List getUserBlog(JdbcTemplate sql, int UID, int privacy, int before) { if (before > 0) { return sql.queryForList("SELECT message_id FROM messages WHERE user_id=? AND message_id=" + - privacy + " ORDER BY message_id DESC LIMIT 20", new Object[]{UID, before}, Integer.class); + privacy + " ORDER BY message_id DESC LIMIT 20", Integer.class, UID, before); } else { return sql.queryForList("SELECT message_id FROM messages WHERE user_id=? AND privacy>=" + privacy + - " ORDER BY message_id DESC LIMIT 20", new Object[]{UID}, Integer.class); + " ORDER BY message_id DESC LIMIT 20", Integer.class, UID); } } @@ -508,12 +513,12 @@ public class MessagesQueries { "USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? " + "AND messages.message_id=" + privacy + " ORDER BY messages.message_id DESC LIMIT 20", - new Object[] {UID, TID, before}, Integer.class); + Integer.class, UID, TID, before); } else { return sql.queryForList("SELECT messages.message_id FROM messages_tags INNER JOIN messages " + "USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? " + "AND messages.privacy>=" + privacy + " ORDER BY messages.message_id DESC LIMIT 20", - new Object[]{UID, TID}, Integer.class); + Integer.class, UID, TID); } } @@ -522,11 +527,11 @@ public class MessagesQueries { return sql.queryForList("SELECT message_id FROM favorites " + "WHERE user_id=? AND message_id getMessages(JdbcTemplate sql, List mids) { if (!mids.isEmpty()) { return sql.query("SELECT messages.message_id,messages.user_id,users.nick," - + "messages_txt.tags,messages.readonly,messages.privacy,messages_txt.txt," + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW())," - + "messages.ts,messages.replies," - + "messages_txt.repliesby,messages.attach,messages.lat," - + "messages.lon,messages.likes " + + "messages.ts," + + "messages.readonly,messages.privacy,messages.replies," + + "messages.attach,messages.place_id,messages.lat," + + "messages.lon,messages.likes,messages.hidden," + + "messages_txt.tags,messages_txt.repliesby, messages_txt.txt " + "FROM (messages INNER JOIN messages_txt " + "ON messages.message_id=messages_txt.message_id) " + "INNER JOIN users ON messages.user_id=users.id " + "WHERE messages.message_id " + "IN (" + StringUtils.arrayToCommaDelimitedString(mids.toArray()) + ") " - + "ORDER BY messages.message_id DESC", (rs, rowNum) -> { - com.juick.Message msg = new com.juick.Message(); - msg.setUser(new User()); - - msg.setMID(rs.getInt(1)); - msg.getUser().setUID(rs.getInt(2)); - msg.getUser().setUName(rs.getString(3)); - if (rs.getString(4) != null) { - msg.parseTags(rs.getString(4)); - } - msg.ReadOnly = rs.getInt(5) == 1; - msg.Privacy = rs.getInt(6); - msg.FriendsOnly = msg.Privacy < 0; - msg.setText(rs.getString(7)); - msg.TimeAgo = rs.getInt(8); - msg.setDate(rs.getTimestamp(9)); - msg.Replies = rs.getInt(10); - msg.RepliesBy = rs.getString(11); - msg.AttachmentType = rs.getString(12); - if (rs.getDouble(13) != 0) { - msg.Place = new com.juick.Place(); - msg.Place.lat = rs.getDouble(13); - msg.Place.lon = rs.getDouble(14); - } - msg.Likes = rs.getInt(15); - return msg; - }); + + "ORDER BY messages.message_id DESC", new MessageMapper()); } return Collections.emptyList(); } diff --git a/juick-core/src/main/java/com/juick/server/PMQueries.java b/juick-core/src/main/java/com/juick/server/PMQueries.java index 875cbbd6..d7855002 100644 --- a/juick-core/src/main/java/com/juick/server/PMQueries.java +++ b/juick-core/src/main/java/com/juick/server/PMQueries.java @@ -75,7 +75,7 @@ public class PMQueries { com.juick.User u = new com.juick.User(); u.setUID(rs.getInt(1)); u.setUName(rs.getString(2)); - u.MessagesCount = rs.getInt(3); + u.setUnreadCount(rs.getInt(3)); return u; }, uid); } diff --git a/juick-core/src/main/java/com/juick/server/TagQueries.java b/juick-core/src/main/java/com/juick/server/TagQueries.java index 766f9bcd..76c12425 100644 --- a/juick-core/src/main/java/com/juick/server/TagQueries.java +++ b/juick-core/src/main/java/com/juick/server/TagQueries.java @@ -42,10 +42,9 @@ public class TagQueries { try { return sql.queryForObject("SELECT synonym_id,name FROM tags WHERE tag_id=?", (rs, num) -> { - Tag ret = new Tag(); + Tag ret = new Tag(rs.getString(2)); ret.TID = tid; ret.SynonymID = rs.getInt(1); - ret.Name = rs.getString(2); return ret; }, tid); } catch (EmptyResultDataAccessException e) { @@ -58,19 +57,17 @@ public class TagQueries { try { ret = sql.queryForObject("SELECT tag_id,synonym_id,name FROM tags WHERE name=?", (rs, rowNum) -> { - Tag ret1 = new Tag(); + Tag ret1 = new Tag(rs.getString(3)); ret1.TID = rs.getInt(1); ret1.SynonymID = rs.getInt(2); - ret1.Name = rs.getString(3); return ret1; }, tag); } catch (EmptyResultDataAccessException e) { // tag not found } if (ret == null && autoCreate) { - ret = new com.juick.Tag(); + ret = new com.juick.Tag(tag); ret.TID = createTag(sql, tag); - ret.Name = tag; } return ret; @@ -117,8 +114,7 @@ public class TagQueries { "AND messages.message_id=messages_tags.message_id)) " + "INNER JOIN tags ON messages_tags.tag_id=tags.tag_id GROUP BY tags.tag_id ORDER BY tags.name ASC", (rs, rowNum) -> { - Tag t = new Tag(); - t.Name = rs.getString(1); + Tag t = new Tag(rs.getString(1)); t.UsageCnt = rs.getInt(2); return t; }, uid); @@ -160,7 +156,7 @@ public class TagQueries { return Collections.emptyList(); } public static String toString(List tags) { - return tags.stream().map(t -> " *" + t.Name) + return tags.stream().map(t -> " *" + t.getName()) .collect(Collectors.joining()); } } diff --git a/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java b/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java index 033417a8..bb7615dd 100644 --- a/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java +++ b/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java @@ -356,9 +356,9 @@ public class JuickProtocol { } } else { if (SubscriptionsQueries.unSubscribeTag(sql, user, tag)) { - return new ProtocolReply("Unsubscribed from " + tag.Name, Optional.empty()); + return new ProtocolReply("Unsubscribed from " + tag.getName(), Optional.empty()); } - return new ProtocolReply("You was not subscribed to " + tag.Name, Optional.empty()); + return new ProtocolReply("You was not subscribed to " + tag.getName(), Optional.empty()); } return new ProtocolReply("Error", Optional.empty()); } diff --git a/juick-core/src/main/java/com/juick/xmpp/extensions/JuickMessage.java b/juick-core/src/main/java/com/juick/xmpp/extensions/JuickMessage.java index 510f7cc1..d259bc43 100644 --- a/juick-core/src/main/java/com/juick/xmpp/extensions/JuickMessage.java +++ b/juick-core/src/main/java/com/juick/xmpp/extensions/JuickMessage.java @@ -17,6 +17,7 @@ */ package com.juick.xmpp.extensions; +import com.juick.Tag; import com.juick.xmpp.utils.XmlUtils; import com.juick.xmpp.*; import java.io.IOException; @@ -95,7 +96,7 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { } else if (tag.equals(JuickUser.TagName) && xmlns != null && xmlns.equals(JuickUser.XMLNS)) { jmsg.setUser(new JuickUser().parse(parser)); } else if (tag.equals("tag")) { - jmsg.Tags.add(XmlUtils.getTagText(parser)); + jmsg.Tags.add(new Tag(XmlUtils.getTagText(parser))); } else { XmlUtils.skip(parser); } @@ -137,10 +138,8 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { if (getText() != null) { ret += "" + XmlUtils.escape(getText()) + ""; } - if (!Tags.isEmpty()) { - for (int i = 0; i < Tags.size(); i++) { - ret += "" + XmlUtils.escape(Tags.get(i)) + ""; - } + for (com.juick.Tag Tag : Tags) { + ret += "" + XmlUtils.escape(Tag.getName()) + ""; } ret += ""; diff --git a/juick-rss/src/main/java/com/juick/rss/Main.java b/juick-rss/src/main/java/com/juick/rss/Main.java index 32e7ebae..fd4deca3 100644 --- a/juick-rss/src/main/java/com/juick/rss/Main.java +++ b/juick-rss/src/main/java/com/juick/rss/Main.java @@ -142,7 +142,7 @@ public class Main extends HttpServlet { out.println("http://juick.com/" + msg.getUser().getUName() + "/" + msg.getMID() + ""); if (!msg.Tags.isEmpty()) { for (int n = 0; n < msg.Tags.size(); n++) { - out.println("" + escapeHtml(msg.Tags.get(n)) + ""); + out.println("" + escapeHtml(msg.Tags.get(n).getName()) + ""); } } if (msg.AttachmentType != null) { diff --git a/juick-www/src/main/java/com/juick/www/Discover.java b/juick-www/src/main/java/com/juick/www/Discover.java index 240d0fdd..e94d6e99 100644 --- a/juick-www/src/main/java/com/juick/www/Discover.java +++ b/juick-www/src/main/java/com/juick/www/Discover.java @@ -47,14 +47,14 @@ public class Discover { return; } else if (paramTag.SynonymID > 0 && paramTag.TID != paramTag.SynonymID) { com.juick.Tag synTag = TagQueries.getTag(sql, paramTag.SynonymID); - String url = "/tag/" + URLEncoder.encode(synTag.Name, "UTF-8"); + String url = "/tag/" + URLEncoder.encode(synTag.getName(), "UTF-8"); if (request.getQueryString() != null) { url += "?" + request.getQueryString(); } Utils.sendPermanentRedirect(response, url); return; - } else if (!paramTag.Name.equals(paramTagStr)) { - String url = "/tag/" + URLEncoder.encode(paramTag.Name, "UTF-8"); + } else if (!paramTag.getName().equals(paramTagStr)) { + String url = "/tag/" + URLEncoder.encode(paramTag.getName(), "UTF-8"); if (request.getQueryString() != null) { url += "?" + request.getQueryString(); } @@ -73,7 +73,7 @@ public class Discover { int visitor_uid = visitor.getUID(); - String title = "*" + Utils.encodeHTML(paramTag.Name); + String title = "*" + Utils.encodeHTML(paramTag.getName()); List mids = MessagesQueries.getTag(sql, paramTag.TID, visitor_uid, paramBefore, (visitor_uid == 0) ? 40 : 20); response.setContentType("text/html; charset=UTF-8"); @@ -104,7 +104,7 @@ public class Discover { } if (mids.size() >= 20) { - String nextpage = "/tag/" + URLEncoder.encode(paramTag.Name, "UTF-8") + "?before=" + mids.get(mids.size() - 1); + String nextpage = "/tag/" + URLEncoder.encode(paramTag.getName(), "UTF-8") + "?before=" + mids.get(mids.size() - 1); out.println("

Читать дальше →

"); } diff --git a/juick-www/src/main/java/com/juick/www/NewMessage.java b/juick-www/src/main/java/com/juick/www/NewMessage.java index 397badbb..bb169b60 100644 --- a/juick-www/src/main/java/com/juick/www/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/NewMessage.java @@ -113,7 +113,9 @@ public class NewMessage { } String taglink = ""; try { - taglink = "" + Utils.encodeHTML(tags.get(i).Name) + ""; + taglink = "" + Utils.encodeHTML(tags.get(i).getName()) + ""; } catch (UnsupportedEncodingException e) { } int usagecnt = tags.get(i).UsageCnt; diff --git a/juick-www/src/main/java/com/juick/www/PageTemplates.java b/juick-www/src/main/java/com/juick/www/PageTemplates.java index 0bb17ff0..c570ae7f 100644 --- a/juick-www/src/main/java/com/juick/www/PageTemplates.java +++ b/juick-www/src/main/java/com/juick/www/PageTemplates.java @@ -191,9 +191,9 @@ public class PageTemplates { public static String formatTags(List tags) { String ret = ""; for (Tag tag : tags) { - String tagName = tag.Name.replaceAll("<", "<").replaceAll(">", ">"); + String tagName = tag.getName().replaceAll("<", "<").replaceAll(">", ">"); try { - ret += " *"; + String head = ""; if (paramTag != null && TagQueries.getTagNoIndex(sql, paramTag.TID)) { head += ""; } else if (paramBefore > 0 || paramShow != null) { @@ -128,7 +129,9 @@ public class User { out.println("
"); if (paramTag != null) { - out.println("

← Все записи с тегом " + Utils.encodeHTML(paramTag.Name) + "

"); + out.println("

← Все записи с тегом " + + Utils.encodeHTML(paramTag.getName()) + "

"); } PageTemplates.printMessages(out, sql, user, mids, visitor, visitor.getUID() == 0 ? 4 : 5, 0); @@ -139,7 +142,7 @@ public class User { nextpage += "&show=" + paramShow; } if (paramTag != null) { - nextpage += "&tag=" + URLEncoder.encode(paramTag.Name, "UTF-8"); + nextpage += "&tag=" + URLEncoder.encode(paramTag.getName(), "UTF-8"); } if (paramSearch != null) { nextpage += "&search=" + URLEncoder.encode(paramSearch, "UTF-8"); @@ -323,9 +326,9 @@ public class User { String ret = ""; int count = cnt > 0 ? Math.min(tags.size(), cnt) : tags.size(); for (int i = 0; i < count; i++) { - String tag = Utils.encodeHTML(tags.get(i).Name); + String tag = Utils.encodeHTML(tags.get(i).getName()); try { - tag = "" + tag + ""; } catch (UnsupportedEncodingException e) { } diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java index 5c6cddf3..533b2428 100644 --- a/src/test/java/com/juick/tests/ApiTests.java +++ b/src/test/java/com/juick/tests/ApiTests.java @@ -81,7 +81,7 @@ public class ApiTests { assertEquals(1, rid); Message msg3 = MessagesQueries.getMessage(jdbc, mid2); assertEquals(1, msg3.Replies); - assertEquals("weather", msg3.Tags.get(0)); + assertEquals("weather", msg3.Tags.get(0).getName()); assertEquals(ugnich.getUID(), UserQueries.checkPassword(jdbc, ugnich.getUName(), "x")); assertEquals(-1, UserQueries.checkPassword(jdbc, ugnich.getUName(), "xy")); SubscriptionsQueries.subscribeMessage(jdbc, msg.getMID(), ugnich.getUID()); @@ -105,7 +105,7 @@ public class ApiTests { assertEquals("text should match", "yoyo", MessagesQueries.getMessage(jdbc, mid).getText()); assertEquals("tag should match", "yo", - MessagesQueries.getMessageTags(jdbc, mid).get(0).Name); + MessagesQueries.getMessageTags(jdbc, mid).get(0).getName()); assertNotEquals("should not be error", "Error", protocol.getReply(user, "#" + mid).getDescription()); assertEquals("should be PONG", "PONG", protocol.getReply(user, " ping \n ").getDescription()); int readerUid = UserQueries.createUser(jdbc, "dummyReader", "dummySecret"); diff --git a/src/test/java/com/juick/tests/JsonTests.java b/src/test/java/com/juick/tests/JsonTests.java index babcc6c7..ab0d6283 100644 --- a/src/test/java/com/juick/tests/JsonTests.java +++ b/src/test/java/com/juick/tests/JsonTests.java @@ -1,9 +1,15 @@ package com.juick.tests; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.Message; +import com.juick.User; import com.juick.json.MessageSerializer; +import com.juick.json.UserSerializer; +import org.json.JSONObject; import org.junit.Test; +import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.TimeZone; @@ -24,6 +30,23 @@ public class JsonTests { } catch (ParseException e) { e.printStackTrace(); } - + } + @Test + public void serializersTest() throws IOException { + User user = new User(); + user.setUName("ugnich"); + user.setUID(1); + user.setFullName("Anton Ugnich"); + UserSerializer userSerializer = new UserSerializer(); + JSONObject handmadeJson = userSerializer.serialize(user); + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); + User jacksonUser = mapper.readValue(handmadeJson.toString(), User.class); + assertEquals("jackson should deserialize ugnich json", user, jacksonUser); + String jacksonString = mapper.writeValueAsString(user); + JSONObject jacksonJson = new JSONObject(jacksonString); + assertEquals("jackson should serialize as ugnich", handmadeJson.length(), jacksonJson.length()); } } \ No newline at end of file diff --git a/src/test/java/com/juick/tests/MessageTests.java b/src/test/java/com/juick/tests/MessageTests.java index 5dceccc0..3e391a67 100644 --- a/src/test/java/com/juick/tests/MessageTests.java +++ b/src/test/java/com/juick/tests/MessageTests.java @@ -26,8 +26,8 @@ public class MessageTests { IOException, SAXException { Message msg = new Message(); msg.parseTags("test test" + (char) 0xA0 + "2 test3"); - assertEquals("First tag must be", "test", msg.Tags.get(0)); - assertEquals("Third tag must be", "test3", msg.Tags.get(2)); + assertEquals("First tag must be", "test", msg.Tags.get(0).getName()); + assertEquals("Third tag must be", "test3", msg.Tags.get(2).getName()); assertEquals("Count of tags must be", 3, msg.Tags.size()); Date currentDate = new Date(); msg.setDate(currentDate); -- cgit v1.2.3