aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/http/WebServer.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/http/WebServer.java')
-rw-r--r--src/org/traccar/http/WebServer.java194
1 files changed, 28 insertions, 166 deletions
diff --git a/src/org/traccar/http/WebServer.java b/src/org/traccar/http/WebServer.java
index aac334cc6..59c212d01 100644
--- a/src/org/traccar/http/WebServer.java
+++ b/src/org/traccar/http/WebServer.java
@@ -21,14 +21,19 @@ import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.util.Iterator;
+import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import javax.naming.Context;
+import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import javax.sql.DataSource;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
+import org.eclipse.jetty.webapp.WebAppContext;
import org.traccar.helper.Log;
import org.traccar.model.DataManager;
import org.traccar.model.Device;
@@ -41,183 +46,40 @@ public class WebServer {
private Server server;
- public class WebHandler extends AbstractHandler {
-
- private DataManager dataManager;
-
- public static final int BUFFER_SIZE = 1024;
-
- public WebHandler(DataManager dataManager) {
- this.dataManager = dataManager;
- }
-
- public void handleIndex(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response)
- throws IOException, ServletException {
-
- response.setContentType("text/html");
-
- InputStream in = this.getClass().getClassLoader().getResourceAsStream("web/index.html");
- OutputStream out = response.getOutputStream();
-
- byte[] buffer = new byte[BUFFER_SIZE];
- int count;
- while ((count = in.read(buffer)) != -1) {
- out.write(buffer, 0, count);
- }
- out.flush();
- }
-
- public void handleIcon(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response)
- throws IOException, ServletException {
-
- response.setContentType("image/x-icon");
-
- InputStream in = this.getClass().getClassLoader().getResourceAsStream("web/favicon.ico");
- OutputStream out = response.getOutputStream();
-
- byte[] buffer = new byte[BUFFER_SIZE];
- int count;
- while ((count = in.read(buffer)) != -1) {
- out.write(buffer, 0, count);
- }
- out.flush();
- }
-
- private Device parseDevice(String json) {
-
- Pattern pattern = Pattern.compile("\\{\"id\":(\\d+),\"imei\":\"(.*)\"\\}");
- Matcher parser = pattern.matcher(json);
- if (parser.matches()) {
- Device device = new Device();
- device.setId(Long.valueOf(parser.group(1)));
- device.setImei(parser.group(2));
- return device;
- }
-
- return null;
- }
-
- // TODO: separate method into small parts
- public void handleData(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response)
- throws IOException, ServletException {
-
- response.setContentType("application/json");
-
- PrintWriter out = response.getWriter();
-
- try {
- if (target.equals("/devices.json")) {
-
- String action = request.getParameter("action");
- if (action == null) {
- Iterator<Device> i = dataManager.getDevices().iterator();
- out.print("{'success':true,'results':[");
- while (i.hasNext()) {
- Device device = i.next();
- out.format("{'id':%d,'imei':'%s'}",
- device.getId(),
- device.getImei());
- if (i.hasNext()) out.print(",");
- }
- } else if (action.equals("create")) {
- Device device = parseDevice(request.getReader().readLine());
- dataManager.addDevice(device);
- out.print("{'success':true,'results':[");
- out.format("{'id':%d,'imei':'%s'}",
- device.getId(),
- device.getImei());
- } else if (action.equals("update")) {
- Device device = parseDevice(request.getReader().readLine());
- dataManager.updateDevice(device);
- out.print("{'success':true,'results':[");
- out.format("{'id':%d,'imei':'%s'}",
- device.getId(),
- device.getImei());
- } else if (action.equals("destroy")) {
- Device device = parseDevice(request.getReader().readLine());
- dataManager.removeDevice(device);
- out.print("{'success':true,'results':[");
- }
-
- } else if (target.equals("/positions.json")) {
-
- out.print("{'success':true,'results':[");
- String deviceId = request.getParameter("deviceId");
- if (deviceId != null) {
- Iterator<Position> i = dataManager.getPositions(Long.valueOf(deviceId)).iterator();
- while (i.hasNext()) {
- Position position = i.next();
- out.format("{'device_id':%d,'time':'%tF %tT','valid':%b,'latitude':%f,'longitude':%f,'speed':%f,'course':%f}",
- position.getDeviceId(),
- position.getTime(), position.getTime(),
- position.getValid(),
- position.getLatitude(),
- position.getLongitude(),
- position.getSpeed(),
- position.getCourse());
- if (i.hasNext()) out.print(",");
- }
- }
-
- }
- } catch (Exception error) {
- out.print("{'success':false,'results':[");
- Log.warning(error.getMessage());
- }
-
- out.print("]}");
- out.flush();
- }
-
- public void handleOther(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response)
- throws IOException, ServletException {
- response.sendRedirect(response.encodeRedirectURL("/"));
- }
+ private void initDataSource(Properties properties) {
+ try {
- @Override
- public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response)
- throws IOException, ServletException
- {
- if (target.equals("/") || target.equals("/index.html")) {
- handleIndex(target, baseRequest, request, response);
- } else if (target.equals("/favicon.ico")) {
- handleIcon(target, baseRequest, request, response);
- } else if (target.matches("/.+\\.json")) {
- handleData(target, baseRequest, request, response);
- } else {
- handleOther(target, baseRequest, request, response);
- }
- }
- }
+ Context context = new InitialContext();
- public WebServer(Integer port, DataManager dataManager) {
- server = new Server(port);
- server.setHandler(new WebHandler(dataManager));
+ Class clazz = Class.forName(properties.getProperty("database.dataSource"));
- /*WebAppContext webapp = new WebAppContext();
- webapp.setContextPath("/");
- webapp.setWar("/home/user/Documents/traccar-web/target/traccar-web-1.0-SNAPSHOT.war");
- try {
-
- Context context = new InitialContext();
-
- Class clazz = Class.forName("org.h2.jdbcx.JdbcDataSource");
-
DataSource ds = (DataSource) clazz.newInstance();
- clazz.getMethod("setURL", String.class).invoke(ds, "jdbc:h2:/home/user/Documents/traccar/target/database2");
- clazz.getMethod("setUser", String.class).invoke(ds, "sa");
- //clazz.getMethod("setPassword", String.class).invoke(ds, "");
+ clazz.getMethod("setURL", String.class).invoke(ds, properties.getProperty("database.url"));
+ clazz.getMethod("setUser", String.class).invoke(ds, properties.getProperty("database.user"));
+ clazz.getMethod("setPassword", String.class).invoke(ds, properties.getProperty("database.password"));
context.bind("java:/DefaultDS", ds);
} catch (Exception error) {
+ Log.warning(error.getMessage());
}
- server.setHandler(webapp);*/
}
- public WebServer(String address, Integer port, DataManager dataManager) {
- server = new Server(new InetSocketAddress(address, port));
- server.setHandler(new WebHandler(dataManager));
+ public WebServer(Properties properties) {
+ String address = properties.getProperty("http.address");
+ Integer port = Integer.valueOf(properties.getProperty("http.port", "8082"));
+ if (address == null) {
+ server = new Server(port);
+ } else {
+ server = new Server(new InetSocketAddress(address, port));
+ }
+
+ initDataSource(properties);
+
+ WebAppContext webapp = new WebAppContext();
+ webapp.setContextPath("/");
+ webapp.setWar(properties.getProperty("http.application"));
+ server.setHandler(webapp);
}
public void start() {