aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/web
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/web')
-rw-r--r--src/org/traccar/web/AsyncServlet.java44
-rw-r--r--src/org/traccar/web/BaseServlet.java16
-rw-r--r--src/org/traccar/web/CommandServlet.java2
-rw-r--r--src/org/traccar/web/DeviceServlet.java8
-rw-r--r--src/org/traccar/web/MainServlet.java4
-rw-r--r--src/org/traccar/web/PositionServlet.java4
-rw-r--r--src/org/traccar/web/ServerServlet.java6
-rw-r--r--src/org/traccar/web/UserServlet.java10
-rw-r--r--src/org/traccar/web/WebServer.java4
9 files changed, 49 insertions, 49 deletions
diff --git a/src/org/traccar/web/AsyncServlet.java b/src/org/traccar/web/AsyncServlet.java
index a62b98c59..f9217aefe 100644
--- a/src/org/traccar/web/AsyncServlet.java
+++ b/src/org/traccar/web/AsyncServlet.java
@@ -41,20 +41,20 @@ import org.traccar.model.Position;
public class AsyncServlet extends BaseServlet {
private static final long ASYNC_TIMEOUT = 120000;
-
+
@Override
protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception {
async(req.startAsync(), getUserId(req));
return true;
}
-
+
public class AsyncSession {
-
+
private static final boolean DEBUG_ASYNC = false;
-
+
private static final long SESSION_TIMEOUT = 30;
private static final long REQUEST_TIMEOUT = 20;
-
+
private boolean destroyed;
private final long userId;
private final Set<Long> devices = new HashSet<>();
@@ -62,13 +62,13 @@ public class AsyncServlet extends BaseServlet {
private Timeout requestTimeout;
private final Map<Long, Position> positions = new HashMap<>();
private AsyncContext activeContext;
-
+
private void logEvent(String message) {
if (DEBUG_ASYNC) {
Log.debug("AsyncSession: " + this.hashCode() + " destroyed: " + destroyed + " " + message);
}
}
-
+
public AsyncSession(long userId, Collection<Long> devices) {
logEvent("create userId: " + userId + " devices: " + devices.size());
this.userId = userId;
@@ -78,14 +78,14 @@ public class AsyncServlet extends BaseServlet {
for (Position position : initialPositions) {
positions.put(position.getDeviceId(), position);
}
-
+
Context.getConnectionManager().addListener(devices, dataListener);
}
-
+
public boolean hasDevice(long deviceId) {
return devices.contains(deviceId);
}
-
+
private final ConnectionManager.DataCacheListener dataListener = new ConnectionManager.DataCacheListener() {
@Override
public void onUpdate(Position position) {
@@ -104,7 +104,7 @@ public class AsyncServlet extends BaseServlet {
}
}
};
-
+
private final TimerTask sessionTimer = new TimerTask() {
@Override
public void run(Timeout tmt) throws Exception {
@@ -118,7 +118,7 @@ public class AsyncServlet extends BaseServlet {
}
}
};
-
+
private final TimerTask requestTimer = new TimerTask() {
@Override
public void run(Timeout tmt) throws Exception {
@@ -132,7 +132,7 @@ public class AsyncServlet extends BaseServlet {
}
}
};
-
+
public synchronized void request(AsyncContext context) {
logEvent("request context: " + context.hashCode());
if (!destroyed) {
@@ -150,7 +150,7 @@ public class AsyncServlet extends BaseServlet {
}
}
}
-
+
private synchronized void response() {
logEvent("response context: " + activeContext.hashCode());
if (!destroyed) {
@@ -174,17 +174,17 @@ public class AsyncServlet extends BaseServlet {
sessionTimer, SESSION_TIMEOUT, TimeUnit.SECONDS);
}
}
-
+
}
-
+
private static final Map<Long, AsyncSession> asyncSessions = new HashMap<>();
-
+
public static void sessionRefreshUser(long userId) {
synchronized (asyncSessions) {
asyncSessions.remove(userId);
}
}
-
+
public static void sessionRefreshDevice(long deviceId) {
synchronized (asyncSessions) {
Iterator<Entry<Long, AsyncSession>> iterator = asyncSessions.entrySet().iterator();
@@ -195,19 +195,19 @@ public class AsyncServlet extends BaseServlet {
}
}
}
-
+
private void async(final AsyncContext context, long userId) {
-
+
context.setTimeout(ASYNC_TIMEOUT);
HttpServletRequest req = (HttpServletRequest) context.getRequest();
synchronized (asyncSessions) {
-
+
if (Boolean.valueOf(req.getParameter("first")) || !asyncSessions.containsKey(userId)) {
Collection<Long> devices = Context.getPermissionsManager().allowedDevices(userId);
asyncSessions.put(userId, new AsyncSession(userId, devices));
}
-
+
asyncSessions.get(userId).request(context);
}
}
diff --git a/src/org/traccar/web/BaseServlet.java b/src/org/traccar/web/BaseServlet.java
index 97432e9fd..6e06e95ba 100644
--- a/src/org/traccar/web/BaseServlet.java
+++ b/src/org/traccar/web/BaseServlet.java
@@ -29,9 +29,9 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public abstract class BaseServlet extends HttpServlet {
-
+
public static final String USER_KEY = "user";
-
+
@Override
protected final void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String command = req.getPathInfo();
@@ -51,9 +51,9 @@ public abstract class BaseServlet extends HttpServlet {
sendResponse(resp.getWriter(), error);
}
}
-
+
protected abstract boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception;
-
+
public long getUserId(HttpServletRequest req) {
Long userId = (Long) req.getSession().getAttribute(USER_KEY);
if (userId == null) {
@@ -61,25 +61,25 @@ public abstract class BaseServlet extends HttpServlet {
}
return userId;
}
-
+
public void sendResponse(Writer writer, boolean success) throws IOException {
JsonObjectBuilder result = Json.createObjectBuilder();
result.add("success", success);
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", Log.exceptionStack(error));
writer.write(result.build().toString());
}
-
+
}
diff --git a/src/org/traccar/web/CommandServlet.java b/src/org/traccar/web/CommandServlet.java
index f7f071f57..958f1a888 100644
--- a/src/org/traccar/web/CommandServlet.java
+++ b/src/org/traccar/web/CommandServlet.java
@@ -12,7 +12,7 @@ public class CommandServlet extends BaseServlet {
@Override
protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception {
-
+
switch (command) {
case "/send":
send(req, resp);
diff --git a/src/org/traccar/web/DeviceServlet.java b/src/org/traccar/web/DeviceServlet.java
index f091be1af..3e8c7f115 100644
--- a/src/org/traccar/web/DeviceServlet.java
+++ b/src/org/traccar/web/DeviceServlet.java
@@ -43,12 +43,12 @@ public class DeviceServlet extends BaseServlet {
}
return true;
}
-
+
private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception {
sendResponse(resp.getWriter(), JsonConverter.arrayToJson(
Context.getDataManager().getDevices(getUserId(req))));
}
-
+
private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Device device = JsonConverter.objectFromJson(req.getReader(), new Device());
long userId = getUserId(req);
@@ -57,14 +57,14 @@ public class DeviceServlet extends BaseServlet {
Context.getPermissionsManager().refresh();
sendResponse(resp.getWriter(), JsonConverter.objectToJson(device));
}
-
+
private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Device device = JsonConverter.objectFromJson(req.getReader(), new Device());
Context.getPermissionsManager().checkDevice(getUserId(req), device.getId());
Context.getDataManager().updateDevice(device);
sendResponse(resp.getWriter(), true);
}
-
+
private void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Device device = JsonConverter.objectFromJson(req.getReader(), new Device());
Context.getPermissionsManager().checkDevice(getUserId(req), device.getId());
diff --git a/src/org/traccar/web/MainServlet.java b/src/org/traccar/web/MainServlet.java
index 21ef04088..63ff27813 100644
--- a/src/org/traccar/web/MainServlet.java
+++ b/src/org/traccar/web/MainServlet.java
@@ -24,7 +24,7 @@ public class MainServlet extends BaseServlet {
@Override
protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception {
-
+
switch (command) {
case "/session":
session(req, resp);
@@ -41,7 +41,7 @@ public class MainServlet extends BaseServlet {
default:
return false;
}
- return true;
+ return true;
}
private void session(HttpServletRequest req, HttpServletResponse resp) throws Exception {
diff --git a/src/org/traccar/web/PositionServlet.java b/src/org/traccar/web/PositionServlet.java
index 78bcb22e7..5ed74102f 100644
--- a/src/org/traccar/web/PositionServlet.java
+++ b/src/org/traccar/web/PositionServlet.java
@@ -39,9 +39,9 @@ public class PositionServlet extends BaseServlet {
default:
return false;
}
- return true;
+ return true;
}
-
+
private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception {
long deviceId = Long.valueOf(req.getParameter("deviceId"));
Context.getPermissionsManager().checkDevice(getUserId(req), deviceId);
diff --git a/src/org/traccar/web/ServerServlet.java b/src/org/traccar/web/ServerServlet.java
index 631737eb0..e2305bba4 100644
--- a/src/org/traccar/web/ServerServlet.java
+++ b/src/org/traccar/web/ServerServlet.java
@@ -35,14 +35,14 @@ public class ServerServlet extends BaseServlet {
default:
return false;
}
- return true;
+ return true;
}
-
+
private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception {
sendResponse(resp.getWriter(), JsonConverter.objectToJson(
Context.getDataManager().getServer()));
}
-
+
private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Server server = JsonConverter.objectFromJson(req.getReader(), new Server());
Context.getPermissionsManager().checkAdmin(getUserId(req));
diff --git a/src/org/traccar/web/UserServlet.java b/src/org/traccar/web/UserServlet.java
index eac4b0b13..6bd870d4d 100644
--- a/src/org/traccar/web/UserServlet.java
+++ b/src/org/traccar/web/UserServlet.java
@@ -41,15 +41,15 @@ public class UserServlet extends BaseServlet {
default:
return false;
}
- return true;
+ return true;
}
-
+
private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Context.getPermissionsManager().checkAdmin(getUserId(req));
sendResponse(resp.getWriter(), JsonConverter.arrayToJson(
Context.getDataManager().getUsers()));
}
-
+
private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
User user = JsonConverter.objectFromJson(req.getReader(), new User());
Context.getPermissionsManager().checkUser(getUserId(req), user.getId());
@@ -57,7 +57,7 @@ public class UserServlet extends BaseServlet {
Context.getPermissionsManager().refresh();
sendResponse(resp.getWriter(), JsonConverter.objectToJson(user));
}
-
+
private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
User user = JsonConverter.objectFromJson(req.getReader(), new User());
if (user.getAdmin()) {
@@ -69,7 +69,7 @@ public class UserServlet extends BaseServlet {
Context.getPermissionsManager().refresh();
sendResponse(resp.getWriter(), true);
}
-
+
private void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
User user = JsonConverter.objectFromJson(req.getReader(), new User());
Context.getPermissionsManager().checkUser(getUserId(req), user.getId());
diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java
index 29af2b16e..fc9b60beb 100644
--- a/src/org/traccar/web/WebServer.java
+++ b/src/org/traccar/web/WebServer.java
@@ -36,7 +36,7 @@ public class WebServer {
private final Config config;
private final DataSource dataSource;
private final HandlerList handlers = new HandlerList();
-
+
private void initServer() {
String address = config.getString("web.address");
@@ -47,7 +47,7 @@ public class WebServer {
server = new Server(new InetSocketAddress(address, port));
}
}
-
+
public WebServer(Config config, DataSource dataSource) {
this.config = config;
this.dataSource = dataSource;