| 
									
										
										
										
											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.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							| 
									
										
										
										
											2021-01-29 07:59:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  | package sonia.scm.web.protocol;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import com.google.inject.Inject;
 | 
					
						
							|  |  |  | import com.google.inject.Singleton;
 | 
					
						
							|  |  |  | import lombok.extern.slf4j.Slf4j;
 | 
					
						
							| 
									
										
										
										
											2018-09-10 08:37:31 +02:00
										 |  |  | import org.apache.http.HttpStatus;
 | 
					
						
							| 
									
										
										
										
											2019-01-23 11:20:43 +01:00
										 |  |  | import org.apache.shiro.authz.AuthorizationException;
 | 
					
						
							| 
									
										
										
										
											2018-10-18 16:08:49 +02:00
										 |  |  | import sonia.scm.NotFoundException;
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  | import sonia.scm.PushStateDispatcher;
 | 
					
						
							| 
									
										
										
										
											2021-01-29 07:59:18 +01:00
										 |  |  | import sonia.scm.config.ScmConfiguration;
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  | import sonia.scm.filter.WebElement;
 | 
					
						
							|  |  |  | import sonia.scm.repository.DefaultRepositoryProvider;
 | 
					
						
							|  |  |  | import sonia.scm.repository.NamespaceAndName;
 | 
					
						
							|  |  |  | import sonia.scm.repository.api.RepositoryService;
 | 
					
						
							|  |  |  | import sonia.scm.repository.api.RepositoryServiceFactory;
 | 
					
						
							|  |  |  | import sonia.scm.repository.spi.HttpScmProtocol;
 | 
					
						
							| 
									
										
										
										
											2019-10-14 16:20:27 +02:00
										 |  |  | import sonia.scm.security.Authentications;
 | 
					
						
							|  |  |  | import sonia.scm.util.HttpUtil;
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  | import sonia.scm.web.UserAgent;
 | 
					
						
							|  |  |  | import sonia.scm.web.UserAgentParser;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import javax.servlet.ServletException;
 | 
					
						
							|  |  |  | import javax.servlet.http.HttpServlet;
 | 
					
						
							|  |  |  | import javax.servlet.http.HttpServletRequest;
 | 
					
						
							|  |  |  | import javax.servlet.http.HttpServletResponse;
 | 
					
						
							|  |  |  | import java.io.IOException;
 | 
					
						
							| 
									
										
										
										
											2018-09-10 08:37:31 +02:00
										 |  |  | import java.util.Optional;
 | 
					
						
							| 
									
										
										
										
											2018-09-06 17:39:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  | @Singleton
 | 
					
						
							|  |  |  | @WebElement(value = HttpProtocolServlet.PATTERN)
 | 
					
						
							|  |  |  | @Slf4j
 | 
					
						
							|  |  |  | public class HttpProtocolServlet extends HttpServlet {
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-10 11:59:21 +02:00
										 |  |  |   public static final String PATH = "/repo";
 | 
					
						
							|  |  |  |   public static final String PATTERN = PATH + "/*";
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-29 07:59:18 +01:00
										 |  |  |   private final ScmConfiguration configuration;
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  |   private final RepositoryServiceFactory serviceFactory;
 | 
					
						
							| 
									
										
										
										
											2019-07-22 13:00:49 +02:00
										 |  |  |   private final NamespaceAndNameFromPathExtractor pathExtractor;
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  |   private final PushStateDispatcher dispatcher;
 | 
					
						
							|  |  |  |   private final UserAgentParser userAgentParser;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-10 08:37:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  |   @Inject
 | 
					
						
							| 
									
										
										
										
											2021-01-29 07:59:18 +01:00
										 |  |  |   public HttpProtocolServlet(ScmConfiguration configuration, RepositoryServiceFactory serviceFactory, NamespaceAndNameFromPathExtractor pathExtractor, PushStateDispatcher dispatcher, UserAgentParser userAgentParser) {
 | 
					
						
							|  |  |  |     this.configuration = configuration;
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  |     this.serviceFactory = serviceFactory;
 | 
					
						
							| 
									
										
										
										
											2019-07-22 13:00:49 +02:00
										 |  |  |     this.pathExtractor = pathExtractor;
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  |     this.dispatcher = dispatcher;
 | 
					
						
							|  |  |  |     this.userAgentParser = userAgentParser;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @Override
 | 
					
						
							| 
									
										
										
										
											2018-09-10 08:37:31 +02:00
										 |  |  |   protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
 | 
					
						
							|  |  |  |     UserAgent userAgent = userAgentParser.parse(request);
 | 
					
						
							| 
									
										
										
										
											2020-10-30 12:20:45 +01:00
										 |  |  |     if (userAgent.isScmClient()) {
 | 
					
						
							| 
									
										
										
										
											2018-09-10 08:37:31 +02:00
										 |  |  |       String pathInfo = request.getPathInfo();
 | 
					
						
							| 
									
										
										
										
											2019-07-22 13:00:49 +02:00
										 |  |  |       Optional<NamespaceAndName> namespaceAndName = pathExtractor.fromUri(pathInfo);
 | 
					
						
							| 
									
										
										
										
											2018-09-10 08:37:31 +02:00
										 |  |  |       if (namespaceAndName.isPresent()) {
 | 
					
						
							|  |  |  |         service(request, response, namespaceAndName.get());
 | 
					
						
							|  |  |  |       } else {
 | 
					
						
							|  |  |  |         log.debug("namespace and name not found in request path {}", pathInfo);
 | 
					
						
							|  |  |  |         response.setStatus(HttpStatus.SC_BAD_REQUEST);
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  |       }
 | 
					
						
							| 
									
										
										
										
											2020-10-30 12:20:45 +01:00
										 |  |  |     } else {
 | 
					
						
							| 
									
										
										
										
											2020-11-02 13:27:18 +01:00
										 |  |  |       log.trace("dispatch non-scm-client request for user agent {}", userAgent);
 | 
					
						
							| 
									
										
										
										
											2020-10-30 12:20:45 +01:00
										 |  |  |       dispatcher.dispatch(request, response, request.getRequestURI());
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-10 08:37:31 +02:00
										 |  |  |   private void service(HttpServletRequest req, HttpServletResponse resp, NamespaceAndName namespaceAndName) throws IOException, ServletException {
 | 
					
						
							|  |  |  |     try (RepositoryService repositoryService = serviceFactory.create(namespaceAndName)) {
 | 
					
						
							| 
									
										
										
										
											2019-07-22 09:44:03 +02:00
										 |  |  |       req.setAttribute(DefaultRepositoryProvider.ATTRIBUTE_NAME, repositoryService.getRepository());
 | 
					
						
							| 
									
										
										
										
											2018-09-10 08:37:31 +02:00
										 |  |  |       HttpScmProtocol protocol = repositoryService.getProtocol(HttpScmProtocol.class);
 | 
					
						
							|  |  |  |       protocol.serve(req, resp, getServletConfig());
 | 
					
						
							| 
									
										
										
										
											2018-10-18 16:08:49 +02:00
										 |  |  |     } catch (NotFoundException e) {
 | 
					
						
							|  |  |  |       log.debug(e.getMessage());
 | 
					
						
							| 
									
										
										
										
											2018-09-10 08:37:31 +02:00
										 |  |  |       resp.setStatus(HttpStatus.SC_NOT_FOUND);
 | 
					
						
							| 
									
										
										
										
											2019-01-23 11:20:43 +01:00
										 |  |  |     } catch (AuthorizationException e) {
 | 
					
						
							|  |  |  |       log.debug(e.getMessage());
 | 
					
						
							| 
									
										
										
										
											2019-10-14 16:20:27 +02:00
										 |  |  |       if (Authentications.isAuthenticatedSubjectAnonymous()) {
 | 
					
						
							| 
									
										
										
										
											2021-01-29 07:59:18 +01:00
										 |  |  |         HttpUtil.sendUnauthorized(resp, configuration.getRealmDescription());
 | 
					
						
							| 
									
										
										
										
											2019-10-14 16:20:27 +02:00
										 |  |  |       } else {
 | 
					
						
							|  |  |  |         resp.setStatus(HttpStatus.SC_FORBIDDEN);
 | 
					
						
							|  |  |  |       }
 | 
					
						
							| 
									
										
										
										
											2018-09-06 10:58:09 +02:00
										 |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | }
 |