Skip to content

Commit

Permalink
Implement OsmAnd web proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Sep 1, 2017
1 parent 7153cce commit ebd2dcd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
<artifactId>jetty-jndi</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-proxy</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-server</artifactId>
Expand Down
27 changes: 27 additions & 0 deletions src/org/traccar/web/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +32,7 @@
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;
Expand All @@ -38,7 +42,9 @@
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;
Expand Down Expand Up @@ -86,6 +92,7 @@ public WebServer(Config config, DataSource dataSource) {
initWebApp();
break;
}
initClientProxy();
server.setHandler(handlers);

server.addBean(new ErrorHandler() {
Expand All @@ -98,6 +105,26 @@ protected void handleErrorPage(
}, 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"));
Expand Down

0 comments on commit ebd2dcd

Please sign in to comment.