From ebd2dcda811a8604d889824f02a79c727c5f1674 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 2 Sep 2017 09:14:02 +1200 Subject: Implement OsmAnd web proxy --- pom.xml | 5 +++++ src/org/traccar/web/WebServer.java | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pom.xml b/pom.xml index 4270a0cd7..f04a3ff68 100644 --- a/pom.xml +++ b/pom.xml @@ -103,6 +103,11 @@ jetty-jndi ${jetty.version} + + org.eclipse.jetty + jetty-proxy + ${jetty.version} + org.eclipse.jetty.websocket websocket-server diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 2055d1161..e145ff554 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -15,7 +15,10 @@ */ package org.traccar.web; +import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.proxy.AsyncProxyServlet; +import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.SessionManager; import org.eclipse.jetty.server.handler.ErrorHandler; @@ -29,6 +32,7 @@ import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.servlet.ServletContainer; import org.traccar.Config; +import org.traccar.Context; import org.traccar.api.AsyncSocketServlet; import org.traccar.api.CorsResponseFilter; import org.traccar.api.ObjectMapperProvider; @@ -38,7 +42,9 @@ import org.traccar.api.resource.ServerResource; import org.traccar.helper.Log; import javax.naming.InitialContext; +import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; import java.io.IOException; import java.io.Writer; @@ -86,6 +92,7 @@ public class WebServer { initWebApp(); break; } + initClientProxy(); server.setHandler(handlers); server.addBean(new ErrorHandler() { @@ -98,6 +105,26 @@ public class WebServer { }, false); } + private void initClientProxy() { + int port = Context.getConfig().getInteger("osmand.port"); + if (port != 0) { + ServletContextHandler servletHandler = new ServletContextHandler() { + @Override + public void doScope( + String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException { + if (target.equals("/") && request.getMethod().equals(HttpMethod.POST.asString())) { + super.doScope(target, baseRequest, request, response); + } + } + }; + ServletHolder servletHolder = new ServletHolder(new AsyncProxyServlet.Transparent()); + servletHolder.setInitParameter("proxyTo", "http://localhost:" + port); + servletHandler.addServlet(servletHolder, "/"); + handlers.addHandler(servletHandler); + } + } + private void initWebApp() { ResourceHandler resourceHandler = new ResourceHandler(); resourceHandler.setResourceBase(config.getString("web.path")); -- cgit v1.2.3