mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 16:39:29 +00:00
Implement AutoCloseable in GzippedFiles
This commit is contained in:
+14
-15
@@ -19,6 +19,7 @@ package org.springframework.web.reactive.resource;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@@ -26,7 +27,6 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import org.junit.jupiter.api.extension.AfterEachCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext.Store;
|
||||
@@ -40,21 +40,13 @@ import org.springframework.util.FileCopyUtils;
|
||||
|
||||
/**
|
||||
* @author Andy Wilkinson
|
||||
* @author Sam Brannen
|
||||
* @since 5.2.2
|
||||
*/
|
||||
class GzipSupport implements AfterEachCallback, ParameterResolver {
|
||||
class GzipSupport implements ParameterResolver {
|
||||
|
||||
private static final Namespace namespace = Namespace.create(GzipSupport.class);
|
||||
|
||||
@Override
|
||||
public void afterEach(ExtensionContext context) {
|
||||
GzippedFiles gzippedFiles = getStore(context).remove(GzippedFiles.class, GzippedFiles.class);
|
||||
if (gzippedFiles != null) {
|
||||
for (File gzippedFile: gzippedFiles.created) {
|
||||
gzippedFile.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
|
||||
@@ -70,13 +62,14 @@ class GzipSupport implements AfterEachCallback, ParameterResolver {
|
||||
return extensionContext.getStore(namespace);
|
||||
}
|
||||
|
||||
static class GzippedFiles {
|
||||
|
||||
static class GzippedFiles implements AutoCloseable {
|
||||
|
||||
private final Set<File> created = new HashSet<>();
|
||||
|
||||
void create(String filePath) {
|
||||
try {
|
||||
Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class);
|
||||
Resource location = new ClassPathResource("test/", getClass());
|
||||
Resource resource = new FileSystemResource(location.createRelative(filePath).getFile());
|
||||
|
||||
Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz");
|
||||
@@ -85,13 +78,19 @@ class GzipSupport implements AfterEachCallback, ParameterResolver {
|
||||
File gzFile = Files.createFile(gzFilePath).toFile();
|
||||
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile));
|
||||
FileCopyUtils.copy(resource.getInputStream(), out);
|
||||
created.add(gzFile);
|
||||
this.created.add(gzFile);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
throw new UncheckedIOException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
for (File file: this.created) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+14
-15
@@ -19,6 +19,7 @@ package org.springframework.web.servlet.resource;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@@ -26,7 +27,6 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import org.junit.jupiter.api.extension.AfterEachCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext.Store;
|
||||
@@ -40,21 +40,13 @@ import org.springframework.util.FileCopyUtils;
|
||||
|
||||
/**
|
||||
* @author Andy Wilkinson
|
||||
* @author Sam Brannen
|
||||
* @since 5.2.2
|
||||
*/
|
||||
class GzipSupport implements AfterEachCallback, ParameterResolver {
|
||||
class GzipSupport implements ParameterResolver {
|
||||
|
||||
private static final Namespace namespace = Namespace.create(GzipSupport.class);
|
||||
|
||||
@Override
|
||||
public void afterEach(ExtensionContext context) {
|
||||
GzippedFiles gzippedFiles = getStore(context).remove(GzippedFiles.class, GzippedFiles.class);
|
||||
if (gzippedFiles != null) {
|
||||
for (File gzippedFile: gzippedFiles.created) {
|
||||
gzippedFile.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
|
||||
@@ -70,13 +62,14 @@ class GzipSupport implements AfterEachCallback, ParameterResolver {
|
||||
return extensionContext.getStore(namespace);
|
||||
}
|
||||
|
||||
static class GzippedFiles {
|
||||
|
||||
static class GzippedFiles implements AutoCloseable {
|
||||
|
||||
private final Set<File> created = new HashSet<>();
|
||||
|
||||
void create(String filePath) {
|
||||
try {
|
||||
Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class);
|
||||
Resource location = new ClassPathResource("test/", getClass());
|
||||
Resource resource = new FileSystemResource(location.createRelative(filePath).getFile());
|
||||
|
||||
Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz");
|
||||
@@ -85,13 +78,19 @@ class GzipSupport implements AfterEachCallback, ParameterResolver {
|
||||
File gzFile = Files.createFile(gzFilePath).toFile();
|
||||
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile));
|
||||
FileCopyUtils.copy(resource.getInputStream(), out);
|
||||
created.add(gzFile);
|
||||
this.created.add(gzFile);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
throw new UncheckedIOException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
for (File file: this.created) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-1
@@ -37,6 +37,7 @@ import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.servlet.resource.GzipSupport.GzippedFiles;
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
|
||||
import org.springframework.web.testfixture.servlet.MockServletContext;
|
||||
@@ -357,7 +358,7 @@ class ResourceHttpRequestHandlerTests {
|
||||
}
|
||||
|
||||
@Test // gh-25976
|
||||
void partialContentByteRangeWithEncodedResource(GzipSupport.GzippedFiles gzippedFiles) throws Exception {
|
||||
void partialContentByteRangeWithEncodedResource(GzippedFiles gzippedFiles) throws Exception {
|
||||
String path = "js/foo.js";
|
||||
gzippedFiles.create(path);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user