2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-08-21 15:23:54 +02:00
|
|
|
package sonia.scm;
|
|
|
|
|
|
2019-02-08 12:55:10 +01:00
|
|
|
import com.github.sdorra.webresources.CacheControl;
|
2018-09-10 12:04:56 +02:00
|
|
|
import com.github.sdorra.webresources.WebResourceSender;
|
2018-08-21 15:23:54 +02:00
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
|
|
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.net.URL;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WebResourceServlet serves resources from the {@link UberWebResourceLoader}.
|
|
|
|
|
*
|
|
|
|
|
* @since 2.0.0
|
|
|
|
|
*/
|
|
|
|
|
@Singleton
|
2018-10-22 14:15:21 +02:00
|
|
|
@Priority(WebResourceServlet.PRIORITY)
|
2018-08-21 15:23:54 +02:00
|
|
|
@WebElement(value = WebResourceServlet.PATTERN, regex = true)
|
|
|
|
|
public class WebResourceServlet extends HttpServlet {
|
|
|
|
|
|
2018-09-10 12:04:56 +02:00
|
|
|
|
2018-08-21 15:23:54 +02:00
|
|
|
/**
|
|
|
|
|
* exclude api requests and the old frontend servlets.
|
|
|
|
|
*
|
2018-08-30 13:12:33 +02:00
|
|
|
* TODO remove old protocol servlets and hook. Move /hook/hg to api?
|
2018-08-21 15:23:54 +02:00
|
|
|
*/
|
|
|
|
|
@VisibleForTesting
|
2018-10-22 14:15:21 +02:00
|
|
|
static final String PATTERN = "/(?!api/|git/|hg/|svn/|hook/|repo/).*";
|
|
|
|
|
|
|
|
|
|
// Be sure that this servlet is the last one in the servlet chain.
|
|
|
|
|
static final int PRIORITY = Integer.MAX_VALUE;
|
2018-08-21 15:23:54 +02:00
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(WebResourceServlet.class);
|
|
|
|
|
|
2018-09-10 12:04:56 +02:00
|
|
|
private final WebResourceSender sender = WebResourceSender.create()
|
|
|
|
|
.withGZIP()
|
|
|
|
|
.withGZIPMinLength(512)
|
2019-02-08 12:55:10 +01:00
|
|
|
.withBufferSize(16384)
|
|
|
|
|
.withCacheControl(CacheControl.create().noCache());
|
2018-09-10 12:04:56 +02:00
|
|
|
|
2018-08-21 15:23:54 +02:00
|
|
|
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) {
|
2018-10-04 15:23:31 +02:00
|
|
|
LOG.trace("found {} -- serve as resource {}", uri, url);
|
2018-09-10 12:04:56 +02:00
|
|
|
serveResource(request, response, url);
|
2018-08-21 15:23:54 +02:00
|
|
|
} else {
|
2018-10-04 15:23:31 +02:00
|
|
|
LOG.trace("could not find {} -- dispatch", uri);
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-10 12:04:56 +02:00
|
|
|
private void serveResource(HttpServletRequest request, HttpServletResponse response, URL url) {
|
|
|
|
|
try {
|
2018-10-04 15:23:31 +02:00
|
|
|
LOG.debug("using sender to serve {}", request.getRequestURI());
|
2018-09-10 12:04:56 +02:00
|
|
|
sender.resource(url).send(request, response);
|
2018-08-21 15:23:54 +02:00
|
|
|
} catch (IOException ex) {
|
|
|
|
|
LOG.warn("failed to serve resource: {}", url);
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|