From 9d2c6f80b88b22714b80911746333f3c76657444 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 23 Apr 2024 16:25:24 +0200 Subject: [PATCH] Polishing --- .../util/StreamUtilsTests.java | 5 ++- .../ResourceHttpMessageConverter.java | 33 +++++++++-------- .../ResourceRegionHttpMessageConverter.java | 36 +++++++++---------- .../support/InvocableHandlerMethod.java | 3 +- 4 files changed, 42 insertions(+), 35 deletions(-) diff --git a/spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java b/spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java index 24330cf6778..6d3a276c207 100644 --- a/spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 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. @@ -45,6 +45,7 @@ class StreamUtilsTests { private String string = ""; + @BeforeEach void setup() { new Random().nextBytes(bytes); @@ -53,6 +54,7 @@ class StreamUtilsTests { } } + @Test void copyToByteArray() throws Exception { InputStream inputStream = new ByteArrayInputStream(bytes); @@ -127,4 +129,5 @@ class StreamUtilsTests { ordered.verify(source).write(bytes, 1, 2); ordered.verify(source, never()).close(); } + } diff --git a/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java index 4f1f34cc855..49ce0b17114 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java @@ -82,6 +82,7 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter regions = (Collection) object; - if (!regions.isEmpty()) { - resource = regions.iterator().next().getResource(); - } - } - return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM); - } - @Override public boolean canRead(Class clazz, @Nullable MediaType mediaType) { return false; @@ -119,7 +103,6 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa } @Override - @SuppressWarnings("unchecked") protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { @@ -127,6 +110,7 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa writeResourceRegion(resourceRegion, outputMessage); } else { + @SuppressWarnings("unchecked") Collection regions = (Collection) object; if (regions.size() == 1) { writeResourceRegion(regions.iterator().next(), outputMessage); @@ -137,6 +121,22 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa } } + @Override + protected MediaType getDefaultContentType(Object object) { + Resource resource = null; + if (object instanceof ResourceRegion resourceRegion) { + resource = resourceRegion.getResource(); + } + else { + @SuppressWarnings("unchecked") + Collection regions = (Collection) object; + if (!regions.isEmpty()) { + resource = regions.iterator().next().getResource(); + } + } + return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM); + } + protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException { Assert.notNull(region, "ResourceRegion must not be null"); diff --git a/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java b/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java index 4fcaec95213..84e26dd4d8a 100644 --- a/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java +++ b/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 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. @@ -230,7 +230,6 @@ public class InvocableHandlerMethod extends HandlerMethod { /** * Invoke the given Kotlin coroutine suspended function. - * *

The default implementation invokes * {@link CoroutinesUtils#invokeSuspendingFunction(Method, Object, Object...)}, * but subclasses can override this method to use