Search in sources :

Example 1 with ApiVersion

use of com.thoughtworks.go.api.ApiVersion 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)

Example 2 with ApiVersion

use of com.thoughtworks.go.api.ApiVersion in project gocd by gocd.

the class RoutesHelper method setDeprecationHeaders.

void setDeprecationHeaders(Request request, Response response, DeprecatedAPI controller) {
    String deprecatedRelease = controller.deprecatedIn();
    String removalRelease = controller.removalIn();
    String entityName = controller.entityName();
    ApiVersion deprecatedApiVersion = controller.deprecatedApiVersion();
    ApiVersion successorApiVersion = controller.successorApiVersion();
    String changelogUrl = format("https://api.gocd.org/%s/#api-changelog", deprecatedRelease);
    String link = format("<%s>; Accept=\"%s\"; rel=\"successor-version\"", request.url(), successorApiVersion.mimeType());
    String warning = format("299 GoCD/v%s \"The %s API version %s has been deprecated in GoCD Release v%s. This version will be removed in GoCD Release v%s. Version %s of the API is available, and users are encouraged to use it\"", deprecatedRelease, entityName, deprecatedApiVersion, deprecatedRelease, removalRelease, successorApiVersion);
    response.header("X-GoCD-API-Deprecated-In", format("v%s", deprecatedRelease));
    response.header("X-GoCD-API-Removal-In", format("v%s", removalRelease));
    response.header("X-GoCD-API-Deprecation-Info", changelogUrl);
    response.header("Link", link);
    response.header("Warning", warning);
}
Also used : ApiVersion(com.thoughtworks.go.api.ApiVersion)

Aggregations

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