From c7d05ca6969e7fd00f7000b47efe3865ff0d2cc2 Mon Sep 17 00:00:00 2001 From: Vitaly Litvak Date: Sat, 5 Sep 2015 13:15:06 +0300 Subject: For #1395 - initialize API for old interface, added possibility to expose only API without any web application --- src/org/traccar/Context.java | 6 +-- src/org/traccar/database/DataManager.java | 2 +- src/org/traccar/web/WebServer.java | 65 ++++++++++++++++++++----------- 3 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index f818368d0..86174d30f 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -125,12 +125,10 @@ public class Context { } if (config.getBoolean("web.enable")) { - if (!config.getBoolean("web.old")) { + if (config.getString("web.type", "NEW").equals("NEW") || config.getString("web.type", "NEW").equals("API")) { permissionsManager = new PermissionsManager(dataManager); - webServer = new WebServer(config); - } else { - webServer = new WebServer(config, dataManager.getDataSource()); } + webServer = new WebServer(config, dataManager.getDataSource()); } serverManager = new ServerManager(); diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 0b8cff03a..08c721e83 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -143,7 +143,7 @@ public class DataManager implements IdentityManager { private void initDatabaseSchema() throws SQLException { - if (!config.getBoolean("web.old")) { + if (config.getString("web.type", "NEW").equals("NEW") || config.getString("web.type", "NEW").equals("API")) { Connection connection = dataSource.getConnection(); ResultSet result = connection.getMetaData().getTables( diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 0bff3a839..415c81c73 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -18,7 +18,6 @@ package org.traccar.web; import java.net.InetSocketAddress; import javax.naming.InitialContext; import javax.sql.DataSource; -import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.ResourceHandler; @@ -34,8 +33,11 @@ import org.traccar.helper.Log; public class WebServer { private Server server; + private final Config config; + private final DataSource dataSource; + private final HandlerList handlers = new HandlerList(); - private void initServer(Config config) { + private void initServer() { String address = config.getString("web.address"); int port = config.getInteger("web.port", 8082); @@ -47,9 +49,41 @@ public class WebServer { } public WebServer(Config config, DataSource dataSource) { - - initServer(config); - + this.config = config; + this.dataSource = dataSource; + + initServer(); + switch (config.getString("web.type", "NEW")) { + case "API": + initApi(); + break; + case "NEW": + initApi(); + initWebApp(); + break; + case "OLD": + initApi(); + initOldWebApp(); + break; + default: + Log.error("Unsupported web application type: " + config.getString("web.type")); + break; + } + server.setHandler(handlers); + } + + private void initWebApp() { + ResourceHandler resourceHandler = new ResourceHandler(); + resourceHandler.setResourceBase(config.getString("web.path")); + if (config.getBoolean("web.debug")) { + resourceHandler.setWelcomeFiles(new String[] { "debug.html" }); + } else { + resourceHandler.setWelcomeFiles(new String[] { "release.html", "index.html" }); + } + handlers.addHandler(resourceHandler); + } + + private void initOldWebApp() { try { javax.naming.Context context = new InitialContext(); context.bind("java:/DefaultDS", dataSource); @@ -60,13 +94,10 @@ public class WebServer { WebAppContext webapp = new WebAppContext(); webapp.setContextPath("/"); webapp.setWar(config.getString("web.application")); - server.setHandler(webapp); + handlers.addHandler(webapp); } - public WebServer(Config config) { - - initServer(config); - + private void initApi() { ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); servletHandler.setContextPath("/api"); servletHandler.addServlet(new ServletHolder(new AsyncServlet()), "/async/*"); @@ -76,19 +107,7 @@ public class WebServer { servletHandler.addServlet(new ServletHolder(new PositionServlet()), "/position/*"); servletHandler.addServlet(new ServletHolder(new CommandServlet()), "/command/*"); servletHandler.addServlet(new ServletHolder(new MainServlet()), "/*"); - - ResourceHandler resourceHandler = new ResourceHandler(); - resourceHandler.setResourceBase(config.getString("web.path")); - if (config.getBoolean("web.debug")) { - resourceHandler.setWelcomeFiles(new String[] { "debug.html" }); - } else { - resourceHandler.setWelcomeFiles(new String[] { "release.html", "index.html" }); - } - - HandlerList handlerList = new HandlerList(); - handlerList.setHandlers(new Handler[] {servletHandler, resourceHandler}); - - server.setHandler(handlerList); + handlers.addHandler(servletHandler); } public void start() { -- cgit v1.2.3 From 466364542c61e103d0e9d38aca70e729e688be35 Mon Sep 17 00:00:00 2001 From: Vitaly Litvak Date: Sun, 6 Sep 2015 11:39:39 +0300 Subject: For #1395 - updated 'web.type' values to lower case --- src/org/traccar/Context.java | 2 +- src/org/traccar/database/DataManager.java | 2 +- src/org/traccar/web/WebServer.java | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 86174d30f..80c9efb40 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -125,7 +125,7 @@ public class Context { } if (config.getBoolean("web.enable")) { - if (config.getString("web.type", "NEW").equals("NEW") || config.getString("web.type", "NEW").equals("API")) { + if (config.getString("web.type", "new").equals("new") || config.getString("web.type", "new").equals("api")) { permissionsManager = new PermissionsManager(dataManager); } webServer = new WebServer(config, dataManager.getDataSource()); diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 08c721e83..add355ea7 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -143,7 +143,7 @@ public class DataManager implements IdentityManager { private void initDatabaseSchema() throws SQLException { - if (config.getString("web.type", "NEW").equals("NEW") || config.getString("web.type", "NEW").equals("API")) { + if (config.getString("web.type", "new").equals("new") || config.getString("web.type", "new").equals("api")) { Connection connection = dataSource.getConnection(); ResultSet result = connection.getMetaData().getTables( diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 415c81c73..29af2b16e 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -53,15 +53,15 @@ public class WebServer { this.dataSource = dataSource; initServer(); - switch (config.getString("web.type", "NEW")) { - case "API": + switch (config.getString("web.type", "new")) { + case "api": initApi(); break; - case "NEW": + case "new": initApi(); initWebApp(); break; - case "OLD": + case "old": initApi(); initOldWebApp(); break; -- cgit v1.2.3