X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FWebInterface.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FWebInterface.java;h=4da21779739268028f8272561816a709a8559099;hp=fbba8e0e0e2fce272963927d3e789c40cef71cf6;hb=76ed638264e531a26e35647d13702db865a52321;hpb=cb0161bb413434c393ae09d46001dba66b876875 diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index fbba8e0..4da2177 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -1,5 +1,5 @@ /* - * Sone - WebInterface.java - Copyright © 2010–2013 David Roden + * Sone - WebInterface.java - Copyright © 2010–2015 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,11 +21,7 @@ import static java.util.logging.Logger.getLogger; import static net.pterodactylus.util.template.TemplateParser.parse; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; import java.io.StringReader; -import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -72,6 +68,7 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.L10nFilter; import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.Trust; +import net.pterodactylus.sone.main.Loaders; import net.pterodactylus.sone.main.ReparseFilter; import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.sone.notify.ListNotification; @@ -129,11 +126,9 @@ import net.pterodactylus.sone.web.ajax.UntrustAjaxPage; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.sone.web.page.PageToadlet; import net.pterodactylus.sone.web.page.PageToadletFactory; -import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.notify.Notification; import net.pterodactylus.util.notify.NotificationManager; import net.pterodactylus.util.notify.TemplateNotification; -import net.pterodactylus.util.template.ClassPathTemplateProvider; import net.pterodactylus.util.template.CollectionSortFilter; import net.pterodactylus.util.template.ContainsFilter; import net.pterodactylus.util.template.DateFilter; @@ -150,7 +145,6 @@ import net.pterodactylus.util.template.TemplateContextFactory; import net.pterodactylus.util.template.TemplateProvider; import net.pterodactylus.util.template.XmlFilter; import net.pterodactylus.util.web.RedirectPage; -import net.pterodactylus.util.web.StaticPage; import net.pterodactylus.util.web.TemplatePage; import com.google.common.collect.Collections2; @@ -176,6 +170,9 @@ public class WebInterface { /** The logger. */ private static final Logger logger = getLogger(WebInterface.class.getName()); + /** The loaders for templates, pages, and classpath providers. */ + private final Loaders loaders; + /** The notification manager. */ private final NotificationManager notificationManager = new NotificationManager(); @@ -246,8 +243,9 @@ public class WebInterface { * The Sone plugin */ @Inject - public WebInterface(SonePlugin sonePlugin) { + public WebInterface(SonePlugin sonePlugin, Loaders loaders) { this.sonePlugin = sonePlugin; + this.loaders = loaders; formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword(); soneTextParser = new SoneTextParser(getCore(), getCore()); @@ -286,60 +284,45 @@ public class WebInterface { templateContextFactory.addFilter("mod", new ModFilter()); templateContextFactory.addFilter("paginate", new PaginationFilter()); templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER); - templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/")); + templateContextFactory.addProvider(loaders.getTemplateProvider()); templateContextFactory.addTemplateObject("webInterface", this); templateContextFactory.addTemplateObject("formPassword", formPassword); /* create notifications. */ - Template newSoneNotificationTemplate = parseTemplate("/templates/notify/newSoneNotification.html"); + Template newSoneNotificationTemplate = loaders.loadTemplate("/templates/notify/newSoneNotification.html"); newSoneNotification = new ListNotification("new-sone-notification", "sones", newSoneNotificationTemplate, false); - Template newPostNotificationTemplate = parseTemplate("/templates/notify/newPostNotification.html"); + Template newPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html"); newPostNotification = new ListNotification("new-post-notification", "posts", newPostNotificationTemplate, false); - Template localPostNotificationTemplate = parseTemplate("/templates/notify/newPostNotification.html"); + Template localPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html"); localPostNotification = new ListNotification("local-post-notification", "posts", localPostNotificationTemplate, false); - Template newReplyNotificationTemplate = parseTemplate("/templates/notify/newReplyNotification.html"); + Template newReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html"); newReplyNotification = new ListNotification("new-reply-notification", "replies", newReplyNotificationTemplate, false); - Template localReplyNotificationTemplate = parseTemplate("/templates/notify/newReplyNotification.html"); + Template localReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html"); localReplyNotification = new ListNotification("local-reply-notification", "replies", localReplyNotificationTemplate, false); - Template mentionNotificationTemplate = parseTemplate("/templates/notify/mentionNotification.html"); + Template mentionNotificationTemplate = loaders.loadTemplate("/templates/notify/mentionNotification.html"); mentionNotification = new ListNotification("mention-notification", "posts", mentionNotificationTemplate, false); - Template lockedSonesTemplate = parseTemplate("/templates/notify/lockedSonesNotification.html"); + Template lockedSonesTemplate = loaders.loadTemplate("/templates/notify/lockedSonesNotification.html"); lockedSonesNotification = new ListNotification("sones-locked-notification", "sones", lockedSonesTemplate); - Template newVersionTemplate = parseTemplate("/templates/notify/newVersionNotification.html"); + Template newVersionTemplate = loaders.loadTemplate("/templates/notify/newVersionNotification.html"); newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate); - Template insertingImagesTemplate = parseTemplate("/templates/notify/inserting-images-notification.html"); + Template insertingImagesTemplate = loaders.loadTemplate("/templates/notify/inserting-images-notification.html"); insertingImagesNotification = new ListNotification("inserting-images-notification", "images", insertingImagesTemplate); - Template insertedImagesTemplate = parseTemplate("/templates/notify/inserted-images-notification.html"); + Template insertedImagesTemplate = loaders.loadTemplate("/templates/notify/inserted-images-notification.html"); insertedImagesNotification = new ListNotification("inserted-images-notification", "images", insertedImagesTemplate); - Template imageInsertFailedTemplate = parseTemplate("/templates/notify/image-insert-failed-notification.html"); + Template imageInsertFailedTemplate = loaders.loadTemplate("/templates/notify/image-insert-failed-notification.html"); imageInsertFailedNotification = new ListNotification("image-insert-failed-notification", "images", imageInsertFailedTemplate); } - private Template parseTemplate(String resourceName) { - InputStream templateInputStream = null; - Reader reader = null; - try { - templateInputStream = getClass().getResourceAsStream(resourceName); - reader = new InputStreamReader(templateInputStream, "UTF-8"); - return parse(reader); - } catch (UnsupportedEncodingException uee1) { - throw new RuntimeException("UTF-8 not supported."); - } finally { - Closer.close(reader); - Closer.close(templateInputStream); - } - } - // // ACCESSORS // @@ -528,7 +511,7 @@ public class WebInterface { */ public void setFirstStart(boolean firstStart) { if (firstStart) { - Template firstStartNotificationTemplate = parseTemplate("/templates/notify/firstStartNotification.html"); + Template firstStartNotificationTemplate = loaders.loadTemplate("/templates/notify/firstStartNotification.html"); Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate); notificationManager.addNotification(firstStartNotification); } @@ -543,7 +526,7 @@ public class WebInterface { */ public void setNewConfig(boolean newConfig) { if (newConfig && !hasFirstStartNotification()) { - Template configNotReadNotificationTemplate = parseTemplate("/templates/notify/configNotReadNotification.html"); + Template configNotReadNotificationTemplate = loaders.loadTemplate("/templates/notify/configNotReadNotification.html"); Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate); notificationManager.addNotification(configNotReadNotification); } @@ -574,7 +557,7 @@ public class WebInterface { registerToadlets(); /* notification templates. */ - Template startupNotificationTemplate = parseTemplate("/templates/notify/startupNotification.html"); + Template startupNotificationTemplate = loaders.loadTemplate("/templates/notify/startupNotification.html"); final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate); notificationManager.addNotification(startupNotification); @@ -587,7 +570,7 @@ public class WebInterface { } }, 2, TimeUnit.MINUTES); - Template wotMissingNotificationTemplate = parseTemplate("/templates/notify/wotMissingNotification.html"); + Template wotMissingNotificationTemplate = loaders.loadTemplate("/templates/notify/wotMissingNotification.html"); final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate); ticker.scheduleAtFixedRate(new Runnable() { @@ -621,37 +604,37 @@ public class WebInterface { */ private void registerToadlets() { Template emptyTemplate = parse(new StringReader("")); - Template loginTemplate = parseTemplate("/templates/login.html"); - Template indexTemplate = parseTemplate("/templates/index.html"); - Template newTemplate = parseTemplate("/templates/new.html"); - Template knownSonesTemplate = parseTemplate("/templates/knownSones.html"); - Template createSoneTemplate = parseTemplate("/templates/createSone.html"); - Template createPostTemplate = parseTemplate("/templates/createPost.html"); - Template createReplyTemplate = parseTemplate("/templates/createReply.html"); - Template bookmarksTemplate = parseTemplate("/templates/bookmarks.html"); - Template searchTemplate = parseTemplate("/templates/search.html"); - Template editProfileTemplate = parseTemplate("/templates/editProfile.html"); - Template editProfileFieldTemplate = parseTemplate("/templates/editProfileField.html"); - Template deleteProfileFieldTemplate = parseTemplate("/templates/deleteProfileField.html"); - Template viewSoneTemplate = parseTemplate("/templates/viewSone.html"); - Template viewPostTemplate = parseTemplate("/templates/viewPost.html"); - Template deletePostTemplate = parseTemplate("/templates/deletePost.html"); - Template deleteReplyTemplate = parseTemplate("/templates/deleteReply.html"); - Template deleteSoneTemplate = parseTemplate("/templates/deleteSone.html"); - Template imageBrowserTemplate = parseTemplate("/templates/imageBrowser.html"); - Template createAlbumTemplate = parseTemplate("/templates/createAlbum.html"); - 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 emptyAlbumTitleTemplate = parseTemplate("/templates/emptyAlbumTitle.html"); - Template optionsTemplate = parseTemplate("/templates/options.html"); - Template rescueTemplate = parseTemplate("/templates/rescue.html"); - Template aboutTemplate = parseTemplate("/templates/about.html"); - Template invalidTemplate = parseTemplate("/templates/invalid.html"); - Template postTemplate = parseTemplate("/templates/include/viewPost.html"); - Template replyTemplate = parseTemplate("/templates/include/viewReply.html"); - Template openSearchTemplate = parseTemplate("/templates/xml/OpenSearch.xml"); + Template loginTemplate = loaders.loadTemplate("/templates/login.html"); + Template indexTemplate = loaders.loadTemplate("/templates/index.html"); + Template newTemplate = loaders.loadTemplate("/templates/new.html"); + Template knownSonesTemplate = loaders.loadTemplate("/templates/knownSones.html"); + Template createSoneTemplate = loaders.loadTemplate("/templates/createSone.html"); + Template createPostTemplate = loaders.loadTemplate("/templates/createPost.html"); + Template createReplyTemplate = loaders.loadTemplate("/templates/createReply.html"); + Template bookmarksTemplate = loaders.loadTemplate("/templates/bookmarks.html"); + Template searchTemplate = loaders.loadTemplate("/templates/search.html"); + Template editProfileTemplate = loaders.loadTemplate("/templates/editProfile.html"); + Template editProfileFieldTemplate = loaders.loadTemplate("/templates/editProfileField.html"); + Template deleteProfileFieldTemplate = loaders.loadTemplate("/templates/deleteProfileField.html"); + Template viewSoneTemplate = loaders.loadTemplate("/templates/viewSone.html"); + Template viewPostTemplate = loaders.loadTemplate("/templates/viewPost.html"); + Template deletePostTemplate = loaders.loadTemplate("/templates/deletePost.html"); + Template deleteReplyTemplate = loaders.loadTemplate("/templates/deleteReply.html"); + Template deleteSoneTemplate = loaders.loadTemplate("/templates/deleteSone.html"); + Template imageBrowserTemplate = loaders.loadTemplate("/templates/imageBrowser.html"); + Template createAlbumTemplate = loaders.loadTemplate("/templates/createAlbum.html"); + Template deleteAlbumTemplate = loaders.loadTemplate("/templates/deleteAlbum.html"); + Template deleteImageTemplate = loaders.loadTemplate("/templates/deleteImage.html"); + Template noPermissionTemplate = loaders.loadTemplate("/templates/noPermission.html"); + Template emptyImageTitleTemplate = loaders.loadTemplate("/templates/emptyImageTitle.html"); + Template emptyAlbumTitleTemplate = loaders.loadTemplate("/templates/emptyAlbumTitle.html"); + Template optionsTemplate = loaders.loadTemplate("/templates/options.html"); + Template rescueTemplate = loaders.loadTemplate("/templates/rescue.html"); + Template aboutTemplate = loaders.loadTemplate("/templates/about.html"); + Template invalidTemplate = loaders.loadTemplate("/templates/invalid.html"); + Template postTemplate = loaders.loadTemplate("/templates/include/viewPost.html"); + Template replyTemplate = loaders.loadTemplate("/templates/include/viewReply.html"); + Template openSearchTemplate = loaders.loadTemplate("/templates/xml/OpenSearch.xml"); PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/"); pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage("", "index.html"))); @@ -700,9 +683,9 @@ public class WebInterface { pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyAlbumTitle.html", emptyAlbumTitleTemplate, "Page.EmptyAlbumTitle.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"))); - pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript"))); - pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png"))); + pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.loadStaticPage("css/", "/static/css/", "text/css"))); + pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.loadStaticPage("javascript/", "/static/javascript/", "text/javascript"))); + pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.loadStaticPage("images/", "/static/images/", "image/png"))); pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate))); pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this))); @@ -795,7 +778,7 @@ public class WebInterface { synchronized (soneInsertNotifications) { TemplateNotification templateNotification = soneInsertNotifications.get(sone); if (templateNotification == null) { - templateNotification = new TemplateNotification(parseTemplate("/templates/notify/soneInsertNotification.html")); + templateNotification = new TemplateNotification(loaders.loadTemplate("/templates/notify/soneInsertNotification.html")); templateNotification.set("insertSone", sone); soneInsertNotifications.put(sone, templateNotification); } @@ -901,72 +884,46 @@ public class WebInterface { newSoneNotification.remove(markSoneKnownEvent.sone()); } - /** - * Notifies the web interface that a {@link Post} was marked as known. - * - * @param markPostKnownEvent - * The event - */ @Subscribe public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) { - newPostNotification.remove(markPostKnownEvent.post()); - localPostNotification.remove(markPostKnownEvent.post()); - if (!localSoneMentionedInNewPostOrReply(markPostKnownEvent.post())) { - mentionNotification.remove(markPostKnownEvent.post()); - } + removePost(markPostKnownEvent.post()); } - /** - * Notifies the web interface that a {@link PostReply} was marked as known. - * - * @param markPostReplyKnownEvent - * The event - */ @Subscribe public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) { - PostReply postReply = markPostReplyKnownEvent.postReply(); - newReplyNotification.remove(postReply); - localReplyNotification.remove(postReply); - if (postReply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(postReply.getPost().get())) { - mentionNotification.remove(postReply.getPost().get()); - } + removeReply(markPostReplyKnownEvent.postReply()); } - /** - * Notifies the web interface that a {@link Sone} was removed. - * - * @param soneRemovedEvent - * The event - */ @Subscribe public void soneRemoved(SoneRemovedEvent soneRemovedEvent) { newSoneNotification.remove(soneRemovedEvent.sone()); + for (Post post : soneRemovedEvent.sone().getPosts()) { + removePost(post); + } + for (PostReply postReply : soneRemovedEvent.sone().getReplies()) { + removeReply(postReply); + } } - /** - * Notifies the web interface that a {@link Post} was removed. - * - * @param postRemovedEvent - * The event - */ @Subscribe public void postRemoved(PostRemovedEvent postRemovedEvent) { - newPostNotification.remove(postRemovedEvent.post()); - localPostNotification.remove(postRemovedEvent.post()); - if (!localSoneMentionedInNewPostOrReply(postRemovedEvent.post())) { - mentionNotification.remove(postRemovedEvent.post()); + removePost(postRemovedEvent.post()); + } + + private void removePost(Post post) { + newPostNotification.remove(post); + localPostNotification.remove(post); + if (!localSoneMentionedInNewPostOrReply(post)) { + mentionNotification.remove(post); } } - /** - * Notifies the web interface that a {@link PostReply} was removed. - * - * @param postReplyRemovedEvent - * The event - */ @Subscribe public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) { - PostReply reply = postReplyRemovedEvent.postReply(); + removeReply(postReplyRemovedEvent.postReply()); + } + + private void removeReply(PostReply reply) { newReplyNotification.remove(reply); localReplyNotification.remove(reply); if (reply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(reply.getPost().get())) {