diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java index 862450fc47..fa8c1fc96f 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java @@ -998,8 +998,8 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply } } - private void processFile(Session session, String directory, String subDirectory, List lsFiles, - boolean recursion, F file) throws IOException { + private void processFile(Session session, String directory, String subDirectory, // NOSONAR - complexity + List lsFiles, boolean recursion, F file) throws IOException { F fileToAdd = file; if (recursion && StringUtils.hasText(subDirectory)) { diff --git a/spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsChannelParser.java b/spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsChannelParser.java index 62f55a46e3..2a6c2046cf 100644 --- a/spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsChannelParser.java +++ b/spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsChannelParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -57,35 +57,16 @@ public class JmsChannelParser extends AbstractChannelParser { builder.addPropertyReference(JmsParserUtils.CONNECTION_FACTORY_PROPERTY, JmsParserUtils.determineConnectionFactoryBeanName(element, parserContext)); if ("channel".equals(element.getLocalName())) { - this.parseDestination(element, parserContext, builder, "queue"); + parseDestination(element, parserContext, builder, "queue"); } else if ("publish-subscribe-channel".equals(element.getLocalName())) { - this.parseDestination(element, parserContext, builder, "topic"); + parseDestination(element, parserContext, builder, "topic"); } IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "max-subscribers"); String containerType = element.getAttribute(CONTAINER_TYPE_ATTRIBUTE); - String containerClass = element.getAttribute(CONTAINER_CLASS_ATTRIBUTE); - if (!StringUtils.hasText(containerClass) && StringUtils.hasText(containerType)) { - if ("default".equals(containerType)) { - containerClass = "org.springframework.jms.listener.DefaultMessageListenerContainer"; - } - else if ("simple".equals(containerType)) { - containerClass = "org.springframework.jms.listener.SimpleMessageListenerContainer"; - } - } - /* - * Schema docs tell the user to ensure that, if they supply a container-class, it - * is their responsibility to set the container-type appropriately, based on the superclass - * of their implementation (default="default"). If it is set incorrectly, some attributes - * may not be applied. - * - * We cannot reliably infer it here. - */ - if (StringUtils.hasText(containerClass)) { - builder.addPropertyValue("containerType", containerClass); - } + setContainerClass(element, builder, containerType); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "receive-timeout"); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "task-executor"); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "transaction-manager"); @@ -98,38 +79,12 @@ public class JmsChannelParser extends AbstractChannelParser { IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "time-to-live"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "priority"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "explicit-qos-enabled"); - String cache = element.getAttribute("cache"); - if (StringUtils.hasText(cache)) { - if (containerType.startsWith("simple")) { - if (!("auto".equals(cache) || "consumer".equals(cache))) { - parserContext.getReaderContext().warning( - "'cache' attribute not actively supported for listener container of type \"simple\". " + - "Effective runtime behavior will be equivalent to \"consumer\" / \"auto\".", element); - } - } - else { - builder.addPropertyValue("cacheLevelName", "CACHE_" + cache.toUpperCase()); - } - } - Integer acknowledgeMode = this.parseAcknowledgeMode(element, parserContext); - if (acknowledgeMode != null) { - if (acknowledgeMode == Session.SESSION_TRANSACTED) { - builder.addPropertyValue("sessionTransacted", Boolean.TRUE); - } - else { - builder.addPropertyValue("sessionAcknowledgeMode", acknowledgeMode); - } - } - IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "concurrency"); - - String prefetch = element.getAttribute("prefetch"); - if (StringUtils.hasText(prefetch) && containerType.startsWith("default")) { - builder.addPropertyValue("maxMessagesPerTask", Integer.valueOf(prefetch)); - } + setCache(element, parserContext, builder, containerType); + setAcknowledgeMode(element, parserContext, builder, containerType); return builder; } - private void parseDestination(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, + private static void parseDestination(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, String type) { boolean isPubSub = "topic".equals(type); @@ -161,7 +116,68 @@ public class JmsChannelParser extends AbstractChannelParser { } } - private Integer parseAcknowledgeMode(Element ele, ParserContext parserContext) { + private static void setContainerClass(Element element, BeanDefinitionBuilder builder, String containerType) { + String containerClass = element.getAttribute(CONTAINER_CLASS_ATTRIBUTE); + if (!StringUtils.hasText(containerClass) && StringUtils.hasText(containerType)) { + if ("default".equals(containerType)) { + containerClass = "org.springframework.jms.listener.DefaultMessageListenerContainer"; + } + else if ("simple".equals(containerType)) { + containerClass = "org.springframework.jms.listener.SimpleMessageListenerContainer"; + } + } + /* + * Schema docs tell the user to ensure that, if they supply a container-class, it + * is their responsibility to set the container-type appropriately, based on the superclass + * of their implementation (default="default"). If it is set incorrectly, some attributes + * may not be applied. + * We cannot reliably infer it here. + */ + if (StringUtils.hasText(containerClass)) { + builder.addPropertyValue("containerType", containerClass); + } + } + + private static void setCache(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, + String containerType) { + + String cache = element.getAttribute("cache"); + if (StringUtils.hasText(cache)) { + if (containerType.startsWith("simple")) { + if (!("auto".equals(cache) || "consumer".equals(cache))) { + parserContext.getReaderContext().warning( + "'cache' attribute not actively supported for listener container of type \"simple\". " + + "Effective runtime behavior will be equivalent to \"consumer\" / \"auto\".", + element); + } + } + else { + builder.addPropertyValue("cacheLevelName", "CACHE_" + cache.toUpperCase()); + } + } + } + + private static void setAcknowledgeMode(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, + String containerType) { + + Integer acknowledgeMode = parseAcknowledgeMode(element, parserContext); + if (acknowledgeMode != null) { + if (acknowledgeMode == Session.SESSION_TRANSACTED) { + builder.addPropertyValue("sessionTransacted", Boolean.TRUE); + } + else { + builder.addPropertyValue("sessionAcknowledgeMode", acknowledgeMode); + } + } + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "concurrency"); + + String prefetch = element.getAttribute("prefetch"); + if (StringUtils.hasText(prefetch) && containerType.startsWith("default")) { + builder.addPropertyValue("maxMessagesPerTask", Integer.valueOf(prefetch)); + } + } + + private static Integer parseAcknowledgeMode(Element ele, ParserContext parserContext) { String acknowledge = ele.getAttribute(ACKNOWLEDGE_ATTRIBUTE); if (StringUtils.hasText(acknowledge)) { int acknowledgeMode = Session.AUTO_ACKNOWLEDGE; diff --git a/spring-integration-stomp/src/main/java/org/springframework/integration/stomp/support/StompHeaderMapper.java b/spring-integration-stomp/src/main/java/org/springframework/integration/stomp/support/StompHeaderMapper.java index 3660350ae9..9a52b0f487 100644 --- a/spring-integration-stomp/src/main/java/org/springframework/integration/stomp/support/StompHeaderMapper.java +++ b/spring-integration-stomp/src/main/java/org/springframework/integration/stomp/support/StompHeaderMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2020 the original author or authors. + * Copyright 2015-2021 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. @@ -128,68 +128,92 @@ public class StompHeaderMapper implements HeaderMapper { } private void setStompHeader(StompHeaders target, String name, Object value) { - if (StompHeaders.CONTENT_LENGTH.equals(name)) { - if (value instanceof Number) { - target.setContentLength(((Number) value).longValue()); - } - else if (value instanceof String) { - target.setContentLength(Long.parseLong((String) value)); - } - else { - Class clazz = (value != null) ? value.getClass() : null; - throw new IllegalArgumentException( - "Expected Number or String value for 'content-length' header value, but received: " + clazz); - } - } - else if (StompHeaders.CONTENT_TYPE.equals(name) || MessageHeaders.CONTENT_TYPE.equals(name)) { - MimeType contentType = target.getContentType(); - if (contentType == null || StompHeaders.CONTENT_TYPE.equals(name)) { - if (value instanceof MediaType) { - target.setContentType((MediaType) value); - } - else if (value instanceof String) { - target.setContentType(MediaType.parseMediaType((String) value)); + switch (name) { + case StompHeaders.CONTENT_LENGTH: + setContentLength(target, value); + break; + + case StompHeaders.CONTENT_TYPE: + case MessageHeaders.CONTENT_TYPE: + setContentType(target, name, value); + break; + + case StompHeaders.DESTINATION: + case IntegrationStompHeaders.DESTINATION: + setDestination(target, value); + break; + + case StompHeaders.RECEIPT: + case IntegrationStompHeaders.RECEIPT: + setReceipt(target, value); + break; + + default: + if (value instanceof String) { + target.set(name, (String) value); } else { Class clazz = (value != null) ? value.getClass() : null; throw new IllegalArgumentException( - "Expected MediaType or String value for 'content-type' header value, but received: " - + clazz); + "Expected String value for any generic STOMP header value, but received: " + clazz); } - } } - else if (StompHeaders.DESTINATION.equals(name) || IntegrationStompHeaders.DESTINATION.equals(name)) { - if (value instanceof String) { - target.setDestination((String) value); - } - else { - Class clazz = (value != null) ? value.getClass() : null; - throw new IllegalArgumentException( - "Expected String value for 'destination' header value, but received: " + clazz); - } + } + + private static void setContentLength(StompHeaders target, Object value) { + if (value instanceof Number) { + target.setContentLength(((Number) value).longValue()); } - else if (StompHeaders.RECEIPT.equals(name) || IntegrationStompHeaders.RECEIPT.equals(name)) { - if (value instanceof String) { - target.setReceipt((String) value); - } - else { - Class clazz = (value != null) ? value.getClass() : null; - throw new IllegalArgumentException( - "Expected String value for 'receipt' header value, but received: " + clazz); - } + else if (value instanceof String) { + target.setContentLength(Long.parseLong((String) value)); } else { - if (value instanceof String) { - target.set(name, (String) value); + Class clazz = (value != null) ? value.getClass() : null; + throw new IllegalArgumentException( + "Expected Number or String value for 'content-length' header value, but received: " + clazz); + } + } + + private static void setContentType(StompHeaders target, String name, Object value) { + MimeType contentType = target.getContentType(); + if (contentType == null || StompHeaders.CONTENT_TYPE.equals(name)) { + if (value instanceof MediaType) { + target.setContentType((MediaType) value); + } + else if (value instanceof String) { + target.setContentType(MediaType.parseMediaType((String) value)); } else { Class clazz = (value != null) ? value.getClass() : null; throw new IllegalArgumentException( - "Expected String value for any generic STOMP header value, but received: " + clazz); + "Expected MediaType or String value for 'content-type' header value, but received: " + + clazz); } } } + private static void setReceipt(StompHeaders target, Object value) { + if (value instanceof String) { + target.setReceipt((String) value); + } + else { + Class clazz = (value != null) ? value.getClass() : null; + throw new IllegalArgumentException( + "Expected String value for 'receipt' header value, but received: " + clazz); + } + } + + private static void setDestination(StompHeaders target, Object value) { + if (value instanceof String) { + target.setDestination((String) value); + } + else { + Class clazz = (value != null) ? value.getClass() : null; + throw new IllegalArgumentException( + "Expected String value for 'destination' header value, but received: " + clazz); + } + } + @Override public Map toHeaders(StompHeaders source) { Map target = new HashMap<>(); @@ -211,7 +235,7 @@ public class StompHeaderMapper implements HeaderMapper { } - private boolean shouldMapHeader(String headerName, String[] patterns) { + private static boolean shouldMapHeader(String headerName, String[] patterns) { if (patterns != null && patterns.length > 0) { for (String pattern : patterns) { if (PatternMatchUtils.simpleMatch(pattern, headerName)) {