added PushStateDispatcher for production and development to WebResourceServlet

This commit is contained in:
Sebastian Sdorra
2018-08-23 11:48:42 +02:00
parent 9e8bd299f0
commit 0fc09f5c0d
10 changed files with 452 additions and 15 deletions

View File

@@ -0,0 +1,24 @@
package sonia.scm;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* This dispatcher forwards every request to the index.html of the application.
*
* @since 2.0.0
*/
public class ForwardingPushStateDispatcher implements PushStateDispatcher {
@Override
public void dispatch(HttpServletRequest request, HttpServletResponse response, String uri) throws IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.html");
try {
dispatcher.forward(request, response);
} catch (ServletException e) {
throw new IOException("failed to forward request", e);
}
}
}

View File

@@ -0,0 +1,134 @@
package sonia.scm;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.ByteStreams;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
/**
* PushStateDispatcher which delegates the request to a different server. This dispatcher should only be used for
* development and never in production.
*
* @since 2.0.0
*/
public final class ProxyPushStateDispatcher implements PushStateDispatcher {
@FunctionalInterface
interface ConnectionFactory {
HttpURLConnection open(URL url) throws IOException;
}
private final String target;
private final ConnectionFactory connectionFactory;
/**
* Creates a new dispatcher for the given target. The target must be a valid url.
*
* @param target proxy target
*/
public ProxyPushStateDispatcher(String target) {
this(target, ProxyPushStateDispatcher::openConnection);
}
/**
* This Constructor should only be used for testing.
*
* @param target proxy target
* @param connectionFactory factory for creating an connection from a url
*/
@VisibleForTesting
ProxyPushStateDispatcher(String target, ConnectionFactory connectionFactory) {
this.target = target;
this.connectionFactory = connectionFactory;
}
@Override
public void dispatch(HttpServletRequest request, HttpServletResponse response, String uri) throws IOException {
try {
proxy(request, response, uri);
} catch (FileNotFoundException ex) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
private void proxy(HttpServletRequest request, HttpServletResponse response, String uri) throws IOException {
URL url = createProxyUrl(uri);
HttpURLConnection connection = connectionFactory.open(url);
connection.setRequestMethod(request.getMethod());
copyRequestHeaders(request, connection);
if (request.getContentLength() > 0) {
copyRequestBody(request, connection);
}
int responseCode = connection.getResponseCode();
response.setStatus(responseCode);
copyResponseHeaders(response, connection);
appendProxyHeader(response, url);
copyResponseBody(response, connection);
}
private void appendProxyHeader(HttpServletResponse response, URL url) {
response.addHeader("X-Forwarded-Port", String.valueOf(url.getPort()));
}
private void copyResponseBody(HttpServletResponse response, HttpURLConnection connection) throws IOException {
try (InputStream input = connection.getInputStream(); OutputStream output = response.getOutputStream()) {
ByteStreams.copy(input, output);
}
}
private void copyResponseHeaders(HttpServletResponse response, HttpURLConnection connection) {
Map<String, List<String>> headerFields = connection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
if (entry.getKey() != null && !"Transfer-Encoding".equalsIgnoreCase(entry.getKey())) {
for (String value : entry.getValue()) {
response.addHeader(entry.getKey(), value);
}
}
}
}
private void copyRequestBody(HttpServletRequest request, HttpURLConnection connection) throws IOException {
connection.setDoOutput(true);
try (InputStream input = request.getInputStream(); OutputStream output = connection.getOutputStream()) {
ByteStreams.copy(input, output);
}
}
private void copyRequestHeaders(HttpServletRequest request, HttpURLConnection connection) {
Enumeration<String> headers = request.getHeaderNames();
while (headers.hasMoreElements()) {
String header = headers.nextElement();
Enumeration<String> values = request.getHeaders(header);
while (values.hasMoreElements()) {
String value = values.nextElement();
connection.setRequestProperty(header, value);
}
}
}
private URL createProxyUrl(String uri) throws MalformedURLException {
return new URL(target + uri);
}
private static HttpURLConnection openConnection(URL url) throws IOException {
return (HttpURLConnection) url.openConnection();
}
}

View File

@@ -0,0 +1,28 @@
package sonia.scm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* The PushStateDispatcher is responsible for dispatching the request, to the main entry point of the ui, if no resource
* could be found for the requested path. This allows us the implementation of a ui which work with "pushstate" of
* html5.
*
* @since 2.0.0
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/History_API">HTML5 Push State</a>
*/
public interface PushStateDispatcher {
/**
* Dispatches the request to the main entry point of the ui.
*
* @param request http request
* @param response http response
* @param uri request uri
*
* @throws IOException
*/
void dispatch(HttpServletRequest request, HttpServletResponse response, String uri) throws IOException;
}

View File

@@ -0,0 +1,28 @@
package sonia.scm;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import javax.inject.Provider;
/**
* Injection Provider for the {@link PushStateDispatcher}. The provider will return a {@link ProxyPushStateDispatcher}
* if the system property {@code PushStateDispatcherProvider#PROPERTY_TARGET} is set to a proxy target url, otherwise
* a {@link ForwardingPushStateDispatcher} is used.
*
* @since 2.0.0
*/
public class PushStateDispatcherProvider implements Provider<PushStateDispatcher> {
@VisibleForTesting
static final String PROPERTY_TARGET = "sonia.scm.ui.proxy";
@Override
public PushStateDispatcher get() {
String target = System.getProperty(PROPERTY_TARGET);
if (Strings.isNullOrEmpty(target)) {
return new ForwardingPushStateDispatcher();
}
return new ProxyPushStateDispatcher(target);
}
}

View File

@@ -313,7 +313,7 @@ public class ScmServletModule extends ServletModule
// bind events
// bind(LastModifiedUpdateListener.class);
bind(PushStateDispatcher.class).toProvider(PushStateDispatcherProvider.class);
}

View File

@@ -33,19 +33,21 @@ public class WebResourceServlet extends HttpServlet {
* TODO remove old frontend servlets
*/
@VisibleForTesting
static final String PATTERN = "/(?!api/|index.html|error.html|plugins/resources).+";
static final String PATTERN = "/(?!api/).*";
private static final Logger LOG = LoggerFactory.getLogger(WebResourceServlet.class);
private final UberWebResourceLoader webResourceLoader;
private final PushStateDispatcher pushStateDispatcher;
@Inject
public WebResourceServlet(PluginLoader pluginLoader) {
public WebResourceServlet(PluginLoader pluginLoader, PushStateDispatcher dispatcher) {
this.webResourceLoader = pluginLoader.getUberWebResourceLoader();
this.pushStateDispatcher = dispatcher;
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String uri = normalizeUri(request);
LOG.trace("try to load {}", uri);
@@ -53,7 +55,7 @@ public class WebResourceServlet extends HttpServlet {
if (url != null) {
serveResource(response, url);
} else {
handleResourceNotFound(response);
pushStateDispatcher.dispatch(request, response, uri);
}
}
@@ -71,7 +73,4 @@ public class WebResourceServlet extends HttpServlet {
}
}
private void handleResourceNotFound(HttpServletResponse response) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}