From: David ‘Bombe’ Roden Date: Fri, 6 Jan 2017 19:26:04 +0000 (+0100) Subject: Add auto-closeable bucket wrapper and use try-with-resources X-Git-Tag: 0.9.7^2~368 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=e6eb262fba58e7d1264e9ee8146353eaa0a86716;p=Sone.git Add auto-closeable bucket wrapper and use try-with-resources --- diff --git a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java index 222aa72..17a1bab 100644 --- a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java +++ b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.OutputStream; import java.net.URI; +import net.pterodactylus.sone.utils.AutoCloseableBucket; import net.pterodactylus.util.web.Header; import net.pterodactylus.util.web.Method; import net.pterodactylus.util.web.Page; @@ -33,9 +34,7 @@ import freenet.clients.http.Toadlet; import freenet.clients.http.ToadletContext; import freenet.clients.http.ToadletContextClosedException; import freenet.support.MultiValueTable; -import freenet.support.api.Bucket; import freenet.support.api.HTTPRequest; -import freenet.support.io.Closer; /** * {@link Toadlet} implementation that is wrapped around a {@link Page}. @@ -145,31 +144,18 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil * if the toadlet context is closed */ private void handleRequest(FreenetRequest pageRequest) throws IOException, ToadletContextClosedException { - Bucket pageBucket = null; - OutputStream pageBucketOutputStream = null; - Response pageResponse; - try { - pageBucket = pageRequest.getToadletContext().getBucketFactory().makeBucket(-1); - pageBucketOutputStream = pageBucket.getOutputStream(); - pageResponse = page.handleRequest(pageRequest, new Response(pageBucketOutputStream)); - } catch (IOException ioe1) { - Closer.close(pageBucket); - throw ioe1; - } finally { - Closer.close(pageBucketOutputStream); - } - MultiValueTable headers = new MultiValueTable(); - if (pageResponse.getHeaders() != null) { - for (Header header : pageResponse.getHeaders()) { - for (String value : header) { - headers.put(header.getName(), value); + try (AutoCloseableBucket pageBucket = new AutoCloseableBucket(pageRequest.getToadletContext().getBucketFactory().makeBucket(-1)); + OutputStream pageBucketOutputStream = pageBucket.getBucket().getOutputStream()) { + Response pageResponse = page.handleRequest(pageRequest, new Response(pageBucketOutputStream)); + MultiValueTable headers = new MultiValueTable(); + if (pageResponse.getHeaders() != null) { + for (Header header : pageResponse.getHeaders()) { + for (String value : header) { + headers.put(header.getName(), value); + } } } - } - try { - writeReply(pageRequest.getToadletContext(), pageResponse.getStatusCode(), pageResponse.getContentType(), pageResponse.getStatusText(), headers, pageBucket); - } finally { - Closer.close(pageBucket); + writeReply(pageRequest.getToadletContext(), pageResponse.getStatusCode(), pageResponse.getContentType(), pageResponse.getStatusText(), headers, pageBucket.getBucket()); } } diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/AutoCloseableBucket.kt b/src/main/kotlin/net/pterodactylus/sone/utils/AutoCloseableBucket.kt new file mode 100644 index 0000000..58c181f --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/utils/AutoCloseableBucket.kt @@ -0,0 +1,11 @@ +package net.pterodactylus.sone.utils + +import freenet.support.api.Bucket + +class AutoCloseableBucket(val bucket: Bucket) : AutoCloseable { + + override fun close() { + bucket.free() + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/AutoCloseableBucketTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/AutoCloseableBucketTest.kt new file mode 100644 index 0000000..a705844 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/utils/AutoCloseableBucketTest.kt @@ -0,0 +1,26 @@ +package net.pterodactylus.sone.utils + +import freenet.support.api.Bucket +import net.pterodactylus.sone.test.mock +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Test +import org.mockito.Mockito.verify + +class AutoCloseableBucketTest { + + private val bucket = mock() + private val autoCloseableBucket = AutoCloseableBucket(bucket) + + @Test + fun `bucket can be retrieved`() { + assertThat(autoCloseableBucket.bucket, equalTo(bucket)) + } + + @Test + fun `bucket will be free’d when close is called`() { + autoCloseableBucket.close() + verify(bucket).free() + } + +}