Search in sources :

Example 1 with RouteEntry

use of com.thoughtworks.go.spark.spring.RouteEntry in project gocd by gocd.

the class RerouteLatestApisImpl method registerLatest.

@Override
public void registerLatest() {
    List<RouteEntry> routes = routeInformationProvider.getRoutes();
    Multimap<String, ApiVersion> pathToVersionsMap = LinkedHashMultimap.create();
    routeToggles = resolveRouteToggles();
    routes.forEach(entry -> {
        if (shouldIgnore(entry) || ignoreFeedApis(entry.getPath())) {
            return;
        }
        ApiVersion version = ApiVersion.parse(entry.getAcceptedType());
        pathToVersionsMap.put(entry.getPath(), version);
    });
    routes.forEach(routeEntry -> {
        if (shouldIgnore(routeEntry) || ignoreFeedApis(routeEntry.getPath())) {
            return;
        }
        // get the api versions for this path
        Collection<ApiVersion> apiVersions = pathToVersionsMap.get(routeEntry.getPath());
        // get the max version supported by this path
        ApiVersion maxVersion = apiVersions.stream().max(Comparator.naturalOrder()).orElseThrow(() -> new IllegalArgumentException("Unable to lookup apiversion for " + routeEntry.getPath()));
        // if this route corresponds to the latest version, then also register that route with latest version.
        if (maxVersion.mimeType().equals(routeEntry.getAcceptedType())) {
            Service service = routeInformationProvider.getService();
            if (routeEntry.getTarget() instanceof Route) {
                service.addRoute(routeEntry.getHttpMethod(), RouteImpl.create(routeEntry.getPath(), LATEST_VERSION_MIMETYPE, (Route) routeEntry.getTarget()));
            } else if (routeEntry.getTarget() instanceof Filter) {
                service.addFilter(routeEntry.getHttpMethod(), new FilterImpl(routeEntry.getPath(), LATEST_VERSION_MIMETYPE, ((Filter) routeEntry.getTarget())) {

                    @Override
                    public void handle(Request request, Response response) throws Exception {
                        ((Filter) routeEntry.getTarget()).handle(request, response);
                    }
                });
            } else {
                throw new IllegalArgumentException("Unexpected target type " + routeEntry.getTarget().getClass());
            }
        }
    });
    routeInformationProvider.cacheRouteInformation();
}
Also used : RouteEntry(com.thoughtworks.go.spark.spring.RouteEntry) ApiVersion(com.thoughtworks.go.api.ApiVersion) FeatureToggleService(com.thoughtworks.go.server.service.support.toggle.FeatureToggleService)

Aggregations

ApiVersion (com.thoughtworks.go.api.ApiVersion)1 FeatureToggleService (com.thoughtworks.go.server.service.support.toggle.FeatureToggleService)1 RouteEntry (com.thoughtworks.go.spark.spring.RouteEntry)1