From a0e43fc8a853a71b6aa85f93843c1692a4d13708 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 27 Jan 2012 08:31:23 +0700 Subject: SAPE, NewMessage (GET) --- nbproject/build-impl.xml | 9 + nbproject/genfiles.properties | 6 +- nbproject/project.properties | 5 +- nbproject/project.xml | 12 ++ src/java/Global.properties | 2 + src/java/Global_ru.properties | 2 + src/java/NewMessage.properties | 13 ++ src/java/NewMessage_ru.properties | 14 ++ src/java/com/juick/http/www/Blogs.java | 2 +- src/java/com/juick/http/www/Chats.java | 2 +- src/java/com/juick/http/www/Login.java | 9 +- src/java/com/juick/http/www/Main.java | 43 ++++- src/java/com/juick/http/www/Map.java | 2 +- src/java/com/juick/http/www/NewMessage.java | 88 +++++++++- src/java/com/juick/http/www/PageTemplates.java | 25 ++- src/java/com/juick/http/www/Photos.java | 2 +- src/java/com/juick/http/www/User.java | 2 +- src/java/com/juick/http/www/UserThread.java | 62 ++++--- src/java/com/juick/http/www/Utils.java | 2 + src/java/ru/sape/Sape.java | 25 +++ src/java/ru/sape/SapeConnection.java | 107 ++++++++++++ src/java/ru/sape/SapePageLinks.java | 95 +++++++++++ src/java/ru/sape/SerializedPhpParser.java | 221 +++++++++++++++++++++++++ 23 files changed, 706 insertions(+), 44 deletions(-) create mode 100644 src/java/NewMessage.properties create mode 100644 src/java/NewMessage_ru.properties create mode 100644 src/java/ru/sape/Sape.java create mode 100644 src/java/ru/sape/SapeConnection.java create mode 100644 src/java/ru/sape/SapePageLinks.java create mode 100644 src/java/ru/sape/SerializedPhpParser.java diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml index 267f24af..4d5df024 100644 --- a/nbproject/build-impl.xml +++ b/nbproject/build-impl.xml @@ -535,6 +535,9 @@ exists or setup the property manually. For example like this: + + + @@ -543,6 +546,9 @@ exists or setup the property manually. For example like this: + + + @@ -689,12 +695,14 @@ exists or setup the property manually. For example like this: + + @@ -1061,6 +1069,7 @@ exists or setup the property manually. For example like this: + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties index e8e9295a..ba8ca356 100644 --- a/nbproject/genfiles.properties +++ b/nbproject/genfiles.properties @@ -1,8 +1,8 @@ -build.xml.data.CRC32=9ff506d8 +build.xml.data.CRC32=8d999c58 build.xml.script.CRC32=c93fa366 build.xml.stylesheet.CRC32=651128d4@1.33.1.1 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=9ff506d8 -nbproject/build-impl.xml.script.CRC32=e315adae +nbproject/build-impl.xml.data.CRC32=8d999c58 +nbproject/build-impl.xml.script.CRC32=9d8ae4f2 nbproject/build-impl.xml.stylesheet.CRC32=0cbf5bb7@1.33.1.1 diff --git a/nbproject/project.properties b/nbproject/project.properties index de13c257..873bcd4c 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -36,7 +36,8 @@ jar.compress=false javac.classpath=\ ${reference.com_juick.jar}:\ ${reference.com_juick_server.jar}:\ - ${libs.MySQLDriver.classpath} + ${libs.MySQLDriver.classpath}:\ + ${reference.com_juick_xmpp.jar} # Space-separated list of extra javac options javac.compilerargs= javac.debug=true @@ -67,8 +68,10 @@ persistence.xml.dir=${conf.dir} platform.active=default_platform project.com_juick=../com.juick project.com_juick_server=../com.juick.server +project.com_juick_xmpp=../com.juick.xmpp reference.com_juick.jar=${project.com_juick}/dist/com.juick.jar reference.com_juick_server.jar=${project.com_juick_server}/dist/com.juick.server.jar +reference.com_juick_xmpp.jar=${project.com_juick_xmpp}/dist/com.juick.xmpp.jar resource.dir=setup run.test.classpath=\ ${javac.test.classpath}:\ diff --git a/nbproject/project.xml b/nbproject/project.xml index 78c0648d..45861aa2 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -17,6 +17,10 @@ ${libs.MySQLDriver.classpath} + + ${reference.com_juick_xmpp.jar} + WEB-INF/lib + @@ -43,6 +47,14 @@ clean jar + + com_juick_xmpp + jar + + jar + clean + jar + diff --git a/src/java/Global.properties b/src/java/Global.properties index 0883bec5..ebede501 100644 --- a/src/java/Global.properties +++ b/src/java/Global.properties @@ -15,3 +15,5 @@ Help=Help Older=Older Newer=Newer (replies)\ by=by +Comment=Comment +Sponsored\ by=Sponsored by diff --git a/src/java/Global_ru.properties b/src/java/Global_ru.properties index 81ba9c4f..34d7418a 100644 --- a/src/java/Global_ru.properties +++ b/src/java/Global_ru.properties @@ -15,3 +15,5 @@ Help=\u0421\u043f\u0440\u0430\u0432\u043a\u0430 Older=\u0421\u0442\u0430\u0440\u044b\u0435 Newer=\u041d\u043e\u0432\u044b\u0435 (replies)\ by=\u043e\u0442 +Comment=\u041e\u0442\u0432\u0435\u0442\u0438\u0442\u044c +Sponsored\ by=\u041f\u0440\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0435 diff --git a/src/java/NewMessage.properties b/src/java/NewMessage.properties new file mode 100644 index 00000000..93d91111 --- /dev/null +++ b/src/java/NewMessage.properties @@ -0,0 +1,13 @@ +# To change this template, choose Tools | Templates +# and open the template in the editor. + +New\ message=New message +Location=Location +Clear=Clear +Attachment=Attachment +from\ webcam=from webcam +Webcam\ photo=Webcam photo +Post=Post +Tags=Tags +Photo_JPG=Photo: JPG, up to 10Mb. Video: MP4/3GP/MOV/WMV/AVI/M4V, up to 100Mb. +or=or diff --git a/src/java/NewMessage_ru.properties b/src/java/NewMessage_ru.properties new file mode 100644 index 00000000..bb61fe23 --- /dev/null +++ b/src/java/NewMessage_ru.properties @@ -0,0 +1,14 @@ +# To change this template, choose Tools | Templates +# and open the template in the editor. + +New\ message=\u041d\u043e\u0432\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 +Location=\u041c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 +Clear=\u0423\u0434\u0430\u043b\u0438\u0442\u044c +Attachment=\u0424\u0430\u0439\u043b + +from\ webcam=\u0441 \u0432\u0435\u0431\u043a\u0430\u043c\u0435\u0440\u044b +Webcam\ photo=\u0424\u043e\u0442\u043e \u0441 \u0432\u0435\u0431\u043a\u0430\u043c\u0435\u0440\u044b +Post=\u041e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c +Tags=\u0422\u0435\u0433\u0438 +Photo_JPG=\u0424\u043e\u0442\u043e: JPG, \u0434\u043e 10\u041c\u0431. \u0412\u0438\u0434\u0435\u043e: MP4/3GP/MOV/WMV/AVI/M4V, \u0434\u043e 100Mb. +or=\u0438\u043b\u0438 diff --git a/src/java/com/juick/http/www/Blogs.java b/src/java/com/juick/http/www/Blogs.java index 3b9b6882..b4d30de0 100644 --- a/src/java/com/juick/http/www/Blogs.java +++ b/src/java/com/juick/http/www/Blogs.java @@ -160,7 +160,7 @@ public class Blogs { out.println(""); out.println(""); - PageTemplates.pageFooter(out, locale); + PageTemplates.pageFooter(request, out, locale, visitor); } finally { out.close(); } diff --git a/src/java/com/juick/http/www/Chats.java b/src/java/com/juick/http/www/Chats.java index 3df37976..d15bb271 100644 --- a/src/java/com/juick/http/www/Chats.java +++ b/src/java/com/juick/http/www/Chats.java @@ -69,7 +69,7 @@ public class Chats { out.println(""); out.println(""); - PageTemplates.pageFooter(out, locale); + PageTemplates.pageFooter(request, out, locale, visitor); } finally { out.close(); } diff --git a/src/java/com/juick/http/www/Login.java b/src/java/com/juick/http/www/Login.java index d03dd64d..1baf6e43 100644 --- a/src/java/com/juick/http/www/Login.java +++ b/src/java/com/juick/http/www/Login.java @@ -57,7 +57,7 @@ public class Login { out.println(""); out.println(""); - PageTemplates.pageFooter(out, locale); + PageTemplates.pageFooter(request, out, locale, visitor); } finally { out.close(); } @@ -98,7 +98,12 @@ public class Login { c.setMaxAge(0); response.addCookie(c); - response.sendRedirect("/"); + String referer = request.getHeader("Referer"); + if (referer != null && referer.startsWith("http://juick.com/") && !referer.equals("http://juick.com/login")) { + response.sendRedirect(referer); + } else { + response.sendRedirect("/"); + } } else { response.sendError(403); } diff --git a/src/java/com/juick/http/www/Main.java b/src/java/com/juick/http/www/Main.java index 709c6588..b7f95a4a 100644 --- a/src/java/com/juick/http/www/Main.java +++ b/src/java/com/juick/http/www/Main.java @@ -17,6 +17,7 @@ */ package com.juick.http.www; +import com.juick.xmpp.*; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; @@ -28,16 +29,18 @@ import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import ru.sape.Sape; /** * * @author Ugnich Anton */ @WebServlet(name = "Main", urlPatterns = {"/"}) -public class Main extends HttpServlet { +public class Main extends HttpServlet implements XmppListener { Connection sql; Connection sqlSearch; + XmppConnection xmpp; Blogs blogs = new Blogs(); Chats chats = new Chats(); Photos photos = new Photos(); @@ -58,6 +61,13 @@ public class Main extends HttpServlet { Class.forName("com.mysql.jdbc.Driver"); sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); sqlSearch = DriverManager.getConnection("jdbc:mysql://127.0.0.1:9306/juick?autoReconnect=true&characterEncoding=utf8&maxAllowedPacket=512000&relaxAutoCommit=true&user=root&password="); + /* + xmpp = new XmppConnectionComponent(new JID("www.juick.com"), conf.getProperty("xmpp_password", ""), "127.0.0.1", 5347, false); + xmpp.addListener((XmppListener) this); + xmpp.start(); + */ + + PageTemplates.sape = new Sape(conf.getProperty("sape_user"), "juick.com", 2000, 3600); } catch (Exception e) { log(null, e); } @@ -69,10 +79,34 @@ public class Main extends HttpServlet { if (sql != null) { try { sql.close(); + sql = null; } catch (SQLException e) { log(null, e); } } + if (sqlSearch != null) { + try { + sqlSearch.close(); + sqlSearch = null; + } catch (SQLException e) { + log(null, e); + } + } + } + + @Override + public void onAuth(String resource) { + log("XMPP AUTH: " + resource); + } + + @Override + public void onAuthFailed(String message) { + log("XMPP AUTH FAILED: " + message); + } + + @Override + public void onConnectionFailed(String message) { + log("XMPP CONNECTION FAILED: " + message); } /** @@ -97,7 +131,12 @@ public class Main extends HttpServlet { } else if (uri.equals("/map")) { map.doGet(sql, request, response); } else if (uri.equals("/post")) { - pagesNewMessage.doGetNewMessage(sql, request, response); + com.juick.User visitor = Utils.getVisitorUser(sql, request); + if (visitor != null) { + pagesNewMessage.doGetNewMessage(sql, request, response, visitor); + } else { + login.doGetLoginForm(sql, request, response); + } } else if (uri.equals("/login")) { if (request.getQueryString() == null) { login.doGetLoginForm(sql, request, response); diff --git a/src/java/com/juick/http/www/Map.java b/src/java/com/juick/http/www/Map.java index 1bc625b8..96524ac0 100644 --- a/src/java/com/juick/http/www/Map.java +++ b/src/java/com/juick/http/www/Map.java @@ -56,7 +56,7 @@ public class Map { out.println("$(window).unload(GUnload);"); out.println(""); - PageTemplates.pageFooter(out, locale); + PageTemplates.pageFooter(request, out, locale, visitor); } finally { out.close(); } diff --git a/src/java/com/juick/http/www/NewMessage.java b/src/java/com/juick/http/www/NewMessage.java index 8a92b475..1beacf62 100644 --- a/src/java/com/juick/http/www/NewMessage.java +++ b/src/java/com/juick/http/www/NewMessage.java @@ -17,8 +17,16 @@ */ package com.juick.http.www; +import com.juick.Tag; +import com.juick.server.TagQueries; import java.io.IOException; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.sql.Connection; +import java.util.ArrayList; +import java.util.Locale; +import java.util.ResourceBundle; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -29,7 +37,85 @@ import javax.servlet.http.HttpServletResponse; */ public class NewMessage { - protected void doGetNewMessage(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGetNewMessage(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { + Locale locale = request.getLocale(); + ResourceBundle rbnm = ResourceBundle.getBundle("NewMessage", locale); + + response.setContentType("text/html; charset=UTF-8"); + PrintWriter out = response.getWriter(); + try { + PageTemplates.pageHead(out, rbnm.getString("New message"), "" + + "" + + "" + + ""); + PageTemplates.pageNavigation(out, locale, visitor); + PageTemplates.pageTitle(out, rbnm.getString("New message")); + + out.println("
"); + out.println("
"); + out.println("

" + rbnm.getString("Location") + ": " + rbnm.getString("Clear") + "

"); + out.println("

" + rbnm.getString("Attachment") + ": " + rbnm.getString("or") + " " + rbnm.getString("from webcam") + "
"); + out.println("" + rbnm.getString("Photo_JPG") + "
" + rbnm.getString("Webcam photo") + " — " + rbnm.getString("Clear") + "

"); + out.println("
"); + out.println("


"); + out.println("" + "" + "

"); + out.println("
"); + out.println("
"); + out.println("

" + rbnm.getString("Tags") + ":

"); + printUserTags(sql, out, visitor.UID); + out.println("
"); + out.println("
"); + + PageTemplates.pageFooter(request, out, locale, visitor); + } finally { + out.close(); + } + } + + void printUserTags(Connection sql, PrintWriter out, int uid) { + ArrayList tags = TagQueries.getUserTagsAll(sql, uid); + + if (tags.isEmpty()) { + return; + } + + int min = tags.get(0).UsageCnt; + int max = tags.get(0).UsageCnt; + for (int i = 1; i < tags.size(); i++) { + int usagecnt = tags.get(i).UsageCnt; + if (usagecnt < min) { + min = usagecnt; + } + if (usagecnt > max) { + max = usagecnt; + } + } + max -= min; + + out.print("

"); + for (int i = 0; i < tags.size(); i++) { + if (i > 0) { + out.print(" "); + } + String taglink = ""; + try { + taglink = "" + Utils.encodeHTML(tags.get(i).Name) + ""; + } catch (UnsupportedEncodingException e) { + } + int usagecnt = tags.get(i).UsageCnt; + if (usagecnt <= max / 5 + min) { + out.print("" + taglink + ""); + } else if (usagecnt <= max / 5 * 2 + min) { + out.print(taglink); + } else if (usagecnt <= max / 5 * 3 + min) { + out.print("" + taglink + ""); + } else if (usagecnt <= max / 5 * 4 + min) { + out.print("" + taglink + ""); + } else { + out.print("" + taglink + ""); + } + } + out.println("

"); } protected void doPostNewMessage(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { diff --git a/src/java/com/juick/http/www/PageTemplates.java b/src/java/com/juick/http/www/PageTemplates.java index 94561dfd..eb98a4f8 100644 --- a/src/java/com/juick/http/www/PageTemplates.java +++ b/src/java/com/juick/http/www/PageTemplates.java @@ -27,6 +27,8 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Locale; import java.util.ResourceBundle; +import javax.servlet.http.HttpServletRequest; +import ru.sape.Sape; /** * @@ -34,6 +36,8 @@ import java.util.ResourceBundle; */ public class PageTemplates { + public static Sape sape = null; + public static void pageHead(PrintWriter out, String title, String headers) { out.println(""); out.println(""); @@ -61,7 +65,7 @@ public class PageTemplates { out.println("
\"Juick\"
"); out.println(" "); @@ -187,11 +191,18 @@ public class PageTemplates { out.println(); } - public static void pageFooter(PrintWriter out, Locale loc) { + public static void pageFooter(HttpServletRequest request, PrintWriter out, Locale loc, com.juick.User visitor) { ResourceBundle rb = ResourceBundle.getBundle("Global", loc); out.println("
"); out.println(" "); - out.println("
juick.com © 2008-2011
"); + out.print("
juick.com © 2008-2012"); + if (sape != null && visitor == null) { + String links = sape.getPageLinks(request.getRequestURI(), request.getCookies()).render(); + if (links != null && !links.isEmpty()) { + out.print("
" + rb.getString("Sponsored by") + ": " + links); + } + } + out.println("
"); out.println("
"); } @@ -331,15 +342,15 @@ public class PageTemplates { txt = formatMessage(txt); + out.print("
  • "); - } else { - out.println("
  • "); + out.print(" style=\"border: 0\""); } + out.println(">"); if (rs.getString(12) != null) { if (rs.getString(12).equals("jpg")) { - out.println("
    \"\"/
    "); + out.println("
    \"\"/
    "); } else { out.println("
    Attachment: Video
    "); out.println(" "); - PageTemplates.pageFooter(out, locale); + PageTemplates.pageFooter(request, out, locale, visitor); } finally { out.close(); } diff --git a/src/java/com/juick/http/www/User.java b/src/java/com/juick/http/www/User.java index fefe1174..9d692a44 100644 --- a/src/java/com/juick/http/www/User.java +++ b/src/java/com/juick/http/www/User.java @@ -140,7 +140,7 @@ public class User { out.println("
  • "); out.println(""); - PageTemplates.pageFooter(out, locale); + PageTemplates.pageFooter(request, out, locale, visitor); } finally { out.close(); } diff --git a/src/java/com/juick/http/www/UserThread.java b/src/java/com/juick/http/www/UserThread.java index 6688e964..30787297 100644 --- a/src/java/com/juick/http/www/UserThread.java +++ b/src/java/com/juick/http/www/UserThread.java @@ -40,7 +40,6 @@ public class UserThread { protected void doGetThread(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user, int MID) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request); Locale locale = request.getLocale(); - ResourceBundle rb = ResourceBundle.getBundle("User", locale); boolean listview = false; String paramView = request.getParameter("view"); @@ -69,24 +68,9 @@ public class UserThread { out.println("
    "); out.println("
    "); - out.println("
      "); printMessage(out, sql, MID, locale); - out.println("
    "); - - out.println("
    "); - out.print(" "); - out.println("

    Replies

    "); - out.println("
    "); - out.println("
      "); printReplies(out, sql, MID, locale, listview); - out.println("
    "); out.println("