Allow serving static files from RouterFunctions

This commit adds the ability to serve Resources (static files) through a
RouterFunction. Two methods have been added to RouterFunctions: one that
exposes a given directory given a path pattern, and a generic method
that requires a lookup function.

Issue: SPR-14913
This commit is contained in:
Arjen Poutsma
2016-12-01 11:40:17 +01:00
parent 20c60650ee
commit 136b33bc4a
14 changed files with 671 additions and 48 deletions

View File

@@ -30,6 +30,7 @@ import org.springframework.util.Assert;
*
* @author Arjen Poutsma
* @since 5.0
* @see BodyInserters
*/
public interface BodyInserter<T, M extends ReactiveHttpOutputMessage> {

View File

@@ -48,6 +48,16 @@ public abstract class BodyInserters {
private static final ResolvableType SERVER_SIDE_EVENT_TYPE =
ResolvableType.forClass(ServerSentEvent.class);
/**
* Return an empty {@code BodyInserter} that writes nothing.
* @return an empty {@code BodyInserter}
*/
public static <T> BodyInserter<T, ReactiveHttpOutputMessage> empty() {
return BodyInserter.of(
(response, context) -> response.setComplete(),
() -> null);
}
/**
* Return a {@code BodyInserter} that writes the given single object.
* @param body the body of the response

View File

@@ -18,6 +18,7 @@ package org.springframework.web.util;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import org.springframework.util.Assert;
@@ -183,8 +184,26 @@ public abstract class UriUtils {
* @see java.net.URLDecoder#decode(String, String)
*/
public static String decode(String source, String encoding) throws UnsupportedEncodingException {
Assert.notNull(source, "Source must not be null");
Assert.hasLength(encoding, "Encoding must not be empty");
return decode(source, Charset.forName(encoding));
}
/**
* Decodes the given encoded source String into an URI. Based on the following rules:
* <ul>
* <li>Alphanumeric characters {@code "a"} through {@code "z"}, {@code "A"} through {@code "Z"}, and
* {@code "0"} through {@code "9"} stay the same.</li>
* <li>Special characters {@code "-"}, {@code "_"}, {@code "."}, and {@code "*"} stay the same.</li>
* <li>A sequence "{@code %<i>xy</i>}" is interpreted as a hexadecimal representation of the character.</li>
* </ul>
* @param source the source string
* @param charset the character set
* @return the decoded URI
* @throws IllegalArgumentException when the given source contains invalid encoded sequences
* @see java.net.URLDecoder#decode(String, String)
*/
public static String decode(String source, Charset charset) {
Assert.notNull(source, "'source' must not be null");
Assert.notNull(charset, "'charset' must not be null");
int length = source.length();
ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
boolean changed = false;
@@ -211,7 +230,7 @@ public abstract class UriUtils {
bos.write(ch);
}
}
return (changed ? new String(bos.toByteArray(), encoding) : source);
return (changed ? new String(bos.toByteArray(), charset) : source);
}
/**