mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 00:45:44 +01:00
Merge with upstream
This commit is contained in:
@@ -16,7 +16,6 @@ import sonia.scm.repository.spi.HttpScmProtocol;
|
||||
import sonia.scm.web.UserAgent;
|
||||
import sonia.scm.web.UserAgentParser;
|
||||
|
||||
import javax.inject.Provider;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -33,17 +32,15 @@ public class HttpProtocolServlet extends HttpServlet {
|
||||
public static final String PATTERN = PATH + "/*";
|
||||
|
||||
private final RepositoryServiceFactory serviceFactory;
|
||||
|
||||
private final Provider<HttpServletRequest> requestProvider;
|
||||
|
||||
private final NamespaceAndNameFromPathExtractor pathExtractor;
|
||||
private final PushStateDispatcher dispatcher;
|
||||
private final UserAgentParser userAgentParser;
|
||||
|
||||
|
||||
@Inject
|
||||
public HttpProtocolServlet(RepositoryServiceFactory serviceFactory, Provider<HttpServletRequest> requestProvider, PushStateDispatcher dispatcher, UserAgentParser userAgentParser) {
|
||||
public HttpProtocolServlet(RepositoryServiceFactory serviceFactory, NamespaceAndNameFromPathExtractor pathExtractor, PushStateDispatcher dispatcher, UserAgentParser userAgentParser) {
|
||||
this.serviceFactory = serviceFactory;
|
||||
this.requestProvider = requestProvider;
|
||||
this.pathExtractor = pathExtractor;
|
||||
this.dispatcher = dispatcher;
|
||||
this.userAgentParser = userAgentParser;
|
||||
}
|
||||
@@ -55,9 +52,8 @@ public class HttpProtocolServlet extends HttpServlet {
|
||||
log.trace("dispatch browser request for user agent {}", userAgent);
|
||||
dispatcher.dispatch(request, response, request.getRequestURI());
|
||||
} else {
|
||||
|
||||
String pathInfo = request.getPathInfo();
|
||||
Optional<NamespaceAndName> namespaceAndName = NamespaceAndNameFromPathExtractor.fromUri(pathInfo);
|
||||
Optional<NamespaceAndName> namespaceAndName = pathExtractor.fromUri(pathInfo);
|
||||
if (namespaceAndName.isPresent()) {
|
||||
service(request, response, namespaceAndName.get());
|
||||
} else {
|
||||
@@ -69,7 +65,7 @@ public class HttpProtocolServlet extends HttpServlet {
|
||||
|
||||
private void service(HttpServletRequest req, HttpServletResponse resp, NamespaceAndName namespaceAndName) throws IOException, ServletException {
|
||||
try (RepositoryService repositoryService = serviceFactory.create(namespaceAndName)) {
|
||||
requestProvider.get().setAttribute(DefaultRepositoryProvider.ATTRIBUTE_NAME, repositoryService.getRepository());
|
||||
req.setAttribute(DefaultRepositoryProvider.ATTRIBUTE_NAME, repositoryService.getRepository());
|
||||
HttpScmProtocol protocol = repositoryService.getProtocol(HttpScmProtocol.class);
|
||||
protocol.serve(req, resp, getServletConfig());
|
||||
} catch (NotFoundException e) {
|
||||
|
||||
@@ -1,18 +1,31 @@
|
||||
package sonia.scm.web.protocol;
|
||||
|
||||
import sonia.scm.Type;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.util.HttpUtil;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.Optional.empty;
|
||||
import static java.util.Optional.of;
|
||||
|
||||
final class NamespaceAndNameFromPathExtractor {
|
||||
|
||||
private NamespaceAndNameFromPathExtractor() {}
|
||||
private final Set<String> types;
|
||||
|
||||
static Optional<NamespaceAndName> fromUri(String uri) {
|
||||
@Inject
|
||||
public NamespaceAndNameFromPathExtractor(RepositoryManager repositoryManager) {
|
||||
this.types = repositoryManager.getConfiguredTypes()
|
||||
.stream()
|
||||
.map(Type::getName)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
Optional<NamespaceAndName> fromUri(String uri) {
|
||||
if (uri.startsWith(HttpUtil.SEPARATOR_PATH)) {
|
||||
uri = uri.substring(1);
|
||||
}
|
||||
@@ -30,12 +43,13 @@ final class NamespaceAndNameFromPathExtractor {
|
||||
}
|
||||
|
||||
String name = uri.substring(endOfNamespace + 1, nameIndex);
|
||||
|
||||
int nameDotIndex = name.indexOf('.');
|
||||
int nameDotIndex = name.lastIndexOf('.');
|
||||
if (nameDotIndex >= 0) {
|
||||
return of(new NamespaceAndName(namespace, name.substring(0, nameDotIndex)));
|
||||
} else {
|
||||
return of(new NamespaceAndName(namespace, name));
|
||||
String suffix = name.substring(nameDotIndex + 1);
|
||||
if (types.contains(suffix)) {
|
||||
name = name.substring(0, nameDotIndex);
|
||||
}
|
||||
}
|
||||
return of(new NamespaceAndName(namespace, name));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user