From c492429b25c1ef711620a4f509975c7edc3ffa4a Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 14 Jan 2016 12:16:49 +0300 Subject: Update libraries and refactoring --- src/main/java/com/juick/CrosspostComponent.java | 24 +-- src/main/java/com/juick/PushComponent.java | 22 +-- src/main/java/com/juick/http/www/Discover.java | 12 +- src/main/java/com/juick/http/www/Home.java | 17 +- src/main/java/com/juick/http/www/Login.java | 2 +- src/main/java/com/juick/http/www/Main.java | 67 ++++---- src/main/java/com/juick/http/www/NewMessage.java | 50 +++--- src/main/java/com/juick/http/www/PM.java | 33 ++-- .../java/com/juick/http/www/PageTemplates.java | 79 +++++----- src/main/java/com/juick/http/www/RSS.java | 36 ++--- src/main/java/com/juick/http/www/SignUp.java | 2 +- src/main/java/com/juick/http/www/TwitterAuth.java | 2 +- src/main/java/com/juick/http/www/User.java | 105 ++++++------- src/main/java/com/juick/http/www/UserThread.java | 148 +++++++++--------- src/main/java/com/juick/http/www/Utils.java | 2 +- .../com/juick/xmpp/extensions/JuickMessage.java | 172 +++++++++++++++++++++ .../java/com/juick/xmpp/extensions/JuickUser.java | 75 +++++++++ 17 files changed, 538 insertions(+), 310 deletions(-) create mode 100644 src/main/java/com/juick/xmpp/extensions/JuickMessage.java create mode 100644 src/main/java/com/juick/xmpp/extensions/JuickUser.java (limited to 'src/main') diff --git a/src/main/java/com/juick/CrosspostComponent.java b/src/main/java/com/juick/CrosspostComponent.java index f6d830ec..7fd50564 100644 --- a/src/main/java/com/juick/CrosspostComponent.java +++ b/src/main/java/com/juick/CrosspostComponent.java @@ -159,7 +159,7 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea @Override public void onMessage(com.juick.xmpp.Message msg) { JuickMessage jmsg = (JuickMessage) msg.getChild(JuickMessage.XMLNS); - if (msg.to != null && msg.to.Username != null && jmsg != null && jmsg.RID == 0) { + if (msg.to != null && msg.to.Username != null && jmsg != null && jmsg.getRID() == 0) { if (msg.to.Username.equals("twitter")) { twitterPost(jmsg); } else if (msg.to.Username.equals("fb")) { @@ -171,18 +171,18 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea } public boolean facebookPost(com.juick.Message jmsg) { - String token = CrosspostQueries.getFacebookToken(sql, jmsg.User.UID); + String token = CrosspostQueries.getFacebookToken(sql, jmsg.getUser().getUID()); if (token == null) { return false; } - logger.info("FB: #" + jmsg.MID); + logger.info("FB: #" + jmsg.getMID()); - String status = getMessageHashTags(jmsg) + "\n" + jmsg.Text; + String status = getMessageHashTags(jmsg) + "\n" + jmsg.getText(); boolean ret = false; try { - String body = "access_token=" + URLEncoder.encode(token, "UTF-8") + "&message=" + URLEncoder.encode(status, "UTF-8") + "&link=http%3A%2F%2Fjuick.com%2F" + jmsg.MID; + String body = "access_token=" + URLEncoder.encode(token, "UTF-8") + "&message=" + URLEncoder.encode(status, "UTF-8") + "&link=http%3A%2F%2Fjuick.com%2F" + jmsg.getMID(); HttpsURLConnection conn = (HttpsURLConnection) new URL(FBURL).openConnection(); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); @@ -208,14 +208,14 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea } public boolean vkontaktePost(com.juick.Message jmsg) { - String tokens[] = CrosspostQueries.getVKTokens(sql, jmsg.User.UID); + String tokens[] = CrosspostQueries.getVKTokens(sql, jmsg.getUser().getUID()); if (tokens == null || tokens.length != 2) { return false; } - logger.info("VK: #" + jmsg.MID); + logger.info("VK: #" + jmsg.getMID()); - String status = getMessageHashTags(jmsg) + "\n" + jmsg.Text + "\nhttp://juick.com/" + jmsg.MID; + String status = getMessageHashTags(jmsg) + "\n" + jmsg.getText() + "\nhttp://juick.com/" + jmsg.getMID(); boolean ret = false; try { @@ -245,20 +245,20 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea } public boolean twitterPost(com.juick.Message jmsg) { - String tokens[] = CrosspostQueries.getTwitterTokens(sql, jmsg.User.UID); + String tokens[] = CrosspostQueries.getTwitterTokens(sql, jmsg.getUser().getUID()); if (tokens == null || tokens.length != 2) { return false; } String token = percentEncode(tokens[0]); String token_secret = percentEncode(tokens[1]); - logger.info("TWITTER: #" + jmsg.MID); + logger.info("TWITTER: #" + jmsg.getMID()); - String status = getMessageHashTags(jmsg) + jmsg.Text; + String status = getMessageHashTags(jmsg) + jmsg.getText(); if (status.length() > 115) { status = status.substring(0, 114) + "…"; } - status += " http://juick.com/" + jmsg.MID; + status += " http://juick.com/" + jmsg.getMID(); status = percentEncode(status); boolean ret = false; diff --git a/src/main/java/com/juick/PushComponent.java b/src/main/java/com/juick/PushComponent.java index 6fccce1e..533ca086 100644 --- a/src/main/java/com/juick/PushComponent.java +++ b/src/main/java/com/juick/PushComponent.java @@ -166,15 +166,15 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe } logger.info("Message to push: " + msg.toString()); - boolean isPM = jmsg.MID == 0; - boolean isReply = jmsg.RID > 0; + boolean isPM = jmsg.getMID() == 0; + boolean isReply = jmsg.getRID() > 0; int senderID = 0, recipientID = 0; // reply pushed to original sender if (isReply) { - com.juick.Message op = MessagesQueries.getMessage(sql, jmsg.MID); - senderID = op.User.UID; + com.juick.Message op = MessagesQueries.getMessage(sql, jmsg.getMID()); + senderID = op.getUser().getUID(); // do not notify self - if (jmsg.User.UID == op.User.UID) { + if (jmsg.getUser().getUID() == op.getUser().getUID()) { return; } } @@ -189,7 +189,7 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe } /*** ANDROID ***/ - ArrayList regids; + List regids; if (isPM) { regids = new ArrayList(); String targetId = PushQueries.getAndroidRegID(sql, recipientID); @@ -221,7 +221,7 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe } /*** WinPhone ***/ - ArrayList urls; + List urls; if (isPM) { urls = new ArrayList(); String targetURL = PushQueries.getWinPhoneURL(sql, recipientID); @@ -233,11 +233,11 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe } if (!urls.isEmpty()) { - String text1 = "@" + jmsg.User.UName; + String text1 = "@" + jmsg.getUser().getUName(); if (!jmsg.Tags.isEmpty()) { text1 += ":" + XmlUtils.escape(jmsg.getTagsString()); } - String text2 = XmlUtils.escape(jmsg.Text); + String text2 = XmlUtils.escape(jmsg.getText()); String xml = "" + "" + "" @@ -248,7 +248,7 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe + "" + "" + "" - + "" + + "" + "" + ""; logger.fine(xml); @@ -277,7 +277,7 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe ApnsService service = APNS.newService().withCert("/etc/juick/ios.p12", "juick") .withSandboxDestination().build(); for (String token : tokens) { - String payload = APNS.newPayload().alertTitle("@" + jmsg.User.UName).alertBody(jmsg.Text).build(); + String payload = APNS.newPayload().alertTitle("@" + jmsg.getUser().getUName()).alertBody(jmsg.getText()).build(); logger.info("APNS: " + token); service.push(token, payload); } diff --git a/src/main/java/com/juick/http/www/Discover.java b/src/main/java/com/juick/http/www/Discover.java index 201139c4..e7d85d8a 100644 --- a/src/main/java/com/juick/http/www/Discover.java +++ b/src/main/java/com/juick/http/www/Discover.java @@ -26,6 +26,7 @@ import java.net.URLDecoder; import java.net.URLEncoder; import java.sql.Connection; import java.util.ArrayList; +import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -70,14 +71,13 @@ public class Discover { } } - int visitor_uid = visitor != null ? visitor.UID : 0; + int visitor_uid = visitor != null ? visitor.getUID() : 0; String title = "*" + Utils.encodeHTML(paramTag.Name); - ArrayList mids = MessagesQueries.getTag(sql, paramTag.TID, visitor_uid, paramBefore, (visitor == null) ? 40 : 20); + List mids = MessagesQueries.getTag(sql, paramTag.TID, visitor_uid, paramBefore, (visitor == null) ? 40 : 20); response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { String head = ""; if (TagQueries.getTagNoIndex(sql, paramTag.TID)) { head = ""; @@ -91,7 +91,7 @@ public class Discover { out.println("
"); if (mids.size() > 0) { - int vuid = visitor != null ? visitor.UID : 0; + int vuid = visitor != null ? visitor.getUID() : 0; int ad_mid = AdsQueries.getAdMID(sql, vuid); if (ad_mid > 0 && mids.indexOf(ad_mid) == -1) { mids.add(0, ad_mid); @@ -113,8 +113,6 @@ public class Discover { PageTemplates.pageFooter(request, out, visitor, true); PageTemplates.pageEnd(out); - } finally { - out.close(); } } } diff --git a/src/main/java/com/juick/http/www/Home.java b/src/main/java/com/juick/http/www/Home.java index 20752a40..8dc3e70c 100644 --- a/src/main/java/com/juick/http/www/Home.java +++ b/src/main/java/com/juick/http/www/Home.java @@ -24,6 +24,7 @@ import java.io.PrintWriter; import java.net.URLEncoder; import java.sql.Connection; import java.util.ArrayList; +import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -50,7 +51,7 @@ public class Home { } String title; - ArrayList mids; + List mids; String paramShow = request.getParameter("show"); if (paramSearch != null) { @@ -68,27 +69,27 @@ public class Home { return; } else if (paramShow.equals("my") && visitor != null) { title = "Моя лента"; - mids = MessagesQueries.getMyFeed(sql, visitor.UID, paramBefore); + mids = MessagesQueries.getMyFeed(sql, visitor.getUID(), paramBefore); } else if (paramShow.equals("private") && visitor != null) { title = "Приватные"; - mids = MessagesQueries.getPrivate(sql, visitor.UID, paramBefore); + mids = MessagesQueries.getPrivate(sql, visitor.getUID(), paramBefore); } else if (paramShow.equals("discuss") && visitor != null) { title = "Обсуждения"; - mids = MessagesQueries.getDiscussions(sql, visitor.UID, paramBefore); + mids = MessagesQueries.getDiscussions(sql, visitor.getUID(), paramBefore); } else if (paramShow.equals("recommended") && visitor != null) { title = "Рекомендации"; - mids = MessagesQueries.getRecommended(sql, visitor.UID, paramBefore); + mids = MessagesQueries.getRecommended(sql, visitor.getUID(), paramBefore); } else if (paramShow.equals("photos")) { title = "Фотографии"; if (visitor != null) { - mids = MessagesQueries.getPhotos(sql, visitor.UID, paramBefore); + mids = MessagesQueries.getPhotos(sql, visitor.getUID(), paramBefore); } else { mids = MessagesQueries.getPhotos(sql, 0, paramBefore); } } else if (paramShow.equals("all")) { title = "Все сообщения"; if (visitor != null) { - mids = MessagesQueries.getAll(sql, visitor.UID, paramBefore); + mids = MessagesQueries.getAll(sql, visitor.getUID(), paramBefore); } else { mids = MessagesQueries.getAll(sql, 0, paramBefore); } @@ -130,7 +131,7 @@ public class Home { if (mids.size() > 0) { int ad_mid = 0; if (paramShow == null || paramShow.equals("top") || paramShow.equals("all")) { - int vuid = visitor != null ? visitor.UID : 0; + int vuid = visitor != null ? visitor.getUID() : 0; ad_mid = AdsQueries.getAdMID(sql, vuid); if (ad_mid > 0 && mids.indexOf(ad_mid) == -1) { mids.add(0, ad_mid); diff --git a/src/main/java/com/juick/http/www/Login.java b/src/main/java/com/juick/http/www/Login.java index a24eac2b..3e9c5e09 100644 --- a/src/main/java/com/juick/http/www/Login.java +++ b/src/main/java/com/juick/http/www/Login.java @@ -236,7 +236,7 @@ public class Login { PreparedStatement stmt = null; try { stmt = sql.prepareStatement("DELETE FROM logins WHERE user_id=?"); - stmt.setInt(1, visitor.UID); + stmt.setInt(1, visitor.getUID()); stmt.executeUpdate(); } catch (SQLException e) { System.err.println(e); diff --git a/src/main/java/com/juick/http/www/Main.java b/src/main/java/com/juick/http/www/Main.java index cc4edd49..cabd50af 100644 --- a/src/main/java/com/juick/http/www/Main.java +++ b/src/main/java/com/juick/http/www/Main.java @@ -21,7 +21,14 @@ import com.juick.server.UserQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Stream; import com.juick.xmpp.StreamComponent; -import java.io.FileInputStream; +import ru.sape.Sape; + +import javax.servlet.ServletException; +import javax.servlet.annotation.MultipartConfig; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.Socket; import java.net.URLEncoder; @@ -29,14 +36,6 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; -import java.util.logging.Level; -import javax.servlet.ServletException; -import javax.servlet.annotation.MultipartConfig; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import ru.sape.Sape; /** * @@ -107,18 +106,14 @@ public class Main extends HttpServlet implements Stream.StreamListener { return sqlSearch; } public void setupXmppComponent(final String password) { - Thread thr = new Thread(new Runnable() { - - @Override - public void run() { - try { - Socket socket = new Socket("localhost", 5347); - xmpp = new StreamComponent(new JID("", "www.juick.com", ""), socket.getInputStream(), socket.getOutputStream(), password); - xmpp.addListener(Main.this); - xmpp.startParsing(); - } catch (IOException e) { - System.err.println(e); - } + Thread thr = new Thread(() -> { + try { + Socket socket = new Socket("localhost", 5347); + xmpp = new StreamComponent(new JID("", "www.juick.com", ""), socket.getInputStream(), socket.getOutputStream(), password); + xmpp.addListener(Main.this); + xmpp.startParsing(); + } catch (IOException e) { + log("xmpp exception", e); } }); thr.start(); @@ -194,12 +189,16 @@ public class Main extends HttpServlet implements Stream.StreamListener { if (visitor == null) { Utils.sendTemporaryRedirect(response, "/login"); } else { - if (uri.equals("/pm/inbox")) { - pm.doGetInbox(sql, request, response, visitor); - } else if (uri.equals("/pm/sent")) { - pm.doGetSent(sql, request, response, visitor); - } else { - Errors.doGet404(sql, request, response); + switch (uri) { + case "/pm/inbox": + pm.doGetInbox(sql, request, response, visitor); + break; + case "/pm/sent": + pm.doGetSent(sql, request, response, visitor); + break; + default: + Errors.doGet404(sql, request, response); + break; } } } else if (uri.startsWith("/rss/")) { @@ -239,7 +238,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { if (mid > 0) { com.juick.User author = com.juick.server.MessagesQueries.getMessageAuthor(sql, mid); if (author != null) { - Utils.sendPermanentRedirect(response, "/" + author.UName + "/" + mid); + Utils.sendPermanentRedirect(response, "/" + author.getUName() + "/" + mid); return; } } @@ -247,14 +246,14 @@ public class Main extends HttpServlet implements Stream.StreamListener { } else if (uri.matches("^/[^/]+$")) { com.juick.User user = com.juick.server.UserQueries.getUserByName(sql, request.getRequestURI().substring(1)); if (user != null) { - Utils.sendPermanentRedirect(response, "/" + user.UName + "/"); + Utils.sendPermanentRedirect(response, "/" + user.getUName() + "/"); } else { Errors.doGet404(sql, request, response); } } else if (uri.matches("^/.+/.*")) { String uriparts[] = uri.split("/"); com.juick.User user = com.juick.server.UserQueries.getUserByName(sql, uriparts[1]); - if (user != null && user.UName.equals(uriparts[1]) && user.Banned == false) { + if (user != null && user.getUName().equals(uriparts[1]) && !user.Banned) { if (uriparts.length == 2) { // http://juick.com/username/ pagesUser.doGetBlog(sql, getSqlSearch(), request, response, user); closeSqlSearch(); @@ -273,8 +272,8 @@ public class Main extends HttpServlet implements Stream.StreamListener { if (mid > 0) { com.juick.User author = com.juick.server.MessagesQueries.getMessageAuthor(sql, mid); if (author != null) { - if (!author.UName.equals(user.UName)) { - Utils.sendPermanentRedirect(response, "/" + author.UName + "/" + mid); + if (!author.getUName().equals(user.getUName())) { + Utils.sendPermanentRedirect(response, "/" + author.getUName() + "/" + mid); } else { pagesUserThread.doGetThread(sql, request, response, mid); } @@ -285,8 +284,8 @@ public class Main extends HttpServlet implements Stream.StreamListener { Errors.doGet404(sql, request, response); } } - } else if (user != null && user.Banned == false) { - Utils.sendPermanentRedirect(response, "/" + user.UName + "/" + (uriparts.length > 2 ? uriparts[2] : "")); + } else if (user != null && !user.Banned) { + Utils.sendPermanentRedirect(response, "/" + user.getUName() + "/" + (uriparts.length > 2 ? uriparts[2] : "")); } else { Errors.doGet404(sql, request, response); } diff --git a/src/main/java/com/juick/http/www/NewMessage.java b/src/main/java/com/juick/http/www/NewMessage.java index 3cf73882..86725db7 100644 --- a/src/main/java/com/juick/http/www/NewMessage.java +++ b/src/main/java/com/juick/http/www/NewMessage.java @@ -37,6 +37,7 @@ import java.net.URL; import java.net.URLEncoder; import java.sql.Connection; import java.util.ArrayList; +import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -88,7 +89,7 @@ public class NewMessage { } void printUserTags(Connection sql, PrintWriter out, com.juick.User visitor) { - ArrayList tags = TagQueries.getUserTagsAll(sql, visitor.UID); + List tags = TagQueries.getUserTagsAll(sql, visitor.getUID()); if (tags.isEmpty()) { return; @@ -114,7 +115,7 @@ public class NewMessage { } String taglink = ""; try { - taglink = "" + Utils.encodeHTML(tags.get(i).Name) + ""; + taglink = "" + Utils.encodeHTML(tags.get(i).Name) + ""; } catch (UnsupportedEncodingException e) { } int usagecnt = tags.get(i).UsageCnt; @@ -142,7 +143,7 @@ public class NewMessage { body = body.replace("\r", ""); String tagsStr = request.getParameter("tags"); - ArrayList tags = new ArrayList(); + List tags = new ArrayList(); String tagsArr[] = new String[1]; if (tagsStr != null && !tagsStr.isEmpty()) { tagsArr = tagsStr.split("[ \\,]"); @@ -182,8 +183,8 @@ public class NewMessage { } String attachmentType = attachmentFName != null ? attachmentFName.substring(attachmentFName.length() - 3) : null; - int mid = MessagesQueries.createMessage(sql, visitor.UID, body, attachmentType, tags); - SubscriptionsQueries.subscribeMessage(sql, mid, visitor.UID); + int mid = MessagesQueries.createMessage(sql, visitor.getUID(), body, attachmentType, tags); + SubscriptionsQueries.subscribeMessage(sql, mid, visitor.getUID()); Message xmsg = new Message(); xmsg.from = new JID("juick", "juick.com", null); @@ -194,7 +195,7 @@ public class NewMessage { xmsg.addChild(jmsg); Nickname nick = new Nickname(); - nick.Nickname = "@" + jmsg.User.UName; + nick.Nickname = "@" + jmsg.getUser().getUName(); xmsg.addChild(nick); if (attachmentFName != null) { @@ -210,10 +211,10 @@ public class NewMessage { } String tagsStr2 = ""; - for (int i = 0; i < tagsArr.length; i++) { - tagsStr2 += " *" + tagsArr[i]; + for (String tag : tagsArr) { + tagsStr2 += " *" + tag; } - xmsg.body = "@" + jmsg.User.UName + ":" + tagsStr2 + "\n" + body + "\n\n#" + mid + " http://juick.com/" + mid; + xmsg.body = "@" + jmsg.getUser().getUName() + ":" + tagsStr2 + "\n" + body + "\n\n#" + mid + " http://juick.com/" + mid; xmsg.to = new JID("juick", "s2s.juick.com", null); xmpp.send(xmsg); @@ -237,8 +238,7 @@ public class NewMessage { // response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { PageTemplates.pageHead(out, "Сообщение опубликовано", null); PageTemplates.pageNavigation(out, visitor, null); PageTemplates.pageHomeColumn(out, sql, visitor); @@ -265,12 +265,12 @@ public class NewMessage { out.println("
"); out.println("

Сообщение опубликовано

"); out.println("

Поделитесь своим новым постом в социальных сетях:

"); - if (CrosspostQueries.getTwitterTokens(sql, visitor.UID) == null) { + if (CrosspostQueries.getTwitterTokens(sql, visitor.getUID()) == null) { out.println("

Отправить в Twitter

"); } out.println("

Отправить в LiveJournal

"); out.println("

Отправить в ВКонтакте

"); - if (CrosspostQueries.getFacebookToken(sql, visitor.UID) == null) { + if (CrosspostQueries.getFacebookToken(sql, visitor.getUID()) == null) { out.println("

Отправить в Facebook

"); } out.println("

Отправить в Google+

"); @@ -279,8 +279,6 @@ public class NewMessage { PageTemplates.pageFooter(request, out, visitor, false); PageTemplates.pageEnd(out); - } finally { - out.close(); } } @@ -313,7 +311,7 @@ public class NewMessage { } body = body.replace("\r", ""); - if ((msg.ReadOnly && msg.User.UID != visitor.UID) || UserQueries.isInBLAny(sql, msg.User.UID, visitor.UID) || (reply != null && UserQueries.isInBLAny(sql, reply.User.UID, visitor.UID))) { + if ((msg.ReadOnly && msg.getUser().getUID() != visitor.getUID()) || UserQueries.isInBLAny(sql, msg.getUser().getUID(), visitor.getUID()) || (reply != null && UserQueries.isInBLAny(sql, reply.getUser().getUID(), visitor.getUID()))) { response.sendError(403); return; } @@ -339,8 +337,8 @@ public class NewMessage { } String attachmentType = attachmentFName != null ? attachmentFName.substring(attachmentFName.length() - 3) : null; - int ridnew = MessagesQueries.createReply(sql, mid, rid, visitor.UID, body, attachmentType); - SubscriptionsQueries.subscribeMessage(sql, mid, visitor.UID); + int ridnew = MessagesQueries.createReply(sql, mid, rid, visitor.getUID(), body, attachmentType); + SubscriptionsQueries.subscribeMessage(sql, mid, visitor.getUID()); Message xmsg = new Message(); xmsg.from = new JID("juick", "juick.com", null); @@ -350,13 +348,13 @@ public class NewMessage { JuickMessage jmsg = new JuickMessage(MessagesQueries.getReply(sql, mid, ridnew)); xmsg.addChild(jmsg); - String quote = reply != null ? reply.Text : msg.Text; + String quote = reply != null ? reply.getText() : msg.getText(); if (quote.length() >= 50) { quote = quote.substring(0, 47) + "..."; } Nickname nick = new Nickname(); - nick.Nickname = "@" + jmsg.User.UName; + nick.Nickname = "@" + jmsg.getUser().getUName(); xmsg.addChild(nick); if (attachmentFName != null) { @@ -371,7 +369,7 @@ public class NewMessage { xmsg.addChild(xoob); } - xmsg.body = "Reply by @" + jmsg.User.UName + ":\n>" + quote + "\n" + body + "\n\n#" + mid + "/" + ridnew + " http://juick.com/" + mid + "#" + ridnew; + xmsg.body = "Reply by @" + jmsg.getUser().getUName() + ":\n>" + quote + "\n" + body + "\n\n#" + mid + "/" + ridnew + " http://juick.com/" + mid + "#" + ridnew; xmsg.to = new JID("juick", "s2s.juick.com", null); xmpp.send(xmsg); @@ -382,7 +380,7 @@ public class NewMessage { xmsg.to.Host = "push.juick.com"; xmpp.send(xmsg); - Utils.sendTemporaryRedirect(response, "/" + msg.User.UName + "/" + mid + "#" + ridnew); + Utils.sendTemporaryRedirect(response, "/" + msg.getUser().getUName() + "/" + mid + "#" + ridnew); } public void doPostRecomm(Connection sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { @@ -396,20 +394,20 @@ public class NewMessage { response.sendError(404); return; } - if (msg.User.UID == visitor.UID) { + if (msg.getUser().getUID() == visitor.getUID()) { response.sendError(403); return; } - boolean res = MessagesQueries.recommendMessage(sql, mid, visitor.UID); + boolean res = MessagesQueries.recommendMessage(sql, mid, visitor.getUID()); if (res) { Message xmsg = new Message(); xmsg.from = new JID("juick", "juick.com", null); xmsg.to = new JID("recomm", "s2s.juick.com", null); JuickMessage jmsg = new JuickMessage(); - jmsg.MID = mid; - jmsg.User = new JuickUser(visitor); + jmsg.setMID(mid); + jmsg.setUser(new JuickUser(visitor)); xmsg.addChild(jmsg); xmpp.send(xmsg); diff --git a/src/main/java/com/juick/http/www/PM.java b/src/main/java/com/juick/http/www/PM.java index d0d2f514..a9505bdb 100644 --- a/src/main/java/com/juick/http/www/PM.java +++ b/src/main/java/com/juick/http/www/PM.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.util.ArrayList; +import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -50,7 +51,7 @@ public class PM { */ String title = "PM: Inbox"; - ArrayList msgs = PMQueries.getLastPMInbox(sql, visitor.UID); + List msgs = PMQueries.getLastPMInbox(sql, visitor.getUID()); response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); @@ -66,16 +67,16 @@ public class PM { for (int i = msgs.size() - 1; i >= 0; i--) { com.juick.Message msg = msgs.get(i); - String txt = PageTemplates.formatMessage(msg.Text); + String txt = PageTemplates.formatMessage(msg.getText()); out.println("
  • "); - out.println("
    \""
    "); + out.println("
    \""
    "); out.println("
    "); - out.println(" "); + out.println(" "); out.println(" "); out.println("
    " + txt + "
    "); - out.println("
    "); + out.println(" "); out.println("
    "); out.println("
    "); @@ -114,7 +115,7 @@ public class PM { */ String title = "PM: Sent"; - ArrayList msgs = PMQueries.getLastPMSent(sql, visitor.UID); + List msgs = PMQueries.getLastPMSent(sql, visitor.getUID()); String uname = request.getParameter("uname"); if (!UserQueries.checkUserNameValid(uname)) { @@ -143,12 +144,12 @@ public class PM { for (int i = msgs.size() - 1; i >= 0; i--) { com.juick.Message msg = msgs.get(i); - String txt = PageTemplates.formatMessage(msg.Text); + String txt = PageTemplates.formatMessage(msg.getText()); out.println("
  • "); - out.println("
    "); + out.println("
    "); out.println("
    "); - out.println(" "); + out.println(" "); out.println(" "); out.println("
    " + txt + "
    "); out.println("
    "); @@ -189,18 +190,18 @@ public class PM { return; } - if (UserQueries.isInBLAny(sql, uid, visitor.UID)) { + if (UserQueries.isInBLAny(sql, uid, visitor.getUID())) { response.sendError(403); return; } - if (PMQueries.createPM(sql, visitor.UID, uid, body)) { + if (PMQueries.createPM(sql, visitor.getUID(), uid, body)) { Message msg = new Message(); msg.from = new JID("juick", "juick.com", null); msg.to = new JID(Integer.toString(uid), "push.juick.com", null); JuickMessage jmsg = new JuickMessage(); - jmsg.User = UserQueries.getUserByUID(sql, visitor.UID); - jmsg.Text = body; + jmsg.setUser(UserQueries.getUserByUID(sql, visitor.getUID())); + jmsg.setText(body); msg.childs.add(jmsg); xmpp.send(msg); @@ -212,12 +213,12 @@ public class PM { Message mm = new Message(); mm.to = new JID(jid); mm.type = Message.Type.chat; - if (PMQueries.havePMinRoster(sql, visitor.UID, jid)) { - mm.from = new JID(jmsg.User.UName, "juick.com", "Juick"); + if (PMQueries.havePMinRoster(sql, visitor.getUID(), jid)) { + mm.from = new JID(jmsg.getUser().getUName(), "juick.com", "Juick"); mm.body = body; } else { mm.from = new JID("juick", "juick.com", "Juick"); - mm.body = "Private message from @" + jmsg.User.UName + ":\n" + body; + mm.body = "Private message from @" + jmsg.getUser().getUName() + ":\n" + body; } xmpp.send(mm); } diff --git a/src/main/java/com/juick/http/www/PageTemplates.java b/src/main/java/com/juick/http/www/PageTemplates.java index 2b041cb2..2f9c8976 100644 --- a/src/main/java/com/juick/http/www/PageTemplates.java +++ b/src/main/java/com/juick/http/www/PageTemplates.java @@ -17,6 +17,8 @@ */ package com.juick.http.www; +import com.juick.Message; +import com.juick.Tag; import com.juick.server.MessagesQueries; import com.juick.server.UserQueries; import java.io.PrintWriter; @@ -30,6 +32,7 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; @@ -92,7 +95,7 @@ public class PageTemplates { out.println(""); out.print(" "); } else { @@ -232,7 +235,7 @@ public class PageTemplates { String queryString = request.getQueryString(); String requestURI = request.getRequestURI(); - if (sapeon && sape != null && (visitor == null || visitor.UID == 1) && queryString == null) { + if (sapeon && sape != null && (visitor == null || visitor.getUID() == 1) && queryString == null) { String links = sape.getPageLinks(requestURI, request.getCookies()).render(); if (links != null && !links.isEmpty()) { out.print("
    Спонсоры: " + links); @@ -244,7 +247,7 @@ public class PageTemplates { if (visitor != null) { out.println(""); } @@ -272,10 +275,9 @@ public class PageTemplates { out.println(""); } - public static String formatTags(ArrayList tags) { + public static String formatTags(List tags) { String ret = ""; - for (int i = 0; i < tags.size(); i++) { - com.juick.Tag tag = tags.get(i); + for (Tag tag : tags) { String tagName = tag.Name.replaceAll("<", "<").replaceAll(">", ">"); try { ret += " * tags, com.juick.User user) { + public static String formatTags(List tags, com.juick.User user) { String ret = ""; - for (int i = 0; i < tags.size(); i++) { - String tag = tags.get(i); + for (String tag : tags) { tag = tag.replaceAll("<", "<"); tag = tag.replaceAll(">", ">"); try { @@ -301,9 +302,9 @@ public class PageTemplates { if (user == null) { ret += "/tag/"; } else { - ret += "/" + user.UName + "/?tag="; + ret += "/" + user.getUName() + "/?tag="; } - ret += URLEncoder.encode(tags.get(i), "utf-8") + "\">" + tag + ""; + ret += URLEncoder.encode(tag, "utf-8") + "\">" + tag + ""; } catch (UnsupportedEncodingException e) { } } @@ -491,24 +492,24 @@ public class PageTemplates { return msg; } - public static void printMessages(PrintWriter out, Connection sql, com.juick.User user, ArrayList mids, com.juick.User visitor, int YandexID, int ad_mid) { - ArrayList msgs = MessagesQueries.getMessages(sql, mids); + public static void printMessages(PrintWriter out, Connection sql, com.juick.User user, List mids, com.juick.User visitor, int YandexID, int ad_mid) { + List msgs = MessagesQueries.getMessages(sql, mids); for (int i = 0; i < msgs.size(); i++) { com.juick.Message msg = msgs.get(i); - if (msg.MID == ad_mid) { + if (msg.getMID() == ad_mid) { msgs.remove(i); msgs.add(0, msg); break; } } - ArrayList blUIDs = new ArrayList(20); + List blUIDs = new ArrayList(20); if (visitor != null) { - for (int i = 0; i < msgs.size(); i++) { - blUIDs.add(msgs.get(i).User.UID); + for (Message msg : msgs) { + blUIDs.add(msg.getUser().getUID()); } - blUIDs = UserQueries.checkBL(sql, visitor.UID, blUIDs); + blUIDs = UserQueries.checkBL(sql, visitor.getUID(), blUIDs); } for (int i = 0; i < msgs.size(); i++) { @@ -519,7 +520,7 @@ public class PageTemplates { com.juick.Message msg = msgs.get(i); - ArrayList tags = MessagesQueries.getMessageTags(sql, msg.MID); + List tags = MessagesQueries.getMessageTags(sql, msg.getMID()); String tagsStr = formatTags(tags); if (msg.ReadOnly) { tagsStr += " *readonly"; @@ -527,23 +528,23 @@ public class PageTemplates { if (msg.Privacy < 0) { tagsStr += " *friends"; } - if (msg.MID == ad_mid) { + if (msg.getMID() == ad_mid) { tagsStr += " *реклама"; } String txt; if (!msg.Tags.isEmpty() && msg.Tags.contains("code")) { - txt = formatMessageCode(msg.Text); + txt = formatMessageCode(msg.getText()); } else { - txt = formatMessage(msg.Text); + txt = formatMessage(msg.getText()); } - out.println("
    "); - out.println(" "); - out.println("
    @" + msg.User.UName + ":" + tagsStr + "
    "); - out.println("
    "); + out.println("
    "); + out.println(" "); + out.println("
    @" + msg.getUser().getUName() + ":" + tagsStr + "
    "); + out.println("
    "); if (msg.AttachmentType != null) { - String fname = msg.MID + "." + msg.AttachmentType; + String fname = msg.getMID() + "." + msg.AttachmentType; out.println("

    \"\"/

    "); } out.println("

    " + txt + "

    "); @@ -551,29 +552,29 @@ public class PageTemplates { out.println("
    "); } out.print(" "); out.print(" "); out.print("
    "); diff --git a/src/main/java/com/juick/http/www/RSS.java b/src/main/java/com/juick/http/www/RSS.java index ab96221e..72893915 100644 --- a/src/main/java/com/juick/http/www/RSS.java +++ b/src/main/java/com/juick/http/www/RSS.java @@ -23,9 +23,9 @@ import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Date; import java.util.Iterator; +import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -40,17 +40,16 @@ public class RSS { private static final SimpleDateFormat sdfRSS = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response, int uid, String uname) throws ServletException, IOException { - ArrayList mids = MessagesQueries.getUserBlog(sql, uid, 0, 0); + List mids = MessagesQueries.getUserBlog(sql, uid, 0, 0); if (mids.isEmpty()) { response.sendError(404); return; } - ArrayList msgs = MessagesQueries.getMessages(sql, mids); + List msgs = MessagesQueries.getMessages(sql, mids); response.setContentType("application/rss+xml; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { out.println(""); out.println(""); out.println(""); @@ -60,22 +59,19 @@ public class RSS { out.println("The latest messages by @" + uname + " at Juick"); out.println("http://i.juick.com/a/" + uid + ".png" + uname + " - Juickhttp://juick.com/" + uname + "/"); - Iterator i = msgs.iterator(); - while (i.hasNext()) { - Message msg = i.next(); - + for (Message msg : msgs) { out.println(""); - out.println("http://juick.com/" + msg.User.UName + "/" + msg.MID + ""); - out.println("http://juick.com/" + msg.User.UName + "/" + msg.MID + ""); + out.println("http://juick.com/" + msg.getUser().getUName() + "/" + msg.getMID() + ""); + out.println("http://juick.com/" + msg.getUser().getUName() + "/" + msg.getMID() + ""); - out.print("<![CDATA[@" + msg.User.UName + ":"); + out.print("<title><![CDATA[@" + msg.getUser().getUName() + ":"); if (!msg.Tags.isEmpty()) { for (int n = 0; n < msg.Tags.size(); n++) { out.print(" *" + msg.Tags.get(n)); } } out.println("]]>"); - out.println(""); + out.println(""); synchronized (sdfSQL) { try { @@ -86,7 +82,7 @@ public class RSS { } } - out.println("http://juick.com/" + msg.User.UName + "/" + msg.MID + ""); + 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("" + msg.Tags.get(n) + ""); @@ -94,20 +90,18 @@ public class RSS { } if (msg.AttachmentType != null) { if (msg.AttachmentType.equals("jpg")) { - out.println(""); - out.println(""); + out.println(""); + out.println(""); } else if (msg.AttachmentType.equals("png")) { - out.println(""); - out.println(""); + out.println(""); + out.println(""); } } - out.println(""); + out.println(""); out.println(""); } out.println(""); - } finally { - out.close(); } } } diff --git a/src/main/java/com/juick/http/www/SignUp.java b/src/main/java/com/juick/http/www/SignUp.java index 395232a6..2fae9b69 100644 --- a/src/main/java/com/juick/http/www/SignUp.java +++ b/src/main/java/com/juick/http/www/SignUp.java @@ -134,7 +134,7 @@ public class SignUp { } uid = com.juick.server.UserQueries.checkPassword(sql, username, password); } else { - uid = visitor.UID; + uid = visitor.getUID(); } if (uid <= 0) { diff --git a/src/main/java/com/juick/http/www/TwitterAuth.java b/src/main/java/com/juick/http/www/TwitterAuth.java index d6abaa75..421c6f64 100644 --- a/src/main/java/com/juick/http/www/TwitterAuth.java +++ b/src/main/java/com/juick/http/www/TwitterAuth.java @@ -45,7 +45,7 @@ public class TwitterAuth { } } com.juick.User user = UserQueries.getUserByHash(sql, hash); - if ( user == null || user.UID == 0) { + if ( user == null || user.getUID() == 0) { response.sendError(403); return; } diff --git a/src/main/java/com/juick/http/www/User.java b/src/main/java/com/juick/http/www/User.java index 83601ade..84d1ad9a 100644 --- a/src/main/java/com/juick/http/www/User.java +++ b/src/main/java/com/juick/http/www/User.java @@ -30,6 +30,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; @@ -44,7 +45,7 @@ public class User { protected void doGetBlog(Connection sql, Connection sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - ArrayList mids; + List mids; String paramShow = request.getParameter("show"); @@ -58,7 +59,7 @@ public class User { Errors.doGet404(sql, request, response); return; } else if (!paramTag.Name.equals(paramTagStr)) { - String url = "/" + user.UName + "/?tag=" + URLEncoder.encode(paramTag.Name, "UTF-8"); + String url = "/" + user.getUName() + "/?tag=" + URLEncoder.encode(paramTag.Name, "UTF-8"); Utils.sendPermanentRedirect(response, url); return; } @@ -80,9 +81,9 @@ public class User { int privacy = 0; if (visitor != null) { - if (user.UID == visitor.UID || visitor.UID == 1) { + if (user.getUID() == visitor.getUID() || visitor.getUID() == 1) { privacy = -3; - } else if (UserQueries.isInWL(sql, user.UID, visitor.UID)) { + } else if (UserQueries.isInWL(sql, user.getUID(), visitor.getUID())) { privacy = -2; } } @@ -90,34 +91,33 @@ public class User { String title; if (paramShow == null) { if (paramTag != null) { - title = "Блог " + user.UName + ": *" + Utils.encodeHTML(paramTag.Name); - mids = MessagesQueries.getUserTag(sql, user.UID, paramTag.TID, privacy, paramBefore); + title = "Блог " + user.getUName() + ": *" + Utils.encodeHTML(paramTag.Name); + mids = MessagesQueries.getUserTag(sql, user.getUID(), paramTag.TID, privacy, paramBefore); } else if (paramSearch != null) { - title = "Блог " + user.UName + ": " + Utils.encodeHTML(paramSearch); - mids = MessagesQueries.getUserSearch(sql, sqlSearch, user.UID, Utils.encodeSphinx(paramSearch), privacy, paramBefore); + title = "Блог " + user.getUName() + ": " + Utils.encodeHTML(paramSearch); + mids = MessagesQueries.getUserSearch(sql, sqlSearch, user.getUID(), Utils.encodeSphinx(paramSearch), privacy, paramBefore); } else { - title = "Блог " + user.UName; - mids = MessagesQueries.getUserBlog(sql, user.UID, privacy, paramBefore); + title = "Блог " + user.getUName(); + mids = MessagesQueries.getUserBlog(sql, user.getUID(), privacy, paramBefore); } } else if (paramShow.equals("recomm")) { - title = "Рекомендации " + user.UName; - mids = MessagesQueries.getUserRecommendations(sql, user.UID, paramBefore); + title = "Рекомендации " + user.getUName(); + mids = MessagesQueries.getUserRecommendations(sql, user.getUID(), paramBefore); } else if (paramShow.equals("photos")) { - title = "Фотографии " + user.UName; - mids = MessagesQueries.getUserPhotos(sql, user.UID, privacy, paramBefore); + title = "Фотографии " + user.getUName(); + mids = MessagesQueries.getUserPhotos(sql, user.getUID(), privacy, paramBefore); } else { Errors.doGet404(sql, request, response); return; } if (visitor == null) { - pageUserRefCookie(request, response, user.UID); + pageUserRefCookie(request, response, user.getUID()); } response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { - String head = ""; + try (PrintWriter out = response.getWriter()) { + String head = ""; if (paramTag != null && TagQueries.getTagNoIndex(sql, paramTag.TID)) { head += ""; } else if (paramBefore > 0 || paramShow != null) { @@ -155,8 +155,6 @@ public class User { PageTemplates.pageFooter(request, out, visitor, true); PageTemplates.pageEnd(out); - } finally { - out.close(); } } @@ -164,14 +162,13 @@ public class User { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor == null) { - pageUserRefCookie(request, response, user.UID); + pageUserRefCookie(request, response, user.getUID()); } response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { String head = ""; - PageTemplates.pageHead(out, "Теги " + user.UName, head); + PageTemplates.pageHead(out, "Теги " + user.getUName(), head); PageTemplates.pageNavigation(out, visitor, null); pageUserColumn(out, sql, user, visitor); @@ -181,8 +178,6 @@ public class User { PageTemplates.pageFooter(request, out, visitor, false); PageTemplates.pageEnd(out); - } finally { - out.close(); } } @@ -190,14 +185,13 @@ public class User { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor == null) { - pageUserRefCookie(request, response, user.UID); + pageUserRefCookie(request, response, user.getUID()); } response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { String head = ""; - PageTemplates.pageHead(out, "Подписки " + user.UName, head); + PageTemplates.pageHead(out, "Подписки " + user.getUName(), head); PageTemplates.pageNavigation(out, visitor, null); pageUserColumn(out, sql, user, visitor); @@ -208,7 +202,7 @@ public class User { ResultSet rs = null; try { stmt = sql.prepareStatement("SELECT users.id,users.nick FROM subscr_users INNER JOIN users ON subscr_users.user_id=users.id WHERE subscr_users.suser_id=? ORDER BY users.nick"); - stmt.setInt(1, user.UID); + stmt.setInt(1, user.getUID()); rs = stmt.executeQuery(); rs.beforeFirst(); int cnt = 0; @@ -230,8 +224,6 @@ public class User { PageTemplates.pageFooter(request, out, visitor, false); PageTemplates.pageEnd(out); - } finally { - out.close(); } } @@ -239,14 +231,13 @@ public class User { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor == null) { - pageUserRefCookie(request, response, user.UID); + pageUserRefCookie(request, response, user.getUID()); } response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { String head = ""; - PageTemplates.pageHead(out, "Читатели " + user.UName, head); + PageTemplates.pageHead(out, "Читатели " + user.getUName(), head); PageTemplates.pageNavigation(out, visitor, null); pageUserColumn(out, sql, user, visitor); @@ -257,7 +248,7 @@ public class User { ResultSet rs = null; try { stmt = sql.prepareStatement("SELECT users.id,users.nick FROM subscr_users INNER JOIN users ON subscr_users.suser_id=users.id WHERE subscr_users.user_id=? ORDER BY users.nick"); - stmt.setInt(1, user.UID); + stmt.setInt(1, user.getUID()); rs = stmt.executeQuery(); rs.beforeFirst(); int cnt = 0; @@ -279,8 +270,6 @@ public class User { PageTemplates.pageFooter(request, out, visitor, false); PageTemplates.pageEnd(out); - } finally { - out.close(); } } @@ -298,21 +287,21 @@ public class User { public static void pageUserColumn(PrintWriter out, Connection sql, com.juick.User user, com.juick.User visitor) { out.println("