/** * Creates a new {@link PathMapping} that matches a {@linkplain ServiceRequestContext#path() path} * under the specified directory prefix. It also removes the specified directory prefix from the matched * path so that {@link ServiceRequestContext#path()} does not have the specified directory prefix. * For example, when {@code pathPrefix} is {@code "/foo/"}: * <ul> * <li>{@code "/foo/"} translates to {@code "/"}</li> * <li>{@code "/foo/bar"} translates to {@code "/bar"}</li> * <li>{@code "/foo/bar/baz"} translates to {@code "/bar/baz"}</li> * </ul> * This method is a shortcut to {@link #ofPrefix(String, boolean) ofPrefix(pathPrefix, true)}. */ static PathMapping ofPrefix(String pathPrefix) { return ofPrefix(pathPrefix, true); }
/** * Creates a new {@link CompositeServiceEntry} whose {@link Service} is bound under the specified * directory. * * @see PathMapping#ofPrefix(String) */ public static <I extends Request, O extends Response> CompositeServiceEntry<I, O> ofPrefix(String pathPrefix, Service<I, O> service) { return new CompositeServiceEntry<>(PathMapping.ofPrefix(pathPrefix), service); }
/** * Binds the specified {@link Service} under the specified directory. */ public B serviceUnder(String pathPrefix, Service<HttpRequest, HttpResponse> service) { service(PathMapping.ofPrefix(pathPrefix), service); return self(); }
return ofPrefix(pathPattern.substring(PREFIX.length()));
return PathMapping.ofPrefix(concatPaths( pathPrefix, pattern.substring(PREFIX.length())));
@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); } }