Skip to content

Commit

Permalink
Fix new Sonar smells
Browse files Browse the repository at this point in the history
  • Loading branch information
artembilan committed Feb 24, 2020
1 parent 50ac603 commit 7dbdbde
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-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.
Expand All @@ -17,6 +17,7 @@
package org.springframework.integration.http.inbound;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -88,8 +89,9 @@
public final class IntegrationRequestMappingHandlerMapping extends RequestMappingHandlerMapping
implements ApplicationListener<ContextRefreshedEvent>, DestructionAwareBeanPostProcessor {

private static final Method HANDLE_REQUEST_METHOD = ReflectionUtils.findMethod(HttpRequestHandler.class,
"handleRequest", HttpServletRequest.class, HttpServletResponse.class);
private static final Method HANDLE_REQUEST_METHOD =
ReflectionUtils.findMethod(HttpRequestHandler.class, "handleRequest", HttpServletRequest.class,
HttpServletResponse.class);

private final AtomicBoolean initialized = new AtomicBoolean();

Expand Down Expand Up @@ -170,21 +172,13 @@ protected CorsConfiguration initCorsConfiguration(Object handler, Method method,
CrossOrigin crossOrigin = ((BaseHttpInboundEndpoint) handler).getCrossOrigin();
if (crossOrigin != null) {
CorsConfiguration config = new CorsConfiguration();
for (String origin : crossOrigin.getOrigin()) {
config.addAllowedOrigin(origin);
}
for (RequestMethod requestMethod : crossOrigin.getMethod()) {
config.addAllowedMethod(requestMethod.name());
}
for (String header : crossOrigin.getAllowedHeaders()) {
config.addAllowedHeader(header);
}
for (String header : crossOrigin.getExposedHeaders()) {
config.addExposedHeader(header);
}
if (crossOrigin.getAllowCredentials() != null) {
config.setAllowCredentials(crossOrigin.getAllowCredentials());
}
config.setAllowedOrigins(Arrays.asList(crossOrigin.getOrigin()));
config.setAllowedHeaders(Arrays.asList(crossOrigin.getAllowedHeaders()));
config.setExposedHeaders(Arrays.asList(crossOrigin.getExposedHeaders()));
config.setAllowCredentials(crossOrigin.getAllowCredentials());
if (crossOrigin.getMaxAge() != -1) {
config.setMaxAge(crossOrigin.getMaxAge());
}
Expand All @@ -194,7 +188,9 @@ protected CorsConfiguration initCorsConfiguration(Object handler, Method method,
}
}
if (CollectionUtils.isEmpty(config.getAllowedHeaders())) {
for (NameValueExpression<String> headerExpression : mappingInfo.getHeadersCondition().getExpressions()) {
for (NameValueExpression<String> headerExpression :
mappingInfo.getHeadersCondition().getExpressions()) {

if (!headerExpression.isNegated()) {
config.addAllowedHeader(headerExpression.getName());
}
Expand All @@ -218,7 +214,7 @@ private RequestMappingInfo getMappingForEndpoint(BaseHttpInboundEndpoint endpoin
return null;
}

Map<String, Object> requestMappingAttributes = new HashMap<String, Object>();
Map<String, Object> requestMappingAttributes = new HashMap<>();
requestMappingAttributes.put("name", endpoint.getComponentName());
requestMappingAttributes.put("value", requestMapping.getPathPatterns());
requestMappingAttributes.put("path", requestMapping.getPathPatterns());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2019 the original author or authors.
* Copyright 2015-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.
Expand All @@ -18,17 +18,15 @@

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExecutionChain;
Expand All @@ -39,8 +37,7 @@
*
* @since 4.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@SpringJUnitConfig
@DirtiesContext
public class CrossOriginTests {

Expand All @@ -49,7 +46,7 @@ public class CrossOriginTests {

private MockHttpServletRequest request;

@Before
@BeforeEach
public void setUp() {
this.request = new MockHttpServletRequest();
this.request.setMethod("GET");
Expand Down Expand Up @@ -91,7 +88,7 @@ public void defaultEndpointWithCrossOrigin() throws Exception {
assertThat(config.getAllowedOrigins().toArray()).isEqualTo(new String[] { "*" });
assertThat(config.getAllowCredentials()).isTrue();
assertThat(config.getAllowedHeaders().toArray()).isEqualTo(new String[] { "*" });
assertThat(config.getExposedHeaders()).isNull();
assertThat(config.getExposedHeaders()).isEmpty();
assertThat(config.getMaxAge()).isEqualTo(new Long(1800));
}

Expand Down Expand Up @@ -122,7 +119,7 @@ public void preFlightRequest() throws Exception {
assertThat(config.getAllowedOrigins().toArray()).isEqualTo(new String[] { "*" });
assertThat(config.getAllowCredentials()).isTrue();
assertThat(config.getAllowedHeaders().toArray()).isEqualTo(new String[] { "*" });
assertThat(config.getExposedHeaders()).isNull();
assertThat(config.getExposedHeaders()).isEmpty();
assertThat(config.getMaxAge()).isEqualTo(new Long(1800));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,7 @@ public void resetBeans(String... beanNames) {
this.beans.entrySet()
.stream()
.filter(e -> names == null || names.contains(e.getKey()))
.forEach(e -> {
Object endpoint = this.beanFactory.getBean(e.getKey());
DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(endpoint);
SmartLifecycle lifecycle = null;
if (endpoint instanceof SmartLifecycle && ((SmartLifecycle) endpoint).isRunning()) {
lifecycle = (SmartLifecycle) endpoint;
lifecycle.stop();
}
if (endpoint instanceof SourcePollingChannelAdapter) {
directFieldAccessor.setPropertyValue("source", e.getValue());
}
else if (endpoint instanceof ReactiveStreamsConsumer) {
Tuple2<?, ?> value = (Tuple2<?, ?>) e.getValue();
directFieldAccessor.setPropertyValue(HANDLER, value.getT1());
directFieldAccessor.setPropertyValue("subscriber", value.getT2());
}
else if (endpoint instanceof IntegrationConsumer) {
directFieldAccessor.setPropertyValue(HANDLER, e.getValue());
}
if (lifecycle != null && lifecycle.isAutoStartup()) {
lifecycle.start();
}
});
.forEach(e -> resetBean(this.beanFactory.getBean(e.getKey()), e.getValue()));

if (!ObjectUtils.isEmpty(beanNames)) {
for (String name : beanNames) {
Expand All @@ -129,6 +107,29 @@ else if (endpoint instanceof IntegrationConsumer) {
}
}

private void resetBean(Object endpoint, Object handler) {
DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(endpoint);
SmartLifecycle lifecycle = null;
if (endpoint instanceof SmartLifecycle && ((SmartLifecycle) endpoint).isRunning()) {
lifecycle = (SmartLifecycle) endpoint;
lifecycle.stop();
}
if (endpoint instanceof SourcePollingChannelAdapter) {
directFieldAccessor.setPropertyValue("source", handler);
}
else if (endpoint instanceof ReactiveStreamsConsumer) {
Tuple2<?, ?> value = (Tuple2<?, ?>) handler;
directFieldAccessor.setPropertyValue(HANDLER, value.getT1());
directFieldAccessor.setPropertyValue("subscriber", value.getT2());
}
else if (endpoint instanceof IntegrationConsumer) {
directFieldAccessor.setPropertyValue(HANDLER, handler);
}
if (lifecycle != null && lifecycle.isAutoStartup()) {
lifecycle.start();
}
}

/**
* Replace the real {@link MessageSource} in the {@link SourcePollingChannelAdapter} bean
* with provided {@link MessageSource} instance.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2019 the original author or authors.
* Copyright 2017-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.
Expand All @@ -17,6 +17,7 @@
package org.springframework.integration.webflux.inbound;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;

import org.springframework.beans.BeansException;
Expand Down Expand Up @@ -83,8 +84,8 @@
public class WebFluxIntegrationRequestMappingHandlerMapping extends RequestMappingHandlerMapping
implements ApplicationListener<ContextRefreshedEvent>, DestructionAwareBeanPostProcessor {

private static final Method HANDLER_METHOD = ReflectionUtils.findMethod(WebHandler.class,
"handle", ServerWebExchange.class);
private static final Method HANDLER_METHOD =
ReflectionUtils.findMethod(WebHandler.class, "handle", ServerWebExchange.class);

private final AtomicBoolean initialized = new AtomicBoolean();

Expand All @@ -98,7 +99,6 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
}

@Override
@SuppressWarnings("unchecked")
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
if (isHandler(bean.getClass())) {
unregisterMapping(getMappingForEndpoint((WebFluxInboundEndpoint) bean));
Expand Down Expand Up @@ -148,21 +148,13 @@ protected CorsConfiguration initCorsConfiguration(Object handler, Method method,
CrossOrigin crossOrigin = ((BaseHttpInboundEndpoint) handler).getCrossOrigin();
if (crossOrigin != null) {
CorsConfiguration config = new CorsConfiguration();
for (String origin : crossOrigin.getOrigin()) {
config.addAllowedOrigin(origin);
}
for (RequestMethod requestMethod : crossOrigin.getMethod()) {
config.addAllowedMethod(requestMethod.name());
}
for (String header : crossOrigin.getAllowedHeaders()) {
config.addAllowedHeader(header);
}
for (String header : crossOrigin.getExposedHeaders()) {
config.addExposedHeader(header);
}
if (crossOrigin.getAllowCredentials() != null) {
config.setAllowCredentials(crossOrigin.getAllowCredentials());
}
config.setAllowedOrigins(Arrays.asList(crossOrigin.getOrigin()));
config.setAllowedHeaders(Arrays.asList(crossOrigin.getAllowedHeaders()));
config.setExposedHeaders(Arrays.asList(crossOrigin.getExposedHeaders()));
config.setAllowCredentials(crossOrigin.getAllowCredentials());
if (crossOrigin.getMaxAge() != -1) {
config.setMaxAge(crossOrigin.getMaxAge());
}
Expand All @@ -172,7 +164,9 @@ protected CorsConfiguration initCorsConfiguration(Object handler, Method method,
}
}
if (CollectionUtils.isEmpty(config.getAllowedHeaders())) {
for (NameValueExpression<String> headerExpression : mappingInfo.getHeadersCondition().getExpressions()) {
for (NameValueExpression<String> headerExpression :
mappingInfo.getHeadersCondition().getExpressions()) {

if (!headerExpression.isNegated()) {
config.addAllowedHeader(headerExpression.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
* The spec for a {@link MarshallingWebServiceOutboundGateway}.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 5.3
*
*/
public class MarshallingWsOutboundGatewaySpec extends BaseWsOutboundGatewaySpec<
MarshallingWsOutboundGatewaySpec, MarshallingWebServiceOutboundGateway> {
public class MarshallingWsOutboundGatewaySpec extends
BaseWsOutboundGatewaySpec<MarshallingWsOutboundGatewaySpec, MarshallingWebServiceOutboundGateway> {

protected MarshallingWsOutboundGatewaySpec(WebServiceTemplate template) {
this.template = template;
Expand All @@ -56,12 +58,13 @@ protected MarshallingWebServiceOutboundGateway create() {
* {@link WebServiceTemplate} is not provided.
*
*/
public static class MarshallingWsOutboundGatewayNoTemplateSpec extends BaseWsOutboundGatewaySpec<
MarshallingWsOutboundGatewayNoTemplateSpec, MarshallingWebServiceOutboundGateway> {
public static class MarshallingWsOutboundGatewayNoTemplateSpec
extends BaseWsOutboundGatewaySpec<MarshallingWsOutboundGatewayNoTemplateSpec,
MarshallingWebServiceOutboundGateway> {

protected Marshaller gatewayMarshaller;
protected Marshaller gatewayMarshaller; // NOSONAR

protected Unmarshaller gatewayUnmarshaller;
protected Unmarshaller gatewayUnmarshaller; // NOSONAR

/**
* Configure the marshaller to use.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import java.util.Arrays;

import org.springframework.integration.ws.MarshallingWebServiceOutboundGateway;
import org.springframework.integration.ws.SimpleWebServiceOutboundGateway;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.client.core.FaultMessageResolver;
import org.springframework.ws.client.core.SourceExtractor;
Expand All @@ -32,11 +30,13 @@
* The spec for a {@link SimpleWebServiceOutboundGateway}.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 5.3
*
*/
public class SimpleWsOutboundGatewaySpec extends BaseWsOutboundGatewaySpec<
SimpleWsOutboundGatewaySpec, SimpleWebServiceOutboundGateway> {
public class SimpleWsOutboundGatewaySpec
extends BaseWsOutboundGatewaySpec<SimpleWsOutboundGatewaySpec, SimpleWebServiceOutboundGateway> {

protected SourceExtractor<?> sourceExtractor; // NOSONAR

Expand All @@ -55,11 +55,11 @@ public SimpleWsOutboundGatewaySpec sourceExtractor(SourceExtractor<?> extractor)
}

/**
* Specify a flag to return the whole {@link WebServiceMessage} or build the
* {@code payload} based on {@link WebServiceMessage}
* Specify a flag to return the whole {@link org.springframework.ws.WebServiceMessage} or build the
* {@code payload} based on {@link org.springframework.ws.WebServiceMessage}
* and populated headers according {@code headerMapper} configuration.
* Defaults to extract payload.
* @param extract build payload or return a whole {@link WebServiceMessage}
* @param extract build payload or return a whole {@link org.springframework.ws.WebServiceMessage}
* @return the spec.
*/
public SimpleWsOutboundGatewaySpec extractPayload(boolean extract) {
Expand Down Expand Up @@ -91,12 +91,12 @@ protected SimpleWebServiceOutboundGateway create() {
}

/**
* Spec for a {@link MarshallingWebServiceOutboundGateway} where an external
* Spec for a {@link SimpleWebServiceOutboundGateway} where an external
* {@link WebServiceTemplate} is not provided.
*
*/
public static class SimpleWsOutboundGatewayNoTemplateSpec extends BaseWsOutboundGatewaySpec<
SimpleWsOutboundGatewayNoTemplateSpec, SimpleWebServiceOutboundGateway> {
public static class SimpleWsOutboundGatewayNoTemplateSpec
extends BaseWsOutboundGatewaySpec<SimpleWsOutboundGatewayNoTemplateSpec, SimpleWebServiceOutboundGateway> {

protected SourceExtractor<?> sourceExtractor; // NOSONAR

Expand Down Expand Up @@ -154,11 +154,11 @@ public SimpleWsOutboundGatewayNoTemplateSpec interceptors(ClientInterceptor... i
}

/**
* Specify a flag to return the whole {@link WebServiceMessage} or build the
* {@code payload} based on {@link WebServiceMessage}
* Specify a flag to return the whole {@link org.springframework.ws.WebServiceMessage} or build the
* {@code payload} based on {@link org.springframework.ws.WebServiceMessage}
* and populated headers according {@code headerMapper} configuration.
* Defaults to extract payload.
* @param extract build payload or return a whole {@link WebServiceMessage}
* @param extract build payload or return a whole {@link org.springframework.ws.WebServiceMessage}
* @return the spec.
*/
public SimpleWsOutboundGatewayNoTemplateSpec extractPayload(boolean extract) {
Expand Down

0 comments on commit 7dbdbde

Please sign in to comment.