From bd3892546b51f0d31e959d45c138b009d35b2692 Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Tue, 2 Feb 2016 14:36:04 +0300
Subject: spring-jdbc
---
src/main/java/com/juick/http/www/Discover.java | 5 +-
src/main/java/com/juick/http/www/Errors.java | 4 +-
.../java/com/juick/http/www/FacebookLogin.java | 81 +++-------
src/main/java/com/juick/http/www/Help.java | 20 +--
src/main/java/com/juick/http/www/Home.java | 8 +-
src/main/java/com/juick/http/www/Login.java | 39 ++---
src/main/java/com/juick/http/www/Main.java | 75 +++------
src/main/java/com/juick/http/www/NewMessage.java | 14 +-
src/main/java/com/juick/http/www/PM.java | 34 ++---
.../java/com/juick/http/www/PageTemplates.java | 125 ++++-----------
src/main/java/com/juick/http/www/RSS.java | 16 +-
src/main/java/com/juick/http/www/Settings.java | 31 ++--
src/main/java/com/juick/http/www/SignUp.java | 169 +++++----------------
src/main/java/com/juick/http/www/TwitterAuth.java | 9 +-
src/main/java/com/juick/http/www/User.java | 155 ++++++++-----------
src/main/java/com/juick/http/www/UserThread.java | 39 ++---
src/main/java/com/juick/http/www/Utils.java | 4 +-
.../java/com/juick/http/www/VKontakteLogin.java | 45 ++----
18 files changed, 294 insertions(+), 579 deletions(-)
(limited to 'src/main/java/com/juick/http')
diff --git a/src/main/java/com/juick/http/www/Discover.java b/src/main/java/com/juick/http/www/Discover.java
index e7d85d8a..779e6ba0 100644
--- a/src/main/java/com/juick/http/www/Discover.java
+++ b/src/main/java/com/juick/http/www/Discover.java
@@ -20,6 +20,8 @@ package com.juick.http.www;
import com.juick.server.AdsQueries;
import com.juick.server.MessagesQueries;
import com.juick.server.TagQueries;
+import org.springframework.jdbc.core.JdbcTemplate;
+
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
@@ -37,7 +39,8 @@ import javax.servlet.http.HttpServletResponse;
*/
public class Discover {
- protected void doGet(Connection sql, Connection sqlSearch, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ protected void doGet(JdbcTemplate sql, JdbcTemplate sqlSearch, HttpServletRequest request,
+ HttpServletResponse response) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
String paramTagStr = URLDecoder.decode(request.getRequestURI().substring(5), "UTF-8");
diff --git a/src/main/java/com/juick/http/www/Errors.java b/src/main/java/com/juick/http/www/Errors.java
index f65b6201..f8c347b7 100644
--- a/src/main/java/com/juick/http/www/Errors.java
+++ b/src/main/java/com/juick/http/www/Errors.java
@@ -1,5 +1,7 @@
package com.juick.http.www;
+import org.springframework.jdbc.core.JdbcTemplate;
+
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
@@ -15,7 +17,7 @@ public class Errors {
public static String tagsHTML = null;
- public static void doGet404(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ public static void doGet404(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
if (tagsHTML == null) {
diff --git a/src/main/java/com/juick/http/www/FacebookLogin.java b/src/main/java/com/juick/http/www/FacebookLogin.java
index fcb08d6c..61464f52 100644
--- a/src/main/java/com/juick/http/www/FacebookLogin.java
+++ b/src/main/java/com/juick/http/www/FacebookLogin.java
@@ -18,20 +18,19 @@
package com.juick.http.www;
import com.juick.server.UserQueries;
+import org.json.JSONObject;
+import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.jdbc.core.JdbcTemplate;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
-import javax.servlet.ServletException;
-import javax.servlet.http.Cookie;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import org.json.JSONObject;
/**
*
@@ -45,7 +44,8 @@ public class FacebookLogin {
private static final String FACEBOOK_SECRET = "95813bfb6ab8f473410c50d4f971649e";
private static final String FACEBOOK_REDIRECT = "http://juick.com/_fblogin";
- protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
String fbstate;
String code = request.getParameter("code");
@@ -132,61 +132,22 @@ public class FacebookLogin {
}
}
- private int getUIDbyFBID(Connection sql, long fbID) {
- int uid = 0;
- PreparedStatement stmt = null;
- ResultSet rs = null;
+ private int getUIDbyFBID(JdbcTemplate sql, long fbID) {
try {
- stmt = sql.prepareStatement("SELECT user_id FROM facebook WHERE fb_id=? AND user_id IS NOT NULL");
- stmt.setLong(1, fbID);
- rs = stmt.executeQuery();
- if (rs.first()) {
- uid = rs.getInt(1);
- }
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(rs, stmt);
+ return sql.queryForObject("SELECT user_id FROM facebook WHERE fb_id=? AND user_id IS NOT NULL",
+ Integer.class, fbID);
+ } catch (EmptyResultDataAccessException e) {
+ return 0;
}
- return uid;
}
- private boolean insertDB(Connection sql, long fbID, String loginhash, String token, String fbName, String fbLink) {
- boolean ret = false;
- PreparedStatement stmt = null;
- try {
- stmt = sql.prepareStatement("INSERT INTO facebook(fb_id,loginhash,access_token,fb_name,fb_link) VALUES (?,?,?,?,?)");
- stmt.setLong(1, fbID);
- stmt.setString(2, loginhash);
- stmt.setString(3, token);
- stmt.setString(4, fbName);
- stmt.setString(5, fbLink);
- stmt.executeUpdate();
- ret = true;
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(null, stmt);
- }
- return ret;
+ private boolean insertDB(JdbcTemplate sql, long fbID, String loginhash, String token, String fbName, String fbLink) {
+ return sql.update("INSERT INTO facebook(fb_id,loginhash,access_token,fb_name,fb_link) VALUES (?,?,?,?,?)",
+ fbID, loginhash, token, fbName, fbLink) > 0;
}
- private boolean updateDB(Connection sql, long fbID, String token, String fbName, String fbLink) {
- boolean ret = false;
- PreparedStatement stmt = null;
- try {
- stmt = sql.prepareStatement("UPDATE facebook SET access_token=?,fb_name=?,fb_link=? WHERE fb_id=?");
- stmt.setString(1, token);
- stmt.setString(2, fbName);
- stmt.setString(3, fbLink);
- stmt.setLong(4, fbID);
- stmt.executeUpdate();
- ret = true;
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(null, stmt);
- }
- return ret;
+ private boolean updateDB(JdbcTemplate sql, long fbID, String token, String fbName, String fbLink) {
+ return sql.update("UPDATE facebook SET access_token=?,fb_name=?,fb_link=? WHERE fb_id=?",
+ token, fbName, fbLink, fbID) > 0;
}
}
diff --git a/src/main/java/com/juick/http/www/Help.java b/src/main/java/com/juick/http/www/Help.java
index 438fc3a9..e8b7532e 100644
--- a/src/main/java/com/juick/http/www/Help.java
+++ b/src/main/java/com/juick/http/www/Help.java
@@ -17,15 +17,12 @@
*/
package com.juick.http.www;
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.sql.Connection;
+import org.springframework.jdbc.core.JdbcTemplate;
+
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import java.io.*;
/**
*
@@ -33,11 +30,13 @@ import javax.servlet.http.HttpServletResponse;
*/
public class Help {
- protected void doRedirectToHelpIndex(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ protected void doRedirectToHelpIndex(JdbcTemplate sql, HttpServletRequest request,
+ HttpServletResponse response) throws ServletException, IOException {
Utils.sendTemporaryRedirect(response, "/help/ru/");
}
- protected void doGetHelp(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ protected void doGetHelp(JdbcTemplate sql, HttpServletRequest request,
+ HttpServletResponse response) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
String path[] = request.getRequestURI().split("/");
@@ -64,8 +63,7 @@ public class Help {
}
response.setContentType("text/html; charset=UTF-8");
- PrintWriter out = response.getWriter();
- try {
+ try (PrintWriter out = response.getWriter()) {
PageTemplates.pageHead(out, "Помощь", null);
PageTemplates.pageNavigation(out, visitor, null);
@@ -81,8 +79,6 @@ public class Help {
PageTemplates.pageFooter(request, out, visitor, false);
PageTemplates.pageEnd(out);
- } finally {
- out.close();
}
}
diff --git a/src/main/java/com/juick/http/www/Home.java b/src/main/java/com/juick/http/www/Home.java
index 8dc3e70c..3688b58e 100644
--- a/src/main/java/com/juick/http/www/Home.java
+++ b/src/main/java/com/juick/http/www/Home.java
@@ -19,6 +19,8 @@ package com.juick.http.www;
import com.juick.server.AdsQueries;
import com.juick.server.MessagesQueries;
+import org.springframework.jdbc.core.JdbcTemplate;
+
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
@@ -35,7 +37,8 @@ import javax.servlet.http.HttpServletResponse;
*/
public class Home {
- protected void doGet(Connection sql, Connection sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException {
+ protected void doGet(JdbcTemplate sql, JdbcTemplate sqlSearch, HttpServletRequest request,
+ HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException {
int paramBefore = 0;
String paramBeforeStr = request.getParameter("before");
if (paramBeforeStr != null) {
@@ -107,7 +110,8 @@ public class Home {
}
PageTemplates.pageHead(out, title, head);
PageTemplates.pageNavigation(out, visitor, paramSearch);
- PageTemplates.pageHomeColumn(out, sql, visitor, paramShow == null && paramBefore == 0 && paramSearch == null && visitor == null);
+ PageTemplates.pageHomeColumn(out, sql, visitor, paramShow == null
+ && paramBefore == 0 && paramSearch == null && visitor == null);
out.println("GET
method.
@@ -168,8 +136,7 @@ public class Main extends HttpServlet implements Stream.StreamListener {
Utils.sendPermanentRedirect(response, "/tag/" + URLEncoder.encode(tag, "UTF-8"));
} else {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
- home.doGet(sql, getSqlSearch(), request, response, visitor);
- closeSqlSearch();
+ home.doGet(sql, sqlSearch, request, response, visitor);
}
} else if (uri.equals("/post")) {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
@@ -226,8 +193,7 @@ public class Main extends HttpServlet implements Stream.StreamListener {
} else if (uri.startsWith("/help/")) {
help.doGetHelp(sql, request, response);
} else if (uri.startsWith("/tag/")) {
- discover.doGet(sql, getSqlSearch(), request, response);
- closeSqlSearch();
+ discover.doGet(sql, sqlSearch, request, response);
} else if (uri.matches("^/\\d+$")) {
String strID = request.getRequestURI().substring(1);
int mid = 0;
@@ -255,8 +221,7 @@ public class Main extends HttpServlet implements Stream.StreamListener {
com.juick.User user = com.juick.server.UserQueries.getUserByName(sql, uriparts[1]);
if (user != null && user.getUName().equals(uriparts[1]) && !user.Banned) {
if (uriparts.length == 2) { // http://juick.com/username/
- pagesUser.doGetBlog(sql, getSqlSearch(), request, response, user);
- closeSqlSearch();
+ pagesUser.doGetBlog(sql, sqlSearch, request, response, user);
} else if (uriparts[2].equals("tags")) {
pagesUser.doGetTags(sql, request, response, user);
} else if (uriparts[2].equals("friends")) {
diff --git a/src/main/java/com/juick/http/www/NewMessage.java b/src/main/java/com/juick/http/www/NewMessage.java
index 86725db7..22d743b3 100644
--- a/src/main/java/com/juick/http/www/NewMessage.java
+++ b/src/main/java/com/juick/http/www/NewMessage.java
@@ -30,6 +30,8 @@ import com.juick.xmpp.extensions.JuickMessage;
import com.juick.xmpp.extensions.JuickUser;
import com.juick.xmpp.extensions.Nickname;
import com.juick.xmpp.extensions.XOOB;
+import org.springframework.jdbc.core.JdbcTemplate;
+
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
@@ -48,7 +50,7 @@ import javax.servlet.http.HttpServletResponse;
*/
public class NewMessage {
- protected void doGetNewMessage(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException {
+ protected void doGetNewMessage(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
try {
@@ -88,7 +90,7 @@ public class NewMessage {
}
}
- void printUserTags(Connection sql, PrintWriter out, com.juick.User visitor) {
+ void printUserTags(JdbcTemplate sql, PrintWriter out, com.juick.User visitor) {
List