From 4b9052a10d998339deec2f9b567d2913a9442f8b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 15 Sep 2014 21:25:19 +0200 Subject: [PATCH] =?utf8?q?Don=E2=80=99t=20allow=20changing=20an=20image?= =?utf8?q?=E2=80=99s=20title=20into=20an=20empty=20string.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/sone/data/ImageImpl.java | 5 +++++ .../net/pterodactylus/sone/web/EditImagePage.java | 2 +- .../net/pterodactylus/sone/web/UploadImagePage.java | 3 +++ .../net/pterodactylus/sone/web/WebInterface.java | 2 ++ src/main/resources/i18n/sone.en.properties | 4 ++++ src/main/resources/templates/emptyImageTitle.html | 7 +++++++ .../net/pterodactylus/sone/data/ImageImplTest.java | 21 +++++++++++++++++++++ 7 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/templates/emptyImageTitle.html create mode 100644 src/test/java/net/pterodactylus/sone/data/ImageImplTest.java diff --git a/src/main/java/net/pterodactylus/sone/data/ImageImpl.java b/src/main/java/net/pterodactylus/sone/data/ImageImpl.java index 447bb82..df901b0 100644 --- a/src/main/java/net/pterodactylus/sone/data/ImageImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/ImageImpl.java @@ -204,6 +204,9 @@ public class ImageImpl implements Image { checkState(!sone.isPresent() || (ImageImpl.this.sone == null) || sone.get().equals(ImageImpl.this.sone), "can not change Sone once set"); checkState(!creationTime.isPresent() || ((ImageImpl.this.creationTime == 0) || (ImageImpl.this.creationTime == creationTime.get())), "can not change creation time once set"); checkState(!key.isPresent() || (ImageImpl.this.key == null) || key.get().equals(ImageImpl.this.key), "can not change key once set"); + if (title.isPresent() && title.get().trim().isEmpty()) { + throw new ImageTitleMustNotBeEmpty(); + } checkState(!width.isPresent() || (ImageImpl.this.width == 0) || width.get().equals(ImageImpl.this.width), "can not change width once set"); checkState(!height.isPresent() || (ImageImpl.this.height == 0) || height.get().equals(ImageImpl.this.height), "can not change height once set"); @@ -234,6 +237,8 @@ public class ImageImpl implements Image { }; } + public static class ImageTitleMustNotBeEmpty extends IllegalStateException { } + // // FINGERPRINTABLE METHODS // diff --git a/src/main/java/net/pterodactylus/sone/web/EditImagePage.java b/src/main/java/net/pterodactylus/sone/web/EditImagePage.java index 9a29c85..178add1 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditImagePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditImagePage.java @@ -71,7 +71,7 @@ public class EditImagePage extends SoneTemplatePage { String title = request.getHttpRequest().getPartAsStringFailsafe("title", 100).trim(); String description = request.getHttpRequest().getPartAsStringFailsafe("description", 1024).trim(); if (title.length() == 0) { - templateContext.set("titleMissing", true); + throw new RedirectException("emptyImageTitle.html"); } image.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); } diff --git a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java index d938641..b5e8d51 100644 --- a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java +++ b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java @@ -31,6 +31,7 @@ import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.ImageImpl.ImageTitleMustNotBeEmpty; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.sone.text.TextFilter; @@ -124,6 +125,8 @@ public class UploadImagePage extends SoneTemplatePage { } catch (IOException ioe1) { logger.log(Level.WARNING, "Could not read uploaded image!", ioe1); return; + } catch (ImageTitleMustNotBeEmpty itmnbe) { + throw new RedirectException("emptyImageTitle.html"); } finally { Closer.close(imageDataInputStream); Closer.flush(uploadedImage); diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index afa324b..f3b77e9 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -641,6 +641,7 @@ public class WebInterface { Template deleteAlbumTemplate = parseTemplate("/templates/deleteAlbum.html"); Template deleteImageTemplate = parseTemplate("/templates/deleteImage.html"); Template noPermissionTemplate = parseTemplate("/templates/noPermission.html"); + Template emptyImageTitleTemplate = parseTemplate("/templates/emptyImageTitle.html"); Template optionsTemplate = parseTemplate("/templates/options.html"); Template rescueTemplate = parseTemplate("/templates/rescue.html"); Template aboutTemplate = parseTemplate("/templates/about.html"); @@ -692,6 +693,7 @@ public class WebInterface { pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue")); pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About")); pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this))); + pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyImageTitle.html", emptyImageTitleTemplate, "Page.EmptyImageTitle.Title", this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css"))); diff --git a/src/main/resources/i18n/sone.en.properties b/src/main/resources/i18n/sone.en.properties index a7b9ab4..ac9a578 100644 --- a/src/main/resources/i18n/sone.en.properties +++ b/src/main/resources/i18n/sone.en.properties @@ -305,6 +305,10 @@ Page.NoPermission.Title=Unauthorized Access - Sone Page.NoPermission.Page.Title=Unauthorized Access Page.NoPermission.Text.NoPermission=You tried to do something that you do not have sufficient authorization for. Please refrain from such actions in the future or we will be forced to take counter-measures! +Page.EmptyImageTitle.Title=Title Must Not Be Empty - Sone +Page.EmptyImageTitle.Page.Title=Title Must Not Be Empty +Page.EmptyImageTitle.Text.EmptyImageTitle=You have to give your image a title. Please go back to the previous page and enter a title. + Page.DismissNotification.Title=Dismiss Notification - Sone Page.WotPluginMissing.Text.WotRequired=Because the Web of Trust is an integral part of Sone, the Web of Trust plugin has to be loaded in order to run Sone. diff --git a/src/main/resources/templates/emptyImageTitle.html b/src/main/resources/templates/emptyImageTitle.html new file mode 100644 index 0000000..59f15e5 --- /dev/null +++ b/src/main/resources/templates/emptyImageTitle.html @@ -0,0 +1,7 @@ +<%include include/head.html> + +

<%= Page.EmptyImageTitle.Page.Title|l10n|html>

+ +

<%= Page.EmptyImageTitle.Text.EmptyImageTitle|l10n|html>

+ +<%include include/tail.html> diff --git a/src/test/java/net/pterodactylus/sone/data/ImageImplTest.java b/src/test/java/net/pterodactylus/sone/data/ImageImplTest.java new file mode 100644 index 0000000..5a096a4 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/data/ImageImplTest.java @@ -0,0 +1,21 @@ +package net.pterodactylus.sone.data; + +import net.pterodactylus.sone.data.ImageImpl.ImageTitleMustNotBeEmpty; + +import org.junit.Test; + +/** + * Unit test for {@link ImageImpl}. + * + * @author David ‘Bombe’ Roden + */ +public class ImageImplTest { + + private final Image image = new ImageImpl(); + + @Test(expected = ImageTitleMustNotBeEmpty.class) + public void modifierDoesNotAllowTitleDoBeEmpty() { + image.modify().setTitle("").update(); + } + +} -- 2.7.4