Skip to content

Commit d4ddbd5

Browse files
committed
Polishing
1 parent 0b9b9b4 commit d4ddbd5

File tree

2 files changed

+59
-53
lines changed

2 files changed

+59
-53
lines changed

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

+21-16
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ public String getFilename() {
108108
}
109109
}
110110

111+
@Override
112+
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
113+
throws IOException, HttpMessageNotWritableException {
114+
115+
writeContent(resource, outputMessage);
116+
}
117+
118+
/**
119+
* Add the default headers for the given resource to the given message.
120+
* @since 6.0
121+
*/
122+
public void addDefaultHeaders(HttpOutputMessage message, Resource resource, @Nullable MediaType contentType)
123+
throws IOException {
124+
125+
addDefaultHeaders(message.getHeaders(), resource, contentType);
126+
}
127+
111128
@Override
112129
protected MediaType getDefaultContentType(Resource resource) {
113130
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
@@ -125,23 +142,15 @@ protected Long getContentLength(Resource resource, @Nullable MediaType contentTy
125142
return (contentLength < 0 ? null : contentLength);
126143
}
127144

128-
/**
129-
* Adds the default headers for the given resource to the given message.
130-
* @since 6.0
131-
*/
132-
public void addDefaultHeaders(HttpOutputMessage message, Resource resource, @Nullable MediaType contentType) throws IOException {
133-
addDefaultHeaders(message.getHeaders(), resource, contentType);
134-
}
135-
136145
@Override
137-
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
138-
throws IOException, HttpMessageNotWritableException {
139-
140-
writeContent(resource, outputMessage);
146+
protected boolean supportsRepeatableWrites(Resource resource) {
147+
return !(resource instanceof InputStreamResource);
141148
}
142149

150+
143151
protected void writeContent(Resource resource, HttpOutputMessage outputMessage)
144152
throws IOException, HttpMessageNotWritableException {
153+
145154
// We cannot use try-with-resources here for the InputStream, since we have
146155
// custom handling of the close() method in a finally-block.
147156
try {
@@ -168,8 +177,4 @@ protected void writeContent(Resource resource, HttpOutputMessage outputMessage)
168177
}
169178
}
170179

171-
@Override
172-
protected boolean supportsRepeatableWrites(Resource resource) {
173-
return !(resource instanceof InputStreamResource);
174-
}
175180
}

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

+38-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,22 +53,6 @@ public ResourceRegionHttpMessageConverter() {
5353
}
5454

5555

56-
@Override
57-
@SuppressWarnings("unchecked")
58-
protected MediaType getDefaultContentType(Object object) {
59-
Resource resource = null;
60-
if (object instanceof ResourceRegion resourceRegion) {
61-
resource = resourceRegion.getResource();
62-
}
63-
else {
64-
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
65-
if (!regions.isEmpty()) {
66-
resource = regions.iterator().next().getResource();
67-
}
68-
}
69-
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
70-
}
71-
7256
@Override
7357
public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
7458
return false;
@@ -138,6 +122,43 @@ protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessa
138122
}
139123
}
140124

125+
@Override
126+
protected MediaType getDefaultContentType(Object object) {
127+
Resource resource = null;
128+
if (object instanceof ResourceRegion resourceRegion) {
129+
resource = resourceRegion.getResource();
130+
}
131+
else {
132+
@SuppressWarnings("unchecked")
133+
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
134+
if (!regions.isEmpty()) {
135+
resource = regions.iterator().next().getResource();
136+
}
137+
}
138+
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
139+
}
140+
141+
@Override
142+
protected boolean supportsRepeatableWrites(Object object) {
143+
if (object instanceof ResourceRegion resourceRegion) {
144+
return supportsRepeatableWrites(resourceRegion);
145+
}
146+
else {
147+
@SuppressWarnings("unchecked")
148+
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
149+
for (ResourceRegion region : regions) {
150+
if (!supportsRepeatableWrites(region)) {
151+
return false;
152+
}
153+
}
154+
return true;
155+
}
156+
}
157+
158+
private boolean supportsRepeatableWrites(ResourceRegion region) {
159+
return !(region.getResource() instanceof InputStreamResource);
160+
}
161+
141162

142163
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
143164
Assert.notNull(region, "ResourceRegion must not be null");
@@ -239,24 +260,4 @@ private static void print(OutputStream os, String buf) throws IOException {
239260
os.write(buf.getBytes(StandardCharsets.US_ASCII));
240261
}
241262

242-
@Override
243-
@SuppressWarnings("unchecked")
244-
protected boolean supportsRepeatableWrites(Object object) {
245-
if (object instanceof ResourceRegion resourceRegion) {
246-
return supportsRepeatableWrites(resourceRegion);
247-
}
248-
else {
249-
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
250-
for (ResourceRegion region : regions) {
251-
if (!supportsRepeatableWrites(region)) {
252-
return false;
253-
}
254-
}
255-
return true;
256-
}
257-
}
258-
259-
private boolean supportsRepeatableWrites(ResourceRegion region) {
260-
return !(region.getResource() instanceof InputStreamResource);
261-
}
262263
}

0 commit comments

Comments
 (0)