From f4a45264cb8f0cfeacdb89ce9bf4c60474923f80 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sat, 14 Dec 2013 00:43:10 +0700 Subject: getVisitorUser sent X-Username header --- src/java/com/juick/http/www/Discover.java | 2 +- src/java/com/juick/http/www/Help.java | 2 +- src/java/com/juick/http/www/Login.java | 8 ++++---- src/java/com/juick/http/www/Main.java | 4 ++-- src/java/com/juick/http/www/Settings.java | 2 +- src/java/com/juick/http/www/SignUp.java | 4 ++-- src/java/com/juick/http/www/User.java | 8 ++++---- src/java/com/juick/http/www/UserThread.java | 2 +- src/java/com/juick/http/www/Utils.java | 21 ++++++--------------- 9 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/java/com/juick/http/www/Discover.java b/src/java/com/juick/http/www/Discover.java index dd871df4..b24164f5 100644 --- a/src/java/com/juick/http/www/Discover.java +++ b/src/java/com/juick/http/www/Discover.java @@ -46,7 +46,7 @@ public class Discover { private String tagsHTML = null; protected void doGet(Connection sql, Connection sqlSearch, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request); + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); Locale locale = request.getLocale(); ResourceBundle rb = ResourceBundle.getBundle("Blogs", locale); diff --git a/src/java/com/juick/http/www/Help.java b/src/java/com/juick/http/www/Help.java index 7adcb920..028539df 100644 --- a/src/java/com/juick/http/www/Help.java +++ b/src/java/com/juick/http/www/Help.java @@ -44,7 +44,7 @@ public class Help { } protected void doGetHelp(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request); + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); Locale locale = request.getLocale(); ResourceBundle rb = ResourceBundle.getBundle("Global", locale); diff --git a/src/java/com/juick/http/www/Login.java b/src/java/com/juick/http/www/Login.java index 329cb767..954d21c9 100644 --- a/src/java/com/juick/http/www/Login.java +++ b/src/java/com/juick/http/www/Login.java @@ -36,7 +36,7 @@ import javax.servlet.http.HttpServletResponse; public class Login { protected void doGetLoginForm(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request); + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor != null) { Utils.sendTemporaryRedirect(response, "/"); return; @@ -244,12 +244,12 @@ public class Login { } protected void doGetLogout(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - int uid = Utils.getVisitorUID(sql, request); - if (uid > 0) { + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + if (visitor != null) { PreparedStatement stmt = null; try { stmt = sql.prepareStatement("DELETE FROM logins WHERE user_id=?"); - stmt.setInt(1, uid); + stmt.setInt(1, visitor.UID); stmt.executeUpdate(); } catch (SQLException e) { System.err.println(e); diff --git a/src/java/com/juick/http/www/Main.java b/src/java/com/juick/http/www/Main.java index 249ae53c..c7295ce6 100644 --- a/src/java/com/juick/http/www/Main.java +++ b/src/java/com/juick/http/www/Main.java @@ -111,14 +111,14 @@ public class Main extends HttpServlet { String uri = request.getRequestURI(); if (uri.equals("/")) { - com.juick.User visitor = Utils.getVisitorUser(sql, request); + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor == null) { login.doGetLoginForm(sql, request, response); } else { home.doGet(sql, sqlSearch, request, response, visitor); } } else if (uri.equals("/post")) { - com.juick.User visitor = Utils.getVisitorUser(sql, request); + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor != null) { pagesNewMessage.doGetNewMessage(sql, request, response, visitor); } else { diff --git a/src/java/com/juick/http/www/Settings.java b/src/java/com/juick/http/www/Settings.java index 7ae0259d..5273507e 100644 --- a/src/java/com/juick/http/www/Settings.java +++ b/src/java/com/juick/http/www/Settings.java @@ -36,7 +36,7 @@ import javax.servlet.http.HttpServletResponse; public class Settings { protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request); + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); Locale locale = request.getLocale(); ResourceBundle rb = ResourceBundle.getBundle("Login", locale); diff --git a/src/java/com/juick/http/www/SignUp.java b/src/java/com/juick/http/www/SignUp.java index 3761c8d3..b5780676 100644 --- a/src/java/com/juick/http/www/SignUp.java +++ b/src/java/com/juick/http/www/SignUp.java @@ -38,7 +38,7 @@ import javax.servlet.http.HttpServletResponse; public class SignUp { protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request); + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); Locale locale = request.getLocale(); ResourceBundle rb = ResourceBundle.getBundle("SignUp", locale); @@ -126,7 +126,7 @@ public class SignUp { } protected void doPost(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request); + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); int uid = 0; String type = request.getParameter("type"); diff --git a/src/java/com/juick/http/www/User.java b/src/java/com/juick/http/www/User.java index 38910b01..94244dca 100644 --- a/src/java/com/juick/http/www/User.java +++ b/src/java/com/juick/http/www/User.java @@ -43,7 +43,7 @@ import javax.servlet.http.HttpServletResponse; 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); + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); Locale locale = request.getLocale(); ResourceBundle rb = ResourceBundle.getBundle("User", locale); @@ -155,7 +155,7 @@ public class User { } protected void doGetTags(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request); + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); Locale locale = request.getLocale(); ResourceBundle rb = ResourceBundle.getBundle("User", locale); @@ -184,7 +184,7 @@ public class User { } protected void doGetFriends(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request); + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); Locale locale = request.getLocale(); ResourceBundle rb = ResourceBundle.getBundle("User", locale); @@ -236,7 +236,7 @@ public class User { } protected void doGetReaders(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request); + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); Locale locale = request.getLocale(); ResourceBundle rb = ResourceBundle.getBundle("User", locale); diff --git a/src/java/com/juick/http/www/UserThread.java b/src/java/com/juick/http/www/UserThread.java index 925e9f02..c37ead00 100644 --- a/src/java/com/juick/http/www/UserThread.java +++ b/src/java/com/juick/http/www/UserThread.java @@ -36,7 +36,7 @@ import javax.servlet.http.HttpServletResponse; 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); + com.juick.User visitor = Utils.getVisitorUser(sql, request, response); Locale locale = request.getLocale(); if (!MessagesQueries.canViewThread(sql, MID, visitor != null ? visitor.UID : 0)) { diff --git a/src/java/com/juick/http/www/Utils.java b/src/java/com/juick/http/www/Utils.java index b2d9a999..35b918b8 100644 --- a/src/java/com/juick/http/www/Utils.java +++ b/src/java/com/juick/http/www/Utils.java @@ -48,28 +48,19 @@ public class Utils { return null; } - public static com.juick.User getVisitorUser(Connection sql, HttpServletRequest request) { + public static com.juick.User getVisitorUser(Connection sql, HttpServletRequest request, HttpServletResponse response) { String hash = getCookie(request, "hash"); if (hash != null) { - return com.juick.server.UserQueries.getUserByHash(sql, hash); + com.juick.User visitor = com.juick.server.UserQueries.getUserByHash(sql, hash); + if (response != null && visitor != null) { + response.setHeader("X-Username", visitor.UName); + } + return visitor; } else { return null; } } - public static int getVisitorUID(Connection sql, HttpServletRequest request) { - Cookie cookies[] = request.getCookies(); - if (cookies != null) { - for (int i = 0; i < cookies.length; i++) { - if (cookies[i].getName().equals("hash")) { - String hash = cookies[i].getValue(); - return com.juick.server.UserQueries.getUIDbyHash(sql, hash); - } - } - } - return 0; - } - public static void sendTemporaryRedirect(HttpServletResponse response, String location) { response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); response.setHeader("Location", location); -- cgit v1.2.3