From cdbf949013fe140fed0e5ad79ad6111c97ea07ad Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Tue, 29 Jan 2013 23:45:00 +0700 Subject: Sape optimization --- src/java/com/juick/http/www/Blogs.java | 66 +++++++++++++------------- src/java/com/juick/http/www/Help.java | 23 ++++----- src/java/com/juick/http/www/Login.java | 1 + src/java/com/juick/http/www/Main.java | 24 ++++++++-- src/java/com/juick/http/www/NewMessage.java | 1 + src/java/com/juick/http/www/PageTemplates.java | 9 ++-- src/java/com/juick/http/www/RootRedirects.java | 57 ---------------------- src/java/com/juick/http/www/User.java | 17 +++++-- src/java/com/juick/http/www/UserThread.java | 1 + 9 files changed, 88 insertions(+), 111 deletions(-) delete mode 100644 src/java/com/juick/http/www/RootRedirects.java (limited to 'src/java/com') diff --git a/src/java/com/juick/http/www/Blogs.java b/src/java/com/juick/http/www/Blogs.java index 527009a1..5acc8947 100644 --- a/src/java/com/juick/http/www/Blogs.java +++ b/src/java/com/juick/http/www/Blogs.java @@ -41,9 +41,9 @@ import javax.servlet.http.HttpServletResponse; * @author Ugnich Anton */ public class Blogs { - + private int relatedTagsStartMID = 0; - + public Blogs(Connection sql) { PreparedStatement stmt = null; ResultSet rs = null; @@ -59,7 +59,7 @@ public class Blogs { Utils.finishSQL(rs, stmt); } } - + protected void doGet(Connection sql, Connection sqlSearch, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String paramTagStrQuery = request.getParameter("tag"); if (paramTagStrQuery != null) { @@ -70,18 +70,18 @@ public class Blogs { } Utils.sendPermanentRedirect(response, url); } - + com.juick.User visitor = Utils.getVisitorUser(sql, request); Locale locale = request.getLocale(); ResourceBundle rb = ResourceBundle.getBundle("Blogs", locale); - + String title; ArrayList mids; - + String paramShow = request.getParameter("show"); - + String requesturi = request.getRequestURI(); - + int paramTag = 0; String paramTagStr = null; if (requesturi.startsWith("/tag/")) { @@ -94,7 +94,7 @@ public class Blogs { } else if (requesturi.startsWith("/places/")) { //TODO перенести сюда блок, который страницей ниже } - + int paramBefore = 0; String paramBeforeStr = request.getParameter("before"); if (paramBeforeStr != null) { @@ -103,15 +103,15 @@ public class Blogs { } catch (NumberFormatException e) { } } - + String paramSearch = request.getParameter("search"); if (paramSearch != null && paramSearch.length() > 64) { paramSearch = null; } - + com.juick.Place place = null; int visitor_uid = visitor != null ? visitor.UID : 0; - + if (paramShow == null) { if (paramTag > 0) { title = "*" + Utils.encodeHTML(paramTagStr); @@ -179,27 +179,27 @@ public class Blogs { response.sendError(404); return; } - + response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); try { PageTemplates.pageHead(out, title, null); PageTemplates.pageNavigation(out, locale, visitor, paramSearch); - + out.println("
"); out.println("
"); out.println("
"); - + if (place != null) { out.println("
\"Map\"/
"); } - + if (mids.size() > 0) { out.println("
    "); PageTemplates.printMessages(out, sql, null, mids, visitor, locale); out.println("
"); } - + if (mids.size() == 20) { String nextpage = "?before=" + mids.get(mids.size() - 1); if (paramShow != null) { @@ -210,11 +210,11 @@ public class Blogs { } out.println("

Older →

"); } - + PageTemplates.pageFooter(request, out, locale, visitor, true); out.println("
"); out.println("
"); // wrapper - + out.println("
"); if (visitor != null) { out.println(" "); out.println("
"); - out.println("

" + getTags(sql, 36) + "

"); + out.println("

" + getTags(sql, 40) + "

"); out.println("
"); - + out.println("
"); PageTemplates.pageYandexAd(out, visitor == null ? 2 : 3); // разный ID для залогиненых и нет if (paramTag > 0 && relatedTagsStartMID > 0) { - String related = getRelatedTags(sql, paramTag, 10); + String related = getRelatedTags(sql, paramTag, 15); if (!related.isEmpty()) { out.println("

Related tags:

"); out.println("
    " + related + "
"); } } out.println("
"); - + out.println("
"); // topwrapper PageTemplates.pageDialogTemplate(out, locale); - PageTemplates.pageFooter(request, out, locale, visitor, true); + PageTemplates.pageEnd(out); } finally { out.close(); } } - + private String getTags(Connection sql, int cnt) { String ret = ""; com.juick.Tag tags[] = new com.juick.Tag[cnt]; - + int maxUsageCnt = 0; PreparedStatement stmt = null; ResultSet rs = null; @@ -281,16 +281,16 @@ public class Blogs { } finally { Utils.finishSQL(rs, stmt); } - + Arrays.sort(tags, 0, cnt); - + for (int i = 0; i < cnt; i++) { String tag = Utils.encodeHTML(tags[i].Name); try { tag = "" + tag + ""; } catch (UnsupportedEncodingException e) { } - + if (tags[i].UsageCnt > maxUsageCnt / 3 * 2) { ret += "" + tag + " "; } else if (tags[i].UsageCnt > maxUsageCnt / 3) { @@ -299,13 +299,13 @@ public class Blogs { ret += tag + " "; } } - + return ret; } - + private String getRelatedTags(Connection sql, int tag_id, int cnt) { String ret = ""; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -325,7 +325,7 @@ public class Blogs { } finally { Utils.finishSQL(rs, stmt); } - + return ret; } } diff --git a/src/java/com/juick/http/www/Help.java b/src/java/com/juick/http/www/Help.java index 3dfe1f80..9fc21aad 100644 --- a/src/java/com/juick/http/www/Help.java +++ b/src/java/com/juick/http/www/Help.java @@ -34,7 +34,7 @@ import javax.servlet.http.HttpServletResponse; * @author Ugnich Anton */ public class Help { - + protected void doRedirectToHelpIndex(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String lang = request.getLocale().getLanguage(); if (!(lang.equals("ru") || lang.equals("fr"))) { @@ -42,19 +42,19 @@ public class Help { } Utils.sendTemporaryRedirect(response, "/help/" + lang + "/"); } - + protected void doGetHelp(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request); Locale locale = request.getLocale(); ResourceBundle rb = ResourceBundle.getBundle("Global", locale); - + String path[] = request.getRequestURI().split("/"); String page; if (path.length < 3 || path.length > 4 || path[2].length() != 2 || !path[2].matches("^[a-z]+$")) { response.sendError(404); return; } - + if (path.length == 4) { page = path[3]; if (!page.matches("^[a-zA-Z0-9\\-]*$") || page.equals("navigation") || page.equals("index")) { @@ -64,40 +64,41 @@ public class Help { } else { page = "index"; } - + File f = new File("/var/www/juick.com/help3/" + path[2] + "/" + page); if (!f.isFile()) { response.sendError(404); return; } - + response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); try { PageTemplates.pageHead(out, rb.getString("Help"), ""); PageTemplates.pageNavigation(out, locale, visitor, null); - + out.println("
"); out.println("
"); out.println("
"); printFile(out, f); out.println("
"); out.println("
"); - + out.println("
"); printFile(out, new File("/var/www/juick.com/help3/" + path[2] + "/navigation")); out.println("
"); printFile(out, new File("/var/www/juick.com/help3/langs")); out.println("
"); - + out.println("
"); - + PageTemplates.pageFooter(request, out, locale, visitor, false); + PageTemplates.pageEnd(out); } finally { out.close(); } } - + private void printFile(PrintWriter out, File f) throws IOException { BufferedReader br = new BufferedReader(new FileReader(f)); String str; diff --git a/src/java/com/juick/http/www/Login.java b/src/java/com/juick/http/www/Login.java index e2585909..d50e6115 100644 --- a/src/java/com/juick/http/www/Login.java +++ b/src/java/com/juick/http/www/Login.java @@ -59,6 +59,7 @@ public class Login { out.println(""); // topwrapper PageTemplates.pageFooter(request, out, locale, visitor, false); + PageTemplates.pageEnd(out); } finally { out.close(); } diff --git a/src/java/com/juick/http/www/Main.java b/src/java/com/juick/http/www/Main.java index 226ac729..a41ccf9b 100644 --- a/src/java/com/juick/http/www/Main.java +++ b/src/java/com/juick/http/www/Main.java @@ -42,7 +42,6 @@ public class Main extends HttpServlet implements XmppListener { Connection sqlSearch; XmppConnection xmpp; Blogs blogs; - RootRedirects rootRedirects = new RootRedirects(); Login login = new Login(); Help help = new Help(); User pagesUser = new User(); @@ -122,6 +121,7 @@ public class Main extends HttpServlet implements XmppListener { request.setCharacterEncoding("UTF-8"); } String uri = request.getRequestURI(); + if (uri.equals("/")) { blogs.doGet(sql, sqlSearch, request, response); } else if (uri.equals("/post")) { @@ -150,9 +150,27 @@ public class Main extends HttpServlet implements XmppListener { } else if (uri.startsWith("/places/")) { blogs.doGet(sql, sqlSearch, request, response); } else if (uri.matches("^/\\d+$")) { - rootRedirects.doGetPostID(sql, request, response); + String strID = request.getRequestURI().substring(1); + int mid = 0; + try { + mid = Integer.parseInt(strID); + } catch (NumberFormatException e) { + } + if (mid > 0) { + com.juick.User author = com.juick.server.MessagesQueries.getMessageAuthor(sql, mid); + if (author != null) { + Utils.sendPermanentRedirect(response, "/" + author.UName + "/" + mid); + return; + } + } + response.sendError(404); } else if (uri.matches("^/[^/]+$")) { - rootRedirects.doGetUsername(sql, request, response); + com.juick.User user = com.juick.server.UserQueries.getUserByNick(sql, request.getRequestURI().substring(1)); + if (user != null) { + Utils.sendPermanentRedirect(response, "/" + user.UName + "/"); + } else { + response.sendError(404); + } } else if (uri.matches("^/.+/.*")) { String uriparts[] = uri.split("/"); com.juick.User user = com.juick.server.UserQueries.getUserByNick(sql, uriparts[1]); diff --git a/src/java/com/juick/http/www/NewMessage.java b/src/java/com/juick/http/www/NewMessage.java index 369e9de3..3f1c0ac0 100644 --- a/src/java/com/juick/http/www/NewMessage.java +++ b/src/java/com/juick/http/www/NewMessage.java @@ -77,6 +77,7 @@ public class NewMessage { out.println(""); PageTemplates.pageFooter(request, out, locale, visitor, false); + PageTemplates.pageEnd(out); } finally { out.close(); } diff --git a/src/java/com/juick/http/www/PageTemplates.java b/src/java/com/juick/http/www/PageTemplates.java index 5851a3ee..42fafaf7 100644 --- a/src/java/com/juick/http/www/PageTemplates.java +++ b/src/java/com/juick/http/www/PageTemplates.java @@ -52,7 +52,7 @@ public class PageTemplates { out.println(""); out.println(""); out.println(" " + title + ""); - out.println(" "); + out.println(" "); out.println(" "); out.println(" "); out.println(" "); @@ -60,7 +60,7 @@ public class PageTemplates { if (headers != null) { out.println(headers); } - out.println(" "); + out.println(" "); out.println(""); out.println(); out.println(""); @@ -183,7 +183,7 @@ public class PageTemplates { out.println("
"); out.println(" "); out.print("
juick.com © 2008-2013"); - if (sapeon && sape != null && (visitor == null || visitor.UID == 1)) { + if (sapeon && sape != null && (visitor == null || visitor.UID == 1) && request.getQueryString() == null) { String links = sape.getPageLinks(request.getRequestURI(), request.getCookies()).render(); if (links != null && !links.isEmpty()) { out.print("
" + rb.getString("Sponsored by") + ": " + links); @@ -191,6 +191,9 @@ public class PageTemplates { } out.println("
"); out.println("
"); + } + + public static void pageEnd(PrintWriter out) { out.println(""); } diff --git a/src/java/com/juick/http/www/RootRedirects.java b/src/java/com/juick/http/www/RootRedirects.java deleted file mode 100644 index 21b0077e..00000000 --- a/src/java/com/juick/http/www/RootRedirects.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Juick - * Copyright (C) 2008-2011, Ugnich Anton - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package com.juick.http.www; - -import java.io.IOException; -import java.sql.Connection; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** - * - * @author Ugnich Anton - */ -public class RootRedirects { - - protected void doGetPostID(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String strID = request.getRequestURI().substring(1); - int mid = 0; - try { - mid = Integer.parseInt(strID); - } catch (NumberFormatException e) { - } - if (mid > 0) { - com.juick.User author = com.juick.server.MessagesQueries.getMessageAuthor(sql, mid); - if (author != null) { - Utils.sendPermanentRedirect(response, "/" + author.UName + "/" + mid); - return; - } - } - response.sendError(404); - } - - protected void doGetUsername(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User user = com.juick.server.UserQueries.getUserByNick(sql, request.getRequestURI().substring(1)); - if (user != null) { - Utils.sendPermanentRedirect(response, "/" + user.UName + "/"); - } else { - response.sendError(404); - } - } -} diff --git a/src/java/com/juick/http/www/User.java b/src/java/com/juick/http/www/User.java index 8574eaab..2faf695b 100644 --- a/src/java/com/juick/http/www/User.java +++ b/src/java/com/juick/http/www/User.java @@ -132,14 +132,20 @@ public class User { } out.println("

Older →

"); } + PageTemplates.pageFooter(request, out, locale, visitor, true); out.println(""); pageUserColumn(out, sql, user, visitor, locale); out.println(""); + + out.println("
"); + PageTemplates.pageYandexAd(out, visitor == null ? 4 : 5); // разный ID для залогиненых и нет + out.println("
"); + out.println(""); // topwrapper PageTemplates.pageDialogTemplate(out, locale); - PageTemplates.pageFooter(request, out, locale, visitor, true); + PageTemplates.pageEnd(out); } finally { out.close(); } @@ -164,13 +170,14 @@ public class User { out.println("
"); out.println("
"); out.println("

" + pageUserTags(sql, user, visitor, 0) + "

"); + PageTemplates.pageFooter(request, out, locale, visitor, false); out.println("
"); pageUserColumn(out, sql, user, visitor, locale); out.println("
"); out.println(""); PageTemplates.pageDialogTemplate(out, locale); - PageTemplates.pageFooter(request, out, locale, visitor, false); + PageTemplates.pageEnd(out); } finally { out.close(); } @@ -215,13 +222,14 @@ public class User { } out.println(""); + PageTemplates.pageFooter(request, out, locale, visitor, false); out.println(""); pageUserColumn(out, sql, user, visitor, locale); out.println(""); out.println(""); PageTemplates.pageDialogTemplate(out, locale); - PageTemplates.pageFooter(request, out, locale, visitor, false); + PageTemplates.pageEnd(out); } finally { out.close(); } @@ -266,13 +274,14 @@ public class User { } out.println(""); + PageTemplates.pageFooter(request, out, locale, visitor, false); out.println(""); pageUserColumn(out, sql, user, visitor, locale); out.println(""); out.println(""); PageTemplates.pageDialogTemplate(out, locale); - PageTemplates.pageFooter(request, out, locale, visitor, false); + PageTemplates.pageEnd(out); } finally { out.close(); } diff --git a/src/java/com/juick/http/www/UserThread.java b/src/java/com/juick/http/www/UserThread.java index 3b315be2..aba97a85 100644 --- a/src/java/com/juick/http/www/UserThread.java +++ b/src/java/com/juick/http/www/UserThread.java @@ -81,6 +81,7 @@ public class UserThread { PageTemplates.pageDialogTemplate(out, locale); PageTemplates.pageFooter(request, out, locale, visitor, false); + PageTemplates.pageEnd(out); } finally { out.close(); } -- cgit v1.2.3