Skip to content

Commit

Permalink
Polishing external contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
poutsma committed Oct 10, 2023
1 parent ad50de1 commit 307a2c7
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -115,6 +115,13 @@ public EntityResponse.Builder<T> headers(HttpHeaders headers) {
return this;
}

@Override
public EntityResponse.Builder<T> headers(Consumer<HttpHeaders> headersConsumer) {
Assert.notNull(headersConsumer, "HeadersConsumer must not be null");
headersConsumer.accept(this.headers);
return this;
}

@Override
public EntityResponse.Builder<T> allow(HttpMethod... allowedMethods) {
this.headers.setAllow(new LinkedHashSet<>(Arrays.asList(allowedMethods)));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -149,6 +149,18 @@ interface Builder<T> {
*/
Builder<T> headers(HttpHeaders headers);

/**
* Manipulate this entity's headers with the given consumer. The
* headers provided to the consumer are "live", so that the consumer can be used to
* {@linkplain HttpHeaders#set(String, String) overwrite} existing header values,
* {@linkplain HttpHeaders#remove(Object) remove} values, or use any of the other
* {@link HttpHeaders} methods.
* @param headersConsumer a function that consumes the {@code HttpHeaders}
* @return this builder
* @since 6.1
*/
Builder<T> headers(Consumer<HttpHeaders> headersConsumer);

/**
* Set the HTTP status.
* @param status the response status
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,11 +23,12 @@
import java.net.URI;
import java.net.URL;
import java.util.Set;
import java.util.function.BiConsumer;

import reactor.core.publisher.Mono;

import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;
Expand All @@ -46,18 +47,13 @@ class ResourceHandlerFunction implements HandlerFunction<ServerResponse> {


private final Resource resource;
private final CacheControl cacheControl;


public ResourceHandlerFunction(Resource resource) {
this.resource = resource;
this.cacheControl = CacheControl.empty();
}
private final BiConsumer<Resource, HttpHeaders> headersConsumer;


public ResourceHandlerFunction(Resource resource, ResourceCacheLookupStrategy strategy) {
public ResourceHandlerFunction(Resource resource, BiConsumer<Resource, HttpHeaders> headersConsumer) {
this.resource = resource;
this.cacheControl = strategy.lookupCacheControl(resource);
this.headersConsumer = headersConsumer;
}


Expand All @@ -66,14 +62,14 @@ public Mono<ServerResponse> handle(ServerRequest request) {
HttpMethod method = request.method();
if (HttpMethod.GET.equals(method)) {
return EntityResponse.fromObject(this.resource)
.cacheControl(this.cacheControl)
.headers(headers -> this.headersConsumer.accept(this.resource, headers))
.build()
.map(response -> response);
}
else if (HttpMethod.HEAD.equals(method)) {
Resource headResource = new HeadMethodResource(this.resource);
return EntityResponse.fromObject(headResource)
.cacheControl(this.cacheControl)
.headers(headers -> this.headersConsumer.accept(this.resource, headers))
.build()
.map(response -> response);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -30,6 +31,7 @@
import reactor.core.publisher.Mono;

import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -242,8 +244,10 @@ public RouterFunctions.Builder resources(String pattern, Resource location) {
}

@Override
public RouterFunctions.Builder resources(String pattern, Resource location, ResourceCacheLookupStrategy resourceCacheLookupStrategy) {
return add(RouterFunctions.resources(pattern,location,resourceCacheLookupStrategy));
public RouterFunctions.Builder resources(String pattern, Resource location,
BiConsumer<Resource, HttpHeaders> headersConsumer) {

return add(RouterFunctions.resources(pattern, location, headersConsumer));
}

@Override
Expand All @@ -252,8 +256,10 @@ public RouterFunctions.Builder resources(Function<ServerRequest, Mono<Resource>>
}

@Override
public RouterFunctions.Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy resourceCacheLookupStrategy) {
return add(RouterFunctions.resources(lookupFunction,resourceCacheLookupStrategy));
public RouterFunctions.Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction,
BiConsumer<Resource, HttpHeaders> headersConsumer) {

return add(RouterFunctions.resources(lookupFunction, headersConsumer));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -32,6 +33,7 @@
import reactor.core.publisher.Mono;

import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.HttpHandler;
Expand Down Expand Up @@ -156,10 +158,26 @@ public static <T extends ServerResponse> RouterFunction<T> nest(
* @see #resourceLookupFunction(String, Resource)
*/
public static RouterFunction<ServerResponse> resources(String pattern, Resource location) {
return resources(resourceLookupFunction(pattern, location), ResourceCacheLookupStrategy.noCaching());
return resources(resourceLookupFunction(pattern, location), (resource, httpHeaders) -> {});
}
public static RouterFunction<ServerResponse> resources(String pattern, Resource location, ResourceCacheLookupStrategy lookupStrategy) {
return resources(resourceLookupFunction(pattern, location), lookupStrategy);

/**
* Route requests that match the given pattern to resources relative to the given root location.
* For instance
* <pre class="code">
* Resource location = new FileSystemResource("public-resources/");
* RouterFunction&lt;ServerResponse&gt; resources = RouterFunctions.resources("/resources/**", location);
* </pre>
* @param pattern the pattern to match
* @param location the location directory relative to which resources should be resolved
* @param headersConsumer provides access to the HTTP headers for served resources
* @return a router function that routes to resources
* @since 6.1
* @see #resourceLookupFunction(String, Resource)
*/
public static RouterFunction<ServerResponse> resources(String pattern, Resource location,
BiConsumer<Resource, HttpHeaders> headersConsumer) {
return resources(resourceLookupFunction(pattern, location), headersConsumer);
}

/**
Expand Down Expand Up @@ -189,10 +207,21 @@ public static Function<ServerRequest, Mono<Resource>> resourceLookupFunction(Str
* @return a router function that routes to resources
*/
public static RouterFunction<ServerResponse> resources(Function<ServerRequest, Mono<Resource>> lookupFunction) {
return new ResourcesRouterFunction(lookupFunction, ResourceCacheLookupStrategy.noCaching());
return new ResourcesRouterFunction(lookupFunction, (resource, httpHeaders) -> {});
}
public static RouterFunction<ServerResponse> resources(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy lookupStrategy) {
return new ResourcesRouterFunction(lookupFunction, lookupStrategy);

/**
* Route to resources using the provided lookup function. If the lookup function provides a
* {@link Resource} for the given request, it will be it will be exposed using a
* {@link HandlerFunction} that handles GET, HEAD, and OPTIONS requests.
* @param lookupFunction the function to provide a {@link Resource} given the {@link ServerRequest}
* @param headersConsumer provides access to the HTTP headers for served resources
* @return a router function that routes to resources
* @since 6.1
*/
public static RouterFunction<ServerResponse> resources(Function<ServerRequest, Mono<Resource>> lookupFunction,
BiConsumer<Resource, HttpHeaders> headersConsumer) {
return new ResourcesRouterFunction(lookupFunction, headersConsumer);
}

/**
Expand Down Expand Up @@ -658,7 +687,21 @@ public interface Builder {
* @return this builder
*/
Builder resources(String pattern, Resource location);
Builder resources(String pattern, Resource location, ResourceCacheLookupStrategy resourceCacheLookupStrategy);

/**
* Route requests that match the given pattern to resources relative to the given root location.
* For instance
* <pre class="code">
* Resource location = new FileSystemResource("public-resources/");
* RouterFunction&lt;ServerResponse&gt; resources = RouterFunctions.resources("/resources/**", location);
* </pre>
* @param pattern the pattern to match
* @param location the location directory relative to which resources should be resolved
* @param headersConsumer provides access to the HTTP headers for served resources
* @return this builder
* @since 6.1
*/
Builder resources(String pattern, Resource location, BiConsumer<Resource, HttpHeaders> headersConsumer);

/**
* Route to resources using the provided lookup function. If the lookup function provides a
Expand All @@ -668,7 +711,17 @@ public interface Builder {
* @return this builder
*/
Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction);
Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy resourceCacheLookupStrategy);

/**
* Route to resources using the provided lookup function. If the lookup function provides a
* {@link Resource} for the given request, it will be it will be exposed using a
* {@link HandlerFunction} that handles GET, HEAD, and OPTIONS requests.
* @param lookupFunction the function to provide a {@link Resource} given the {@link ServerRequest}
* @param headersConsumer provides access to the HTTP headers for served resources
* @return this builder
* @since 6.1
*/
Builder resources(Function<ServerRequest, Mono<Resource>> lookupFunction, BiConsumer<Resource, HttpHeaders> headersConsumer);

/**
* Route to the supplied router function if the given request predicate applies. This method
Expand Down Expand Up @@ -1150,22 +1203,20 @@ private static class ResourcesRouterFunction extends AbstractRouterFunction<Ser

private final Function<ServerRequest, Mono<Resource>> lookupFunction;

private final ResourceCacheLookupStrategy lookupStrategy;
private final BiConsumer<Resource, HttpHeaders> headersConsumer;

public ResourcesRouterFunction(Function<ServerRequest, Mono<Resource>> lookupFunction) {
this(lookupFunction, ResourceCacheLookupStrategy.noCaching());
}

public ResourcesRouterFunction(Function<ServerRequest, Mono<Resource>> lookupFunction, ResourceCacheLookupStrategy lookupStrategy) {
public ResourcesRouterFunction(Function<ServerRequest, Mono<Resource>> lookupFunction,
BiConsumer<Resource, HttpHeaders> headersConsumer) {
Assert.notNull(lookupFunction, "Function must not be null");
Assert.notNull(lookupStrategy, "Strategy must not be null");
Assert.notNull(headersConsumer, "HeadersConsumer must not be null");
this.lookupFunction = lookupFunction;
this.lookupStrategy = lookupStrategy;
this.headersConsumer = headersConsumer;
}

@Override
public Mono<HandlerFunction<ServerResponse>> route(ServerRequest request) {
return this.lookupFunction.apply(request).map(resource -> new ResourceHandlerFunction(resource, this.lookupStrategy));
return this.lookupFunction.apply(request).map(resource -> new ResourceHandlerFunction(resource, this.headersConsumer));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,7 +47,7 @@ public class ResourceHandlerFunctionTests {

private final Resource resource = new ClassPathResource("response.txt", getClass());

private final ResourceHandlerFunction handlerFunction = new ResourceHandlerFunction(this.resource);
private final ResourceHandlerFunction handlerFunction = new ResourceHandlerFunction(this.resource, (r, h) -> {});

private ServerResponse.Context context;

Expand Down

0 comments on commit 307a2c7

Please sign in to comment.