Polishing

This commit is contained in:
Juergen Hoeller 2024-04-23 13:16:33 +02:00
parent 0b9b9b4602
commit d4ddbd537b
2 changed files with 59 additions and 53 deletions

View File

@ -108,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 @Override
protected MediaType getDefaultContentType(Resource resource) { protected MediaType getDefaultContentType(Resource resource) {
return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM); return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
@ -125,23 +142,15 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
return (contentLength < 0 ? null : contentLength); 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 @Override
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage) protected boolean supportsRepeatableWrites(Resource resource) {
throws IOException, HttpMessageNotWritableException { return !(resource instanceof InputStreamResource);
writeContent(resource, outputMessage);
} }
protected void writeContent(Resource resource, HttpOutputMessage outputMessage) protected void writeContent(Resource resource, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException { throws IOException, HttpMessageNotWritableException {
// We cannot use try-with-resources here for the InputStream, since we have // We cannot use try-with-resources here for the InputStream, since we have
// custom handling of the close() method in a finally-block. // custom handling of the close() method in a finally-block.
try { try {
@ -168,8 +177,4 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
} }
} }
@Override
protected boolean supportsRepeatableWrites(Resource resource) {
return !(resource instanceof InputStreamResource);
}
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -53,22 +53,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 @Override
public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) { public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
return false; return false;
@ -138,6 +122,43 @@ 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);
}
@Override
protected boolean supportsRepeatableWrites(Object object) {
if (object instanceof ResourceRegion resourceRegion) {
return supportsRepeatableWrites(resourceRegion);
}
else {
@SuppressWarnings("unchecked")
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
for (ResourceRegion region : regions) {
if (!supportsRepeatableWrites(region)) {
return false;
}
}
return true;
}
}
private boolean supportsRepeatableWrites(ResourceRegion region) {
return !(region.getResource() instanceof InputStreamResource);
}
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException { protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
Assert.notNull(region, "ResourceRegion must not be null"); Assert.notNull(region, "ResourceRegion must not be null");
@ -239,24 +260,4 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
os.write(buf.getBytes(StandardCharsets.US_ASCII)); os.write(buf.getBytes(StandardCharsets.US_ASCII));
} }
@Override
@SuppressWarnings("unchecked")
protected boolean supportsRepeatableWrites(Object object) {
if (object instanceof ResourceRegion resourceRegion) {
return supportsRepeatableWrites(resourceRegion);
}
else {
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
for (ResourceRegion region : regions) {
if (!supportsRepeatableWrites(region)) {
return false;
}
}
return true;
}
}
private boolean supportsRepeatableWrites(ResourceRegion region) {
return !(region.getResource() instanceof InputStreamResource);
}
} }