aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/api/Main.java
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/java/com/juick/api/Main.java
parent5c207f65b0cafbaa0cbdae72a5a485cf7aa62556 (diff)
spring-jdbc WIP
Diffstat (limited to 'src/main/java/com/juick/api/Main.java')
-rw-r--r--src/main/java/com/juick/api/Main.java47
1 files changed, 27 insertions, 20 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();
}
}
}