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.web.client;
018
019import java.net.URI;
020import java.util.Map;
021
022import org.springframework.util.Assert;
023import org.springframework.util.StringUtils;
024import org.springframework.web.client.RestTemplate;
025import org.springframework.web.util.DefaultUriBuilderFactory;
026import org.springframework.web.util.UriTemplateHandler;
027
028/**
029 * {@link UriTemplateHandler} to set the root for URI that starts with {@code '/'}.
030 *
031 * @author Phillip Webb
032 * @since 1.4.0
033 */
034public class RootUriTemplateHandler implements UriTemplateHandler {
035
036        private final String rootUri;
037
038        private final UriTemplateHandler handler;
039
040        protected RootUriTemplateHandler(UriTemplateHandler handler) {
041                Assert.notNull(handler, "Handler must not be null");
042                this.rootUri = null;
043                this.handler = handler;
044        }
045
046        /**
047         * Create a new {@link RootUriTemplateHandler} instance.
048         * @param rootUri the root URI to be used to prefix relative URLs
049         */
050        public RootUriTemplateHandler(String rootUri) {
051                this(rootUri, new DefaultUriBuilderFactory());
052        }
053
054        /**
055         * Create a new {@link RootUriTemplateHandler} instance.
056         * @param rootUri the root URI to be used to prefix relative URLs
057         * @param handler the delegate handler
058         */
059        public RootUriTemplateHandler(String rootUri, UriTemplateHandler handler) {
060                Assert.notNull(rootUri, "RootUri must not be null");
061                Assert.notNull(handler, "Handler must not be null");
062                this.rootUri = rootUri;
063                this.handler = handler;
064        }
065
066        @Override
067        public URI expand(String uriTemplate, Map<String, ?> uriVariables) {
068                return this.handler.expand(apply(uriTemplate), uriVariables);
069        }
070
071        @Override
072        public URI expand(String uriTemplate, Object... uriVariables) {
073                return this.handler.expand(apply(uriTemplate), uriVariables);
074        }
075
076        private String apply(String uriTemplate) {
077                if (StringUtils.startsWithIgnoreCase(uriTemplate, "/")) {
078                        return getRootUri() + uriTemplate;
079                }
080                return uriTemplate;
081        }
082
083        public String getRootUri() {
084                return this.rootUri;
085        }
086
087        /**
088         * Add a {@link RootUriTemplateHandler} instance to the given {@link RestTemplate}.
089         * @param restTemplate the {@link RestTemplate} to add the handler to
090         * @param rootUri the root URI
091         * @return the added {@link RootUriTemplateHandler}.
092         */
093        public static RootUriTemplateHandler addTo(RestTemplate restTemplate,
094                        String rootUri) {
095                Assert.notNull(restTemplate, "RestTemplate must not be null");
096                RootUriTemplateHandler handler = new RootUriTemplateHandler(rootUri,
097                                restTemplate.getUriTemplateHandler());
098                restTemplate.setUriTemplateHandler(handler);
099                return handler;
100        }
101
102}