Add auto-closeable bucket wrapper and use try-with-resources
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 6 Jan 2017 19:26:04 +0000 (20:26 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 6 Jan 2017 19:26:04 +0000 (20:26 +0100)
src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java
src/main/kotlin/net/pterodactylus/sone/utils/AutoCloseableBucket.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/utils/AutoCloseableBucketTest.kt [new file with mode: 0644]

index 222aa72..17a1bab 100644 (file)
@@ -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<String, String> headers = new MultiValueTable<String, String>();
-               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<String, String> headers = new MultiValueTable<String, String>();
+                       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 (file)
index 0000000..58c181f
--- /dev/null
@@ -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 (file)
index 0000000..a705844
--- /dev/null
@@ -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<Bucket>()
+       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()
+       }
+
+}