From 9adb555fe41f6322310be1fea406d9d8d44acafe Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Wed, 26 Dec 2012 14:30:03 +0700 Subject: New tags URLs --- src/java/com/juick/http/www/Blogs.java | 53 ++++++++-------- src/java/com/juick/http/www/Main.java | 6 +- src/java/com/juick/http/www/NewMessage.java | 8 +-- src/java/com/juick/http/www/PageTemplates.java | 14 +++-- src/java/com/juick/http/www/Tags.java | 87 -------------------------- src/java/com/juick/http/www/User.java | 2 +- src/java/com/juick/http/www/UserThread.java | 6 +- 7 files changed, 48 insertions(+), 128 deletions(-) delete mode 100644 src/java/com/juick/http/www/Tags.java (limited to 'src/java/com/juick') diff --git a/src/java/com/juick/http/www/Blogs.java b/src/java/com/juick/http/www/Blogs.java index 0250a157..fb0657ad 100644 --- a/src/java/com/juick/http/www/Blogs.java +++ b/src/java/com/juick/http/www/Blogs.java @@ -22,6 +22,7 @@ import com.juick.server.TagQueries; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.net.URLEncoder; import java.sql.Connection; import java.sql.PreparedStatement; @@ -42,6 +43,16 @@ import javax.servlet.http.HttpServletResponse; public class Blogs { protected void doGet(Connection sql, Connection sqlSearch, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String paramTagStrQuery = request.getParameter("tag"); + if (paramTagStrQuery != null) { + String url = "/tag/" + URLEncoder.encode(paramTagStrQuery, "UTF-8"); + String paramBeforeStr = request.getParameter("before"); + if (paramBeforeStr != null) { + url += "?before=" + paramBeforeStr; + } + Utils.sendPermanentRedirect(response, url); + } + com.juick.User visitor = Utils.getVisitorUser(sql, request); Locale locale = request.getLocale(); ResourceBundle rb = ResourceBundle.getBundle("Blogs", locale); @@ -52,9 +63,14 @@ public class Blogs { String paramShow = request.getParameter("show"); int paramTag = 0; - String paramTagStr = request.getParameter("tag"); - if (paramTagStr != null && paramTagStr.length() < 64) { + String paramTagStr = null; + if (request.getRequestURI().startsWith("/tag/")) { + paramTagStr = URLDecoder.decode(request.getRequestURI().substring(5), "UTF-8"); paramTag = TagQueries.getTagID(sql, paramTagStr, false); + if (paramTag == 0) { + response.sendError(404); + return; + } } int paramBefore = 0; @@ -135,20 +151,10 @@ public class Blogs { out.println("
"); out.println("
"); - //PageTemplates.pageFilters(out, locale, visitor); - if (mids.size() > 0) { - /* - if (paramShow != null && paramShow.equals("photos")) { - PageTemplates.printPhotos(out, sql, mids, locale); - } else { - */ out.println(""); - /* - } - */ } if (mids.size() == 20) { @@ -156,9 +162,6 @@ public class Blogs { if (paramShow != null) { nextpage += "&show=" + paramShow; } - if (paramTag > 0) { - nextpage += "&tag=" + URLEncoder.encode(paramTagStr, "UTF-8"); - } out.println("

Older →

"); } @@ -169,20 +172,20 @@ public class Blogs { out.println("
"); if (visitor != null) { out.println(" "); out.println("
"); } out.println(" "); out.println("
"); - out.println("

" + getTags(sql, 30) + "

"); + out.println("

" + getTags(sql, 36) + "

"); out.println("
"); out.println("
"); @@ -229,7 +232,7 @@ public class Blogs { for (int i = 0; i < cnt; i++) { String tag = Utils.encodeHTML(tags[i].Name); try { - tag = "" + tag + ""; + tag = "" + tag + ""; } catch (UnsupportedEncodingException e) { } diff --git a/src/java/com/juick/http/www/Main.java b/src/java/com/juick/http/www/Main.java index 20885906..ee601ede 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 = new Blogs(); - Tags tags = new Tags(); RootRedirects rootRedirects = new RootRedirects(); Login login = new Login(); Help help = new Help(); @@ -123,9 +122,6 @@ public class Main extends HttpServlet implements XmppListener { String uri = request.getRequestURI(); if (uri.equals("/")) { blogs.doGet(sql, sqlSearch, request, response); - } else if (uri.equals("/tags")) { - com.juick.User visitor = Utils.getVisitorUser(sql, request); - tags.doGetTags(sql, request, response, visitor); } else if (uri.equals("/post")) { com.juick.User visitor = Utils.getVisitorUser(sql, request); if (visitor != null) { @@ -147,6 +143,8 @@ public class Main extends HttpServlet implements XmppListener { help.doRedirectToHelpIndex(sql, request, response); } else if (uri.startsWith("/help/")) { help.doGetHelp(sql, request, response); + } else if (uri.startsWith("/tag/")) { + blogs.doGet(sql, sqlSearch, request, response); } else if (uri.matches("^/\\d+$")) { rootRedirects.doGetPostID(sql, request, response); } else if (uri.matches("^/[^/]+$")) { diff --git a/src/java/com/juick/http/www/NewMessage.java b/src/java/com/juick/http/www/NewMessage.java index 40ffeb46..9ca7e7ed 100644 --- a/src/java/com/juick/http/www/NewMessage.java +++ b/src/java/com/juick/http/www/NewMessage.java @@ -72,7 +72,7 @@ public class NewMessage { out.println(""); out.println("
"); out.println("

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

"); - printUserTags(sql, out, visitor.UID); + printUserTags(sql, out, visitor); out.println("
"); out.println("
"); @@ -82,8 +82,8 @@ public class NewMessage { } } - void printUserTags(Connection sql, PrintWriter out, int uid) { - ArrayList tags = TagQueries.getUserTagsAll(sql, uid); + void printUserTags(Connection sql, PrintWriter out, com.juick.User visitor) { + ArrayList tags = TagQueries.getUserTagsAll(sql, visitor.UID); if (tags.isEmpty()) { return; @@ -109,7 +109,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; diff --git a/src/java/com/juick/http/www/PageTemplates.java b/src/java/com/juick/http/www/PageTemplates.java index 5032cbf5..4e499344 100644 --- a/src/java/com/juick/http/www/PageTemplates.java +++ b/src/java/com/juick/http/www/PageTemplates.java @@ -179,14 +179,20 @@ public class PageTemplates { out.println(""); } - public static String formatTags(ArrayList tags) { + public static String formatTags(ArrayList tags, com.juick.User user) { String ret = ""; for (int i = 0; i < tags.size(); i++) { String tag = tags.get(i); tag = tag.replaceAll("<", "<"); tag = tag.replaceAll(">", ">"); try { - ret += " *" + tag + ""; + ret += " *" + tag + ""; } catch (UnsupportedEncodingException e) { } } @@ -304,7 +310,7 @@ public class PageTemplates { return msg; } - public static void printMessages(PrintWriter out, Connection sql, ArrayList mids, com.juick.User visitor, Locale locale) { + public static void printMessages(PrintWriter out, Connection sql, com.juick.User user, ArrayList mids, com.juick.User visitor, Locale locale) { ResourceBundle rb = ResourceBundle.getBundle("Global", locale); ArrayList msgs = MessagesQueries.getMessages(sql, mids); @@ -320,7 +326,7 @@ public class PageTemplates { for (int i = 0; i < msgs.size(); i++) { com.juick.Message msg = msgs.get(i); - String tags = msg.Tags.isEmpty() ? "" : formatTags(msg.Tags); + String tags = msg.Tags.isEmpty() ? "" : formatTags(msg.Tags, user); if (msg.ReadOnly) { tags += " *readonly"; } diff --git a/src/java/com/juick/http/www/Tags.java b/src/java/com/juick/http/www/Tags.java deleted file mode 100644 index 534ca8ef..00000000 --- a/src/java/com/juick/http/www/Tags.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package com.juick.http.www; - -import java.io.IOException; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.Locale; -import java.util.ResourceBundle; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** - * - * @author ugnich - */ -public class Tags { - - protected void doGetTags(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { - Locale locale = request.getLocale(); - ResourceBundle rbnm = ResourceBundle.getBundle("Blogs", locale); - - response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { - PageTemplates.pageHead(out, rbnm.getString("Tags"), null); - PageTemplates.pageNavigation(out, locale, visitor); - - int cnt = 30; - com.juick.Tag tags[] = new com.juick.Tag[cnt]; - int maxUsageCnt = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT tags.name AS name,COUNT(DISTINCT messages.user_id) AS cnt FROM (messages INNER JOIN messages_tags ON (messages.ts>TIMESTAMPADD(DAY,-3,NOW()) AND messages.message_id=messages_tags.message_id)) INNER JOIN tags ON messages_tags.tag_id=tags.tag_id WHERE tags.tag_id NOT IN (SELECT tag_id FROM tags_ignore) GROUP BY tags.tag_id ORDER BY cnt DESC LIMIT ?"); - stmt.setInt(1, cnt); - rs = stmt.executeQuery(); - rs.beforeFirst(); - cnt = 0; - while (rs.next()) { - tags[cnt] = new com.juick.Tag(); - tags[cnt].Name = rs.getString(1); - tags[cnt].UsageCnt = rs.getInt(2); - if (tags[cnt].UsageCnt > maxUsageCnt) { - maxUsageCnt = tags[cnt].UsageCnt; - } - cnt++; - } - } catch (SQLException e) { - System.err.println(e); - } 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) { - out.print("" + tag + " "); - } else if (tags[i].UsageCnt > maxUsageCnt / 3) { - out.print("" + tag + " "); - } else { - out.print(tag + " "); - } - } - - 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 d8c179b5..b60afd0e 100644 --- a/src/java/com/juick/http/www/User.java +++ b/src/java/com/juick/http/www/User.java @@ -106,7 +106,7 @@ public class User { out.println("
"); out.println("
    "); if (mids.size() > 0) { - PageTemplates.printMessages(out, sql, mids, visitor, locale); + PageTemplates.printMessages(out, sql, user, mids, visitor, locale); } out.println("
"); diff --git a/src/java/com/juick/http/www/UserThread.java b/src/java/com/juick/http/www/UserThread.java index c796767a..dcdb66da 100644 --- a/src/java/com/juick/http/www/UserThread.java +++ b/src/java/com/juick/http/www/UserThread.java @@ -72,7 +72,7 @@ public class UserThread { out.println("
"); out.println("
"); - com.juick.Message msg = printMessage(out, sql, MID, visitor, locale); + com.juick.Message msg = printMessage(out, sql, user, MID, visitor, locale); printReplies(out, sql, msg, visitor, locale, listview); out.println("
"); @@ -86,7 +86,7 @@ public class UserThread { } } - public static com.juick.Message printMessage(PrintWriter out, Connection sql, int mid, com.juick.User visitor, Locale locale) { + public static com.juick.Message printMessage(PrintWriter out, Connection sql, com.juick.User user, int mid, com.juick.User visitor, Locale locale) { ResourceBundle rb = ResourceBundle.getBundle("Global", locale); ResourceBundle rbthread = ResourceBundle.getBundle("UserThread", locale); @@ -94,7 +94,7 @@ public class UserThread { msg.VisitorCanComment = visitor != null; - String tags = (msg.Tags.isEmpty()) ? "" : PageTemplates.formatTags(msg.Tags); + String tags = (msg.Tags.isEmpty()) ? "" : PageTemplates.formatTags(msg.Tags, user); if (msg.ReadOnly) { tags += " *readonly"; msg.VisitorCanComment = false; -- cgit v1.2.3