Search in sources :

Example 1 with RestRequestHandler

use of org.infinispan.rest.RestRequestHandler in project infinispan by infinispan.

the class ChannelInboundHandlerDelegator method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) {
    String[] uriSplitted = msg.uri().split("/");
    // we are paring something like this: /rest/<context or prefix>/...
    if (uriSplitted.length < 2) {
        throw RouterLogger.SERVER.noRouteFound();
    }
    String context = uriSplitted[2];
    RouterLogger.SERVER.debugf("Decoded context %s", context);
    Optional<Route<PrefixedRouteSource, RestServerRouteDestination>> route = routingTable.streamRoutes(PrefixedRouteSource.class, RestServerRouteDestination.class).filter(r -> r.getRouteSource().getRoutePrefix().equals(context)).findAny();
    RestServerRouteDestination routeDestination = route.orElseThrow(RouterLogger.SERVER::noRouteFound).getRouteDestination();
    RestRequestHandler restHandler = (RestRequestHandler) routeDestination.getProtocolServer().getRestChannelInitializer().getRestHandler();
    // before passing it to REST Handler, we need to replace path. The handler should not be aware of additional context
    // used for multi-tenant prefixes
    StringBuilder uriWithoutMultiTenantPrefix = new StringBuilder();
    for (int i = 0; i < uriSplitted.length; ++i) {
        if (i == 1) {
            // this is the main uri prefix - "rest", we want to get rid of that.
            continue;
        }
        uriWithoutMultiTenantPrefix.append(uriSplitted[i]);
        if (i < uriSplitted.length - 1) {
            uriWithoutMultiTenantPrefix.append("/");
        }
    }
    msg.setUri(uriWithoutMultiTenantPrefix.toString());
    restHandler.channelRead0(ctx, msg);
}
Also used : RouterLogger(org.infinispan.server.router.logging.RouterLogger) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) PrefixedRouteSource(org.infinispan.server.router.routes.PrefixedRouteSource) RestRequestHandler(org.infinispan.rest.RestRequestHandler) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) RestServerRouteDestination(org.infinispan.server.router.routes.rest.RestServerRouteDestination) Optional(java.util.Optional) RoutingTable(org.infinispan.server.router.RoutingTable) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Route(org.infinispan.server.router.routes.Route) RouterLogger(org.infinispan.server.router.logging.RouterLogger) RestServerRouteDestination(org.infinispan.server.router.routes.rest.RestServerRouteDestination) Route(org.infinispan.server.router.routes.Route) RestRequestHandler(org.infinispan.rest.RestRequestHandler)

Aggregations

ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 Optional (java.util.Optional)1 RestRequestHandler (org.infinispan.rest.RestRequestHandler)1 RoutingTable (org.infinispan.server.router.RoutingTable)1 RouterLogger (org.infinispan.server.router.logging.RouterLogger)1 PrefixedRouteSource (org.infinispan.server.router.routes.PrefixedRouteSource)1 Route (org.infinispan.server.router.routes.Route)1 RestServerRouteDestination (org.infinispan.server.router.routes.rest.RestServerRouteDestination)1