2018-08-21 15:23:54 +02:00
|
|
|
package sonia.scm;
|
|
|
|
|
|
|
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
|
|
import com.google.common.io.Resources;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import sonia.scm.filter.WebElement;
|
|
|
|
|
import sonia.scm.plugin.PluginLoader;
|
|
|
|
|
import sonia.scm.plugin.UberWebResourceLoader;
|
|
|
|
|
import sonia.scm.util.HttpUtil;
|
|
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
import javax.inject.Singleton;
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WebResourceServlet serves resources from the {@link UberWebResourceLoader}.
|
|
|
|
|
*
|
|
|
|
|
* @since 2.0.0
|
|
|
|
|
*/
|
|
|
|
|
@Singleton
|
|
|
|
|
@WebElement(value = WebResourceServlet.PATTERN, regex = true)
|
|
|
|
|
public class WebResourceServlet extends HttpServlet {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* exclude api requests and the old frontend servlets.
|
|
|
|
|
*
|
2018-08-30 12:42:33 +02:00
|
|
|
* TODO remove old protocol servlets
|
2018-08-21 15:23:54 +02:00
|
|
|
*/
|
|
|
|
|
@VisibleForTesting
|
2018-08-30 12:43:59 +02:00
|
|
|
static final String PATTERN = "/(?!api/|git/|hg/|svn/|debug/).*";
|
2018-08-21 15:23:54 +02:00
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(WebResourceServlet.class);
|
|
|
|
|
|
|
|
|
|
private final UberWebResourceLoader webResourceLoader;
|
2018-08-23 11:48:42 +02:00
|
|
|
private final PushStateDispatcher pushStateDispatcher;
|
2018-08-21 15:23:54 +02:00
|
|
|
|
|
|
|
|
@Inject
|
2018-08-23 11:48:42 +02:00
|
|
|
public WebResourceServlet(PluginLoader pluginLoader, PushStateDispatcher dispatcher) {
|
2018-08-21 15:23:54 +02:00
|
|
|
this.webResourceLoader = pluginLoader.getUberWebResourceLoader();
|
2018-08-23 11:48:42 +02:00
|
|
|
this.pushStateDispatcher = dispatcher;
|
2018-08-21 15:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2018-08-30 11:03:15 +02:00
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
|
2018-08-21 15:23:54 +02:00
|
|
|
String uri = normalizeUri(request);
|
|
|
|
|
|
|
|
|
|
LOG.trace("try to load {}", uri);
|
|
|
|
|
URL url = webResourceLoader.getResource(uri);
|
|
|
|
|
if (url != null) {
|
|
|
|
|
serveResource(response, url);
|
|
|
|
|
} else {
|
2018-08-30 11:03:15 +02:00
|
|
|
dispatch(request, response, uri);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void dispatch(HttpServletRequest request, HttpServletResponse response, String uri) {
|
|
|
|
|
try {
|
2018-08-23 11:48:42 +02:00
|
|
|
pushStateDispatcher.dispatch(request, response, uri);
|
2018-08-30 11:03:15 +02:00
|
|
|
} catch (IOException ex) {
|
|
|
|
|
LOG.error("failed to dispatch: " + uri, ex);
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
2018-08-21 15:23:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String normalizeUri(HttpServletRequest request) {
|
|
|
|
|
return HttpUtil.getStrippedURI(request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void serveResource(HttpServletResponse response, URL url) {
|
|
|
|
|
// TODO lastModifiedDate, if-... ???
|
|
|
|
|
try (OutputStream output = response.getOutputStream()) {
|
|
|
|
|
Resources.copy(url, output);
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
LOG.warn("failed to serve resource: {}", url);
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|