diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-01-29 16:01:53 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-01-29 16:01:53 +1300 |
commit | 0ee8100a5535f2d44802919a2a7bb3cec1e547af (patch) | |
tree | b36f6165da092be78e4731362cb7f5a92577f6f8 /src/org/traccar/http/WebServer.java | |
parent | 75aa9f8ba896fe88f7842126081b367872a26c46 (diff) | |
download | trackermap-server-0ee8100a5535f2d44802919a2a7bb3cec1e547af.tar.gz trackermap-server-0ee8100a5535f2d44802919a2a7bb3cec1e547af.tar.bz2 trackermap-server-0ee8100a5535f2d44802919a2a7bb3cec1e547af.zip |
Serve static files and embedded servlets
Diffstat (limited to 'src/org/traccar/http/WebServer.java')
-rw-r--r-- | src/org/traccar/http/WebServer.java | 58 |
1 files changed, 48 insertions, 10 deletions
diff --git a/src/org/traccar/http/WebServer.java b/src/org/traccar/http/WebServer.java index 9a7cb9e4e..dac252be1 100644 --- a/src/org/traccar/http/WebServer.java +++ b/src/org/traccar/http/WebServer.java @@ -15,12 +15,25 @@ */ package org.traccar.http; +import java.io.IOException; +import java.io.PrintWriter; import java.net.InetSocketAddress; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; + +import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.DefaultHandler; +import org.eclipse.jetty.server.handler.HandlerList; +import org.eclipse.jetty.server.handler.ResourceHandler; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.webapp.WebAppContext; import org.traccar.helper.Log; @@ -40,17 +53,42 @@ public class WebServer { server = new Server(new InetSocketAddress(address, port)); } - try { - Context context = new InitialContext(); - context.bind("java:/DefaultDS", dataSource); - } catch (Exception error) { - Log.warning(error); - } + if (Boolean.valueOf(properties.getProperty("http.new"))) { + + ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); + servletHandler.setContextPath("/api"); + servletHandler.addServlet(new ServletHolder(new HttpServlet() { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + PrintWriter out = resp.getWriter(); + out.println("<html><body>api</body></html>"); + } + }), "/*"); + + ResourceHandler resourceHandler = new ResourceHandler(); + resourceHandler.setResourceBase(properties.getProperty("http.path")); + resourceHandler.setWelcomeFiles(new String[] {"index.html"}); - WebAppContext webapp = new WebAppContext(); - webapp.setContextPath("/"); - webapp.setWar(properties.getProperty("http.application")); - server.setHandler(webapp); + HandlerList handlerList = new HandlerList(); + handlerList.setHandlers(new Handler[] {servletHandler, resourceHandler}); + + server.setHandler(handlerList); + + } else { + + try { + Context context = new InitialContext(); + context.bind("java:/DefaultDS", dataSource); + } catch (Exception error) { + Log.warning(error); + } + + WebAppContext webapp = new WebAppContext(); + webapp.setContextPath("/"); + webapp.setWar(properties.getProperty("http.application")); + server.setHandler(webapp); + + } } public void start() { |