001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.http.server.reactive;
018
019import java.util.LinkedHashMap;
020import java.util.Map;
021
022import reactor.core.publisher.Mono;
023
024import org.springframework.http.HttpStatus;
025import org.springframework.util.Assert;
026
027/**
028 * {@code HttpHandler} delegating requests to one of several {@code HttpHandler}'s
029 * based on simple, prefix-based mappings.
030 *
031 * <p>This is intended as a coarse-grained mechanism for delegating requests to
032 * one of several applications -- each represented by an {@code HttpHandler}, with
033 * the application "context path" (the prefix-based mapping) exposed via
034 * {@link ServerHttpRequest#getPath()}.
035 *
036 * @author Rossen Stoyanchev
037 * @since 5.0
038 */
039public class ContextPathCompositeHandler implements HttpHandler {
040
041        private final Map<String, HttpHandler> handlerMap;
042
043
044        public ContextPathCompositeHandler(Map<String, ? extends HttpHandler> handlerMap) {
045                Assert.notEmpty(handlerMap, "Handler map must not be empty");
046                this.handlerMap = initHandlers(handlerMap);
047        }
048
049        private static Map<String, HttpHandler> initHandlers(Map<String, ? extends HttpHandler> map) {
050                map.keySet().forEach(ContextPathCompositeHandler::assertValidContextPath);
051                return new LinkedHashMap<>(map);
052        }
053
054        private static void assertValidContextPath(String contextPath) {
055                Assert.hasText(contextPath, "Context path must not be empty");
056                if (contextPath.equals("/")) {
057                        return;
058                }
059                Assert.isTrue(contextPath.startsWith("/"), "Context path must begin with '/'");
060                Assert.isTrue(!contextPath.endsWith("/"), "Context path must not end with '/'");
061        }
062
063
064        @Override
065        public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
066                // Remove underlying context path first (e.g. Servlet container)
067                String path = request.getPath().pathWithinApplication().value();
068                return this.handlerMap.entrySet().stream()
069                                .filter(entry -> path.startsWith(entry.getKey()))
070                                .findFirst()
071                                .map(entry -> {
072                                        String contextPath = request.getPath().contextPath().value() + entry.getKey();
073                                        ServerHttpRequest newRequest = request.mutate().contextPath(contextPath).build();
074                                        return entry.getValue().handle(newRequest, response);
075                                })
076                                .orElseGet(() -> {
077                                        response.setStatusCode(HttpStatus.NOT_FOUND);
078                                        return response.setComplete();
079                                });
080        }
081
082}