aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-01-15 12:51:21 +0300
committerGravatar Vitaly Takmazov2016-01-15 12:51:21 +0300
commit690b3ea447a7a2434ecf1d210e5f5ef89ce27f04 (patch)
tree2e96768580044b2144b38e423f0fe20f73f522d8 /src/main
parent5c207f65b0cafbaa0cbdae72a5a485cf7aa62556 (diff)
spring-jdbc WIP
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/com/juick/api/Main.java47
-rw-r--r--src/main/java/com/juick/api/Messages.java54
-rw-r--r--src/main/java/com/juick/api/Others.java5
-rw-r--r--src/main/java/com/juick/api/PM.java12
-rw-r--r--src/main/java/com/juick/api/Users.java6
-rw-r--r--src/main/java/com/juick/api/Utils.java7
6 files changed, 88 insertions, 43 deletions
diff --git a/src/main/java/com/juick/api/Main.java b/src/main/java/com/juick/api/Main.java
index 09dd4cbd..71f7f2d7 100644
--- a/src/main/java/com/juick/api/Main.java
+++ b/src/main/java/com/juick/api/Main.java
@@ -20,6 +20,9 @@ package com.juick.api;
import com.juick.xmpp.JID;
import com.juick.xmpp.Stream;
import com.juick.xmpp.StreamComponent;
+import org.apache.commons.dbcp2.BasicDataSource;
+import org.springframework.jdbc.core.JdbcTemplate;
+
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
@@ -28,7 +31,6 @@ import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
-import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
@@ -46,6 +48,7 @@ public class Main extends HttpServlet implements Stream.StreamListener {
Connection sql;
Connection sqlSearch;
+ JdbcTemplate jdbc;
Stream xmpp;
Messages messages;
Users users;
@@ -58,19 +61,22 @@ public class Main extends HttpServlet implements Stream.StreamListener {
try {
Properties conf = new Properties();
conf.load(new FileInputStream("/etc/juick/api.conf"));
-
- Class.forName("com.mysql.jdbc.Driver");
+ final String driverClassName = "com.mysql.jdbc.Driver";
+ Class.forName(driverClassName);
sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", ""));
sqlSearch = DriverManager.getConnection("jdbc:mysql://127.0.0.1:9306/juick?autoReconnect=true&characterEncoding=utf8&maxAllowedPacket=512000&relaxAutoCommit=true&user=root&password=");
-
- messages = new Messages(sql);
- users = new Users(sql);
- pm = new PM(sql);
- others = new Others(sql);
+ BasicDataSource dataSource = new BasicDataSource();
+ dataSource.setUrl(sql.getMetaData().getURL());
+ dataSource.setDriverClassName(driverClassName);
+ jdbc = new JdbcTemplate(dataSource);
+ messages = new Messages(jdbc);
+ users = new Users(jdbc);
+ pm = new PM(jdbc);
+ others = new Others(jdbc);
setupXmppComponent(conf.getProperty("xmpp_password"));
- } catch (Exception e) {
+ } catch (IOException | ClassNotFoundException | SQLException e) {
log(null, e);
}
}
@@ -135,7 +141,7 @@ public class Main extends HttpServlet implements Stream.StreamListener {
int vuid = Utils.getHttpAuthUID(sql, request);
if (vuid == 0) {
- vuid = Utils.getVisitorQueryStringUID(sql, request);
+ vuid = Utils.getVisitorQueryStringUID(jdbc, request);
}
String uri = request.getRequestURI();
@@ -193,7 +199,7 @@ public class Main extends HttpServlet implements Stream.StreamListener {
int vuid = Utils.getHttpAuthUID(sql, request);
if (vuid == 0) {
- vuid = Utils.getVisitorQueryStringUID(sql, request);
+ vuid = Utils.getVisitorQueryStringUID(jdbc, request);
}
if (vuid == 0) {
response.sendError(401);
@@ -201,11 +207,15 @@ public class Main extends HttpServlet implements Stream.StreamListener {
}
String uri = request.getRequestURI();
- if (uri.equals("/post")) {
- } else if (uri.equals("/pm")) {
- pm.doPostPM(request, response, xmpp, vuid);
- } else {
- response.sendError(405);
+ switch (uri) {
+ case "/post":
+ break;
+ case "/pm":
+ pm.doPostPM(request, response, xmpp, vuid);
+ break;
+ default:
+ response.sendError(405);
+ break;
}
}
@@ -218,8 +228,7 @@ public class Main extends HttpServlet implements Stream.StreamListener {
callback = null;
}
- PrintWriter out = response.getWriter();
- try {
+ try (PrintWriter out = response.getWriter()) {
if (callback != null) {
out.print(callback + "(");
out.print(json);
@@ -227,8 +236,6 @@ public class Main extends HttpServlet implements Stream.StreamListener {
} else {
out.print(json);
}
- } finally {
- out.close();
}
}
}
diff --git a/src/main/java/com/juick/api/Messages.java b/src/main/java/com/juick/api/Messages.java
index cde3987f..e3dcabfd 100644
--- a/src/main/java/com/juick/api/Messages.java
+++ b/src/main/java/com/juick/api/Messages.java
@@ -1,17 +1,21 @@
package com.juick.api;
+import com.juick.Tag;
+import com.juick.User;
import com.juick.json.MessageSerializer;
import com.juick.server.MessagesQueries;
+import com.juick.server.TagQueries;
+import com.juick.server.UserQueries;
import com.juick.xmpp.JID;
import com.juick.xmpp.Message;
import com.juick.xmpp.Stream;
import com.juick.xmpp.extensions.JuickMessage;
+import org.springframework.jdbc.core.JdbcTemplate;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
-import java.sql.Connection;
import java.util.List;
/**
@@ -20,11 +24,32 @@ import java.util.List;
*/
public class Messages {
- Connection sql;
+ JdbcTemplate sql;
MessageSerializer messageSerializer = new MessageSerializer();
- public Messages(Connection sql) {
+ enum MediaType {
+ ALL("all"),
+ PHOTO("photo"),
+ VIDEO("video"),
+ NONE("");
+
+ private final String value;
+
+ MediaType(final String value) {
+ this.value = value;
+ }
+ public String getValue() {
+ return value;
+ }
+
+ @Override
+ public String toString() {
+ return this.getValue();
+ }
+ }
+
+ public Messages(JdbcTemplate sql) {
this.sql = sql;
}
@@ -45,8 +70,19 @@ public class Messages {
}
}
- public void doGetRecommended(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException {
+ public void doGet(HttpServletRequest request, HttpServletResponse response, int vuid)
+ throws ServletException, IOException {
+ User user = UserQueries.getUserByName(sql, request.getParameter("uname"));
int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0);
+ int page = Utils.parseInt(request.getParameter("page"), 0);
+ MediaType media = MediaType.valueOf(request.getParameter("media"));
+ boolean popular = Boolean.valueOf(request.getParameter("popular"));
+ Tag tag = TagQueries.getTag(sql, request.getParameter("tag"), false);
+
+ }
+
+ public void doGetRecommended(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException {
+ /*int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0);
List<Integer> mids = MessagesQueries.getUserRecommendations(sql, vuid, before_mid);
if (mids != null && !mids.isEmpty()) {
@@ -59,21 +95,21 @@ public class Messages {
}
} else {
response.sendError(404);
- }
+ }*/
}
public void doSetPrivacy(HttpServletRequest request, HttpServletResponse response, Stream xmpp, int vuid) throws ServletException, IOException {
- int mid = Utils.parseInt(request.getParameter("mid"), 0);
+ /*int mid = Utils.parseInt(request.getParameter("mid"), 0);
com.juick.User user = MessagesQueries.getMessageAuthor(sql, mid);
if (user != null && user.UID == vuid && MessagesQueries.setMessagePrivacy(sql, mid)) {
Main.replyJSON(request, response, "{\"status\":\"ok\"}");
} else {
response.sendError(400);
- }
+ }*/
}
public void doSetPopular(HttpServletRequest request, HttpServletResponse response, Stream xmpp) throws ServletException, IOException {
- int mid = Utils.parseInt(request.getParameter("mid"), 0);
+ /*int mid = Utils.parseInt(request.getParameter("mid"), 0);
int popular = Utils.parseInt(request.getParameter("popular"), 0);
if (mid > 0) {
@@ -105,6 +141,6 @@ public class Messages {
}
Main.replyJSON(request, response, "{\"status\":\"ok\"}");
- }
+ }*/
}
}
diff --git a/src/main/java/com/juick/api/Others.java b/src/main/java/com/juick/api/Others.java
index 323ed41a..3c8d4a96 100644
--- a/src/main/java/com/juick/api/Others.java
+++ b/src/main/java/com/juick/api/Others.java
@@ -3,6 +3,7 @@ package com.juick.api;
import com.juick.User;
import com.juick.json.UserSerializer;
import com.juick.server.PMQueries;
+import org.springframework.jdbc.core.JdbcTemplate;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@@ -17,11 +18,11 @@ import java.util.List;
*/
public class Others {
- Connection sql;
+ JdbcTemplate sql;
UserSerializer userSerializer = new UserSerializer();
- public Others(Connection sql) {
+ public Others(JdbcTemplate sql) {
this.sql = sql;
}
diff --git a/src/main/java/com/juick/api/PM.java b/src/main/java/com/juick/api/PM.java
index f22a4add..01a5669a 100644
--- a/src/main/java/com/juick/api/PM.java
+++ b/src/main/java/com/juick/api/PM.java
@@ -7,9 +7,9 @@ import com.juick.xmpp.JID;
import com.juick.xmpp.Message;
import com.juick.xmpp.Stream;
import com.juick.xmpp.extensions.JuickMessage;
+import org.springframework.jdbc.core.JdbcTemplate;
+
import java.io.IOException;
-import java.sql.Connection;
-import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@@ -21,11 +21,11 @@ import javax.servlet.http.HttpServletResponse;
*/
public class PM {
- Connection sql;
-
- MessageSerializer messageSerializer = new MessageSerializer();
+ JdbcTemplate sql;
+
+ MessageSerializer messageSerializer = new MessageSerializer();
- public PM(Connection sql) {
+ public PM(JdbcTemplate sql) {
this.sql = sql;
}
diff --git a/src/main/java/com/juick/api/Users.java b/src/main/java/com/juick/api/Users.java
index 5bae4028..d3818a5a 100644
--- a/src/main/java/com/juick/api/Users.java
+++ b/src/main/java/com/juick/api/Users.java
@@ -2,12 +2,12 @@ package com.juick.api;
import com.juick.json.UserSerializer;
import com.juick.server.UserQueries;
+import org.springframework.jdbc.core.JdbcTemplate;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
-import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@@ -19,11 +19,11 @@ import java.util.List;
*/
public class Users {
- Connection sql;
+ JdbcTemplate sql;
UserSerializer userSerializer = new UserSerializer();
- public Users(Connection sql) {
+ public Users(JdbcTemplate sql) {
this.sql = sql;
}
diff --git a/src/main/java/com/juick/api/Utils.java b/src/main/java/com/juick/api/Utils.java
index 10e0cccb..db73fdaa 100644
--- a/src/main/java/com/juick/api/Utils.java
+++ b/src/main/java/com/juick/api/Utils.java
@@ -27,6 +27,7 @@ import java.util.ArrayList;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.springframework.jdbc.core.JdbcTemplate;
import sun.misc.BASE64Decoder;
/**
@@ -47,7 +48,7 @@ public class Utils {
return null;
}
- public static com.juick.User getVisitorUser(Connection sql, HttpServletRequest request) {
+ public static com.juick.User getVisitorUser(JdbcTemplate sql, HttpServletRequest request) {
String hash = getCookie(request, "hash");
if (hash != null) {
return com.juick.server.UserQueries.getUserByHash(sql, hash);
@@ -56,7 +57,7 @@ public class Utils {
}
}
- public static int getVisitorUID(Connection sql, HttpServletRequest request) {
+ public static int getVisitorUID(JdbcTemplate sql, HttpServletRequest request) {
Cookie cookies[] = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
@@ -84,7 +85,7 @@ public class Utils {
return 0;
}
- public static int getVisitorQueryStringUID(Connection sql, HttpServletRequest request) {
+ public static int getVisitorQueryStringUID(JdbcTemplate sql, HttpServletRequest request) {
String hash = request.getParameter("hash");
if (hash != null && hash.length() == 16) {
return com.juick.server.UserQueries.getUIDbyHash(sql, hash);