Use new Java features (switch expressions, text blocks, new JDK methods)

Closes gh-29747
This commit is contained in:
Krzysztof Krason 2022-12-28 08:59:08 +01:00 committed by Sam Brannen
parent 48abd493fe
commit afb8a0d1b1
53 changed files with 498 additions and 552 deletions

View File

@ -244,31 +244,18 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov
int algorithmicStep = STEP_JOIN_POINT_BINDING;
while ((this.numberOfRemainingUnboundArguments > 0) && algorithmicStep < STEP_FINISHED) {
switch (algorithmicStep++) {
case STEP_JOIN_POINT_BINDING:
case STEP_JOIN_POINT_BINDING -> {
if (!maybeBindThisJoinPoint()) {
maybeBindThisJoinPointStaticPart();
}
break;
case STEP_THROWING_BINDING:
maybeBindThrowingVariable();
break;
case STEP_ANNOTATION_BINDING:
maybeBindAnnotationsFromPointcutExpression();
break;
case STEP_RETURNING_BINDING:
maybeBindReturningVariable();
break;
case STEP_PRIMITIVE_ARGS_BINDING:
maybeBindPrimitiveArgsFromPointcutExpression();
break;
case STEP_THIS_TARGET_ARGS_BINDING:
maybeBindThisOrTargetOrArgsFromPointcutExpression();
break;
case STEP_REFERENCE_PCUT_BINDING:
maybeBindReferencePointcutParameter();
break;
default:
throw new IllegalStateException("Unknown algorithmic step: " + (algorithmicStep - 1));
}
case STEP_THROWING_BINDING -> maybeBindThrowingVariable();
case STEP_ANNOTATION_BINDING -> maybeBindAnnotationsFromPointcutExpression();
case STEP_RETURNING_BINDING -> maybeBindReturningVariable();
case STEP_PRIMITIVE_ARGS_BINDING -> maybeBindPrimitiveArgsFromPointcutExpression();
case STEP_THIS_TARGET_ARGS_BINDING -> maybeBindThisOrTargetOrArgsFromPointcutExpression();
case STEP_REFERENCE_PCUT_BINDING -> maybeBindReferencePointcutParameter();
default -> throw new IllegalStateException("Unknown algorithmic step: " + (algorithmicStep - 1));
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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,8 +128,7 @@ abstract class AutowireUtils {
* @return the resolved value
*/
public static Object resolveAutowiringValue(Object autowiringValue, Class<?> requiredType) {
if (autowiringValue instanceof ObjectFactory && !requiredType.isInstance(autowiringValue)) {
ObjectFactory<?> factory = (ObjectFactory<?>) autowiringValue;
if (autowiringValue instanceof ObjectFactory<?> factory && !requiredType.isInstance(autowiringValue)) {
if (autowiringValue instanceof Serializable && requiredType.isInterface()) {
autowiringValue = Proxy.newProxyInstance(requiredType.getClassLoader(),
new Class<?>[] {requiredType}, new ObjectFactoryDelegatingInvocationHandler(factory));
@ -275,22 +274,19 @@ abstract class AutowireUtils {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case "equals":
// Only consider equal when proxies are identical.
return (proxy == args[0]);
case "hashCode":
// Use hashCode of proxy.
return System.identityHashCode(proxy);
case "toString":
return this.objectFactory.toString();
}
try {
return method.invoke(this.objectFactory.getObject(), args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
return switch (method.getName()) {
case "equals" -> (proxy == args[0]); // Only consider equal when proxies are identical.
case "hashCode" -> System.identityHashCode(proxy); // Use hashCode of proxy.
case "toString" -> this.objectFactory.toString();
default -> {
try {
yield method.invoke(this.objectFactory.getObject(), args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
};
}
}

View File

@ -365,8 +365,7 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
for (Map.Entry<String, Object> entry : this.beans.entrySet()) {
String beanName = entry.getKey();
Object beanInstance = entry.getValue();
if (beanInstance instanceof FactoryBean && !isFactoryType) {
FactoryBean<?> factoryBean = (FactoryBean<?>) beanInstance;
if (beanInstance instanceof FactoryBean<?> factoryBean && !isFactoryType) {
Class<?> objectType = factoryBean.getObjectType();
if ((includeNonSingletons || factoryBean.isSingleton()) &&
objectType != null && (type == null || type.isAssignableFrom(objectType))) {
@ -409,9 +408,8 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
String beanName = entry.getKey();
Object beanInstance = entry.getValue();
// Is bean a FactoryBean?
if (beanInstance instanceof FactoryBean && !isFactoryType) {
if (beanInstance instanceof FactoryBean<?> factory && !isFactoryType) {
// Match object created by FactoryBean.
FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
Class<?> objectType = factory.getObjectType();
if ((includeNonSingletons || factory.isSingleton()) &&
objectType != null && (type == null || type.isAssignableFrom(objectType))) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 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.
@ -389,16 +389,17 @@ public class FreeMarkerConfigurationFactory {
*/
@Nullable
protected TemplateLoader getAggregateTemplateLoader(List<TemplateLoader> templateLoaders) {
switch (templateLoaders.size()) {
case 0:
return switch (templateLoaders.size()) {
case 0 -> {
logger.debug("No FreeMarker TemplateLoaders specified");
return null;
case 1:
return templateLoaders.get(0);
default:
yield null;
}
case 1 -> templateLoaders.get(0);
default -> {
TemplateLoader[] loaders = templateLoaders.toArray(new TemplateLoader[0]);
return new MultiTemplateLoader(loaders);
}
yield new MultiTemplateLoader(loaders);
}
};
}
/**

View File

@ -587,12 +587,11 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
*/
protected ObjectName registerBeanNameOrInstance(Object mapValue, String beanKey) throws MBeanExportException {
try {
if (mapValue instanceof String) {
if (mapValue instanceof String beanName) {
// Bean name pointing to a potentially lazy-init bean in the factory.
if (this.beanFactory == null) {
throw new MBeanExportException("Cannot resolve bean names if not running in a BeanFactory");
}
String beanName = (String) mapValue;
if (isBeanDefinitionLazyInit(this.beanFactory, beanName)) {
ObjectName objectName = registerLazyInit(beanName, beanKey);
replaceNotificationListenerBeanNameKeysIfNecessary(beanName, objectName);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 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.
@ -90,8 +90,7 @@ public abstract class BshScriptUtils {
throws EvalError {
Object result = evaluateBshScript(scriptSource, scriptInterfaces, classLoader);
if (result instanceof Class) {
Class<?> clazz = (Class<?>) result;
if (result instanceof Class<?> clazz) {
try {
return ReflectionUtils.accessibleConstructor(clazz).newInstance();
}

View File

@ -160,8 +160,7 @@ public class StandardScriptFactory implements ScriptFactory, BeanClassLoaderAwar
}
}
if (script instanceof Class) {
Class<?> scriptClass = (Class<?>) script;
if (script instanceof Class<?> scriptClass) {
try {
return ReflectionUtils.accessibleConstructor(scriptClass).newInstance();
}

View File

@ -191,8 +191,7 @@ public class ResourceBundleThemeSource implements HierarchicalThemeSource, BeanC
* @param theme the Theme to (re-)initialize
*/
protected void initParent(Theme theme) {
if (theme.getMessageSource() instanceof HierarchicalMessageSource) {
HierarchicalMessageSource messageSource = (HierarchicalMessageSource) theme.getMessageSource();
if (theme.getMessageSource() instanceof HierarchicalMessageSource messageSource) {
if (getParentThemeSource() != null && messageSource.getParentMessageSource() == null) {
Theme parentTheme = getParentThemeSource().getTheme(theme.getName());
if (parentTheme != null) {

View File

@ -852,8 +852,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
if (pv.getValue() instanceof String) {
empty = !StringUtils.hasText((String) pv.getValue());
}
else if (pv.getValue() instanceof String[]) {
String[] values = (String[]) pv.getValue();
else if (pv.getValue() instanceof String[] values) {
empty = (values.length == 0 || !StringUtils.hasText(values[0]));
}
}

View File

@ -49,33 +49,35 @@ class ScriptFactoryPostProcessorTests {
private static final String PROCESSOR_BEAN_NAME = "processor";
private static final String CHANGED_SCRIPT = "package org.springframework.scripting.groovy\n" +
"import org.springframework.scripting.Messenger\n" +
"class GroovyMessenger implements Messenger {\n" +
" private String message = \"Bingo\"\n" +
" public String getMessage() {\n" +
// quote the returned message (this is the change)...
" return \"'\" + this.message + \"'\"\n" +
" }\n" +
" public void setMessage(String message) {\n" +
" this.message = message\n" +
" }\n" +
"}";
// quote the returned message (this is the change)...
private static final String CHANGED_SCRIPT = """
package org.springframework.scripting.groovy
import org.springframework.scripting.Messenger
class GroovyMessenger implements Messenger {
private String message = "Bingo"
public String getMessage() {
return "'" + this.message + "'"
}
public void setMessage(String message) {
this.message = message
}
}""";
private static final String EXPECTED_CHANGED_MESSAGE_TEXT = "'" + MESSAGE_TEXT + "'";
private static final int DEFAULT_SECONDS_TO_PAUSE = 1;
private static final String DELEGATING_SCRIPT = "inline:package org.springframework.scripting;\n" +
"class DelegatingMessenger implements Messenger {\n" +
" private Messenger wrappedMessenger;\n" +
" public String getMessage() {\n" +
" return this.wrappedMessenger.getMessage()\n" +
" }\n" +
" public void setMessenger(Messenger wrappedMessenger) {\n" +
" this.wrappedMessenger = wrappedMessenger\n" +
" }\n" +
"}";
private static final String DELEGATING_SCRIPT = """
inline:package org.springframework.scripting;
class DelegatingMessenger implements Messenger {
private Messenger wrappedMessenger;
public String getMessage() {
return this.wrappedMessenger.getMessage()
}
public void setMessenger(Messenger wrappedMessenger) {
this.wrappedMessenger = wrappedMessenger
}
}""";
@Test
@ -245,16 +247,17 @@ class ScriptFactoryPostProcessorTests {
private static BeanDefinition createScriptedGroovyBean() {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(GroovyScriptFactory.class);
builder.addConstructorArgValue("inline:package org.springframework.scripting;\n" +
"class GroovyMessenger implements Messenger {\n" +
" private String message = \"Bingo\"\n" +
" public String getMessage() {\n" +
" return this.message\n" +
" }\n" +
" public void setMessage(String message) {\n" +
" this.message = message\n" +
" }\n" +
"}");
builder.addConstructorArgValue("""
inline:package org.springframework.scripting;
class GroovyMessenger implements Messenger {
private String message = "Bingo"
public String getMessage() {
return this.message
}
public void setMessage(String message) {
this.message = message
}
}""");
builder.addPropertyValue("message", MESSAGE_TEXT);
return builder.getBeanDefinition();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@ -78,11 +78,14 @@ public class StringDecoderBenchmark {
@Setup(Level.Trial)
public void setup() {
String eventTemplate = "id:$1\n" +
"event:some-event\n" +
":some-comment-$1-aa\n" +
":some-comment-$1-bb\n" +
"data:abcdefg-$1-hijklmnop-$1-qrstuvw-$1-xyz-$1\n\n";
String eventTemplate = """
id:$1
event:some-event
:some-comment-$1-aa
:some-comment-$1-bb
data:abcdefg-$1-hijklmnop-$1-qrstuvw-$1-xyz-$1
""";
int eventLength = String.format(eventTemplate, String.format("%05d", 1)).length();
int eventCount = this.totalSize / eventLength;

View File

@ -152,42 +152,27 @@ class BasicJsonWriter {
private static String escape(CharSequence input) {
StringBuilder builder = new StringBuilder();
input.chars().forEach(c -> {
switch (c) {
case '"':
builder.append("\\\"");
break;
case '\\':
builder.append("\\\\");
break;
case '/':
builder.append("\\/");
break;
case '\b':
builder.append("\\b");
break;
case '\f':
builder.append("\\f");
break;
case '\n':
builder.append("\\n");
break;
case '\r':
builder.append("\\r");
break;
case '\t':
builder.append("\\t");
break;
default:
if (c <= 0x1F) {
builder.append(String.format("\\u%04x", c));
}
else {
builder.append((char) c);
}
break;
}
});
input.chars().forEach(c -> builder.append(
switch (c) {
case '"' -> "\\\"";
case '\\' -> "\\\\";
case '/' -> "\\/";
case '\b' -> "\\b";
case '\f' -> "\\f";
case '\n' -> "\\n";
case '\r' -> "\\r";
case '\t' -> "\\t";
default -> {
if (c <= 0x1F) {
yield String.format("\\u%04x", c);
}
else {
yield (char) c;
}
}
}
)
);
return builder.toString();
}

View File

@ -314,18 +314,14 @@ public abstract class MimeTypeUtils {
int i = 0;
while (i < mimeTypes.length()) {
switch (mimeTypes.charAt(i)) {
case '"':
inQuotes = !inQuotes;
break;
case ',':
case '"' -> inQuotes = !inQuotes;
case ',' -> {
if (!inQuotes) {
tokens.add(mimeTypes.substring(startIndex, i));
startIndex = i + 1;
}
break;
case '\\':
i++;
break;
}
case '\\' -> i++;
}
i++;
}

View File

@ -96,18 +96,18 @@ public abstract class FutureAdapter<T, S> implements Future<T> {
@Nullable
final T adaptInternal(S adapteeResult) throws ExecutionException {
synchronized (this.mutex) {
switch (this.state) {
case SUCCESS:
return (T) this.result;
case FAILURE:
return switch (this.state) {
case SUCCESS -> (T) this.result;
case FAILURE -> {
Assert.state(this.result instanceof ExecutionException, "Failure without exception");
throw (ExecutionException) this.result;
case NEW:
}
case NEW -> {
try {
T adapted = adapt(adapteeResult);
this.result = adapted;
this.state = State.SUCCESS;
return adapted;
yield adapted;
}
catch (ExecutionException ex) {
this.result = ex;
@ -120,9 +120,8 @@ public abstract class FutureAdapter<T, S> implements Future<T> {
this.state = State.FAILURE;
throw execEx;
}
default:
throw new IllegalStateException();
}
}
};
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -65,21 +65,19 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader {
@Override
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
switch (name) {
case NAMESPACES_FEATURE_NAME:
return this.namespacesFeature;
case NAMESPACE_PREFIXES_FEATURE_NAME:
return this.namespacePrefixesFeature;
case IS_STANDALONE_FEATURE_NAME:
return switch (name) {
case NAMESPACES_FEATURE_NAME -> this.namespacesFeature;
case NAMESPACE_PREFIXES_FEATURE_NAME -> this.namespacePrefixesFeature;
case IS_STANDALONE_FEATURE_NAME -> {
if (this.isStandalone != null) {
return this.isStandalone;
yield this.isStandalone;
}
else {
throw new SAXNotSupportedException("startDocument() callback not completed yet");
}
default:
return super.getFeature(name);
}
}
default -> super.getFeature(name);
};
}
@Override

View File

@ -274,7 +274,7 @@ public class MethodReference extends SpelNodeImpl {
public boolean isCompilable() {
CachedMethodExecutor executorToCheck = this.cachedExecutor;
if (executorToCheck == null || executorToCheck.hasProxyTarget() ||
!(executorToCheck.get() instanceof ReflectiveMethodExecutor)) {
!(executorToCheck.get() instanceof ReflectiveMethodExecutor executor)) {
return false;
}
@ -284,26 +284,20 @@ public class MethodReference extends SpelNodeImpl {
}
}
ReflectiveMethodExecutor executor = (ReflectiveMethodExecutor) executorToCheck.get();
if (executor.didArgumentConversionOccur()) {
return false;
}
Class<?> clazz = executor.getMethod().getDeclaringClass();
if (!Modifier.isPublic(clazz.getModifiers()) && executor.getPublicDeclaringClass() == null) {
return false;
}
return true;
return Modifier.isPublic(clazz.getModifiers()) || executor.getPublicDeclaringClass() != null;
}
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
CachedMethodExecutor executorToCheck = this.cachedExecutor;
if (executorToCheck == null || !(executorToCheck.get() instanceof ReflectiveMethodExecutor)) {
if (executorToCheck == null || !(executorToCheck.get() instanceof ReflectiveMethodExecutor methodExecutor)) {
throw new IllegalStateException("No applicable cached executor found: " + executorToCheck);
}
ReflectiveMethodExecutor methodExecutor = (ReflectiveMethodExecutor) executorToCheck.get();
Method method = methodExecutor.getMethod();
boolean isStaticMethod = Modifier.isStatic(method.getModifiers());
String descriptor = cf.lastDescriptor();

View File

@ -365,45 +365,54 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
}
else if (isCacheConsumers()) {
// let raw JMS invocation throw an exception if Destination (i.e. args[0]) is null
if ((methodName.equals("createConsumer") || methodName.equals("createReceiver") ||
methodName.equals("createSubscriber"))) {
Destination dest = (Destination) args[0];
if (dest != null && !(dest instanceof TemporaryQueue || dest instanceof TemporaryTopic)) {
return getCachedConsumer(dest,
(args.length > 1 ? (String) args[1] : null),
(args.length > 2 && (Boolean) args[2]),
null,
false);
switch (methodName) {
case "createConsumer", "createReceiver", "createSubscriber" -> {
Destination dest = (Destination) args[0];
if (dest != null && !(dest instanceof TemporaryQueue || dest instanceof TemporaryTopic)) {
return getCachedConsumer(
dest,
(args.length > 1 ? (String) args[1] : null),
(args.length > 2 && (Boolean) args[2]),
null,
false
);
}
}
}
else if (methodName.equals("createDurableConsumer") || methodName.equals("createDurableSubscriber")) {
Destination dest = (Destination) args[0];
if (dest != null) {
return getCachedConsumer(dest,
(args.length > 2 ? (String) args[2] : null),
(args.length > 3 && (Boolean) args[3]),
(String) args[1],
true);
case "createDurableConsumer", "createDurableSubscriber" -> {
Destination dest = (Destination) args[0];
if (dest != null) {
return getCachedConsumer(
dest,
(args.length > 2 ? (String) args[2] : null),
(args.length > 3 && (Boolean) args[3]),
(String) args[1],
true
);
}
}
}
else if (methodName.equals("createSharedConsumer")) {
Destination dest = (Destination) args[0];
if (dest != null) {
return getCachedConsumer(dest,
(args.length > 2 ? (String) args[2] : null),
null,
(String) args[1],
false);
case "createSharedConsumer" -> {
Destination dest = (Destination) args[0];
if (dest != null) {
return getCachedConsumer(
dest,
(args.length > 2 ? (String) args[2] : null),
null,
(String) args[1],
false
);
}
}
}
else if (methodName.equals("createSharedDurableConsumer")) {
Destination dest = (Destination) args[0];
if (dest != null) {
return getCachedConsumer(dest,
(args.length > 2 ? (String) args[2] : null),
null,
(String) args[1],
true);
case "createSharedDurableConsumer" -> {
Destination dest = (Destination) args[0];
if (dest != null) {
return getCachedConsumer(
dest,
(args.length > 2 ? (String) args[2] : null),
null,
(String) args[1],
true
);
}
}
}
}

View File

@ -378,10 +378,9 @@ public abstract class AbstractMethodMessageHandler<T>
*/
protected HandlerMethod createHandlerMethod(Object handler, Method method) {
HandlerMethod handlerMethod;
if (handler instanceof String) {
if (handler instanceof String beanName) {
ApplicationContext context = getApplicationContext();
Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
String beanName = (String) handler;
handlerMethod = new HandlerMethod(beanName, context.getAutowireCapableBeanFactory(), method);
}
else {

View File

@ -93,10 +93,9 @@ public class GenericMessage<T> implements Message<T>, Serializable {
if (this == other) {
return true;
}
if (!(other instanceof GenericMessage)) {
if (!(other instanceof GenericMessage<?> otherMsg)) {
return false;
}
GenericMessage<?> otherMsg = (GenericMessage<?>) other;
// Using nullSafeEquals for proper array equals comparisons
return (ObjectUtils.nullSafeEquals(this.payload, otherMsg.payload) && this.headers.equals(otherMsg.headers));
}

View File

@ -617,8 +617,7 @@ public class MessageHeaderAccessor {
* @since 4.1
*/
public static MessageHeaderAccessor getMutableAccessor(Message<?> message) {
if (message.getHeaders() instanceof MutableMessageHeaders) {
MutableMessageHeaders mutableHeaders = (MutableMessageHeaders) message.getHeaders();
if (message.getHeaders() instanceof MutableMessageHeaders mutableHeaders) {
MessageHeaderAccessor accessor = mutableHeaders.getAccessor();
return (accessor.isMutable() ? accessor : accessor.createAccessor(message));
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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,13 +62,13 @@ public class ProtobufMessageConverterTests {
this.message = MessageBuilder.withPayload(this.testMsg.toByteArray())
.setHeader(CONTENT_TYPE, ProtobufMessageConverter.PROTOBUF).build();
this.messageWithoutContentType = MessageBuilder.withPayload(this.testMsg.toByteArray()).build();
this.messageJson = MessageBuilder.withPayload(
"{\n" +
" \"foo\": \"Foo\",\n" +
" \"blah\": {\n" +
" \"blah\": 123\n" +
" }\n" +
"}")
this.messageJson = MessageBuilder.withPayload("""
{
"foo": "Foo",
"blah": {
"blah": 123
}
}""".replace("\t", " "))
.setHeader(CONTENT_TYPE, APPLICATION_JSON)
.build();
}

View File

@ -319,9 +319,7 @@ public class MessageHeaderAccessorTests {
})).isEqualTo(expected);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 80; i++) {
sb.append('a');
}
sb.append("a".repeat(80));
final String payload = sb.toString() + " > 80";
String actual = accessor.getShortLogMessage(payload);
@ -355,10 +353,8 @@ public class MessageHeaderAccessorTests {
})).isEqualTo(expected);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 80; i++) {
sb.append('a');
}
final String payload = sb.toString() + " > 80";
sb.append("a".repeat(80));
final String payload = sb + " > 80";
String actual = accessor.getDetailedLogMessage(payload);
assertThat(actual).isEqualTo("headers={contentType=text/plain} payload=" + sb + " > 80");

View File

@ -362,11 +362,10 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana
if (this.entityInterceptor instanceof Interceptor) {
return (Interceptor) this.entityInterceptor;
}
else if (this.entityInterceptor instanceof String) {
else if (this.entityInterceptor instanceof String beanName) {
if (this.beanFactory == null) {
throw new IllegalStateException("Cannot get entity interceptor via bean name if no bean factory set");
}
String beanName = (String) this.entityInterceptor;
return this.beanFactory.getBean(beanName, Interceptor.class);
}
else {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@ -344,8 +344,7 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager
if (getEntityManagerFactory() == null) {
throw new IllegalArgumentException("'entityManagerFactory' or 'persistenceUnitName' is required");
}
if (getEntityManagerFactory() instanceof EntityManagerFactoryInfo) {
EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) getEntityManagerFactory();
if (getEntityManagerFactory() instanceof EntityManagerFactoryInfo emfInfo) {
DataSource dataSource = emfInfo.getDataSource();
if (dataSource != null) {
setDataSource(dataSource);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@ -107,21 +107,19 @@ class InfrastructureProxyTransactionalSqlScriptsTests extends AbstractTransactio
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case "equals":
return (proxy == args[0]);
case "hashCode":
return System.identityHashCode(proxy);
case "getWrappedObject":
return this.dataSource;
default:
return switch (method.getName()) {
case "equals" -> (proxy == args[0]);
case "hashCode" -> System.identityHashCode(proxy);
case "getWrappedObject" -> this.dataSource;
default -> {
try {
return method.invoke(this.dataSource, args);
yield method.invoke(this.dataSource, args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
};
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 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.
@ -45,15 +45,13 @@ public class TransactionManagementConfigurationSelector extends AdviceModeImport
*/
@Override
protected String[] selectImports(AdviceMode adviceMode) {
switch (adviceMode) {
case PROXY:
return new String[] {AutoProxyRegistrar.class.getName(),
ProxyTransactionManagementConfiguration.class.getName()};
case ASPECTJ:
return new String[] {determineTransactionAspectClass()};
default:
return null;
}
return switch (adviceMode) {
case PROXY -> new String[]{
AutoProxyRegistrar.class.getName(),
ProxyTransactionManagementConfiguration.class.getName()
};
case ASPECTJ -> new String[]{determineTransactionAspectClass()};
};
}
private String determineTransactionAspectClass() {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -56,12 +56,12 @@ public class TransactionAttributeSourceEditorTests {
@Test
public void matchesSpecific() throws Exception {
editor.setAsText(
"java.lang.Object.hashCode=PROPAGATION_REQUIRED\n" +
"java.lang.Object.equals=PROPAGATION_MANDATORY\n" +
"java.lang.Object.*it=PROPAGATION_SUPPORTS\n" +
"java.lang.Object.notify=PROPAGATION_SUPPORTS\n" +
"java.lang.Object.not*=PROPAGATION_REQUIRED");
editor.setAsText("""
java.lang.Object.hashCode=PROPAGATION_REQUIRED
java.lang.Object.equals=PROPAGATION_MANDATORY
java.lang.Object.*it=PROPAGATION_SUPPORTS
java.lang.Object.notify=PROPAGATION_SUPPORTS
java.lang.Object.not*=PROPAGATION_REQUIRED""");
TransactionAttributeSource tas = (TransactionAttributeSource) editor.getValue();
checkTransactionProperties(tas, Object.class.getMethod("hashCode"),

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -97,32 +97,26 @@ public class HttpServerErrorException extends HttpStatusCodeException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
if (statusCode instanceof HttpStatus status) {
switch (status) {
case INTERNAL_SERVER_ERROR:
return message != null ?
new HttpServerErrorException.InternalServerError(message, statusText, headers, body, charset) :
new HttpServerErrorException.InternalServerError(statusText, headers, body, charset);
case NOT_IMPLEMENTED:
return message != null ?
new HttpServerErrorException.NotImplemented(message, statusText, headers, body, charset) :
new HttpServerErrorException.NotImplemented(statusText, headers, body, charset);
case BAD_GATEWAY:
return message != null ?
new HttpServerErrorException.BadGateway(message, statusText, headers, body, charset) :
new HttpServerErrorException.BadGateway(statusText, headers, body, charset);
case SERVICE_UNAVAILABLE:
return message != null ?
new HttpServerErrorException.ServiceUnavailable(message, statusText, headers, body, charset) :
new HttpServerErrorException.ServiceUnavailable(statusText, headers, body, charset);
case GATEWAY_TIMEOUT:
return message != null ?
new HttpServerErrorException.GatewayTimeout(message, statusText, headers, body, charset) :
new HttpServerErrorException.GatewayTimeout(statusText, headers, body, charset);
default:
return message != null ?
new HttpServerErrorException(message, statusCode, statusText, headers, body, charset) :
new HttpServerErrorException(statusCode, statusText, headers, body, charset);
}
return switch (status) {
case INTERNAL_SERVER_ERROR -> message != null ?
new InternalServerError(message, statusText, headers, body, charset) :
new InternalServerError(statusText, headers, body, charset);
case NOT_IMPLEMENTED -> message != null ?
new NotImplemented(message, statusText, headers, body, charset) :
new NotImplemented(statusText, headers, body, charset);
case BAD_GATEWAY -> message != null ?
new BadGateway(message, statusText, headers, body, charset) :
new BadGateway(statusText, headers, body, charset);
case SERVICE_UNAVAILABLE -> message != null ?
new ServiceUnavailable(message, statusText, headers, body, charset) :
new ServiceUnavailable(statusText, headers, body, charset);
case GATEWAY_TIMEOUT -> message != null ?
new GatewayTimeout(message, statusText, headers, body, charset) :
new GatewayTimeout(statusText, headers, body, charset);
default -> message != null ?
new HttpServerErrorException(message, statusCode, statusText, headers, body, charset) :
new HttpServerErrorException(statusCode, statusText, headers, body, charset);
};
}
if (message != null) {
return new HttpServerErrorException(message, statusCode, statusText, headers, body, charset);

View File

@ -263,18 +263,14 @@ public class CorsConfiguration {
boolean withinPortRange = false;
for (int current = 0; current < rawValue.length(); current++) {
switch (rawValue.charAt(current)) {
case '[':
withinPortRange = true;
break;
case ']':
withinPortRange = false;
break;
case ',':
case '[' -> withinPortRange = true;
case ']' -> withinPortRange = false;
case ',' -> {
if (!withinPortRange) {
valueConsumer.accept(rawValue.substring(start, current).trim());
start = current + 1;
}
break;
}
}
}
if (start < rawValue.length()) {

View File

@ -159,8 +159,12 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractLeakCheckingT
public void readPojo() {
MockServerHttpRequest request = MockServerHttpRequest.post("/")
.body(Mono.just(stringBuffer(
"data:{\"foo\": \"foofoo\", \"bar\": \"barbar\"}\n\n" +
"data:{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}\n\n")));
"""
data:{"foo": "foofoo", "bar": "barbar"}
data:{"foo": "foofoofoo", "bar": "barbarbar"}
""")));
Flux<Pojo> data = reader.read(ResolvableType.forClass(Pojo.class), request,
Collections.emptyMap()).cast(Pojo.class);

View File

@ -173,14 +173,18 @@ class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAllocating
StepVerifier.create(outputMessage.getBody())
.consumeNextWith(stringConsumer("data:"))
.consumeNextWith(stringConsumer("{\n" +
"data: \"foo\" : \"foofoo\",\n" +
"data: \"bar\" : \"barbar\"\n" + "data:}"))
.consumeNextWith(stringConsumer("""
{
data: "foo" : "foofoo",
data: "bar" : "barbar"
data:}"""))
.consumeNextWith(stringConsumer("\n\n"))
.consumeNextWith(stringConsumer("data:"))
.consumeNextWith(stringConsumer("{\n" +
"data: \"foo\" : \"foofoofoo\",\n" +
"data: \"bar\" : \"barbarbar\"\n" + "data:}"))
.consumeNextWith(stringConsumer("""
{
data: "foo" : "foofoofoo",
data: "bar" : "barbarbar"
data:}"""))
.consumeNextWith(stringConsumer("\n\n"))
.expectComplete()
.verify();

View File

@ -181,21 +181,22 @@ public class Jaxb2CollectionHttpMessageConverterTests {
public void testXmlBomb() throws Exception {
// https://en.wikipedia.org/wiki/Billion_laughs
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
String content = "<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE lolz [\n" +
" <!ENTITY lol \"lol\">\n" +
" <!ELEMENT lolz (#PCDATA)>\n" +
" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
"]>\n" +
"<list><rootElement><external>&lol9;</external></rootElement></list>";
String content = """
<?xml version="1.0"?>
<!DOCTYPE lolz [
<!ENTITY lol "lol">
<!ELEMENT lolz (#PCDATA)>
<!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
<!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
<!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
<!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
<!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
<!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
<!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
]>
<list><rootElement><external>&lol9;</external></rootElement></list>""";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8));
assertThatExceptionOfType(HttpMessageNotReadableException.class)
.isThrownBy(() -> this.converter.read(this.rootElementListType, null, inputMessage))

View File

@ -153,21 +153,22 @@ public class Jaxb2RootElementHttpMessageConverterTests {
public void testXmlBomb() throws Exception {
// https://en.wikipedia.org/wiki/Billion_laughs
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
String content = "<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE lolz [\n" +
" <!ENTITY lol \"lol\">\n" +
" <!ELEMENT lolz (#PCDATA)>\n" +
" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
"]>\n" +
"<rootElement><external>&lol9;</external></rootElement>";
String content = """
<?xml version="1.0"?>
<!DOCTYPE lolz [
<!ENTITY lol "lol">
<!ELEMENT lolz (#PCDATA)>
<!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
<!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
<!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
<!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
<!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
<!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
<!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
]>
<rootElement><external>&lol9;</external></rootElement>""";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8));
assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() ->
this.converter.read(RootElement.class, inputMessage))

View File

@ -168,21 +168,22 @@ public class MappingJackson2XmlHttpMessageConverterTests {
public void readWithXmlBomb() throws IOException {
// https://en.wikipedia.org/wiki/Billion_laughs
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
String body = "<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE lolz [\n" +
" <!ENTITY lol \"lol\">\n" +
" <!ELEMENT lolz (#PCDATA)>\n" +
" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
"]>\n" +
"<MyBean>&lol9;</MyBean>";
String body = """
<?xml version="1.0"?>
<!DOCTYPE lolz [
<!ENTITY lol "lol">
<!ELEMENT lolz (#PCDATA)>
<!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
<!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
<!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
<!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
<!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
<!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
<!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
]>
<MyBean>&lol9;</MyBean>""";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);

View File

@ -112,21 +112,22 @@ public class SourceHttpMessageConverterTests {
public void readDomSourceWithXmlBomb() throws Exception {
// https://en.wikipedia.org/wiki/Billion_laughs
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
String content = "<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE lolz [\n" +
" <!ENTITY lol \"lol\">\n" +
" <!ELEMENT lolz (#PCDATA)>\n" +
" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
"]>\n" +
"<root>&lol9;</root>";
String content = """
<?xml version="1.0"?>
<!DOCTYPE lolz [
<!ENTITY lol "lol">
<!ELEMENT lolz (#PCDATA)>
<!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
<!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
<!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
<!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
<!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
<!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
<!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
]>
<root>&lol9;</root>""";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8));
assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() ->
@ -166,21 +167,22 @@ public class SourceHttpMessageConverterTests {
public void readSAXSourceWithXmlBomb() throws Exception {
// https://en.wikipedia.org/wiki/Billion_laughs
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
String content = "<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE lolz [\n" +
" <!ENTITY lol \"lol\">\n" +
" <!ELEMENT lolz (#PCDATA)>\n" +
" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
"]>\n" +
"<root>&lol9;</root>";
String content = """
<?xml version="1.0"?>
<!DOCTYPE lolz [
<!ENTITY lol "lol">
<!ELEMENT lolz (#PCDATA)>
<!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
<!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
<!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
<!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
<!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
<!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
<!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
]>
<root>&lol9;</root>""";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8));
SAXSource result = (SAXSource) this.converter.read(SAXSource.class, inputMessage);
@ -232,21 +234,22 @@ public class SourceHttpMessageConverterTests {
public void readStAXSourceWithXmlBomb() throws Exception {
// https://en.wikipedia.org/wiki/Billion_laughs
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
String content = "<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE lolz [\n" +
" <!ENTITY lol \"lol\">\n" +
" <!ELEMENT lolz (#PCDATA)>\n" +
" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
"]>\n" +
"<root>&lol9;</root>";
String content = """
<?xml version="1.0"?>
<!DOCTYPE lolz [
<!ENTITY lol "lol">
<!ELEMENT lolz (#PCDATA)>
<!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
<!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
<!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
<!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
<!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
<!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
<!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
]>
<root>&lol9;</root>""";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8));
StAXSource result = (StAXSource) this.converter.read(StAXSource.class, inputMessage);

View File

@ -84,11 +84,13 @@ public class StandardMultipartHttpServletRequestTests {
new FormHttpMessageConverter().write(map, null, output);
assertThat(output.getBodyAsString(StandardCharsets.UTF_8)).contains(
"Content-Disposition: form-data; name=\"file\"; filename=\"myFile.txt\"\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Length: 6\r\n" +
"\r\n" +
"myBody\r\n");
"""
Content-Disposition: form-data; name="file"; filename="myFile.txt"\r
Content-Type: text/plain\r
Content-Length: 6\r
\r
myBody\r
""");
}

View File

@ -201,34 +201,22 @@ public final class CloseStatus {
*/
public static CloseStatus create(int code, @Nullable String reason) {
if (!StringUtils.hasText(reason)) {
switch (code) {
case 1000:
return NORMAL;
case 1001:
return GOING_AWAY;
case 1002:
return PROTOCOL_ERROR;
case 1003:
return NOT_ACCEPTABLE;
case 1005:
return NO_STATUS_CODE;
case 1006:
return NO_CLOSE_FRAME;
case 1007:
return BAD_DATA;
case 1008:
return POLICY_VIOLATION;
case 1009:
return TOO_BIG_TO_PROCESS;
case 1010:
return REQUIRED_EXTENSION;
case 1011:
return SERVER_ERROR;
case 1012:
return SERVICE_RESTARTED;
case 1013:
return SERVICE_OVERLOAD;
}
return switch (code) {
case 1000 -> NORMAL;
case 1001 -> GOING_AWAY;
case 1002 -> PROTOCOL_ERROR;
case 1003 -> NOT_ACCEPTABLE;
case 1005 -> NO_STATUS_CODE;
case 1006 -> NO_CLOSE_FRAME;
case 1007 -> BAD_DATA;
case 1008 -> POLICY_VIOLATION;
case 1009 -> TOO_BIG_TO_PROCESS;
case 1010 -> REQUIRED_EXTENSION;
case 1011 -> SERVER_ERROR;
case 1012 -> SERVICE_RESTARTED;
case 1013 -> SERVICE_OVERLOAD;
default -> new CloseStatus(code, reason);
};
}
return new CloseStatus(code, reason);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@ -125,26 +125,20 @@ class FlushingIntegrationTests extends AbstractHttpHandlerIntegrationTests {
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
String path = request.getURI().getPath();
switch (path) {
case "/write-and-flush":
return response.writeAndFlushWith(
testInterval(Duration.ofMillis(50), 2)
.map(longValue -> wrap("data" + longValue + "\n", response))
.map(Flux::just)
.mergeWith(Flux.never()));
case "/write-and-complete":
return response.writeWith(
chunks1K().take(64).map(s -> wrap(s, response)));
case "/write-and-never-complete":
return switch (path) {
case "/write-and-flush" -> response.writeAndFlushWith(
testInterval(Duration.ofMillis(50), 2)
.map(longValue -> wrap("data" + longValue + "\n", response))
.map(Flux::just)
.mergeWith(Flux.never()));
case "/write-and-complete" -> response.writeWith(
chunks1K().take(64).map(s -> wrap(s, response)));
case "/write-and-never-complete" ->
// Reactor requires at least 50 to flush, Tomcat/Undertow 8, Jetty 1
return response.writeWith(
chunks1K().take(64).map(s -> wrap(s, response)).mergeWith(Flux.never()));
default:
return response.writeWith(Flux.empty());
}
response.writeWith(
chunks1K().take(64).map(s -> wrap(s, response)).mergeWith(Flux.never()));
default -> response.writeWith(Flux.empty());
};
}
private Flux<String> chunks1K() {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@ -336,23 +336,25 @@ public class BodyExtractorsTests {
public void toParts() {
BodyExtractor<Flux<Part>, ServerHttpRequest> extractor = BodyExtractors.toParts();
String bodyContents = "-----------------------------9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"text\"\r\n" +
"\r\n" +
"text default\r\n" +
"-----------------------------9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\"\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"Content of a.txt.\r\n" +
"\r\n" +
"-----------------------------9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"file2\"; filename=\"a.html\"\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"<!DOCTYPE html><title>Content of a.html.</title>\r\n" +
"\r\n" +
"-----------------------------9051914041544843365972754266--\r\n";
String bodyContents = """
-----------------------------9051914041544843365972754266\r
Content-Disposition: form-data; name="text"\r
\r
text default\r
-----------------------------9051914041544843365972754266\r
Content-Disposition: form-data; name="file1"; filename="a.txt"\r
Content-Type: text/plain\r
\r
Content of a.txt.\r
\r
-----------------------------9051914041544843365972754266\r
Content-Disposition: form-data; name="file2"; filename="a.html"\r
Content-Type: text/html\r
\r
<!DOCTYPE html><title>Content of a.html.</title>\r
\r
-----------------------------9051914041544843365972754266--\r
""";
byte[] bytes = bodyContents.getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));

View File

@ -726,8 +726,8 @@ class WebClientIntegrationTests {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
request.getBody().copyTo(bos);
String actual = bos.toString("UTF-8");
String expected = new String(Files.readAllBytes(resource.getFile().toPath()), StandardCharsets.UTF_8);
String actual = bos.toString(StandardCharsets.UTF_8);
String expected = Files.readString(resource.getFile().toPath(), StandardCharsets.UTF_8);
assertThat(actual).isEqualTo(expected);
}
catch (IOException ex) {

View File

@ -412,15 +412,17 @@ public class DefaultServerRequestTests {
@Test
public void multipartData() {
String data = "--12345\r\n" +
"Content-Disposition: form-data; name=\"foo\"\r\n" +
"\r\n" +
"bar\r\n" +
"--12345\r\n" +
"Content-Disposition: form-data; name=\"baz\"\r\n" +
"\r\n" +
"qux\r\n" +
"--12345--\r\n";
String data = """
--12345\r
Content-Disposition: form-data; name="foo"\r
\r
bar\r
--12345\r
Content-Disposition: form-data; name="baz"\r
\r
qux\r
--12345--\r
""";
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -80,13 +80,17 @@ public class CssLinkResourceTransformerTests {
MockServerWebExchange exchange = MockServerWebExchange.from(get("/static/main.css"));
Resource css = getResource("main.css");
String expected = "\n" +
"@import url(\"/static/bar-11e16cf79faee7ac698c805cf28248d2.css?#iefix\");\n" +
"@import url('/static/bar-11e16cf79faee7ac698c805cf28248d2.css#bla-normal');\n" +
"@import url(/static/bar-11e16cf79faee7ac698c805cf28248d2.css);\n\n" +
"@import \"/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css\";\n" +
"@import '/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css';\n\n" +
"body { background: url(\"/static/images/image-f448cd1d5dba82b774f3202c878230b3.png?#iefix\") }\n";
String expected = """
@import url("/static/bar-11e16cf79faee7ac698c805cf28248d2.css?#iefix");
@import url('/static/bar-11e16cf79faee7ac698c805cf28248d2.css#bla-normal');
@import url(/static/bar-11e16cf79faee7ac698c805cf28248d2.css);
@import "/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css";
@import '/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css';
body { background: url("/static/images/image-f448cd1d5dba82b774f3202c878230b3.png?#iefix") }
""";
StepVerifier.create(this.transformerChain.transform(exchange, css)
.cast(TransformedResource.class))
@ -118,9 +122,10 @@ public class CssLinkResourceTransformerTests {
ResourceTransformerChain chain = new DefaultResourceTransformerChain(mockChain, transformers);
Resource resource = getResource("external.css");
String expected = "@import url(\"https://example.org/fonts/css\");\n" +
"body { background: url(\"file:///home/spring/image.png\") }\n" +
"figure { background: url(\"//example.org/style.css\")}";
String expected = """
@import url("https://example.org/fonts/css");
body { background: url("file:///home/spring/image.png") }
figure { background: url("//example.org/style.css")}""";
StepVerifier.create(chain.transform(exchange, resource)
.cast(TransformedResource.class))
@ -167,10 +172,10 @@ public class CssLinkResourceTransformerTests {
public void transformEmptyUrlFunction() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(get("/static/empty_url_function.css"));
Resource css = getResource("empty_url_function.css");
String expected =
".fooStyle {\n" +
"\tbackground: transparent url() no-repeat left top;\n" +
"}";
String expected = """
.fooStyle {
\tbackground: transparent url() no-repeat left top;
}""";
StepVerifier.create(this.transformerChain.transform(exchange, css)
.cast(TransformedResource.class))

View File

@ -45,7 +45,6 @@ import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
import org.springframework.web.testfixture.server.MockServerWebExchange;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
@ -357,8 +356,9 @@ public class FreeMarkerMacroTests {
}
private void storeTemplateInTempDir(String macro) throws IOException {
Files.write(this.templateLoaderPath.resolve("tmp.ftl"),
("<#import \"spring.ftl\" as spring />\n" + macro).getBytes(UTF_8));
Files.writeString(this.templateLoaderPath.resolve("tmp.ftl"),
"<#import \"spring.ftl\" as spring />\n" + macro
);
}
private List<String> getOutput() {

View File

@ -381,8 +381,7 @@ class ReactiveTypeHandler {
@Override
protected void send(Object element) throws IOException {
if (element instanceof ServerSentEvent) {
ServerSentEvent<?> event = (ServerSentEvent<?>) element;
if (element instanceof ServerSentEvent<?> event) {
((SseEmitter) getEmitter()).send(adapt(event));
}
else {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 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.
@ -223,16 +223,14 @@ public abstract class AbstractMultiCheckedElementTag extends AbstractCheckedElem
writeObjectEntry(tagWriter, valueProperty, labelProperty, item, i);
}
}
else if (itemsObject instanceof Collection) {
final Collection<?> optionCollection = (Collection<?>) itemsObject;
else if (itemsObject instanceof final Collection<?> optionCollection) {
int itemIndex = 0;
for (Iterator<?> it = optionCollection.iterator(); it.hasNext(); itemIndex++) {
Object item = it.next();
writeObjectEntry(tagWriter, valueProperty, labelProperty, item, itemIndex);
}
}
else if (itemsObject instanceof Map) {
final Map<?, ?> optionMap = (Map<?, ?>) itemsObject;
else if (itemsObject instanceof final Map<?, ?> optionMap) {
int itemIndex = 0;
for (Iterator it = optionMap.entrySet().iterator(); it.hasNext(); itemIndex++) {
Map.Entry entry = (Map.Entry) it.next();

View File

@ -537,8 +537,7 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView {
}
return true;
}
if (value instanceof Collection) {
Collection<?> coll = (Collection<?>) value;
if (value instanceof Collection<?> coll) {
if (coll.isEmpty()) {
return false;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@ -109,12 +109,15 @@ class SseServerResponseTests {
ModelAndView mav = response.writeTo(this.mockRequest, this.mockResponse, context);
assertThat(mav).isNull();
String expected = "id:id\n" +
"event:name\n" +
":comment line 1\n" +
":comment line 2\n" +
"retry:1000\n" +
"data:data\n\n";
String expected = """
id:id
event:name
:comment line 1
:comment line 2
retry:1000
data:data
""";
assertThat(this.mockResponse.getContentAsString()).isEqualTo(expected);
}

View File

@ -52,11 +52,12 @@ public class ToStringVisitorTests {
routerFunction.accept(visitor);
String result = visitor.toString();
String expected = "/foo => {\n" +
" /bar => {\n" +
" (GET && /baz) -> \n" +
" }\n" +
"}";
String expected = """
/foo => {
/bar => {
(GET && /baz) ->\s
}
}""".replace('\t', ' ');
assertThat(result).isEqualTo(expected);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -84,13 +84,17 @@ public class CssLinkResourceTransformerTests {
public void transform() throws Exception {
this.request = new MockHttpServletRequest("GET", "/static/main.css");
Resource css = getResource("main.css");
String expected = "\n" +
"@import url(\"/static/bar-11e16cf79faee7ac698c805cf28248d2.css?#iefix\");\n" +
"@import url('/static/bar-11e16cf79faee7ac698c805cf28248d2.css#bla-normal');\n" +
"@import url(/static/bar-11e16cf79faee7ac698c805cf28248d2.css);\n\n" +
"@import \"/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css\";\n" +
"@import '/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css';\n\n" +
"body { background: url(\"/static/images/image-f448cd1d5dba82b774f3202c878230b3.png?#iefix\") }\n";
String expected = """
@import url("/static/bar-11e16cf79faee7ac698c805cf28248d2.css?#iefix");
@import url('/static/bar-11e16cf79faee7ac698c805cf28248d2.css#bla-normal');
@import url(/static/bar-11e16cf79faee7ac698c805cf28248d2.css);
@import "/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css";
@import '/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css';
body { background: url("/static/images/image-f448cd1d5dba82b774f3202c878230b3.png?#iefix") }
""";
TransformedResource actual = (TransformedResource) this.transformerChain.transform(this.request, css);
String result = new String(actual.getByteArray(), StandardCharsets.UTF_8);
@ -115,9 +119,10 @@ public class CssLinkResourceTransformerTests {
ResourceTransformerChain chain = new DefaultResourceTransformerChain(mockChain, transformers);
Resource resource = getResource("external.css");
String expected = "@import url(\"https://example.org/fonts/css\");\n" +
"body { background: url(\"file:///home/spring/image.png\") }\n" +
"figure { background: url(\"//example.org/style.css\")}";
String expected = """
@import url("https://example.org/fonts/css");
body { background: url("file:///home/spring/image.png") }
figure { background: url("//example.org/style.css")}""";
TransformedResource transformedResource = (TransformedResource) chain.transform(this.request, resource);
String result = new String(transformedResource.getByteArray(), StandardCharsets.UTF_8);
@ -155,10 +160,10 @@ public class CssLinkResourceTransformerTests {
public void transformEmptyUrlFunction() throws Exception {
this.request = new MockHttpServletRequest("GET", "/static/empty_url_function.css");
Resource css = getResource("empty_url_function.css");
String expected =
".fooStyle {\n" +
"\tbackground: transparent url() no-repeat left top;\n" +
"}";
String expected = """
.fooStyle {
\tbackground: transparent url() no-repeat left top;
}""";
TransformedResource actual = (TransformedResource) this.transformerChain.transform(this.request, css);
String result = new String(actual.getByteArray(), StandardCharsets.UTF_8);

View File

@ -48,7 +48,6 @@ import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import org.springframework.web.testfixture.servlet.MockServletContext;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
/**
@ -346,8 +345,9 @@ public class FreeMarkerMacroTests {
}
private void storeTemplateInTempDir(String macro) throws IOException {
Files.write(this.templateLoaderPath.resolve("tmp.ftl"),
("<#import \"spring.ftl\" as spring />\n" + macro).getBytes(UTF_8));
Files.writeString(this.templateLoaderPath.resolve("tmp.ftl"),
"<#import \"spring.ftl\" as spring />\n" + macro
);
}
private String getOutput() throws IOException {

View File

@ -427,7 +427,7 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
@Override
@SuppressWarnings("unchecked")
public void handleMessageToClient(WebSocketSession session, Message<?> message) {
if (!(message.getPayload() instanceof byte[])) {
if (!(message.getPayload() instanceof byte[] payload)) {
if (logger.isErrorEnabled()) {
logger.error("Expected byte[] payload. Ignoring " + message + ".");
}
@ -464,7 +464,6 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
}
}
byte[] payload = (byte[]) message.getPayload();
if (StompCommand.ERROR.equals(command) && getErrorHandler() != null) {
Message<byte[]> errorMessage = getErrorHandler().handleErrorMessageToClient((Message<byte[]>) message);
if (errorMessage != null) {

View File

@ -229,19 +229,14 @@ public abstract class AbstractClientSockJsSession implements WebSocketSession {
public void handleFrame(String payload) {
SockJsFrame frame = new SockJsFrame(payload);
switch (frame.getType()) {
case OPEN:
handleOpenFrame();
break;
case HEARTBEAT:
case OPEN -> handleOpenFrame();
case HEARTBEAT -> {
if (logger.isTraceEnabled()) {
logger.trace("Received heartbeat in " + this);
}
break;
case MESSAGE:
handleMessageFrame(frame);
break;
case CLOSE:
handleCloseFrame(frame);
}
case MESSAGE -> handleMessageFrame(frame);
case CLOSE -> handleCloseFrame(frame);
}
}

View File

@ -276,12 +276,11 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem
SockJsSession session = this.sessions.get(sessionId);
boolean isNewSession = false;
if (session == null) {
if (transportHandler instanceof SockJsSessionFactory) {
if (transportHandler instanceof SockJsSessionFactory sessionFactory) {
Map<String, Object> attributes = new HashMap<>();
if (!chain.applyBeforeHandshake(request, response, attributes)) {
return;
}
SockJsSessionFactory sessionFactory = (SockJsSessionFactory) transportHandler;
session = createSockJsSession(sessionId, sessionFactory, handler, attributes);
isNewSession = true;
}