Skip to content

Fix code smell in websocket and webflux modules #2669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected Converter<T, U> getConverter() {
}

@Override
protected void onInit() throws Exception {
protected void onInit() throws Exception { // NOSONAR thrown by super class
super.onInit();
Assert.notNull(this.converter, () -> getClass().getName() + " requires a Converter<Object, Object>");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void afterPropertiesSet() {
String filename = this.script.getFilename();
int index =
filename != null
? filename.lastIndexOf(".") + 1
? filename.lastIndexOf('.') + 1
: -1;
if (index < 1) {
throw new BeanCreationException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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,8 @@ public boolean supports(HandlerResult result) {
@Override
@SuppressWarnings("unchecked")
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
return (Mono<Void>) result.getReturnValue();
Object returnValue = result.getReturnValue();
return returnValue == null ? Mono.empty() : (Mono<Void>) returnValue;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public class WebFluxInboundEndpoint extends BaseHttpInboundEndpoint implements W

private static final MediaType MEDIA_TYPE_APPLICATION_ALL = new MediaType("application");

private static final String UNCHECKED = "unchecked";

private static final List<HttpMethod> SAFE_METHODS = Arrays.asList(HttpMethod.GET, HttpMethod.HEAD);

private ServerCodecConfigurer codecConfigurer = ServerCodecConfigurer.create();
Expand Down Expand Up @@ -130,11 +132,6 @@ public String getComponentType() {
return super.getComponentType().replaceFirst("http", "webflux");
}

@Override
protected void onInit() throws Exception {
super.onInit();
}

@Override
public Mono<Void> handle(ServerWebExchange exchange) {
return Mono.defer(() -> {
Expand All @@ -147,7 +144,6 @@ public Mono<Void> handle(ServerWebExchange exchange) {
});
}

@SuppressWarnings("unchecked")
private Mono<Void> doHandle(ServerWebExchange exchange) {
return extractRequestBody(exchange)
.doOnSubscribe(s -> this.activeCount.incrementAndGet())
Expand All @@ -170,7 +166,7 @@ private Mono<Void> doHandle(ServerWebExchange exchange) {

}

@SuppressWarnings("unchecked")
@SuppressWarnings(UNCHECKED)
private <T> Mono<T> extractRequestBody(ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
Expand Down Expand Up @@ -201,7 +197,8 @@ else if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {

Class<?> resolvedType = bodyType.resolve();

ReactiveAdapter adapter = (resolvedType != null ? this.adapterRegistry.getAdapter(resolvedType) : null);
ReactiveAdapter adapter = (resolvedType != null ? this.adapterRegistry.getAdapter(resolvedType) :
null);
ResolvableType elementType = (adapter != null ? bodyType.getGeneric() : bodyType);

HttpMessageReader<?> httpMessageReader = this.codecConfigurer
Expand Down Expand Up @@ -237,40 +234,14 @@ else if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
}
}

@SuppressWarnings("unchecked")
@SuppressWarnings(UNCHECKED)
private Mono<Tuple2<Message<Object>, RequestEntity<?>>> buildMessage(RequestEntity<?> httpEntity,
ServerWebExchange exchange) {

ServerHttpRequest request = exchange.getRequest();
HttpHeaders requestHeaders = request.getHeaders();
Map<String, Object> exchangeAttributes = exchange.getAttributes();

StandardEvaluationContext evaluationContext = createEvaluationContext();

evaluationContext.setVariable("requestAttributes", exchangeAttributes);
MultiValueMap<String, String> requestParams = request.getQueryParams();
evaluationContext.setVariable("requestParams", requestParams);
evaluationContext.setVariable("requestHeaders", requestHeaders);
if (!CollectionUtils.isEmpty(request.getCookies())) {
evaluationContext.setVariable("cookies", request.getCookies());
}

Map<String, String> pathVariables =
(Map<String, String>) exchangeAttributes.get(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);

if (!CollectionUtils.isEmpty(pathVariables)) {
evaluationContext.setVariable("pathVariables", pathVariables);
}

Map<String, MultiValueMap<String, String>> matrixVariables =
(Map<String, MultiValueMap<String, String>>) exchangeAttributes
.get(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);

if (!CollectionUtils.isEmpty(matrixVariables)) {
evaluationContext.setVariable("matrixVariables", matrixVariables);
}

evaluationContext.setRootObject(httpEntity);
StandardEvaluationContext evaluationContext = buildEvaluationContext(httpEntity, exchange);
Object payload;
if (getPayloadExpression() != null) {
payload = getPayloadExpression().getValue(evaluationContext);
Expand Down Expand Up @@ -310,10 +281,13 @@ private Mono<Tuple2<Message<Object>, RequestEntity<?>>> buildMessage(RequestEnti
.copyHeaders(headers);
}

messageBuilder
.setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_URL, request.getURI().toString())
.setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_METHOD,
request.getMethod().toString());
messageBuilder.setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_URL,
request.getURI().toString());
HttpMethod httpMethod = request.getMethod();
if (httpMethod != null) {
messageBuilder.setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_METHOD,
httpMethod.toString());
}

return exchange.getPrincipal()
.map(principal ->
Expand All @@ -324,6 +298,41 @@ private Mono<Tuple2<Message<Object>, RequestEntity<?>>> buildMessage(RequestEnti
.zipWith(Mono.just(httpEntity));
}

@SuppressWarnings(UNCHECKED)
private StandardEvaluationContext buildEvaluationContext(RequestEntity<?> httpEntity, ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
HttpHeaders requestHeaders = request.getHeaders();
MultiValueMap<String, String> requestParams = request.getQueryParams();
Map<String, Object> exchangeAttributes = exchange.getAttributes();

StandardEvaluationContext evaluationContext = createEvaluationContext();

evaluationContext.setVariable("requestAttributes", exchangeAttributes);
evaluationContext.setVariable("requestParams", requestParams);
evaluationContext.setVariable("requestHeaders", requestHeaders);
if (!CollectionUtils.isEmpty(request.getCookies())) {
evaluationContext.setVariable("cookies", request.getCookies());
}

Map<String, String> pathVariables =
(Map<String, String>) exchangeAttributes.get(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);

if (!CollectionUtils.isEmpty(pathVariables)) {
evaluationContext.setVariable("pathVariables", pathVariables);
}

Map<String, MultiValueMap<String, String>> matrixVariables =
(Map<String, MultiValueMap<String, String>>) exchangeAttributes
.get(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);

if (!CollectionUtils.isEmpty(matrixVariables)) {
evaluationContext.setVariable("matrixVariables", matrixVariables);
}

evaluationContext.setRootObject(httpEntity);
return evaluationContext;
}

private Mono<Void> populateResponse(ServerWebExchange exchange, Message<?> replyMessage) {
ServerHttpResponse response = exchange.getResponse();
getHeaderMapper().fromHeaders(replyMessage.getHeaders(), response.getHeaders());
Expand Down Expand Up @@ -379,7 +388,7 @@ private Mono<Void> populateResponse(ServerWebExchange exchange, Message<?> reply
}
}

@SuppressWarnings("unchecked")
@SuppressWarnings(UNCHECKED)
private Mono<Void> writeResponseBody(ServerWebExchange exchange, Object body) {
ResolvableType bodyType = ResolvableType.forInstance(body);
ReactiveAdapter adapter = this.adapterRegistry.getAdapter(bodyType.resolve(), body);
Expand Down Expand Up @@ -473,7 +482,7 @@ private List<MediaType> getAcceptableTypes(ServerWebExchange exchange) {
return (mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes);
}

@SuppressWarnings("unchecked")
@SuppressWarnings(UNCHECKED)
private List<MediaType> getProducibleTypes(ServerWebExchange exchange,
Supplier<List<MediaType>> producibleTypesSupplier) {

Expand All @@ -482,9 +491,9 @@ private List<MediaType> getProducibleTypes(ServerWebExchange exchange,
}

private MediaType selectMoreSpecificMediaType(MediaType acceptable, MediaType producible) {
producible = producible.copyQualityValue(acceptable);
MediaType producibleToUse = producible.copyQualityValue(acceptable);
Comparator<MediaType> comparator = MediaType.SPECIFICITY_COMPARATOR;
return (comparator.compare(acceptable, producible) <= 0 ? acceptable : producible);
return (comparator.compare(acceptable, producibleToUse) <= 0 ? acceptable : producibleToUse);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ protected boolean isHandler(Class<?> beanType) {
@Override
protected void detectHandlerMethods(Object handler) {
if (handler instanceof String) {
handler = getApplicationContext().getBean((String) handler);
handler = getApplicationContext().getBean((String) handler); // NOSONAR never null
}
RequestMappingInfo mapping = getMappingForEndpoint((WebFluxInboundEndpoint) handler);
if (mapping != null) {
registerMapping(mapping, handler, HANDLER_METHOD);
registerMapping(mapping, handler, HANDLER_METHOD); // NOSONAR never null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected Object exchange(Supplier<URI> uriSupplier, HttpMethod httpMethod, Http
.headers(headers -> headers.putAll(httpRequest.getHeaders()));

if (httpRequest.hasBody()) {
requestSpec.body(BodyInserters.fromObject(httpRequest.getBody()));
requestSpec.body(BodyInserters.fromObject(httpRequest.getBody())); // NOSONAR protected with hasBody()
}

Mono<ClientResponse> responseMono =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2018 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 @@ -16,12 +16,11 @@

package org.springframework.integration.websocket.config;

import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.beans.factory.config.BeanDefinition;
Expand Down Expand Up @@ -50,10 +49,12 @@
*/
public class WebSocketIntegrationConfigurationInitializer implements IntegrationConfigurationInitializer {

private static final Log logger = LogFactory.getLog(WebSocketIntegrationConfigurationInitializer.class);
private static final Log logger = // NOSONAR lower case
LogFactory.getLog(WebSocketIntegrationConfigurationInitializer.class);

private static final boolean servletPresent = ClassUtils.isPresent("javax.servlet.Servlet",
WebSocketIntegrationConfigurationInitializer.class.getClassLoader());
private static final boolean servletPresent = // NOSONAR lower case
ClassUtils.isPresent("javax.servlet.Servlet",
WebSocketIntegrationConfigurationInitializer.class.getClassLoader());

private static final String WEB_SOCKET_HANDLER_MAPPING_BEAN_NAME = "integrationWebSocketHandlerMapping";

Expand Down Expand Up @@ -130,11 +131,13 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
}

@Override
protected HandlerMapping createInstance() throws Exception {
Collection<WebSocketConfigurer> webSocketConfigurers =
((ListableBeanFactory) getBeanFactory()).getBeansOfType(WebSocketConfigurer.class).values();
for (WebSocketConfigurer configurer : webSocketConfigurers) {
configurer.registerWebSocketHandlers(this.registry);
protected HandlerMapping createInstance() {
BeanFactory beanFactory = getBeanFactory();
if (beanFactory != null) {
((ListableBeanFactory) beanFactory)
.getBeansOfType(WebSocketConfigurer.class)
.values()
.forEach(configurer -> configurer.registerWebSocketHandlers(this.registry));
}
if (this.registry.requiresTaskScheduler()) {
this.registry.setTaskScheduler(this.sockJsTaskScheduler);
Expand Down
Loading