aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-11-30 16:49:11 +0300
committerGravatar Vitaly Takmazov2016-11-30 16:49:11 +0300
commit1a2830588131173d8fbfeb1fe03c466b4cf30e46 (patch)
tree53889640b356f22e6620acffed0bda1e56babcf6
parentf5fd9c7bea380eb4c0c2d58f2167ad3f07c45c33 (diff)
juick-core: fix attachment serialization
-rw-r--r--juick-core/src/main/java/com/juick/Message.java33
-rw-r--r--juick-core/src/main/java/com/juick/Photo.java34
-rw-r--r--juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java4
-rw-r--r--juick-rss/src/main/java/com/juick/rss/Main.java6
-rw-r--r--juick-server/src/main/java/com/juick/server/MessagesQueries.java4
-rw-r--r--juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java4
-rw-r--r--juick-www/src/main/java/com/juick/www/PageTemplates.java6
-rw-r--r--juick-www/src/main/java/com/juick/www/UserThread.java12
-rw-r--r--juick-xmpp/src/main/java/com/juick/xmpp/extensions/JuickMessage.java6
-rw-r--r--src/test/java/com/juick/json/MessageSerializer.java13
-rw-r--r--src/test/java/com/juick/tests/SerializationTests.java15
11 files changed, 98 insertions, 39 deletions
diff --git a/juick-core/src/main/java/com/juick/Message.java b/juick-core/src/main/java/com/juick/Message.java
index b5fc2b89..d3f28e22 100644
--- a/juick-core/src/main/java/com/juick/Message.java
+++ b/juick-core/src/main/java/com/juick/Message.java
@@ -57,9 +57,9 @@ public class Message implements Comparable {
@XmlTransient
public int Replies = 0;
public String RepliesBy = null;
+ private String attachmentType;
@XmlTransient
- public String AttachmentType = null;
- public String Photo = null;
+ private Photo photo;
public String Video = null;
public Place Place = null;
@XmlTransient
@@ -87,8 +87,8 @@ public class Message implements Comparable {
ReadOnly = msg.ReadOnly;
Hidden = msg.Hidden;
Replies = msg.Replies;
- AttachmentType = msg.AttachmentType;
- Photo = msg.Photo;
+ attachmentType = msg.attachmentType;
+ photo = msg.photo;
Video = msg.Video;
Place = msg.Place;
Likes = msg.Likes;
@@ -157,15 +157,16 @@ public class Message implements Comparable {
}
}
+ @JsonIgnore
public String getAttachmentURL() {
- if (AttachmentType != null) {
+ if (attachmentType != null) {
String url = "http://i.juick.com/";
- url += AttachmentType.equals("mp4") ? "video" : "photos-1024";
+ url += attachmentType.equals("mp4") ? "video" : "photos-1024";
url += "/" + getMid();
if (getRid() > 0) {
url += "-" + getRid();
}
- url += "." + AttachmentType;
+ url += "." + attachmentType;
return url;
} else {
return null;
@@ -309,4 +310,22 @@ public class Message implements Comparable {
public void setPrivacy(int privacy) {
this.privacy = privacy;
}
+
+ public Photo getPhoto() {
+ return photo;
+ }
+
+ public void setPhoto(Photo photo) {
+ this.photo = photo;
+ }
+
+ @XmlAttribute(name = "attach")
+ @JsonProperty("attach")
+ public String getAttachmentType() {
+ return attachmentType;
+ }
+
+ public void setAttachmentType(String attachmentType) {
+ this.attachmentType = attachmentType;
+ }
}
diff --git a/juick-core/src/main/java/com/juick/Photo.java b/juick-core/src/main/java/com/juick/Photo.java
new file mode 100644
index 00000000..010d81a4
--- /dev/null
+++ b/juick-core/src/main/java/com/juick/Photo.java
@@ -0,0 +1,34 @@
+package com.juick;
+
+/**
+ * Created by vitalyster on 30.11.2016.
+ */
+public class Photo {
+ private String small;
+ private String medium;
+ private String thumbnail;
+
+ public String getSmall() {
+ return small;
+ }
+
+ public void setSmall(String small) {
+ this.small = small;
+ }
+
+ public String getMedium() {
+ return medium;
+ }
+
+ public void setMedium(String medium) {
+ this.medium = medium;
+ }
+
+ public String getThumbnail() {
+ return thumbnail;
+ }
+
+ public void setThumbnail(String thumbnail) {
+ this.thumbnail = thumbnail;
+ }
+}
diff --git a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java
index be2c6838..c6e9022a 100644
--- a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java
+++ b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java
@@ -11,8 +11,8 @@ public class PlainTextFormatter {
String subtitle = isReply ? jmsg.getReplyQuote() : jmsg.getTagsString();
sb.append(title).append(jmsg.getUser().getName()).append(":\n")
.append(subtitle).append("\n").append(jmsg.getText()).append("\n");
- if (jmsg.Photo != null) {
- sb.append(jmsg.Photo);
+ if (jmsg.getPhoto() != null) {
+ sb.append(jmsg.getAttachmentURL());
}
return sb.toString();
}
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 04d87729..adb20a0d 100644
--- a/juick-rss/src/main/java/com/juick/rss/Main.java
+++ b/juick-rss/src/main/java/com/juick/rss/Main.java
@@ -146,11 +146,11 @@ public class Main extends HttpServlet {
out.println("<category>" + StringEscapeUtils.escapeHtml4(msg.getTags().get(n).getName()) + "</category>");
}
}
- if (msg.AttachmentType != null) {
- if (msg.AttachmentType.equals("jpg")) {
+ if (msg.getAttachmentType() != null) {
+ if (msg.getAttachmentType().equals("jpg")) {
out.println("<media:content url='http://i.juick.com/photos-1024/" + msg.getMid() + ".jpg' type='image/jpeg'/>");
out.println("<media:thumbnail url='http://i.juick.com/ps/" + msg.getMid() + ".jpg'/>");
- } else if (msg.AttachmentType.equals("png")) {
+ } else if (msg.getAttachmentType().equals("png")) {
out.println("<media:content url='http://i.juick.com/photos-1024/" + msg.getMid() + ".png' type='image/png'/>");
out.println("<media:thumbnail url='http://i.juick.com/ps/" + msg.getMid() + ".png'/>");
}
diff --git a/juick-server/src/main/java/com/juick/server/MessagesQueries.java b/juick-server/src/main/java/com/juick/server/MessagesQueries.java
index 9d4284bc..fd1e5b51 100644
--- a/juick-server/src/main/java/com/juick/server/MessagesQueries.java
+++ b/juick-server/src/main/java/com/juick/server/MessagesQueries.java
@@ -61,7 +61,7 @@ public class MessagesQueries {
msg.setPrivacy(rs.getInt(10));
msg.FriendsOnly = msg.getPrivacy() < 0;
msg.Replies = rs.getInt(11);
- msg.AttachmentType = rs.getString(12);
+ msg.setAttachmentType(rs.getString(12));
if (rs.getDouble(13) != 0) {
msg.Place = new com.juick.Place();
msg.Place.lat = rs.getDouble(14);
@@ -279,7 +279,7 @@ public class MessagesQueries {
msg.getUser().setName(rs.getString(2));
msg.setReplyto(rs.getInt(3));
msg.setDate(rs.getTimestamp(4));
- msg.AttachmentType = rs.getString(5);
+ msg.setAttachmentType(rs.getString(5));
msg.setText(rs.getString(6));
String quote = rs.getString(7);
if (!StringUtils.isEmpty(quote)) {
diff --git a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
index 422c9616..948e9137 100644
--- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -62,7 +62,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
msg.setPrivacy(rs.getInt(10));
msg.FriendsOnly = msg.getPrivacy() < 0;
msg.Replies = rs.getInt(11);
- msg.AttachmentType = rs.getString(12);
+ msg.setAttachmentType(rs.getString(12));
if (rs.getDouble(13) != 0) {
msg.Place = new com.juick.Place();
msg.Place.lat = rs.getDouble(14);
@@ -311,7 +311,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
msg.getUser().setName(rs.getString(2));
msg.setReplyto(rs.getInt(3));
msg.setDate(rs.getTimestamp(4));
- msg.AttachmentType = rs.getString(5);
+ msg.setAttachmentType(rs.getString(5));
msg.setText(rs.getString(6));
String quote = rs.getString(7);
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 74b8b543..180e852f 100644
--- a/juick-www/src/main/java/com/juick/www/PageTemplates.java
+++ b/juick-www/src/main/java/com/juick/www/PageTemplates.java
@@ -329,12 +329,12 @@ public class PageTemplates {
out.println(" <div class=\"msg-tags\">" + tagsStr + "</div>");
out.println(" </header>");
- if (msg.AttachmentType != null) {
- String fname = msg.getMid() + "." + msg.AttachmentType;
+ if (msg.getAttachmentType() != null) {
+ String fname = msg.getMid() + "." + msg.getAttachmentType();
out.println(" <p class=\"ir\"><a href=\"//i.juick.com/photos-512/" + fname + "\" data-fname=\"" + fname + "\"><img src=\"//i.juick.com/photos-512/" + fname + "\" alt=\"\"/></a></p>");
}
out.println(" <p>" + txt + "</p>");
- if (msg.AttachmentType != null) {
+ if (msg.getAttachmentType() != null) {
out.println(" <div class=\"irbr\"></div>");
}
out.print(" <nav class=\"l\">");
diff --git a/juick-www/src/main/java/com/juick/www/UserThread.java b/juick-www/src/main/java/com/juick/www/UserThread.java
index 6355e87e..d94f2156 100644
--- a/juick-www/src/main/java/com/juick/www/UserThread.java
+++ b/juick-www/src/main/java/com/juick/www/UserThread.java
@@ -125,8 +125,8 @@ public class UserThread {
out.println(" <div class=\"msg-header\">@<a href=\"/" + msg.getUser().getName() + "/\">" + msg.getUser().getName() + "</a>:" + tagsStr + "</div>");
out.println(" <div class=\"msg-txt\">" + txt + "</div>");
- if (msg.AttachmentType != null) {
- out.println(" <div class=\"msg-media\"><a href=\"//i.juick.com/p/" + msg.getMid() + "." + msg.AttachmentType + "\"><img src=\"//i.juick.com/photos-512/" + msg.getMid() + "." + msg.AttachmentType + "\" alt=\"\"/></a></div>");
+ if (msg.getAttachmentType() != null) {
+ out.println(" <div class=\"msg-media\"><a href=\"//i.juick.com/p/" + msg.getMid() + "." + msg.getAttachmentType() + "\"><img src=\"//i.juick.com/photos-512/" + msg.getMid() + "." + msg.getAttachmentType() + "\" alt=\"\"/></a></div>");
}
boolean visitorInBL = false;
@@ -292,8 +292,8 @@ public class UserThread {
out.println(" <div class=\"msg-ts\"><a href=\"/" + msg.getMid() + "#" + msg.getRid() + "\" title=\"" + PageTemplates.sdfSQL.format(msg.getDate()) + " GMT\">" + PageTemplates.formatDate(msg.TimeAgo, msg.getDate()) + "</a></div>");
out.println(" </div>");
out.println(" <div class=\"msg-txt\">" + MessageUtils.formatMessage(msg.getText()) + "</div>");
- if (msg.AttachmentType != null) {
- out.println(" <div class=\"msg-media\"><a href=\"//i.juick.com/p/" + msg.getMid() + "-" + msg.getRid() + "." + msg.AttachmentType + "\"><img src=\"//i.juick.com/photos-512/" + msg.getMid() + "-" + msg.getRid() + "." + msg.AttachmentType + "\" alt=\"\"/></a></div>");
+ if (msg.getAttachmentType() != null) {
+ out.println(" <div class=\"msg-media\"><a href=\"//i.juick.com/p/" + msg.getMid() + "-" + msg.getRid() + "." + msg.getAttachmentType() + "\"><img src=\"//i.juick.com/photos-512/" + msg.getMid() + "-" + msg.getRid() + "." + msg.getAttachmentType() + "\" alt=\"\"/></a></div>");
}
out.print(" <div class=\"msg-links\">/" + msg.getRid());
if (msg.getReplyto() > 0) {
@@ -338,8 +338,8 @@ public class UserThread {
out.println(" <div class=\"msg-ts\"><a href=\"/" + msg.getMid() + "#" + msg.getRid() + "\" title=\"" + PageTemplates.sdfSQL.format(msg.getDate()) + " GMT\">" + PageTemplates.formatDate(msg.TimeAgo, msg.getDate()) + "</a></div>");
out.println(" </div>");
out.println(" <div class=\"msg-txt\">" + MessageUtils.formatMessage(msg.getText()) + "</div>");
- if (msg.AttachmentType != null) {
- out.println(" <div class=\"msg-media\"><a href=\"//i.juick.com/p/" + msg.getMid() + "-" + msg.getRid() + "." + msg.AttachmentType + "\"><img src=\"//i.juick.com/photos-512/" + msg.getMid() + "-" + msg.getRid() + "." + msg.AttachmentType + "\" alt=\"\"/></a></div>");
+ if (msg.getAttachmentType() != null) {
+ out.println(" <div class=\"msg-media\"><a href=\"//i.juick.com/p/" + msg.getMid() + "-" + msg.getRid() + "." + msg.getAttachmentType() + "\"><img src=\"//i.juick.com/photos-512/" + msg.getMid() + "-" + msg.getRid() + "." + msg.getAttachmentType() + "\" alt=\"\"/></a></div>");
}
out.print(" <div class=\"msg-links\">/" + msg.getRid());
if (msg.getReplyto() > 0) {
diff --git a/juick-xmpp/src/main/java/com/juick/xmpp/extensions/JuickMessage.java b/juick-xmpp/src/main/java/com/juick/xmpp/extensions/JuickMessage.java
index 2c3875c5..18477b2a 100644
--- a/juick-xmpp/src/main/java/com/juick/xmpp/extensions/JuickMessage.java
+++ b/juick-xmpp/src/main/java/com/juick/xmpp/extensions/JuickMessage.java
@@ -86,7 +86,7 @@ public class JuickMessage extends com.juick.Message implements StanzaChild {
if (ts != null) {
jmsg.setDate(df.parse(ts));
}
- jmsg.AttachmentType = parser.getAttributeValue(null, "attach");
+ jmsg.setAttachmentType(parser.getAttributeValue(null, "attach"));
while (parser.next() == XmlPullParser.START_TAG) {
final String tag = parser.getName();
@@ -128,8 +128,8 @@ public class JuickMessage extends com.juick.Message implements StanzaChild {
if (getDate() != null) {
ret += " ts=\"" + df.format(getDate()) + "\"";
}
- if (AttachmentType != null) {
- ret += " attach=\"" + AttachmentType + "\"";
+ if (getAttachmentType() != null) {
+ ret += " attach=\"" + getAttachmentType() + "\"";
}
ret += ">";
if (getUser() != null) {
diff --git a/src/test/java/com/juick/json/MessageSerializer.java b/src/test/java/com/juick/json/MessageSerializer.java
index ebc3b0b0..38596a1a 100644
--- a/src/test/java/com/juick/json/MessageSerializer.java
+++ b/src/test/java/com/juick/json/MessageSerializer.java
@@ -18,6 +18,7 @@
package com.juick.json;
import com.juick.Message;
+import com.juick.Photo;
import com.juick.Tag;
import org.json.JSONArray;
import org.json.JSONException;
@@ -79,7 +80,12 @@ public class MessageSerializer extends JSONSerializer<Message> {
}
if (json.has("photo")) {
- jmsg.Photo = json.getJSONObject("photo").getString("small");
+ JSONObject obj = json.getJSONObject("photo");
+ Photo photo = new Photo();
+ photo.setSmall(obj.getString("small"));
+ photo.setMedium(obj.getString("medium"));
+ photo.setThumbnail(obj.getString("thumbnail"));
+ jmsg.setPhoto(photo);
}
return jmsg;
@@ -123,8 +129,9 @@ public class MessageSerializer extends JSONSerializer<Message> {
if (msg.Place != null) {
json.put("place", placeSerializer.serialize(msg.Place));
}
- if (msg.AttachmentType != null) {
- String fname = msg.getMid() + (msg.getRid() > 0 ? "-" + msg.getRid() : "") + "." + msg.AttachmentType;
+ if (msg.getAttachmentType() != null) {
+ json.put("attach", msg.getAttachmentType());
+ String fname = msg.getMid() + (msg.getRid() > 0 ? "-" + msg.getRid() : "") + "." + msg.getAttachmentType();
JSONObject photo = new JSONObject();
String protocol = getUriScheme() == URIScheme.Plain ? "http:" : "https:";
photo.put("thumbnail", protocol + "//i.juick.com/ps/" + fname);
diff --git a/src/test/java/com/juick/tests/SerializationTests.java b/src/test/java/com/juick/tests/SerializationTests.java
index dd2812c7..61b86c5f 100644
--- a/src/test/java/com/juick/tests/SerializationTests.java
+++ b/src/test/java/com/juick/tests/SerializationTests.java
@@ -49,17 +49,10 @@ public class SerializationTests {
user.setName("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());
Message msg = new Message();
msg.setMid(1);
msg.setUser(user);
@@ -67,11 +60,16 @@ public class SerializationTests {
msg.setDate(new Date());
msg.getTags().add(new Tag("test"));
msg.getTags().add(new Tag("json"));
+ msg.setAttachmentType("png");
MessageSerializer messageSerializer = new MessageSerializer();
JSONObject handmadeJsonMessage = messageSerializer.serialize(msg);
Message jacksonMessage = mapper.readValue(handmadeJsonMessage.toString(), Message.class);
assertEquals("jackson should deserialize ugnich json", msg, jacksonMessage);
String jacksonStringMessage = mapper.writeValueAsString(jacksonMessage);
+ assertEquals("jackson message should have an attachment url",
+ "http://i.juick.com/photos-1024/1.png", jacksonMessage.getAttachmentURL());
+ assertEquals("jackson message should have image previews",
+ "http://i.juick.com/photos-1024/1.png", jacksonMessage.getPhoto().getMedium());
JSONObject jacksonJsonMessage = new JSONObject(jacksonStringMessage);
assertEquals("jackson should serialize as ugnich", handmadeJsonMessage.length(), jacksonJsonMessage.length());
@@ -91,6 +89,7 @@ public class SerializationTests {
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new InputSource(new StringReader(xmlString)));
assertEquals("jaxb should marshal as ugnich", 4, doc.getDocumentElement().getChildNodes().getLength());
- assertEquals("jaxb should marshal as ugnich", 7, doc.getDocumentElement().getAttributes().getLength());
+ assertEquals("jaxb should marshal as ugnich", 8, doc.getDocumentElement().getAttributes().getLength());
+ assertEquals("xmpp message should have an attach attribute", "png", doc.getDocumentElement().getAttribute("attach"));
}
} \ No newline at end of file