/*
 * Copyright 2013-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.springframework.cloud.gateway.discovery;

import java.net.URI;

import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
import org.springframework.cloud.gateway.filter.factory.RewritePathWebFilterFactory;
import org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;

import static org.springframework.cloud.gateway.filter.factory.RewritePathWebFilterFactory.REGEXP_KEY;
import static org.springframework.cloud.gateway.filter.factory.RewritePathWebFilterFactory.REPLACEMENT_KEY;
import static org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory.PATTERN_KEY;
import static org.springframework.cloud.gateway.support.NameUtils.normalizeFilterName;
import static org.springframework.cloud.gateway.support.NameUtils.normalizePredicateName;

import reactor.core.publisher.Flux;

/**
 * TODO: developer configuration, in zuul, this was opt out, should be opt in
 * @author Spencer Gibb
 */
public class DiscoveryClientRouteDefinitionLocator implements RouteDefinitionLocator {

	private final DiscoveryClient discoveryClient;
	private final String routeIdPrefix;

	public DiscoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient) {
		this.discoveryClient = discoveryClient;
		this.routeIdPrefix = this.discoveryClient.getClass().getSimpleName() + "_";
	}

	@Override
	public Flux<RouteDefinition> getRouteDefinitions() {
		return Flux.fromIterable(discoveryClient.getServices())
				.map(serviceId -> {
					RouteDefinition routeDefinition = new RouteDefinition();
					routeDefinition.setId(this.routeIdPrefix + serviceId);
					routeDefinition.setUri(URI.create("lb://" + serviceId));

					// add a predicate that matches the url at /serviceId*
					PredicateDefinition predicate = new PredicateDefinition();
					predicate.setName(normalizePredicateName(PathRoutePredicateFactory.class));
					predicate.addArg(PATTERN_KEY, "/" + serviceId + "*");
					routeDefinition.getPredicates().add(predicate);

					//TODO: support for other default predicates

					// add a filter that removes /serviceId by default
					FilterDefinition filter = new FilterDefinition();
					filter.setName(normalizeFilterName(RewritePathWebFilterFactory.class));
					String regex = "/" + serviceId + "/(?<remaining>.*)";
					String replacement = "/${remaining}";
					filter.addArg(REGEXP_KEY, regex);
					filter.addArg(REPLACEMENT_KEY, replacement);
					routeDefinition.getFilters().add(filter);

					//TODO: support for default filters

					return routeDefinition;
				});
	}
}

@@ -798,35 +798,37 @@
 oud.gateway.
-route.Rou
+filter.Fil
 te
+r
 DefinitionLo
@@ -825,23 +825,16 @@
 finition
-Locator
 ;%0Aimport
@@ -1002,74 +1002,8 @@
 ry;%0A
-import org.springframework.cloud.gateway.filter.FilterDefinition;%0A
 impo
@@ -1141,16 +1141,87 @@
 inition;
+%0Aimport org.springframework.cloud.gateway.route.RouteDefinitionLocator;
 %0A%0Aimport
@@ -1827,16 +1827,62 @@
  opt in%0A
+ * TODO: change to RouteLocator? use java dsl%0A
  * @auth
@@ -2674,15 +2674,88 @@
 ceId
-*
+%0A%09%09%09%09%09/*PredicateDefinition barePredicate = new PredicateDefinition();
 %0A%09%09%09%09%09
+bare
 Pred
@@ -2763,20 +2763,283 @@
 cate
-Definition p
+.setName(normalizePredicateName(PathRoutePredicateFactory.class));%0A%09%09%09%09%09barePredicate.addArg(PATTERN_KEY, %22/%22 + serviceId);%0A%09%09%09%09%09routeDefinition.getPredicates().add(barePredicate);*/%0A%0A%09%09%09%09%09// add a predicate that matches the url at /serviceId/**%0A%09%09%09%09%09PredicateDefinition subP
 redi
@@ -3069,33 +3069,36 @@
 inition();%0A%09%09%09%09%09
-p
+subP
 redicate.setName
@@ -3161,17 +3161,20 @@
 );%0A%09%09%09%09%09
-p
+subP
 redicate
@@ -3213,16 +3213,18 @@
 ceId + %22
+/*
 *%22);%0A%09%09%09
@@ -3261,17 +3261,20 @@
 s().add(
-p
+subP
 redicate

Update to match /serviceId/**