Polishing

This commit is contained in:
Juergen Hoeller
2020-06-10 11:19:52 +02:00
parent 7f87eb56d9
commit a045574418
38 changed files with 284 additions and 268 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 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.
@@ -60,8 +60,11 @@ import org.springframework.util.ObjectUtils;
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @since 4.1
* @param <T> the body type
* @see #getMethod()
* @see #getUrl()
* @see org.springframework.web.client.RestOperations#exchange(RequestEntity, Class)
* @see ResponseEntity
*/
public class RequestEntity<T> extends HttpEntity<T> {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -46,12 +46,12 @@ import org.springframework.lang.Nullable;
*/
public class ServerSentEventHttpMessageReader implements HttpMessageReader<Object> {
private static final ResolvableType STRING_TYPE = ResolvableType.forClass(String.class);
private static final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
private static final StringDecoder stringDecoder = StringDecoder.textPlainOnly();
private static final ResolvableType STRING_TYPE = ResolvableType.forClass(String.class);
@Nullable
private final Decoder<?> decoder;
@@ -130,7 +130,7 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
sseBuilder.event(line.substring(6));
}
else if (line.startsWith("retry:")) {
sseBuilder.retry(Duration.ofMillis(Long.valueOf(line.substring(6))));
sseBuilder.retry(Duration.ofMillis(Long.parseLong(line.substring(6))));
}
else if (line.startsWith(":")) {
comment = (comment != null ? comment : new StringBuilder());
@@ -143,7 +143,7 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
if (shouldWrap) {
if (comment != null) {
sseBuilder.comment(comment.toString().substring(0, comment.length() - 1));
sseBuilder.comment(comment.substring(0, comment.length() - 1));
}
return decodedData.map(o -> {
sseBuilder.data(o);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -38,6 +38,7 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* {@code HttpMessageWriter} for {@code "text/event-stream"} responses.
@@ -135,7 +136,7 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter<Objec
writeField("retry", retry.toMillis(), sb);
}
if (comment != null) {
sb.append(':').append(comment.replaceAll("\\n", "\n:")).append("\n");
sb.append(':').append(StringUtils.replace(comment, "\n", "\n:")).append("\n");
}
if (data != null) {
sb.append("data:");
@@ -147,24 +148,24 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter<Objec
});
}
private void writeField(String fieldName, Object fieldValue, StringBuilder stringBuilder) {
stringBuilder.append(fieldName);
stringBuilder.append(':');
stringBuilder.append(fieldValue.toString());
stringBuilder.append("\n");
private void writeField(String fieldName, Object fieldValue, StringBuilder sb) {
sb.append(fieldName);
sb.append(':');
sb.append(fieldValue.toString());
sb.append("\n");
}
@SuppressWarnings("unchecked")
private <T> Flux<DataBuffer> encodeData(@Nullable T data, ResolvableType valueType,
private <T> Flux<DataBuffer> encodeData(@Nullable T dataValue, ResolvableType valueType,
MediaType mediaType, DataBufferFactory factory, Map<String, Object> hints) {
if (data == null) {
if (dataValue == null) {
return Flux.empty();
}
if (data instanceof String) {
String text = (String) data;
return Flux.from(encodeText(text.replaceAll("\\n", "\ndata:") + "\n", mediaType, factory));
if (dataValue instanceof String) {
String text = (String) dataValue;
return Flux.from(encodeText(StringUtils.replace(text, "\n", "\ndata:") + "\n", mediaType, factory));
}
if (this.encoder == null) {
@@ -172,7 +173,7 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter<Objec
}
return ((Encoder<T>) this.encoder)
.encode(Mono.just(data), factory, valueType, mediaType, hints)
.encode(Mono.just(dataValue), factory, valueType, mediaType, hints)
.concatWith(encodeText("\n", mediaType, factory));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -215,14 +215,14 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
return this.cookies;
}
@Nullable
@Override
@Nullable
public InetSocketAddress getRemoteAddress() {
return this.remoteAddress;
}
@Nullable
@Override
@Nullable
protected SslInfo initSslInfo() {
return this.sslInfo;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -155,6 +155,7 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
return this.request.remoteAddress();
}
@Override
@Nullable
protected SslInfo initSslInfo() {
SslHandler sslHandler = this.request.context().channel().pipeline().get(SslHandler.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -92,12 +92,13 @@ public class ServerHttpRequestDecorator implements ServerHttpRequest {
}
@Override
@Nullable
public InetSocketAddress getRemoteAddress() {
return getDelegate().getRemoteAddress();
}
@Nullable
@Override
@Nullable
public SslInfo getSslInfo() {
return getDelegate().getSslInfo();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -42,6 +42,7 @@ import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.LinkedCaseInsensitiveMap;
@@ -168,10 +169,12 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest {
}
@Override
@NonNull
public InetSocketAddress getRemoteAddress() {
return new InetSocketAddress(this.request.getRemoteHost(), this.request.getRemotePort());
}
@Override
@Nullable
protected SslInfo initSslInfo() {
X509Certificate[] certificates = getX509Certificates();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -102,6 +102,7 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
}
@Override
@Nullable
public InetSocketAddress getRemoteAddress() {
return this.exchange.getSourceAddress();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -39,55 +39,6 @@ import org.springframework.util.StringUtils;
*/
public class DefaultUriBuilderFactory implements UriBuilderFactory {
/**
* Enum to represent multiple URI encoding strategies.
* @see #setEncodingMode
*/
public enum EncodingMode {
/**
* Pre-encode the URI template first, then strictly encode URI variables
* when expanded, with the following rules:
* <ul>
* <li>For the URI template replace <em>only</em> non-ASCII and illegal
* (within a given URI component type) characters with escaped octets.
* <li>For URI variables do the same and also replace characters with
* reserved meaning.
* </ul>
* <p>For most cases, this mode is most likely to give the expected
* result because in treats URI variables as opaque data to be fully
* encoded, while {@link #URI_COMPONENT} by comparison is useful only
* if intentionally expanding URI variables with reserved characters.
* @since 5.0.8
* @see UriComponentsBuilder#encode()
*/
TEMPLATE_AND_VALUES,
/**
* Does not encode the URI template and instead applies strict encoding
* to URI variables via {@link UriUtils#encodeUriVariables} prior to
* expanding them into the template.
* @see UriUtils#encodeUriVariables(Object...)
* @see UriUtils#encodeUriVariables(Map)
*/
VALUES_ONLY,
/**
* Expand URI variables first, and then encode the resulting URI
* component values, replacing <em>only</em> non-ASCII and illegal
* (within a given URI component type) characters, but not characters
* with reserved meaning.
* @see UriComponents#encode()
*/
URI_COMPONENT,
/**
* No encoding should be applied.
*/
NONE
}
@Nullable
private final UriComponentsBuilder baseUri;
@@ -194,16 +145,19 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
// UriTemplateHandler
@Override
public URI expand(String uriTemplate, Map<String, ?> uriVars) {
return uriString(uriTemplate).build(uriVars);
}
@Override
public URI expand(String uriTemplate, Object... uriVars) {
return uriString(uriTemplate).build(uriVars);
}
// UriBuilderFactory
@Override
public UriBuilder uriString(String uriTemplate) {
return new DefaultUriBuilder(uriTemplate);
}
@@ -214,6 +168,55 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
}
/**
* Enum to represent multiple URI encoding strategies.
* @see #setEncodingMode
*/
public enum EncodingMode {
/**
* Pre-encode the URI template first, then strictly encode URI variables
* when expanded, with the following rules:
* <ul>
* <li>For the URI template replace <em>only</em> non-ASCII and illegal
* (within a given URI component type) characters with escaped octets.
* <li>For URI variables do the same and also replace characters with
* reserved meaning.
* </ul>
* <p>For most cases, this mode is most likely to give the expected
* result because in treats URI variables as opaque data to be fully
* encoded, while {@link #URI_COMPONENT} by comparison is useful only
* if intentionally expanding URI variables with reserved characters.
* @since 5.0.8
* @see UriComponentsBuilder#encode()
*/
TEMPLATE_AND_VALUES,
/**
* Does not encode the URI template and instead applies strict encoding
* to URI variables via {@link UriUtils#encodeUriVariables} prior to
* expanding them into the template.
* @see UriUtils#encodeUriVariables(Object...)
* @see UriUtils#encodeUriVariables(Map)
*/
VALUES_ONLY,
/**
* Expand URI variables first, and then encode the resulting URI
* component values, replacing <em>only</em> non-ASCII and illegal
* (within a given URI component type) characters, but not characters
* with reserved meaning.
* @see UriComponents#encode()
*/
URI_COMPONENT,
/**
* No encoding should be applied.
*/
NONE
}
/**
* {@link DefaultUriBuilderFactory} specific implementation of UriBuilder.
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.util;
/**
@@ -22,13 +23,14 @@ package org.springframework.web.util;
*
* @author Rossen Stoyanchev
* @since 5.0
* @see DefaultUriBuilderFactory
*/
public interface UriBuilderFactory extends UriTemplateHandler {
/**
* Initialize a builder with the given URI template.
* @param uriTemplate the URI template to use
* @return the URI builder instance
* @return the builder instance
*/
UriBuilder uriString(String uriTemplate);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 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.
@@ -97,8 +97,8 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
return this.remoteAddress;
}
@Nullable
@Override
@Nullable
protected SslInfo initSslInfo() {
return this.sslInfo;
}
@@ -342,9 +342,9 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
* @see BodyBuilder#body(String)
*/
MockServerHttpRequest build();
}
/**
* A builder that adds a body to the request.
*/
@@ -383,7 +383,6 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
* @return the built request entity
*/
MockServerHttpRequest body(String body);
}
@@ -391,7 +390,6 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
private static final DataBufferFactory BUFFER_FACTORY = new DefaultDataBufferFactory();
private final HttpMethod method;
private final URI url;
@@ -411,7 +409,6 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
@Nullable
private SslInfo sslInfo;
public DefaultBodyBuilder(HttpMethod method, URI url) {
this.method = method;
this.url = url;
@@ -558,13 +555,11 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
private URI getUrlToUse() {
MultiValueMap<String, String> params =
this.queryParamsBuilder.buildAndExpand().encode().getQueryParams();
if (!params.isEmpty()) {
return UriComponentsBuilder.fromUri(this.url).queryParams(params).build(true).toUri();
}
return this.url;
}
}
}
}