private void handleNonExistentMapping(ChannelHandlerContext ctx, DecodedHttpRequest req, VirtualHost host, PathAndQuery pathAndQuery, PathMappingContext mappingCtx) { final String path = mappingCtx.path(); if (path.charAt(path.length() - 1) != '/') { // Handle the case where /path doesn't exist but /path/ exists. final String pathWithSlash = path + '/'; if (host.findServiceConfig(mappingCtx.overridePath(pathWithSlash)).isPresent()) { final String location; final String originalPath = req.path(); if (path.length() == originalPath.length()) { location = pathWithSlash; } else { location = pathWithSlash + originalPath.substring(path.length()); } redirect(ctx, req, pathAndQuery, location); return; } } respond(ctx, req, HttpStatus.NOT_FOUND, null); }
@Override protected PathMappingResult doApply(PathMappingContext mappingCtx) { final String path = mappingCtx.path(); if (!path.startsWith(pathPrefix)) { return PathMappingResult.empty(); } final PathMappingResult result = mapping.apply(mappingCtx.overridePath(path.substring(pathPrefix.length() - 1))); if (result.isPresent()) { return PathMappingResult.of(path, mappingCtx.query(), result.pathParams()); } else { return PathMappingResult.empty(); } }
@Override public O serve(ServiceRequestContext ctx, I req) throws Exception { final PathMappingContext mappingCtx = ctx.pathMappingContext(); final PathMapped<Service<I, O>> mapped = findService(mappingCtx.overridePath(ctx.mappedPath())); if (!mapped.isPresent()) { throw HttpStatusException.of(HttpStatus.NOT_FOUND); } final Optional<String> childPrefix = mapped.mapping().prefix(); if (childPrefix.isPresent()) { final PathMapping newMapping = PathMapping.ofPrefix(ctx.pathMapping().prefix().get() + childPrefix.get().substring(1)); final ServiceRequestContext newCtx = new CompositeServiceRequestContext( ctx, newMapping, mapped.mappingResult().path()); try (SafeCloseable ignored = newCtx.push(false)) { return mapped.value().serve(newCtx, req); } } else { return mapped.value().serve(ctx, req); } }