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) --- 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 + 11 files changed, 199 insertions(+), 40 deletions(-) (limited to 'src/java/com/juick/http') 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("
" + rbnm.getString("Tags") + ":
"); + printUserTags(sql, out, visitor.UID); + out.println(""); + 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(""); 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("