aboutsummaryrefslogtreecommitdiff
path: root/src/org
diff options
context:
space:
mode:
Diffstat (limited to 'src/org')
-rw-r--r--src/org/traccar/database/DataManager.java19
-rw-r--r--src/org/traccar/http/AsyncServlet.java26
-rw-r--r--src/org/traccar/http/BaseServlet.java76
-rw-r--r--src/org/traccar/http/DeviceServlet.java115
-rw-r--r--src/org/traccar/http/JsonConverter.java2
-rw-r--r--src/org/traccar/http/MainServlet.java48
6 files changed, 137 insertions, 149 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index 5473c6c77..535365af0 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -19,9 +19,20 @@ import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
-import java.sql.*;
-import java.util.*;
-import javax.json.JsonArray;
+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;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
import javax.sql.DataSource;
import org.traccar.helper.DriverDelegate;
import org.traccar.model.Device;
@@ -203,7 +214,7 @@ public class DataManager {
break;
}
}
- if (!exist) {
+ if (exist) {
return;
}
diff --git a/src/org/traccar/http/AsyncServlet.java b/src/org/traccar/http/AsyncServlet.java
index 927e93c78..502c551cb 100644
--- a/src/org/traccar/http/AsyncServlet.java
+++ b/src/org/traccar/http/AsyncServlet.java
@@ -15,14 +15,11 @@
*/
package org.traccar.http;
-import org.jboss.netty.util.Timeout;
-import org.jboss.netty.util.TimerTask;
-import org.traccar.Context;
-import org.traccar.GlobalTimer;
-import org.traccar.database.DataCache;
-import org.traccar.helper.Log;
-import org.traccar.model.Position;
-
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
import javax.json.Json;
import javax.json.JsonObjectBuilder;
import javax.servlet.AsyncContext;
@@ -31,12 +28,13 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.sql.SQLException;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.TimeUnit;
+import org.jboss.netty.util.Timeout;
+import org.jboss.netty.util.TimerTask;
+import org.traccar.Context;
+import org.traccar.GlobalTimer;
+import org.traccar.database.DataCache;
+import org.traccar.helper.Log;
+import org.traccar.model.Position;
public class AsyncServlet extends HttpServlet {
diff --git a/src/org/traccar/http/BaseServlet.java b/src/org/traccar/http/BaseServlet.java
new file mode 100644
index 000000000..9249b33d8
--- /dev/null
+++ b/src/org/traccar/http/BaseServlet.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.http;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.security.AccessControlException;
+import javax.json.Json;
+import javax.json.JsonObjectBuilder;
+import javax.json.JsonStructure;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+public abstract class BaseServlet extends HttpServlet {
+
+ public static final String USER_ID = "userId";
+
+ @Override
+ protected final void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ String command = req.getPathInfo();
+ try {
+ if (command == null || !handle(command, req, resp)) {
+ resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
+ }
+ } catch (Exception error) {
+ sendResponse(resp.getWriter(), error);
+ }
+ }
+
+ 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) {
+ throw new AccessControlException("User is not logged in");
+ }
+ return userId;
+ }
+
+ public void sendResponse(Writer writer) throws IOException {
+ JsonObjectBuilder result = Json.createObjectBuilder();
+ result.add("success", true);
+ writer.write(result.build().toString());
+ }
+
+ public void sendResponse(Writer writer, JsonStructure json) throws IOException {
+ JsonObjectBuilder result = Json.createObjectBuilder();
+ result.add("success", true);
+ result.add("data", json);
+ writer.write(result.build().toString());
+ }
+
+ public void sendResponse(Writer writer, Exception error) throws IOException {
+ JsonObjectBuilder result = Json.createObjectBuilder();
+ result.add("success", false);
+ result.add("error", error.getMessage());
+ writer.write(result.build().toString());
+ }
+
+}
diff --git a/src/org/traccar/http/DeviceServlet.java b/src/org/traccar/http/DeviceServlet.java
index 81ec168d5..a06ea5368 100644
--- a/src/org/traccar/http/DeviceServlet.java
+++ b/src/org/traccar/http/DeviceServlet.java
@@ -15,28 +15,16 @@
*/
package org.traccar.http;
-import java.io.IOException;
-import java.sql.SQLException;
-import java.text.ParseException;
-import javax.json.Json;
-import javax.json.JsonObjectBuilder;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.traccar.Context;
import org.traccar.model.Device;
-public class DeviceServlet extends HttpServlet {
-
- @Override
- protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-
- String command = req.getPathInfo();
+public class DeviceServlet extends BaseServlet {
- if (command == null) {
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
- } else if (command.equals("/get")) {
+ @Override
+ protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ if (command.equals("/get")) {
get(req, resp);
} else if (command.equals("/add")) {
add(req, resp);
@@ -45,92 +33,33 @@ public class DeviceServlet extends HttpServlet {
} else if (command.equals("/remove")) {
remove(req, resp);
} else {
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
+ return false;
}
+ return true;
}
- private void get(HttpServletRequest req, HttpServletResponse resp) throws IOException {
-
- long userId = (Long) req.getSession().getAttribute(MainServlet.USER_ID);
-
- JsonObjectBuilder result = Json.createObjectBuilder();
-
- try {
- result.add("success", true);
- result.add("data", JsonConverter.arrayToJson(
- Context.getDataManager().getDevices(userId)));
- } catch(SQLException error) {
- result.add("success", false);
- result.add("error", error.getMessage());
- }
-
- resp.getWriter().println(result.build().toString());
+ private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ sendResponse(resp.getWriter(), JsonConverter.arrayToJson(
+ Context.getDataManager().getDevices(getUserId(req.getSession()))));
}
- private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
-
- long userId = (Long) req.getSession().getAttribute(MainServlet.USER_ID);
-
- Device device = new Device();
- try {
- JsonConverter.objectFromJson(req.getReader(), device);
- } catch (ParseException error) {
- }
-
- JsonObjectBuilder result = Json.createObjectBuilder();
-
- try {
- result.add("success", true);
- Context.getDataManager().addDevice(device);
- Context.getDataManager().linkDevice(userId, device.getId());
- } catch(SQLException error) {
- result.add("success", false);
- result.add("error", error.getMessage());
- }
-
- resp.getWriter().println(result.build().toString());
+ private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ Device device = JsonConverter.objectFromJson(req.getReader(), new Device());
+ Context.getDataManager().addDevice(device);
+ Context.getDataManager().linkDevice(getUserId(req.getSession()), device.getId());
+ sendResponse(resp.getWriter());
}
- private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
-
- Device device = new Device();
- try {
- JsonConverter.objectFromJson(req.getReader(), device);
- } catch (ParseException error) {
- }
-
- JsonObjectBuilder result = Json.createObjectBuilder();
-
- try {
- result.add("success", true);
- Context.getDataManager().updateDevice(device);
- } catch(SQLException error) {
- result.add("success", false);
- result.add("error", error.getMessage());
- }
-
- resp.getWriter().println(result.build().toString());
+ private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ Context.getDataManager().updateDevice(JsonConverter.objectFromJson(
+ req.getReader(), new Device()));
+ sendResponse(resp.getWriter());
}
- private void remove(HttpServletRequest req, HttpServletResponse resp) throws IOException {
-
- Device device = new Device();
- try {
- JsonConverter.objectFromJson(req.getReader(), device);
- } catch (ParseException error) {
- }
-
- JsonObjectBuilder result = Json.createObjectBuilder();
-
- try {
- result.add("success", true);
- Context.getDataManager().removeDevice(device);
- } catch(SQLException error) {
- result.add("success", false);
- result.add("error", error.getMessage());
- }
-
- resp.getWriter().println(result.build().toString());
+ private void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ Context.getDataManager().removeDevice(JsonConverter.objectFromJson(
+ req.getReader(), new Device()));
+ sendResponse(resp.getWriter());
}
}
diff --git a/src/org/traccar/http/JsonConverter.java b/src/org/traccar/http/JsonConverter.java
index 1b02e33d2..48fd934b7 100644
--- a/src/org/traccar/http/JsonConverter.java
+++ b/src/org/traccar/http/JsonConverter.java
@@ -82,7 +82,7 @@ public class JsonConverter {
for (Method method : methods) {
if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
- String name = method.getName().substring(3);
+ String name = Introspector.decapitalize(method.getName().substring(3));
try {
if (method.getReturnType().equals(boolean.class)) {
json.add(name, (Boolean) method.invoke(object));
diff --git a/src/org/traccar/http/MainServlet.java b/src/org/traccar/http/MainServlet.java
index a881d3f45..8fa0a13af 100644
--- a/src/org/traccar/http/MainServlet.java
+++ b/src/org/traccar/http/MainServlet.java
@@ -16,66 +16,40 @@
package org.traccar.http;
import java.io.IOException;
-import java.sql.SQLException;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.TimeUnit;
-import javax.json.Json;
-import javax.json.JsonObjectBuilder;
-import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
-import javax.servlet.ServletResponse;
-import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.jboss.netty.util.Timeout;
-import org.jboss.netty.util.TimerTask;
import org.traccar.Context;
-import org.traccar.GlobalTimer;
-import org.traccar.database.DataCache;
-import org.traccar.helper.Log;
-import org.traccar.model.Position;
-public class MainServlet extends HttpServlet {
-
- public static final String USER_ID = "userId";
+public class MainServlet extends BaseServlet {
@Override
- protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-
- String command = req.getPathInfo();
-
- if (command == null) {
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
- } else if (command.equals("/session")) {
+ protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ if (command.equals("/session")) {
session(req, resp);
} else if (command.equals("/login")) {
login(req, resp);
} else if (command.equals("/logout")) {
logout(req, resp);
} else {
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
+ return false;
}
+ return true;
}
private void session(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("{ success: true, session: " + (req.getSession().getAttribute(USER_ID) != null) + " }");
}
- private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- try {
- req.getSession().setAttribute(USER_ID,
- Context.getDataManager().login(req.getParameter("email"), req.getParameter("password")));
- resp.getWriter().println("{ success: true }");
- } catch (SQLException e) {
- throw new ServletException(e);
- }
+ 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());
}
- private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ private void logout(HttpServletRequest req, HttpServletResponse resp) throws Exception {
req.getSession().removeAttribute(USER_ID);
- resp.getWriter().println("{ success: true }");
+ sendResponse(resp.getWriter());
}
}