aboutsummaryrefslogtreecommitdiff
path: root/src/org
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-05-10 18:11:40 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-05-10 18:11:40 +1200
commitbd09a39f12c4471bdc342e5a16fbabdffdc645d1 (patch)
treedb94476ee604c2026c5653975c526352093d5628 /src/org
parent953852e6e984f8a639cf1a5ac4116fff755d538a (diff)
downloadtraccar-server-bd09a39f12c4471bdc342e5a16fbabdffdc645d1.tar.gz
traccar-server-bd09a39f12c4471bdc342e5a16fbabdffdc645d1.tar.bz2
traccar-server-bd09a39f12c4471bdc342e5a16fbabdffdc645d1.zip
Refactor web app architecture
Diffstat (limited to 'src/org')
-rw-r--r--src/org/traccar/Context.java11
-rw-r--r--src/org/traccar/database/DataManager.java38
-rw-r--r--src/org/traccar/http/AsyncServlet.java2
-rw-r--r--src/org/traccar/http/BaseServlet.java13
-rw-r--r--src/org/traccar/http/DeviceServlet.java6
-rw-r--r--src/org/traccar/http/JsonConverter.java5
-rw-r--r--src/org/traccar/http/MainServlet.java24
7 files changed, 56 insertions, 43 deletions
diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java
index ca45a34d6..dcab28a9c 100644
--- a/src/org/traccar/Context.java
+++ b/src/org/traccar/Context.java
@@ -15,8 +15,11 @@
*/
package org.traccar;
+import java.io.FileInputStream;
+import java.util.Properties;
import org.traccar.database.DataCache;
import org.traccar.database.DataManager;
+import org.traccar.database.PermissionsManager;
import org.traccar.geocode.GisgraphyReverseGeocoder;
import org.traccar.geocode.GoogleReverseGeocoder;
import org.traccar.geocode.NominatimReverseGeocoder;
@@ -24,10 +27,6 @@ import org.traccar.geocode.ReverseGeocoder;
import org.traccar.helper.Log;
import org.traccar.http.WebServer;
-import java.io.FileInputStream;
-import java.util.Properties;
-import org.traccar.database.PermissionsManager;
-
public class Context {
private static Properties properties;
@@ -92,7 +91,9 @@ public class Context {
dataManager = new DataManager(properties);
dataCache = new DataCache(dataManager);
- permissionsManager = new PermissionsManager();
+ if (Boolean.valueOf(properties.getProperty("http.new"))) {
+ permissionsManager = new PermissionsManager();
+ }
if (Boolean.parseBoolean(properties.getProperty("geocoder.enable"))) {
String type = properties.getProperty("geocoder.type");
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index 3a34be2d7..5e921c0ea 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -22,7 +22,6 @@ import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
-import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
@@ -307,28 +306,27 @@ public class DataManager {
admin.setName("admin");
admin.setEmail("admin");
admin.setPassword("admin");
+ admin.setAdmin(true);
addUser(admin);
+
+ Device device = new Device();
+ device.setName("test1");
+ device.setUniqueId("123456789012345");
+ addDevice(device);
+ linkDevice(admin.getId(), device.getId());
}
- public long login(String email, String password) throws SQLException {
-
- Connection connection = dataSource.getConnection();
- try {
- PreparedStatement statement = connection.prepareStatement(
- "SELECT id FROM user WHERE email = ? AND " +
- "password = CAST(HASH('SHA256', STRINGTOUTF8(?), 1000) AS VARCHAR);");
- try {
- statement.setString(1, email);
- statement.setString(2, password);
-
- ResultSet result = statement.executeQuery();
- result.next();
- return result.getLong("id");
- } finally {
- statement.close();
- }
- } finally {
- connection.close();
+ public User login(String email, String password) throws SQLException {
+ Collection<User> result = QueryBuilder.create(dataSource,
+ "SELECT * FROM user WHERE email = :email AND " +
+ "password = CAST(HASH('SHA256', STRINGTOUTF8(:password), 1000) AS VARCHAR);")
+ .setString("email", email)
+ .setString("password", password)
+ .executeQuery(new User());
+ if (!result.isEmpty()) {
+ return result.iterator().next();
+ } else {
+ return null;
}
}
diff --git a/src/org/traccar/http/AsyncServlet.java b/src/org/traccar/http/AsyncServlet.java
index f00257f80..2bc6ff7a1 100644
--- a/src/org/traccar/http/AsyncServlet.java
+++ b/src/org/traccar/http/AsyncServlet.java
@@ -182,7 +182,7 @@ public class AsyncServlet extends HttpServlet {
context.setTimeout(ASYNC_TIMEOUT);
HttpServletRequest req = (HttpServletRequest) context.getRequest();
- long userId = (Long) req.getSession().getAttribute(MainServlet.USER_ID);
+ long userId = (Long) req.getSession().getAttribute(MainServlet.USER_KEY);
synchronized (asyncSessions) {
diff --git a/src/org/traccar/http/BaseServlet.java b/src/org/traccar/http/BaseServlet.java
index 9249b33d8..122dbe70b 100644
--- a/src/org/traccar/http/BaseServlet.java
+++ b/src/org/traccar/http/BaseServlet.java
@@ -26,10 +26,11 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
+import org.traccar.model.User;
public abstract class BaseServlet extends HttpServlet {
- public static final String USER_ID = "userId";
+ public static final String USER_KEY = "user";
@Override
protected final void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
@@ -46,16 +47,16 @@ public abstract class BaseServlet extends HttpServlet {
protected abstract boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception;
public long getUserId(HttpSession session) {
- Long userId = (Long) session.getAttribute(USER_ID);
- if (userId == null) {
+ User user = (User) session.getAttribute(USER_KEY);
+ if (user == null) {
throw new AccessControlException("User is not logged in");
}
- return userId;
+ return user.getId();
}
- public void sendResponse(Writer writer) throws IOException {
+ public void sendResponse(Writer writer, boolean success) throws IOException {
JsonObjectBuilder result = Json.createObjectBuilder();
- result.add("success", true);
+ result.add("success", success);
writer.write(result.build().toString());
}
diff --git a/src/org/traccar/http/DeviceServlet.java b/src/org/traccar/http/DeviceServlet.java
index a06ea5368..1387c2a13 100644
--- a/src/org/traccar/http/DeviceServlet.java
+++ b/src/org/traccar/http/DeviceServlet.java
@@ -47,19 +47,19 @@ public class DeviceServlet extends BaseServlet {
Device device = JsonConverter.objectFromJson(req.getReader(), new Device());
Context.getDataManager().addDevice(device);
Context.getDataManager().linkDevice(getUserId(req.getSession()), device.getId());
- sendResponse(resp.getWriter());
+ sendResponse(resp.getWriter(), JsonConverter.objectToJson(device));
}
private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Context.getDataManager().updateDevice(JsonConverter.objectFromJson(
req.getReader(), new Device()));
- sendResponse(resp.getWriter());
+ sendResponse(resp.getWriter(), true);
}
private void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Context.getDataManager().removeDevice(JsonConverter.objectFromJson(
req.getReader(), new Device()));
- sendResponse(resp.getWriter());
+ sendResponse(resp.getWriter(), true);
}
}
diff --git a/src/org/traccar/http/JsonConverter.java b/src/org/traccar/http/JsonConverter.java
index 0802fb9ea..f7c5a5622 100644
--- a/src/org/traccar/http/JsonConverter.java
+++ b/src/org/traccar/http/JsonConverter.java
@@ -29,6 +29,7 @@ import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
+import javax.json.JsonValue;
import org.traccar.model.Factory;
public class JsonConverter {
@@ -57,7 +58,9 @@ public class JsonConverter {
} else if (parameterType.equals(int.class)) {
method.invoke(object, json.getJsonNumber(name).intValue());
} else if (parameterType.equals(long.class)) {
- method.invoke(object, json.getJsonNumber(name).longValue());
+ if (json.get(name).getValueType() == JsonValue.ValueType.NUMBER) {
+ method.invoke(object, json.getJsonNumber(name).longValue());
+ }
} else if (parameterType.equals(double.class)) {
method.invoke(object, json.getJsonNumber(name).doubleValue());
} else if (parameterType.equals(String.class)) {
diff --git a/src/org/traccar/http/MainServlet.java b/src/org/traccar/http/MainServlet.java
index 036c472f8..18430f0c3 100644
--- a/src/org/traccar/http/MainServlet.java
+++ b/src/org/traccar/http/MainServlet.java
@@ -41,24 +41,34 @@ public class MainServlet extends BaseServlet {
}
private void session(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- resp.getWriter().println("{ success: true, session: " + (req.getSession().getAttribute(USER_ID) != null) + " }");
+ User user = (User) req.getSession().getAttribute(USER_KEY);
+ if (user != null) {
+ sendResponse(resp.getWriter(), JsonConverter.objectToJson(user));
+ } else {
+ sendResponse(resp.getWriter(), false);
+ }
}
private void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
- req.getSession().setAttribute(USER_ID, Context.getDataManager().login(
- req.getParameter("email"), req.getParameter("password")));
- sendResponse(resp.getWriter());
+ User user = Context.getDataManager().login(
+ req.getParameter("email"), req.getParameter("password"));
+ if (user != null) {
+ req.getSession().setAttribute(USER_KEY, user);
+ sendResponse(resp.getWriter(), JsonConverter.objectToJson(user));
+ } else {
+ sendResponse(resp.getWriter(), false);
+ }
}
private void logout(HttpServletRequest req, HttpServletResponse resp) throws Exception {
- req.getSession().removeAttribute(USER_ID);
- sendResponse(resp.getWriter());
+ req.getSession().removeAttribute(USER_KEY);
+ sendResponse(resp.getWriter(), true);
}
private void register(HttpServletRequest req, HttpServletResponse resp) throws Exception {
User user = JsonConverter.objectFromJson(req.getReader(), new User());
Context.getDataManager().addUser(user);
- sendResponse(resp.getWriter());
+ sendResponse(resp.getWriter(), true);
}
}