aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/http/www/SignUp.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/http/www/SignUp.java')
-rw-r--r--src/main/java/com/juick/http/www/SignUp.java169
1 files changed, 42 insertions, 127 deletions
diff --git a/src/main/java/com/juick/http/www/SignUp.java b/src/main/java/com/juick/http/www/SignUp.java
index 2fae9b69..70ffad3d 100644
--- a/src/main/java/com/juick/http/www/SignUp.java
+++ b/src/main/java/com/juick/http/www/SignUp.java
@@ -18,16 +18,17 @@
package com.juick.http.www;
import com.juick.server.UserQueries;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
+import org.apache.commons.lang3.tuple.Pair;
+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.io.PrintWriter;
+import java.util.List;
/**
*
@@ -35,12 +36,14 @@ import javax.servlet.http.HttpServletResponse;
*/
public class SignUp {
- protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ protected void doGet(JdbcTemplate sql, HttpServletRequest request,
+ HttpServletResponse response) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
String type = request.getParameter("type");
String hash = request.getParameter("hash");
- if (type == null || type.isEmpty() || hash == null || hash.isEmpty() || hash.length() > 36 || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) {
+ if (type == null || type.isEmpty() || hash == null || hash.isEmpty() || hash.length() > 36
+ || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
@@ -59,8 +62,7 @@ public class SignUp {
}
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);
@@ -106,18 +108,18 @@ public class SignUp {
PageTemplates.pageFooter(request, out, visitor, false);
PageTemplates.pageEnd(out);
- } finally {
- out.close();
}
}
- protected void doPost(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ protected void doPost(JdbcTemplate sql, HttpServletRequest request,
+ HttpServletResponse response) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
int uid = 0;
String type = request.getParameter("type");
String hash = request.getParameter("hash");
- if (type == null || type.isEmpty() || hash == null || hash.isEmpty() || hash.length() > 36 || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) {
+ if (type == null || type.isEmpty() || hash == null || hash.isEmpty()
+ || hash.length() > 36 || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
@@ -152,7 +154,8 @@ public class SignUp {
} else { // Create new account
String username = request.getParameter("username");
String password = request.getParameter("password");
- if (username == null || password == null || username.length() < 2 || username.length() > 16 || !username.matches("^[a-zA-Z0-9\\-]+$") || password.length() < 6 || password.length() > 32) {
+ if (username == null || password == null || username.length() < 2 || username.length() > 16
+ || !username.matches("^[a-zA-Z0-9\\-]+$") || password.length() < 6 || password.length() > 32) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
@@ -198,134 +201,46 @@ public class SignUp {
response.sendRedirect("/");
}
- private boolean setUserRef(Connection sql, int uid, int ref) {
- boolean ret = false;
- PreparedStatement stmt = null;
- try {
- stmt = sql.prepareStatement("INSERT INTO users_refs(user_id,ref) VALUES (?,?)");
- stmt.setInt(1, uid);
- stmt.setInt(2, ref);
- stmt.executeUpdate();
- ret = true;
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(null, stmt);
- }
- return ret;
+ private boolean setUserRef(JdbcTemplate sql, int uid, int ref) {
+ return sql.update("INSERT INTO users_refs(user_id,ref) VALUES (?,?)", uid, ref) > 0;
}
- private String getFacebookNameByHash(Connection sql, String hash) {
- String ret = null;
-
- PreparedStatement stmt = null;
- ResultSet rs = null;
+ private String getFacebookNameByHash(JdbcTemplate sql, String hash) {
try {
- stmt = sql.prepareStatement("SELECT fb_name,fb_link FROM facebook WHERE loginhash=?");
- stmt.setString(1, hash);
- rs = stmt.executeQuery();
- if (rs.first()) {
- ret = "<a href=\"" + rs.getString(2) + "\" rel=\"nofollow\">" + rs.getString(1) + "</a>";
- }
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(rs, stmt);
+ return sql.queryForObject("SELECT fb_name,fb_link FROM facebook WHERE loginhash=?", String.class, hash);
+ } catch (EmptyResultDataAccessException e) {
+ return "";
}
-
- return ret;
}
- private boolean setFacebookUser(Connection sql, String hash, int uid) {
- boolean ret = false;
- PreparedStatement stmt = null;
- try {
- stmt = sql.prepareStatement("UPDATE facebook SET user_id=?,loginhash=NULL WHERE loginhash=?");
- stmt.setInt(1, uid);
- stmt.setString(2, hash);
- stmt.executeUpdate();
- ret = true;
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(null, stmt);
- }
- return ret;
+ private boolean setFacebookUser(JdbcTemplate sql, String hash, int uid) {
+ return sql.update("UPDATE facebook SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0;
}
- private String getVKNameByHash(Connection sql, String hash) {
- String ret = null;
-
- PreparedStatement stmt = null;
- ResultSet rs = null;
- try {
- stmt = sql.prepareStatement("SELECT vk_name,vk_link FROM vk WHERE loginhash=?");
- stmt.setString(1, hash);
- rs = stmt.executeQuery();
- if (rs.first()) {
- ret = "<a href=\"http://vk.com/" + rs.getString(2) + "\" rel=\"nofollow\">" + rs.getString(1) + "</a>";
- }
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(rs, stmt);
+ private String getVKNameByHash(JdbcTemplate sql, String hash) {
+ List<Pair<String, String>> logins = sql.query("SELECT vk_name,vk_link FROM vk WHERE loginhash=?",
+ (rs, num) -> {
+ return Pair.of(rs.getString(1), rs.getString(2));
+ }, hash);
+ if (logins.size() > 0) {
+ return "<a href=\"http://vk.com/" + logins.get(0).getRight() + "\" rel=\"nofollow\">" + logins.get(0).getLeft() + "</a>";
}
-
- return ret;
+ return "";
}
- private boolean setVKUser(Connection sql, String hash, int uid) {
- boolean ret = false;
- PreparedStatement stmt = null;
- try {
- stmt = sql.prepareStatement("UPDATE vk SET user_id=?,loginhash=NULL WHERE loginhash=?");
- stmt.setInt(1, uid);
- stmt.setString(2, hash);
- stmt.executeUpdate();
- ret = true;
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(null, stmt);
- }
- return ret;
+ private boolean setVKUser(JdbcTemplate sql, String hash, int uid) {
+ return sql.update("UPDATE vk SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0;
}
- private String getJIDByHash(Connection sql, String hash) {
- String ret = null;
-
- PreparedStatement stmt = null;
- ResultSet rs = null;
+ private String getJIDByHash(JdbcTemplate sql, String hash) {
try {
- stmt = sql.prepareStatement("SELECT jid FROM jids WHERE loginhash=?");
- stmt.setString(1, hash);
- rs = stmt.executeQuery();
- if (rs.first()) {
- ret = rs.getString(1);
- }
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(rs, stmt);
+ return sql.queryForObject("SELECT jid FROM jids WHERE loginhash=?", String.class, hash);
+ } catch (EmptyResultDataAccessException e) {
+ return "";
}
-
- return ret;
}
- private boolean setJIDUser(Connection sql, String hash, int uid) {
- boolean ret = false;
- PreparedStatement stmt = null;
- try {
- stmt = sql.prepareStatement("UPDATE jids SET user_id=?,loginhash=NULL WHERE loginhash=?");
- stmt.setInt(1, uid);
- stmt.setString(2, hash);
- stmt.executeUpdate();
- ret = true;
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(null, stmt);
- }
- return ret;
+ private boolean setJIDUser(JdbcTemplate sql, String hash, int uid) {
+ return sql.update("UPDATE jids SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0;
}
}