|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -53,22 +53,6 @@ public ResourceRegionHttpMessageConverter() {
|
53 | 53 | }
|
54 | 54 |
|
55 | 55 |
|
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 |
| - |
72 | 56 | @Override
|
73 | 57 | public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
|
74 | 58 | return false;
|
@@ -138,6 +122,43 @@ protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessa
|
138 | 122 | }
|
139 | 123 | }
|
140 | 124 |
|
| 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 | + |
141 | 162 |
|
142 | 163 | protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
|
143 | 164 | Assert.notNull(region, "ResourceRegion must not be null");
|
@@ -239,24 +260,4 @@ private static void print(OutputStream os, String buf) throws IOException {
|
239 | 260 | os.write(buf.getBytes(StandardCharsets.US_ASCII));
|
240 | 261 | }
|
241 | 262 |
|
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 |
| - } |
262 | 263 | }
|
0 commit comments