Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Nov 12, 2018
1 parent c58da71 commit 3a66927
Show file tree
Hide file tree
Showing 20 changed files with 77 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
public static final int DEFAULT_CACHE_LIMIT = 1024;

/** Static evaluation context to reuse. */
private static EvaluationContext messageEvalContext =
private static final EvaluationContext messageEvalContext =
SimpleEvaluationContext.forPropertyAccessors(new SimpMessageHeaderPropertyAccessor()).build();


Expand Down Expand Up @@ -130,7 +130,7 @@ public int getCacheLimit() {
* @since 4.2
*/
public void setSelectorHeaderName(@Nullable String selectorHeaderName) {
this.selectorHeaderName = StringUtils.hasText(selectorHeaderName) ? selectorHeaderName : null;
this.selectorHeaderName = (StringUtils.hasText(selectorHeaderName) ? selectorHeaderName : null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1017,9 +1017,10 @@ public void setHost(@Nullable InetSocketAddress host) {
}

/**
* Return the value of the required {@code Host} header.
* <p>If the header value does not contain a port, the returned
* {@linkplain InetSocketAddress#getPort() port} will be {@code 0}.
* Return the value of the {@code Host} header, if available.
* <p>If the header value does not contain a port, the
* {@linkplain InetSocketAddress#getPort() port} in the returned address will
* be {@code 0}.
* @since 5.0
*/
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ default <T> T getAttribute(String name) {
@SuppressWarnings("unchecked")
default <T> T getRequiredAttribute(String name) {
T value = getAttribute(name);
Assert.notNull(value, () -> "Required attribute '" + name + "' is missing.");
Assert.notNull(value, () -> "Required attribute '" + name + "' is missing");
return value;
}

Expand Down Expand Up @@ -124,7 +124,6 @@ default <T> T getAttributeOrDefault(String name, T defaultValue) {
/**
* Return the form data from the body of the request if the Content-Type is
* {@code "application/x-www-form-urlencoded"} or an empty map otherwise.
*
* <p><strong>Note:</strong> calling this method causes the request body to
* be read and parsed in full and the resulting {@code MultiValueMap} is
* cached so that this method is safe to call more than once.
Expand All @@ -134,7 +133,6 @@ default <T> T getAttributeOrDefault(String name, T defaultValue) {
/**
* Return the parts of a multipart request if the Content-Type is
* {@code "multipart/form-data"} or an empty map otherwise.
*
* <p><strong>Note:</strong> calling this method causes the request body to
* be read and parsed in full and the resulting {@code MultiValueMap} is
* cached so that this method is safe to call more than once.
Expand All @@ -150,10 +148,9 @@ default <T> T getAttributeOrDefault(String name, T defaultValue) {
/**
* Return the {@link ApplicationContext} associated with the web application,
* if it was initialized with one via
* {@link org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext
* WebHttpHandlerBuilder#applicationContext}.
* {@link org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext}.
* @since 5.0.3
* @see org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext(ApplicationContext)
* @see org.springframework.web.server.adapter.WebHttpHandlerBuilder#applicationContext
*/
@Nullable
ApplicationContext getApplicationContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public static <T> BodyInserter<T, ReactiveHttpOutputMessage> fromObject(T body)
public static <T, P extends Publisher<T>> BodyInserter<P, ReactiveHttpOutputMessage> fromPublisher(
P publisher, Class<T> elementClass) {

Assert.notNull(publisher, "Publisher must not be null");
Assert.notNull(elementClass, "Element Class must not be null");
return (message, context) ->
writeWithMessageWriters(message, context, publisher, ResolvableType.forClass(elementClass));
}
Expand All @@ -109,16 +111,18 @@ public static <T, P extends Publisher<T>> BodyInserter<P, ReactiveHttpOutputMess
* {@link org.springframework.web.reactive.function.client.WebClient WebClient} and
* {@link org.springframework.web.reactive.function.server.ServerResponse ServerResponse}.
* @param publisher the publisher to write with
* @param typeRef the type of elements contained in the publisher
* @param typeReference the type of elements contained in the publisher
* @param <T> the type of the elements contained in the publisher
* @param <P> the {@code Publisher} type
* @return the inserter to write a {@code Publisher}
*/
public static <T, P extends Publisher<T>> BodyInserter<P, ReactiveHttpOutputMessage> fromPublisher(
P publisher, ParameterizedTypeReference<T> typeRef) {
P publisher, ParameterizedTypeReference<T> typeReference) {

Assert.notNull(publisher, "Publisher must not be null");
Assert.notNull(typeReference, "ParameterizedTypeReference must not be null");
return (message, context) ->
writeWithMessageWriters(message, context, publisher, ResolvableType.forType(typeRef.getType()));
writeWithMessageWriters(message, context, publisher, ResolvableType.forType(typeReference.getType()));
}

/**
Expand All @@ -130,6 +134,7 @@ public static <T, P extends Publisher<T>> BodyInserter<P, ReactiveHttpOutputMess
* @return the inserter to write a {@code Publisher}
*/
public static <T extends Resource> BodyInserter<T, ReactiveHttpOutputMessage> fromResource(T resource) {
Assert.notNull(resource, "Resource must not be null");
return (outputMessage, context) -> {
ResolvableType elementType = RESOURCE_TYPE;
HttpMessageWriter<Resource> writer = findWriter(context, elementType, null);
Expand All @@ -151,6 +156,7 @@ public static <T extends Resource> BodyInserter<T, ReactiveHttpOutputMessage> fr
public static <T, S extends Publisher<ServerSentEvent<T>>> BodyInserter<S, ServerHttpResponse> fromServerSentEvents(
S eventsPublisher) {

Assert.notNull(eventsPublisher, "Publisher must not be null");
return (serverResponse, context) -> {
ResolvableType elmentType = SSE_TYPE;
MediaType mediaType = MediaType.TEXT_EVENT_STREAM;
Expand All @@ -163,13 +169,11 @@ public static <T, S extends Publisher<ServerSentEvent<T>>> BodyInserter<S, Serve
* Return a {@link FormInserter} to write the given {@code MultiValueMap}
* as URL-encoded form data. The returned inserter allows for additional
* entries to be added via {@link FormInserter#with(String, Object)}.
*
* <p>Note that you can also use the {@code syncBody(Object)} method in the
* request builders of both the {@code WebClient} and {@code WebTestClient}.
* In that case the setting of the request content type is also not required,
* just be sure the map contains String values only or otherwise it would be
* interpreted as a multipart request.
*
* @param formData the form data to write to the output message
* @return the inserter that allows adding more form data
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ default Optional<Object> attribute(String name) {
String logPrefix();

/**
* Writes this request to the given {@link ClientHttpRequest}.
*
* Write this request to the given {@link ClientHttpRequest}.
* @param request the client http request to write to
* @param strategies the strategies to use when writing
* @return {@code Mono<Void>} to indicate when writing is complete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
private BodyInserter<?, ? super ClientHttpRequest> body = BodyInserters.empty();


public DefaultClientRequestBuilder(HttpMethod method, URI url) {
Assert.notNull(method, "HttpMethod must not be null");
Assert.notNull(url, "URI must not be null");
this.method = method;
this.url = url;
}

public DefaultClientRequestBuilder(ClientRequest other) {
Assert.notNull(other, "ClientRequest must not be null");
this.method = other.method();
Expand All @@ -81,17 +74,24 @@ public DefaultClientRequestBuilder(ClientRequest other) {
body(other.body());
}

public DefaultClientRequestBuilder(HttpMethod method, URI url) {
Assert.notNull(method, "HttpMethod must not be null");
Assert.notNull(url, "URI must not be null");
this.method = method;
this.url = url;
}


@Override
public ClientRequest.Builder method(HttpMethod method) {
Assert.notNull(method, "'method' must not be null");
Assert.notNull(method, "HttpMethod must not be null");
this.method = method;
return this;
}

@Override
public ClientRequest.Builder url(URI url) {
Assert.notNull(url, "'url' must not be null");
Assert.notNull(url, "URI must not be null");
this.url = url;
return this;
}
Expand Down Expand Up @@ -126,9 +126,6 @@ public ClientRequest.Builder cookies(Consumer<MultiValueMap<String, String>> coo

@Override
public <S, P extends Publisher<S>> ClientRequest.Builder body(P publisher, Class<S> elementClass) {
Assert.notNull(publisher, "'publisher' must not be null");
Assert.notNull(elementClass, "'elementClass' must not be null");

this.body = BodyInserters.fromPublisher(publisher, elementClass);
return this;
}
Expand All @@ -137,9 +134,6 @@ public <S, P extends Publisher<S>> ClientRequest.Builder body(P publisher, Class
public <S, P extends Publisher<S>> ClientRequest.Builder body(
P publisher, ParameterizedTypeReference<S> typeReference) {

Assert.notNull(publisher, "'publisher' must not be null");
Assert.notNull(typeReference, "'typeReference' must not be null");

this.body = BodyInserters.fromPublisher(publisher, typeReference);
return this;
}
Expand Down Expand Up @@ -184,7 +178,6 @@ private static class BodyInserterRequest implements ClientRequest {

private final String logPrefix;


public BodyInserterRequest(HttpMethod method, URI url, HttpHeaders headers,
MultiValueMap<String, String> cookies, BodyInserter<?, ? super ClientHttpRequest> body,
Map<String, Object> attributes) {
Expand All @@ -200,7 +193,6 @@ public BodyInserterRequest(HttpMethod method, URI url, HttpHeaders headers,
this.logPrefix = "[" + id + "] ";
}


@Override
public HttpMethod method() {
return this.method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ public static ExchangeFilterFunction basicAuthentication(String user, String pas
@Deprecated
public static ExchangeFilterFunction basicAuthentication() {
return (request, next) -> {
Credentials cred = (Credentials) request
.attribute(BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE).orElse(null);
if (cred != null) {
Object attr = request.attributes().get(BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE);
if (attr instanceof Credentials) {
Credentials cred = (Credentials) attr;
return next.exchange(ClientRequest.from(request)
.headers(headers -> headers.setBasicAuth(cred.username, cred.password))
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ public ServerRequest.Builder cookies(Consumer<MultiValueMap<String, HttpCookie>>

@Override
public ServerRequest.Builder body(Flux<DataBuffer> body) {
Assert.notNull(body, "'body' must not be null");
Assert.notNull(body, "Body must not be null");
releaseBody();
this.body = body;
return this;
}

@Override
public ServerRequest.Builder body(String body) {
Assert.notNull(body, "'body' must not be null");
Assert.notNull(body, "Body must not be null");
releaseBody();
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
this.body = Flux.just(body).
Expand All @@ -165,7 +165,6 @@ private void releaseBody() {

@Override
public ServerRequest.Builder attribute(String name, Object value) {
Assert.notNull(name, "'name' must not be null");
this.attributes.put(name, value);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

/**
* Unit tests for {@link DefaultWebClient}.
*
* @author Rossen Stoyanchev
*/
public class DefaultWebClientTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

/**
* Unit tests for {@link ExchangeFilterFunctions}.
*
* @author Arjen Poutsma
*/
public class ExchangeFilterFunctionsTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

/**
* WebClient integration tests focusing on data buffer management.
*
* @author Rossen Stoyanchev
*/
public class WebClientDataBufferAllocatingTests extends AbstractDataBufferAllocatingTestCase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ public class WebClientIntegrationTests {

@Parameterized.Parameters(name = "webClient [{0}]")
public static Object[][] arguments() {

return new Object[][] {
{new JettyClientHttpConnector()},
{new ReactorClientHttpConnector()}
};
}


@Before
public void setup() {
this.server = new MockWebServer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public List<ViewResolver> viewResolvers() {
}
};


@Test
public void from() {
ServerResponse other = ServerResponse.ok().header("foo", "bar").build().block();
Expand Down Expand Up @@ -383,5 +384,4 @@ public void notModifiedLastModified() {
.verify();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@
import org.springframework.web.util.pattern.PathPattern;

import static org.junit.Assert.*;
import static org.springframework.web.reactive.function.BodyInserters.fromPublisher;
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.BodyInserters.*;
import static org.springframework.web.reactive.function.server.RouterFunctions.*;

/**
* Tests the use of {@link HandlerFunction} and {@link RouterFunction} in a
Expand Down Expand Up @@ -134,7 +133,6 @@ public AttributesHandler attributesHandler() {
return new AttributesHandler();
}


@Bean
public RouterFunction<EntityResponse<Person>> monoRouterFunction(PersonHandler personHandler) {
return route(RequestPredicates.GET("/mono"), personHandler::mono);
Expand All @@ -150,7 +148,6 @@ public RouterFunction<ServerResponse> attributesRouterFunction(AttributesHandler
return nest(RequestPredicates.GET("/attributes"),
route(RequestPredicates.GET("/{foo}"), attributesHandler::attributes));
}

}


Expand All @@ -167,16 +164,15 @@ public Mono<ServerResponse> flux(ServerRequest request) {
return ServerResponse.ok().body(
fromPublisher(Flux.just(person1, person2), Person.class));
}

}


private static class AttributesHandler {

@SuppressWarnings("unchecked")
public Mono<ServerResponse> attributes(ServerRequest request) {
assertTrue(request.attributes().containsKey(RouterFunctions.REQUEST_ATTRIBUTE));
assertTrue(request.attributes()
.containsKey(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));
assertTrue(request.attributes().containsKey(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));

Map<String, String> pathVariables =
(Map<String, String>) request.attributes().get(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
Expand All @@ -203,9 +199,9 @@ public Mono<ServerResponse> attributes(ServerRequest request) {

return ServerResponse.ok().build();
}

}


@Controller
public static class PersonController {

Expand All @@ -216,6 +212,7 @@ public Mono<Person> controller() {
}
}


private static class Person {

private String name;
Expand Down
Loading

0 comments on commit 3a66927

Please sign in to comment.