Don’t allow changing an image’s title into an empty string.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 15 Sep 2014 19:25:19 +0000 (21:25 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 17 Sep 2014 15:02:34 +0000 (17:02 +0200)
src/main/java/net/pterodactylus/sone/data/ImageImpl.java
src/main/java/net/pterodactylus/sone/web/EditImagePage.java
src/main/java/net/pterodactylus/sone/web/UploadImagePage.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/resources/i18n/sone.en.properties
src/main/resources/templates/emptyImageTitle.html [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/data/ImageImplTest.java [new file with mode: 0644]

index 447bb82..df901b0 100644 (file)
@@ -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
        //
index 9a29c85..178add1 100644 (file)
@@ -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();
                        }
index d938641..b5e8d51 100644 (file)
@@ -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);
index afa324b..f3b77e9 100644 (file)
@@ -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<FreenetRequest>("css/", "/static/css/", "text/css")));
index a7b9ab4..ac9a578 100644 (file)
@@ -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 (file)
index 0000000..59f15e5
--- /dev/null
@@ -0,0 +1,7 @@
+<%include include/head.html>
+
+       <h1><%= Page.EmptyImageTitle.Page.Title|l10n|html></h1>
+
+       <p><%= Page.EmptyImageTitle.Text.EmptyImageTitle|l10n|html></p>
+
+<%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 (file)
index 0000000..5a096a4
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ImageImplTest {
+
+       private final Image image = new ImageImpl();
+
+       @Test(expected = ImageTitleMustNotBeEmpty.class)
+       public void modifierDoesNotAllowTitleDoBeEmpty() {
+               image.modify().setTitle("").update();
+       }
+
+}