Search in sources :

Example 1 with RestAPIVersion

use of org.apache.flink.runtime.rest.versioning.RestAPIVersion in project flink by apache.

the class RestServerEndpoint method registerHandler.

private static void registerHandler(Router router, Tuple2<RestHandlerSpecification, ChannelInboundHandler> specificationHandler, Logger log) {
    final String handlerURL = specificationHandler.f0.getTargetRestEndpointURL();
    // setup versioned urls
    for (final RestAPIVersion supportedVersion : specificationHandler.f0.getSupportedAPIVersions()) {
        final String versionedHandlerURL = '/' + supportedVersion.getURLVersionPrefix() + handlerURL;
        log.debug("Register handler {} under {}@{}.", specificationHandler.f1, specificationHandler.f0.getHttpMethod(), versionedHandlerURL);
        registerHandler(router, versionedHandlerURL, specificationHandler.f0.getHttpMethod(), specificationHandler.f1);
        if (supportedVersion.isDefaultVersion()) {
            // setup unversioned url for convenience and backwards compatibility
            log.debug("Register handler {} under {}@{}.", specificationHandler.f1, specificationHandler.f0.getHttpMethod(), handlerURL);
            registerHandler(router, handlerURL, specificationHandler.f0.getHttpMethod(), specificationHandler.f1);
        }
    }
}
Also used : RestAPIVersion(org.apache.flink.runtime.rest.versioning.RestAPIVersion)

Example 2 with RestAPIVersion

use of org.apache.flink.runtime.rest.versioning.RestAPIVersion in project flink by apache.

the class RestServerEndpoint method checkAllEndpointsAndHandlersAreUnique.

private static void checkAllEndpointsAndHandlersAreUnique(final List<Tuple2<RestHandlerSpecification, ChannelInboundHandler>> handlers) {
    // check for all handlers that
    // 1) the instance is only registered once
    // 2) only 1 handler is registered for each endpoint (defined by (version, method, url))
    // technically the first check is redundant since a duplicate instance also returns the same
    // headers which
    // should fail the second check, but we get a better error message
    final Set<String> uniqueEndpoints = new HashSet<>();
    final Set<ChannelInboundHandler> distinctHandlers = Collections.newSetFromMap(new IdentityHashMap<>());
    for (Tuple2<RestHandlerSpecification, ChannelInboundHandler> handler : handlers) {
        boolean isNewHandler = distinctHandlers.add(handler.f1);
        if (!isNewHandler) {
            throw new FlinkRuntimeException("Duplicate REST handler instance found." + " Please ensure each instance is registered only once.");
        }
        final RestHandlerSpecification headers = handler.f0;
        for (RestAPIVersion supportedAPIVersion : headers.getSupportedAPIVersions()) {
            final String parameterizedEndpoint = supportedAPIVersion.toString() + headers.getHttpMethod() + headers.getTargetRestEndpointURL();
            // normalize path parameters; distinct path parameters still clash at runtime
            final String normalizedEndpoint = parameterizedEndpoint.replaceAll(":[\\w-]+", ":param");
            boolean isNewEndpoint = uniqueEndpoints.add(normalizedEndpoint);
            if (!isNewEndpoint) {
                throw new FlinkRuntimeException(String.format("REST handler registration overlaps with another registration for: version=%s, method=%s, url=%s.", supportedAPIVersion, headers.getHttpMethod(), headers.getTargetRestEndpointURL()));
            }
        }
    }
}
Also used : RestAPIVersion(org.apache.flink.runtime.rest.versioning.RestAPIVersion) RestHandlerSpecification(org.apache.flink.runtime.rest.handler.RestHandlerSpecification) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) ChannelInboundHandler(org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandler) HashSet(java.util.HashSet)

Aggregations

RestAPIVersion (org.apache.flink.runtime.rest.versioning.RestAPIVersion)2 HashSet (java.util.HashSet)1 RestHandlerSpecification (org.apache.flink.runtime.rest.handler.RestHandlerSpecification)1 ChannelInboundHandler (org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandler)1 FlinkRuntimeException (org.apache.flink.util.FlinkRuntimeException)1