001/*
002 * Copyright 2012-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 *      http://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.boot.test.web.client;
018
019import org.springframework.boot.web.client.RootUriTemplateHandler;
020import org.springframework.core.env.Environment;
021import org.springframework.util.Assert;
022import org.springframework.web.util.DefaultUriBuilderFactory;
023import org.springframework.web.util.UriTemplateHandler;
024
025/**
026 * {@link UriTemplateHandler} will automatically prefix relative URIs with
027 * <code>localhost:$&#123;local.server.port&#125;</code>.
028 *
029 * @author Phillip Webb
030 * @author Andy Wilkinson
031 * @author EddĂș MelĂ©ndez
032 * @author Madhura Bhave
033 * @since 1.4.0
034 */
035public class LocalHostUriTemplateHandler extends RootUriTemplateHandler {
036
037        private static final String PREFIX = "server.servlet.";
038
039        private final Environment environment;
040
041        private final String scheme;
042
043        /**
044         * Create a new {@code LocalHostUriTemplateHandler} that will generate {@code http}
045         * URIs using the given {@code environment} to determine the context path and port.
046         * @param environment the environment used to determine the port
047         */
048        public LocalHostUriTemplateHandler(Environment environment) {
049                this(environment, "http");
050        }
051
052        /**
053         * Create a new {@code LocalHostUriTemplateHandler} that will generate URIs with the
054         * given {@code scheme} and use the given {@code environment} to determine the
055         * context-path and port.
056         * @param environment the environment used to determine the port
057         * @param scheme the scheme of the root uri
058         * @since 1.4.1
059         */
060        public LocalHostUriTemplateHandler(Environment environment, String scheme) {
061                this(environment, scheme, new DefaultUriBuilderFactory());
062        }
063
064        /**
065         * Create a new {@code LocalHostUriTemplateHandler} that will generate URIs with the
066         * given {@code scheme}, use the given {@code environment} to determine the
067         * context-path and port and delegate to the given template {@code handler}.
068         * @param environment the environment used to determine the port
069         * @param scheme the scheme of the root uri
070         * @param handler the delegate handler
071         * @since 2.0.3
072         */
073        public LocalHostUriTemplateHandler(Environment environment, String scheme,
074                        UriTemplateHandler handler) {
075                super(handler);
076                Assert.notNull(environment, "Environment must not be null");
077                Assert.notNull(scheme, "Scheme must not be null");
078                this.environment = environment;
079                this.scheme = scheme;
080        }
081
082        @Override
083        public String getRootUri() {
084                String port = this.environment.getProperty("local.server.port", "8080");
085                String contextPath = this.environment.getProperty(PREFIX + "context-path", "");
086                return this.scheme + "://localhost:" + port + contextPath;
087        }
088
089}