Browse Source

Polishing

pull/33048/head
Juergen Hoeller 2 years ago
parent
commit
9d2c6f80b8
  1. 5
      spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java
  2. 33
      spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java
  3. 36
      spring-web/src/main/java/org/springframework/http/converter/ResourceRegionHttpMessageConverter.java
  4. 3
      spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java

5
spring-core/src/test/java/org/springframework/util/StreamUtilsTests.java

@ -1,5 +1,5 @@ @@ -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 { @@ -45,6 +45,7 @@ class StreamUtilsTests {
private String string = "";
@BeforeEach
void setup() {
new Random().nextBytes(bytes);
@ -53,6 +54,7 @@ class StreamUtilsTests { @@ -53,6 +54,7 @@ class StreamUtilsTests {
}
}
@Test
void copyToByteArray() throws Exception {
InputStream inputStream = new ByteArrayInputStream(bytes);
@ -127,4 +129,5 @@ class StreamUtilsTests { @@ -127,4 +129,5 @@ class StreamUtilsTests {
ordered.verify(source).write(bytes, 1, 2);
ordered.verify(source, never()).close();
}
}

33
spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java

@ -82,6 +82,7 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R @@ -82,6 +82,7 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
if (this.supportsReadStreaming && InputStreamResource.class == clazz) {
return new InputStreamResource(inputMessage.getBody()) {
@Override
@Nullable
public String getFilename() {
return inputMessage.getHeaders().getContentDisposition().getFilename();
}
@ -107,6 +108,23 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R @@ -107,6 +108,23 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
}
}
@Override
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
writeContent(resource, outputMessage);
}
/**
* Add the default headers for the given resource to the given message.
* @since 6.0
*/
public void addDefaultHeaders(HttpOutputMessage message, Resource resource, @Nullable MediaType contentType)
throws IOException {
addDefaultHeaders(message.getHeaders(), resource, contentType);
}
@Override
protected MediaType getDefaultContentType(Resource resource) {
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
@ -124,23 +142,10 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R @@ -124,23 +142,10 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
return (contentLength < 0 ? null : contentLength);
}
/**
* Adds the default headers for the given resource to the given message.
* @since 6.0
*/
public void addDefaultHeaders(HttpOutputMessage message, Resource resource, @Nullable MediaType contentType) throws IOException {
addDefaultHeaders(message.getHeaders(), resource, contentType);
}
@Override
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
writeContent(resource, outputMessage);
}
protected void writeContent(Resource resource, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
// We cannot use try-with-resources here for the InputStream, since we have
// custom handling of the close() method in a finally-block.
try {

36
spring-web/src/main/java/org/springframework/http/converter/ResourceRegionHttpMessageConverter.java

@ -1,5 +1,5 @@ @@ -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.
@ -52,22 +52,6 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa @@ -52,22 +52,6 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
}
@Override
@SuppressWarnings("unchecked")
protected MediaType getDefaultContentType(Object object) {
Resource resource = null;
if (object instanceof ResourceRegion resourceRegion) {
resource = resourceRegion.getResource();
}
else {
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) 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 @@ -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 @@ -127,6 +110,7 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
writeResourceRegion(resourceRegion, outputMessage);
}
else {
@SuppressWarnings("unchecked")
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
if (regions.size() == 1) {
writeResourceRegion(regions.iterator().next(), outputMessage);
@ -137,6 +121,22 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa @@ -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<ResourceRegion> regions = (Collection<ResourceRegion>) 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");

3
spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java

@ -1,5 +1,5 @@ @@ -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 { @@ -230,7 +230,6 @@ public class InvocableHandlerMethod extends HandlerMethod {
/**
* Invoke the given Kotlin coroutine suspended function.
*
* <p>The default implementation invokes
* {@link CoroutinesUtils#invokeSuspendingFunction(Method, Object, Object...)},
* but subclasses can override this method to use

Loading…
Cancel
Save