Sonar Fixes

Critical smells `o.s.i.h*`.
This commit is contained in:
Gary Russell
2018-12-07 11:13:45 -05:00
committed by Artem Bilan
parent 536b6b1786
commit 4760c54097
14 changed files with 85 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-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.
@@ -25,6 +25,7 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.Lifecycle;
import org.springframework.integration.handler.support.MessagingMethodInvokerHelper;
import org.springframework.integration.util.AbstractExpressionEvaluator;
import org.springframework.lang.NonNull;
import org.springframework.messaging.Message;
/**
@@ -32,6 +33,7 @@ import org.springframework.messaging.Message;
*
* @author Dave Syer
* @author Artem Bilan
* @author Gary Russell
* @since 2.0
*/
public class MethodInvokingMessageListProcessor<T> extends AbstractExpressionEvaluator
@@ -61,7 +63,7 @@ public class MethodInvokingMessageListProcessor<T> extends AbstractExpressionEva
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
public void setBeanFactory(@NonNull BeanFactory beanFactory) {
super.setBeanFactory(beanFactory);
this.delegate.setBeanFactory(beanFactory);
}
@@ -77,6 +79,7 @@ public class MethodInvokingMessageListProcessor<T> extends AbstractExpressionEva
this.delegate.setUseSpelInvoker(useSpelInvoker);
}
@Override
public String toString() {
return this.delegate.toString();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-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.
@@ -23,6 +23,7 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.Lifecycle;
import org.springframework.core.convert.ConversionService;
import org.springframework.integration.handler.support.MessagingMethodInvokerHelper;
import org.springframework.lang.NonNull;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandlingException;
@@ -37,6 +38,7 @@ import org.springframework.messaging.MessageHandlingException;
*
* @author Dave Syer
* @author Artem Bilan
* @author Gary Russell
*
* @since 2.0
*/
@@ -67,7 +69,7 @@ public class MethodInvokingMessageProcessor<T> extends AbstractMessageProcessor<
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
public void setBeanFactory(@NonNull BeanFactory beanFactory) {
super.setBeanFactory(beanFactory);
this.delegate.setBeanFactory(beanFactory);
}

View File

@@ -86,18 +86,25 @@ public class ErrorMessageSendingRecoverer extends ErrorMessagePublisher implemen
@Override
protected Throwable payloadWhenNull(AttributeAccessor context) {
return new RetryExceptionNotAvailableException(
(Message<?>) context.getAttribute(ErrorMessageUtils.FAILED_MESSAGE_CONTEXT_KEY),
"No retry exception available; " +
"this can occur, for example, if the RetryPolicy allowed zero attempts " +
"to execute the handler; " +
"RetryContext: " + context.toString());
Message<?> message = (Message<?>) context.getAttribute(ErrorMessageUtils.FAILED_MESSAGE_CONTEXT_KEY);
String description = "No retry exception available; " +
"this can occur, for example, if the RetryPolicy allowed zero attempts " +
"to execute the handler; " +
"RetryContext: " + context.toString();
return message == null
? new RetryExceptionNotAvailableException(description)
: new RetryExceptionNotAvailableException(message, description);
}
public static class RetryExceptionNotAvailableException extends MessagingException {
private static final long serialVersionUID = 1L;
RetryExceptionNotAvailableException(String description) {
super(description);
}
public RetryExceptionNotAvailableException(Message<?> message, String description) {
super(message, description);
}

View File

@@ -171,10 +171,6 @@ public class IdempotentReceiverInterceptor extends AbstractHandleMessageAdvice {
private MessageChannel obtainDiscardChannel() {
if (this.discardChannel == null) {
if (this.discardChannelName != null) {
if (getChannelResolver() == null) {
throw new IllegalStateException("No channel resolver available to resolve the discard channel '"
+ this.discardChannelName + "'");
}
this.discardChannel = getChannelResolver()
.resolveDestination(this.discardChannelName);
}

View File

@@ -61,6 +61,7 @@ public class SpelExpressionRetryStateGenerator implements RetryStateGenerator, B
}
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(beanFactory);
}
@@ -69,11 +70,13 @@ public class SpelExpressionRetryStateGenerator implements RetryStateGenerator, B
this.classifier = classifier;
}
@Override
public RetryState determineRetryState(Message<?> message) {
Boolean forceRefresh = this.forceRefreshExpression == null
? Boolean.FALSE
: this.forceRefreshExpression.getValue(this.evaluationContext, message, Boolean.class);
return new DefaultRetryState(this.keyExpression.getValue(this.evaluationContext, message),
this.forceRefreshExpression == null
? false
: this.forceRefreshExpression.getValue(this.evaluationContext, message, Boolean.class),
forceRefresh == null ? false : forceRefresh,
this.classifier);
}

View File

@@ -84,6 +84,7 @@ import org.springframework.integration.util.ClassUtils;
import org.springframework.integration.util.FixedMethodFilter;
import org.springframework.integration.util.MessagingAnnotationUtils;
import org.springframework.integration.util.UniqueMethodFilter;
import org.springframework.lang.NonNull;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.MessageHeaders;
@@ -155,7 +156,8 @@ public class MessagingMethodInvokerHelper<T> extends AbstractExpressionEvaluator
private static final Collection<Message<?>> dummyMessages = Collections.emptyList();
private static final TypeDescriptor messageListTypeDescriptor =
new TypeDescriptor(ReflectionUtils.findField(MessagingMethodInvokerHelper.class, "dummyMessages"));
new TypeDescriptor(ReflectionUtils.findField(MessagingMethodInvokerHelper.class, // NOSONAR never null
"dummyMessages"));
private static final TypeDescriptor messageArrayTypeDescriptor = TypeDescriptor.valueOf(Message[].class);
@@ -291,7 +293,7 @@ public class MessagingMethodInvokerHelper<T> extends AbstractExpressionEvaluator
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
public void setBeanFactory(@NonNull BeanFactory beanFactory) {
super.setBeanFactory(beanFactory);
this.messageHandlerMethodFactory.setBeanFactory(beanFactory);
if (beanFactory instanceof ConfigurableListableBeanFactory) {

View File

@@ -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.
@@ -32,6 +32,7 @@ import org.springframework.util.StringUtils;
* as a SpEL expression against {@code message} and converting result to expected parameter type.
*
* @author Artem Bilan
* @author Gary Russell
*
* @since 5.0
*
@@ -53,7 +54,7 @@ public class PayloadExpressionArgumentResolver extends AbstractExpressionEvaluat
Expression expression = this.expressionCache.get(parameter);
if (expression == null) {
Payload ann = parameter.getParameterAnnotation(Payload.class);
expression = EXPRESSION_PARSER.parseExpression(ann.expression());
expression = EXPRESSION_PARSER.parseExpression(ann.expression()); // NOSONAR never null - supportsParameter()
this.expressionCache.put(parameter, expression);
}
return evaluateExpression(expression, message.getPayload(), parameter.getParameterType());

View File

@@ -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.
@@ -62,7 +62,7 @@ public class PayloadsArgumentResolver extends AbstractExpressionEvaluator
Collection<Message<?>> messages = (Collection<Message<?>>) payload;
if (!this.expressionCache.containsKey(parameter)) {
Payloads payloads = parameter.getParameterAnnotation(Payloads.class);
Payloads payloads = parameter.getParameterAnnotation(Payloads.class); // NOSONAR never null - supportsParameter()
String expression = payloads.value();
if (StringUtils.hasText(expression)) {
this.expressionCache.put(parameter, EXPRESSION_PARSER.parseExpression("![payload." + expression + "]"));

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.http.config;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -55,6 +56,9 @@ class IntegrationGraphControllerRegistrar implements ImportBeanDefinitionRegistr
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
Map<String, Object> annotationAttributes =
importingClassMetadata.getAnnotationAttributes(EnableIntegrationGraphController.class.getName());
if (annotationAttributes == null) {
annotationAttributes = Collections.emptyMap(); // To satisfy sonar for subsequent references
}
if (!registry.containsBeanDefinition(IntegrationContextUtils.INTEGRATION_GRAPH_SERVER_BEAN_NAME)) {
registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_GRAPH_SERVER_BEAN_NAME,

View File

@@ -52,13 +52,15 @@ import org.springframework.util.CollectionUtils;
*/
public class BaseHttpInboundEndpoint extends MessagingGatewaySupport implements OrderlyShutdownCapable {
protected static final boolean jaxb2Present = ClassUtils.isPresent("javax.xml.bind.Binder",
BaseHttpInboundEndpoint.class.getClassLoader());
protected static final boolean jaxb2Present = // NOSONAR lower case static
ClassUtils.isPresent("javax.xml.bind.Binder",
BaseHttpInboundEndpoint.class.getClassLoader());
protected static final boolean romeToolsPresent = ClassUtils.isPresent("com.rometools.rome.feed.atom.Feed",
BaseHttpInboundEndpoint.class.getClassLoader());
protected static final boolean romeToolsPresent = // NOSONAR lower case static
ClassUtils.isPresent("com.rometools.rome.feed.atom.Feed",
BaseHttpInboundEndpoint.class.getClassLoader());
protected static final List<HttpMethod> nonReadableBodyHttpMethods =
protected static final List<HttpMethod> nonReadableBodyHttpMethods = // NOSONAR lower case static
Arrays.asList(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS);
protected final boolean expectReply;
@@ -327,6 +329,8 @@ public class BaseHttpInboundEndpoint extends MessagingGatewaySupport implements
* @return true or false if HTTP request can contain the body
*/
protected boolean isReadable(HttpRequest request) {
return !(CollectionUtils.containsInstance(nonReadableBodyHttpMethods, request.getMethod()));
HttpMethod method = request.getMethod();
return method == null ? false : !(CollectionUtils.containsInstance(nonReadableBodyHttpMethods, method));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-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.
@@ -34,6 +34,7 @@ import org.springframework.core.ResolvableType;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
@@ -339,14 +340,19 @@ public abstract class HttpRequestHandlingEndpointSupport extends BaseHttpInbound
.copyHeadersIfAbsent(headers);
}
else {
Assert.state(payload != null, "payload cannot be null");
messageBuilder = this.getMessageBuilderFactory().withPayload(payload).copyHeaders(headers);
}
HttpMethod method = httpEntity.getMethod();
if (method != null) {
messageBuilder.setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_METHOD,
method.toString());
}
Message<?> message = messageBuilder
.setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_URL,
httpEntity.getUrl().toString())
.setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_METHOD,
httpEntity.getMethod().toString())
.setHeader(org.springframework.integration.http.HttpHeaders.USER_PRINCIPAL,
servletRequest.getUserPrincipal())
.build();

View File

@@ -26,9 +26,11 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
@@ -76,6 +78,7 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
* them during the {@link BaseHttpInboundEndpoint} destruction.
*
* @author Artem Bilan
* @author Gary Russell
*
* @since 3.0
*
@@ -100,10 +103,12 @@ public final class IntegrationRequestMappingHandlerMapping extends RequestMappin
}
@Override
@SuppressWarnings("unchecked")
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
if (isHandler(bean.getClass())) {
unregisterMapping(getMappingForEndpoint((BaseHttpInboundEndpoint) bean));
RequestMappingInfo mapping = getMappingForEndpoint((BaseHttpInboundEndpoint) bean);
if (mapping != null) {
unregisterMapping(mapping);
}
}
}
@@ -141,11 +146,19 @@ public final class IntegrationRequestMappingHandlerMapping extends RequestMappin
}
@Override
protected void detectHandlerMethods(Object handler) {
protected void detectHandlerMethods(Object handlerArg) {
Object handler = handlerArg;
if (handler instanceof String) {
handler = this.getApplicationContext().getBean((String) handler);
ApplicationContext applicationContext = getApplicationContext();
if (applicationContext != null) {
handler = applicationContext.getBean((String) handler);
}
else {
throw new IllegalStateException("No application context available to lookup bean '"
+ handler + "'");
}
}
RequestMappingInfo mapping = this.getMappingForEndpoint((BaseHttpInboundEndpoint) handler);
RequestMappingInfo mapping = getMappingForEndpoint((BaseHttpInboundEndpoint) handler);
if (mapping != null) {
registerMapping(mapping, handler, HANDLE_REQUEST_METHOD);
}
@@ -196,6 +209,7 @@ public final class IntegrationRequestMappingHandlerMapping extends RequestMappin
* 'Spring Integration HTTP Inbound Endpoint' {@link RequestMapping}.
* @see RequestMappingHandlerMapping#getMappingForMethod
*/
@Nullable
private RequestMappingInfo getMappingForEndpoint(BaseHttpInboundEndpoint endpoint) {
final RequestMapping requestMapping = endpoint.getRequestMapping();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-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.
@@ -29,6 +29,7 @@ import org.springframework.web.util.WebUtils;
* content directly as either a String or byte array depending on the Content-Type.
*
* @author Mark Fisher
* @author Gary Russell
* @since 2.0
*/
public class SimpleMultipartFileReader implements MultipartFileReader<Object> {
@@ -49,8 +50,9 @@ public class SimpleMultipartFileReader implements MultipartFileReader<Object> {
@Override
public Object readMultipartFile(MultipartFile multipartFile) throws IOException {
if (multipartFile.getContentType() != null && multipartFile.getContentType().startsWith("text")) {
MediaType contentType = MediaType.parseMediaType(multipartFile.getContentType());
String mpContentType = multipartFile.getContentType();
if (mpContentType != null && mpContentType.startsWith("text")) {
MediaType contentType = MediaType.parseMediaType(mpContentType);
Charset charset = contentType.getCharset();
if (charset == null) {
charset = this.defaultCharset;

View File

@@ -325,7 +325,7 @@ public abstract class AbstractHttpRequestExecutingMessageHandler extends Abstrac
Object responseBody = httpResponse.getBody();
replyBuilder = (responseBody instanceof Message<?>)
? messageBuilderFactory.fromMessage((Message<?>) responseBody)
: messageBuilderFactory.withPayload(responseBody);
: messageBuilderFactory.withPayload(responseBody); // NOSONAR - hasBody()
}
else {