GH-9620: Use Locale.ROOT for neutral, case insensitive comparisons

Fixes: #9620
Issue link: https://github.com/spring-projects/spring-integration/issues/9620

(cherry picked from commit 0d2595ef7c)
This commit is contained in:
Artem Bilan
2024-10-31 10:59:05 -04:00
committed by Spring Builds
parent 4ffc6641b7
commit 5c76a47e55
7 changed files with 66 additions and 58 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 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.
@@ -22,6 +22,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
@@ -490,13 +491,13 @@ public abstract class AbstractHeaderMapper<T> implements RequestReplyHeaderMappe
Assert.notNull(patterns, "Patterns must no be null");
Assert.notEmpty(patterns, "At least one pattern must be specified");
for (String pattern : patterns) {
this.patterns.add(pattern.toLowerCase());
this.patterns.add(pattern.toLowerCase(Locale.ROOT));
}
}
@Override
public boolean matchHeader(String headerName) {
String header = headerName.toLowerCase();
String header = headerName.toLowerCase(Locale.ROOT);
for (String pattern : this.patterns) {
if (PatternMatchUtils.simpleMatch(pattern, header)) {
if (LOGGER.isDebugEnabled()) {
@@ -534,13 +535,13 @@ public abstract class AbstractHeaderMapper<T> implements RequestReplyHeaderMappe
public SinglePatternBasedHeaderMatcher(String pattern, boolean negate) {
Assert.notNull(pattern, "Pattern must no be null");
this.pattern = pattern.toLowerCase();
this.pattern = pattern.toLowerCase(Locale.ROOT);
this.negate = negate;
}
@Override
public boolean matchHeader(String headerName) {
String header = headerName.toLowerCase();
String header = headerName.toLowerCase(Locale.ROOT);
if (PatternMatchUtils.simpleMatch(this.pattern, header)) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(MessageFormat.format(

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2024 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.
@@ -17,6 +17,7 @@
package org.springframework.integration.support.utils;
import java.util.Arrays;
import java.util.Locale;
/**
* Utility methods for pattern matching.
@@ -48,9 +49,9 @@ public final class PatternMatchUtils {
*/
public static Boolean smartMatchIgnoreCase(String str, String... patterns) {
if (patterns != null) {
return smartMatch(str.toLowerCase(),
return smartMatch(str.toLowerCase(Locale.ROOT),
Arrays.stream(patterns)
.map(String::toLowerCase)
.map((pattern) -> pattern.toLowerCase(Locale.ROOT))
.toArray(String[]::new));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 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.
@@ -202,10 +202,10 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
static {
for (String header : HTTP_REQUEST_HEADER_NAMES) {
HTTP_REQUEST_HEADER_NAMES_LOWER.add(header.toLowerCase());
HTTP_REQUEST_HEADER_NAMES_LOWER.add(header.toLowerCase(Locale.ROOT));
}
for (String header : HTTP_RESPONSE_HEADER_NAMES) {
HTTP_RESPONSE_HEADER_NAMES_LOWER.add(header.toLowerCase());
HTTP_RESPONSE_HEADER_NAMES_LOWER.add(header.toLowerCase(Locale.ROOT));
}
}
@@ -266,13 +266,13 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
outboundHeaderNamesLower[i] = this.outboundHeaderNames[i];
}
else {
outboundHeaderNamesLower[i] = this.outboundHeaderNames[i].toLowerCase();
outboundHeaderNamesLower[i] = this.outboundHeaderNames[i].toLowerCase(Locale.ROOT);
}
}
this.outboundHeaderNamesLowerWithContentType =
Arrays.copyOf(outboundHeaderNamesLower, this.outboundHeaderNames.length + 1);
this.outboundHeaderNamesLowerWithContentType[this.outboundHeaderNamesLowerWithContentType.length - 1]
= MessageHeaders.CONTENT_TYPE.toLowerCase();
= MessageHeaders.CONTENT_TYPE.toLowerCase(Locale.ROOT);
}
/**
@@ -298,7 +298,7 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
this.inboundHeaderNamesLower[i] = this.inboundHeaderNames[i];
}
else {
this.inboundHeaderNamesLower[i] = this.inboundHeaderNames[i].toLowerCase();
this.inboundHeaderNamesLower[i] = this.inboundHeaderNames[i].toLowerCase(Locale.ROOT);
}
}
}
@@ -360,7 +360,7 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
for (Entry<String, Object> entry : headers.entrySet()) {
String name = entry.getKey();
Object value = entry.getValue();
String lowerName = name.toLowerCase();
String lowerName = name.toLowerCase(Locale.ROOT);
if (value != null && shouldMapOutboundHeader(lowerName)) {
if (!HTTP_REQUEST_HEADER_NAMES_LOWER.contains(lowerName) &&
!HTTP_RESPONSE_HEADER_NAMES_LOWER.contains(lowerName) &&
@@ -380,7 +380,7 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
}
private void setHttpHeader(HttpHeaders target, String name, Object value) { // NOSONAR
switch (name.toLowerCase()) {
switch (name.toLowerCase(Locale.ROOT)) {
case ACCEPT_LOWER:
setAccept(target, value);
break;
@@ -775,7 +775,7 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
Map<String, Object> target = new HashMap<>();
Set<String> headerNames = source.keySet();
for (String name : headerNames) {
String lowerName = name.toLowerCase();
String lowerName = name.toLowerCase(Locale.ROOT);
if (shouldMapInboundHeader(lowerName)) {
if (!HTTP_REQUEST_HEADER_NAMES_LOWER.contains(lowerName)
&& !HTTP_RESPONSE_HEADER_NAMES_LOWER.contains(lowerName)) {
@@ -810,49 +810,51 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
}
protected Object getHttpHeader(HttpHeaders source, String name) { // NOSONAR
switch (name.toLowerCase()) {
case ACCEPT_LOWER:
return source.getAccept();
case ACCEPT_CHARSET_LOWER:
return source.getAcceptCharset();
case ALLOW_LOWER:
return source.getAllow();
case CACHE_CONTROL_LOWER:
return switch (name.toLowerCase(Locale.ROOT)) {
case ACCEPT_LOWER -> source.getAccept();
case ACCEPT_CHARSET_LOWER -> source.getAcceptCharset();
case ALLOW_LOWER -> source.getAllow();
case CACHE_CONTROL_LOWER -> {
String cacheControl = source.getCacheControl();
return (StringUtils.hasText(cacheControl)) ? cacheControl : null;
case CONTENT_LENGTH_LOWER:
yield (StringUtils.hasText(cacheControl)) ? cacheControl : null;
}
case CONTENT_LENGTH_LOWER -> {
long contentLength = source.getContentLength();
return (contentLength > -1) ? contentLength : null;
case CONTENT_TYPE_LOWER:
return source.getContentType();
case DATE_LOWER:
yield (contentLength > -1) ? contentLength : null;
}
case CONTENT_TYPE_LOWER -> source.getContentType();
case DATE_LOWER -> {
long date = source.getDate();
return (date > -1) ? date : null;
case ETAG_LOWER:
yield (date > -1) ? date : null;
}
case ETAG_LOWER -> {
String eTag = source.getETag();
return (StringUtils.hasText(eTag)) ? eTag : null;
case EXPIRES_LOWER:
yield (StringUtils.hasText(eTag)) ? eTag : null;
}
case EXPIRES_LOWER -> {
long expires = source.getExpires();
return (expires > -1) ? expires : null;
case IF_NONE_MATCH_LOWER:
return source.getIfNoneMatch();
case IF_MODIFIED_SINCE_LOWER:
yield (expires > -1) ? expires : null;
}
case IF_NONE_MATCH_LOWER -> source.getIfNoneMatch();
case IF_MODIFIED_SINCE_LOWER -> {
long modifiedSince = source.getIfModifiedSince();
return (modifiedSince > -1) ? modifiedSince : null;
case IF_UNMODIFIED_SINCE_LOWER:
yield (modifiedSince > -1) ? modifiedSince : null;
}
case IF_UNMODIFIED_SINCE_LOWER -> {
long unmodifiedSince = source.getIfUnmodifiedSince();
return (unmodifiedSince > -1) ? unmodifiedSince : null;
case LAST_MODIFIED_LOWER:
yield (unmodifiedSince > -1) ? unmodifiedSince : null;
}
case LAST_MODIFIED_LOWER -> {
long lastModified = source.getLastModified();
return (lastModified > -1) ? lastModified : null;
case LOCATION_LOWER:
return source.getLocation();
case PRAGMA_LOWER:
yield (lastModified > -1) ? lastModified : null;
}
case LOCATION_LOWER -> source.getLocation();
case PRAGMA_LOWER -> {
String pragma = source.getPragma();
return (StringUtils.hasText(pragma)) ? pragma : null;
default:
return source.get(name);
}
yield (StringUtils.hasText(pragma)) ? pragma : null;
}
default -> source.get(name);
};
}
private void setMessageHeader(Map<String, Object> target, String name, Object value) {

View File

@@ -19,6 +19,7 @@ package org.springframework.integration.jdbc.channel;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Duration;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
@@ -192,7 +193,7 @@ public final class PostgresChannelMessageTableSubscriber implements SmartLifecyc
try {
PgConnection conn = this.connectionSupplier.get();
try (Statement stmt = conn.createStatement()) {
stmt.execute("LISTEN " + this.tablePrefix.toLowerCase() + "channel_message_notify");
stmt.execute("LISTEN " + this.tablePrefix.toLowerCase(Locale.ROOT) + "channel_message_notify");
}
catch (Exception ex) {
try {

View File

@@ -18,6 +18,7 @@ package org.springframework.integration.mail;
import java.time.Instant;
import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.ScheduledFuture;
@@ -79,7 +80,7 @@ public class ImapMailReceiver extends AbstractMailReceiver {
public ImapMailReceiver(String url) {
super(url);
if (url != null) {
Assert.isTrue(url.toLowerCase().startsWith(PROTOCOL),
Assert.isTrue(url.toLowerCase(Locale.ROOT).startsWith(PROTOCOL),
"URL must start with 'imap' for the IMAP Mail receiver.");
}
else {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 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.
@@ -16,6 +16,7 @@
package org.springframework.integration.mail.config;
import java.util.Locale;
import java.util.Properties;
import jakarta.mail.Authenticator;
@@ -162,8 +163,8 @@ public class MailReceiverFactoryBean extends AbstractFactoryBean<MailReceiver> {
private MailReceiver createReceiver() { // NOSONAR
verifyProtocol();
boolean isPop3 = this.protocol.toLowerCase().startsWith("pop3");
boolean isImap = this.protocol.toLowerCase().startsWith("imap");
boolean isPop3 = this.protocol.toLowerCase(Locale.ROOT).startsWith("pop3");
boolean isImap = this.protocol.toLowerCase(Locale.ROOT).startsWith("imap");
Assert.isTrue(isPop3 || isImap, "the store URI must begin with 'pop3' or 'imap'");
AbstractMailReceiver mailReceiver = isPop3
? new Pop3MailReceiver(this.storeUri)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 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.
@@ -19,6 +19,7 @@ package org.springframework.integration.websocket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
@@ -124,7 +125,7 @@ public abstract class IntegrationWebSocketContainer implements DisposableBean {
public void addSupportedProtocols(String... protocols) {
for (String protocol : protocols) {
this.supportedProtocols.add(protocol.toLowerCase());
this.supportedProtocols.add(protocol.toLowerCase(Locale.ROOT));
}
}