2 * Sone - WebInterface.java - Copyright © 2010 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.web;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.Reader;
24 import java.io.StringReader;
25 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
34 import java.util.UUID;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
38 import net.pterodactylus.sone.core.Core;
39 import net.pterodactylus.sone.core.CoreListener;
40 import net.pterodactylus.sone.data.Album;
41 import net.pterodactylus.sone.data.Image;
42 import net.pterodactylus.sone.data.Post;
43 import net.pterodactylus.sone.data.PostReply;
44 import net.pterodactylus.sone.data.Reply;
45 import net.pterodactylus.sone.data.Sone;
46 import net.pterodactylus.sone.freenet.L10nFilter;
47 import net.pterodactylus.sone.freenet.wot.Identity;
48 import net.pterodactylus.sone.freenet.wot.Trust;
49 import net.pterodactylus.sone.main.SonePlugin;
50 import net.pterodactylus.sone.notify.ListNotification;
51 import net.pterodactylus.sone.template.AlbumAccessor;
52 import net.pterodactylus.sone.template.CollectionAccessor;
53 import net.pterodactylus.sone.template.CssClassNameFilter;
54 import net.pterodactylus.sone.template.HttpRequestAccessor;
55 import net.pterodactylus.sone.template.IdentityAccessor;
56 import net.pterodactylus.sone.template.ImageLinkFilter;
57 import net.pterodactylus.sone.template.JavascriptFilter;
58 import net.pterodactylus.sone.template.ParserFilter;
59 import net.pterodactylus.sone.template.PostAccessor;
60 import net.pterodactylus.sone.template.ReplyAccessor;
61 import net.pterodactylus.sone.template.ReplyGroupFilter;
62 import net.pterodactylus.sone.template.RequestChangeFilter;
63 import net.pterodactylus.sone.template.SoneAccessor;
64 import net.pterodactylus.sone.template.SubstringFilter;
65 import net.pterodactylus.sone.template.TrustAccessor;
66 import net.pterodactylus.sone.template.UniqueElementFilter;
67 import net.pterodactylus.sone.template.UnknownDateFilter;
68 import net.pterodactylus.sone.text.Part;
69 import net.pterodactylus.sone.text.SonePart;
70 import net.pterodactylus.sone.text.SoneTextParser;
71 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
72 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
73 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
74 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
75 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
76 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
77 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
78 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
79 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
80 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
81 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
82 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
83 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
84 import net.pterodactylus.sone.web.ajax.GetNotificationAjaxPage;
85 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
86 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
87 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
88 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
89 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
90 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
91 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
92 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
93 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
94 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
95 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
96 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
97 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
98 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
99 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
100 import net.pterodactylus.sone.web.page.FreenetRequest;
101 import net.pterodactylus.sone.web.page.PageToadlet;
102 import net.pterodactylus.sone.web.page.PageToadletFactory;
103 import net.pterodactylus.util.cache.Cache;
104 import net.pterodactylus.util.cache.CacheException;
105 import net.pterodactylus.util.cache.CacheItem;
106 import net.pterodactylus.util.cache.DefaultCacheItem;
107 import net.pterodactylus.util.cache.MemoryCache;
108 import net.pterodactylus.util.cache.ValueRetriever;
109 import net.pterodactylus.util.collection.SetBuilder;
110 import net.pterodactylus.util.filter.Filters;
111 import net.pterodactylus.util.logging.Logging;
112 import net.pterodactylus.util.notify.Notification;
113 import net.pterodactylus.util.notify.NotificationManager;
114 import net.pterodactylus.util.notify.TemplateNotification;
115 import net.pterodactylus.util.template.CollectionSortFilter;
116 import net.pterodactylus.util.template.ContainsFilter;
117 import net.pterodactylus.util.template.DateFilter;
118 import net.pterodactylus.util.template.FormatFilter;
119 import net.pterodactylus.util.template.HtmlFilter;
120 import net.pterodactylus.util.template.MatchFilter;
121 import net.pterodactylus.util.template.ModFilter;
122 import net.pterodactylus.util.template.Provider;
123 import net.pterodactylus.util.template.ReflectionAccessor;
124 import net.pterodactylus.util.template.ReplaceFilter;
125 import net.pterodactylus.util.template.StoreFilter;
126 import net.pterodactylus.util.template.Template;
127 import net.pterodactylus.util.template.TemplateContext;
128 import net.pterodactylus.util.template.TemplateContextFactory;
129 import net.pterodactylus.util.template.TemplateException;
130 import net.pterodactylus.util.template.TemplateParser;
131 import net.pterodactylus.util.template.XmlFilter;
132 import net.pterodactylus.util.thread.Ticker;
133 import net.pterodactylus.util.version.Version;
134 import net.pterodactylus.util.web.RedirectPage;
135 import net.pterodactylus.util.web.StaticPage;
136 import net.pterodactylus.util.web.TemplatePage;
137 import freenet.clients.http.SessionManager;
138 import freenet.clients.http.ToadletContainer;
139 import freenet.clients.http.ToadletContext;
140 import freenet.clients.http.SessionManager.Session;
141 import freenet.l10n.BaseL10n;
142 import freenet.support.api.HTTPRequest;
145 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
146 * references to l10n helpers.
148 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
150 public class WebInterface implements CoreListener {
153 private static final Logger logger = Logging.getLogger(WebInterface.class);
155 /** The notification manager. */
156 private final NotificationManager notificationManager = new NotificationManager();
158 /** The Sone plugin. */
159 private final SonePlugin sonePlugin;
161 /** The registered toadlets. */
162 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
164 /** The form password. */
165 private final String formPassword;
167 /** The template context factory. */
168 private final TemplateContextFactory templateContextFactory;
170 /** The Sone text parser. */
171 private final SoneTextParser soneTextParser;
173 /** The “new Sone” notification. */
174 private final ListNotification<Sone> newSoneNotification;
176 /** The “new post” notification. */
177 private final ListNotification<Post> newPostNotification;
179 /** The “new reply” notification. */
180 private final ListNotification<PostReply> newReplyNotification;
182 /** The invisible “local post” notification. */
183 private final ListNotification<Post> localPostNotification;
185 /** The invisible “local reply” notification. */
186 private final ListNotification<PostReply> localReplyNotification;
188 /** The “you have been mentioned” notification. */
189 private final ListNotification<Post> mentionNotification;
191 /** Notifications for sone inserts. */
192 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
194 /** Sone locked notification ticker objects. */
195 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
197 /** The “Sone locked” notification. */
198 private final ListNotification<Sone> lockedSonesNotification;
200 /** The “new version” notification. */
201 private final TemplateNotification newVersionNotification;
203 /** The “inserting images” notification. */
204 private final ListNotification<Image> insertingImagesNotification;
206 /** The “inserted images” notification. */
207 private final ListNotification<Image> insertedImagesNotification;
209 /** The “image insert failed” notification. */
210 private final ListNotification<Image> imageInsertFailedNotification;
213 * Creates a new web interface.
218 @SuppressWarnings("synthetic-access")
219 public WebInterface(SonePlugin sonePlugin) {
220 this.sonePlugin = sonePlugin;
221 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
222 soneTextParser = new SoneTextParser(getCore(), getCore());
224 templateContextFactory = new TemplateContextFactory();
225 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
226 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
227 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
228 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
229 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
230 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
231 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
232 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
233 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
234 templateContextFactory.addFilter("date", new DateFilter());
235 templateContextFactory.addFilter("html", new HtmlFilter());
236 templateContextFactory.addFilter("replace", new ReplaceFilter());
237 templateContextFactory.addFilter("store", new StoreFilter());
238 templateContextFactory.addFilter("l10n", new L10nFilter(getL10n()));
239 templateContextFactory.addFilter("substring", new SubstringFilter());
240 templateContextFactory.addFilter("xml", new XmlFilter());
241 templateContextFactory.addFilter("change", new RequestChangeFilter());
242 templateContextFactory.addFilter("match", new MatchFilter());
243 templateContextFactory.addFilter("css", new CssClassNameFilter());
244 templateContextFactory.addFilter("js", new JavascriptFilter());
245 templateContextFactory.addFilter("parse", new ParserFilter(getCore(), templateContextFactory, soneTextParser));
246 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
247 templateContextFactory.addFilter("format", new FormatFilter());
248 templateContextFactory.addFilter("sort", new CollectionSortFilter());
249 templateContextFactory.addFilter("image-link", new ImageLinkFilter(templateContextFactory));
250 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
251 templateContextFactory.addFilter("in", new ContainsFilter());
252 templateContextFactory.addFilter("unique", new UniqueElementFilter());
253 templateContextFactory.addFilter("mod", new ModFilter());
254 templateContextFactory.addProvider(Provider.TEMPLATE_CONTEXT_PROVIDER);
255 templateContextFactory.addProvider(new ClassPathTemplateProvider());
256 templateContextFactory.addTemplateObject("webInterface", this);
257 templateContextFactory.addTemplateObject("formPassword", formPassword);
259 /* create notifications. */
260 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
261 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
263 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
264 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
266 Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
267 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
269 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
270 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
272 Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
273 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
275 Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
276 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
278 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
279 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
281 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
282 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
284 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
285 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
287 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
288 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
290 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
291 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
299 * Returns the Sone core used by the Sone plugin.
301 * @return The Sone core
303 public Core getCore() {
304 return sonePlugin.core();
308 * Returns the template context factory of the web interface.
310 * @return The template context factory
312 public TemplateContextFactory getTemplateContextFactory() {
313 return templateContextFactory;
317 * Returns the current session, creating a new session if there is no
320 * @param toadletContenxt
321 * The toadlet context
322 * @return The current session, or {@code null} if there is no current
325 public Session getCurrentSession(ToadletContext toadletContenxt) {
326 return getCurrentSession(toadletContenxt, true);
330 * Returns the current session, creating a new session if there is no
331 * current session and {@code create} is {@code true}.
333 * @param toadletContenxt
334 * The toadlet context
336 * {@code true} to create a new session if there is no current
337 * session, {@code false} otherwise
338 * @return The current session, or {@code null} if there is no current
341 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
342 Session session = getSessionManager().useSession(toadletContenxt);
343 if (create && (session == null)) {
344 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
350 * Returns the currently logged in Sone.
352 * @param toadletContext
353 * The toadlet context
354 * @return The currently logged in Sone, or {@code null} if no Sone is
355 * currently logged in
357 public Sone getCurrentSone(ToadletContext toadletContext) {
358 return getCurrentSone(toadletContext, true);
362 * Returns the currently logged in Sone.
364 * @param toadletContext
365 * The toadlet context
367 * {@code true} to create a new session if no session exists,
368 * {@code false} to not create a new session
369 * @return The currently logged in Sone, or {@code null} if no Sone is
370 * currently logged in
372 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
373 Set<Sone> localSones = getCore().getLocalSones();
374 if (localSones.size() == 1) {
375 return localSones.iterator().next();
377 return getCurrentSone(getCurrentSession(toadletContext, create));
381 * Returns the currently logged in Sone.
385 * @return The currently logged in Sone, or {@code null} if no Sone is
386 * currently logged in
388 public Sone getCurrentSone(Session session) {
389 if (session == null) {
392 String soneId = (String) session.getAttribute("Sone.CurrentSone");
393 if (soneId == null) {
396 return getCore().getLocalSone(soneId, false);
400 * Sets the currently logged in Sone.
402 * @param toadletContext
403 * The toadlet context
405 * The Sone to set as currently logged in
407 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
408 Session session = getCurrentSession(toadletContext);
410 session.removeAttribute("Sone.CurrentSone");
412 session.setAttribute("Sone.CurrentSone", sone.getId());
417 * Returns the notification manager.
419 * @return The notification manager
421 public NotificationManager getNotifications() {
422 return notificationManager;
426 * Returns the l10n helper of the node.
428 * @return The node’s l10n helper
430 public BaseL10n getL10n() {
431 return sonePlugin.l10n().getBase();
435 * Returns the session manager of the node.
437 * @return The node’s session manager
439 public SessionManager getSessionManager() {
440 return sonePlugin.pluginRespirator().getSessionManager("Sone");
444 * Returns the node’s form password.
446 * @return The form password
448 public String getFormPassword() {
453 * Returns the posts that have been announced as new in the
454 * {@link #newPostNotification}.
456 * @return The new posts
458 public Set<Post> getNewPosts() {
459 return new SetBuilder<Post>().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).get();
463 * Returns the replies that have been announced as new in the
464 * {@link #newReplyNotification}.
466 * @return The new replies
468 public Set<PostReply> getNewReplies() {
469 return new SetBuilder<PostReply>().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).get();
473 * Sets whether the current start of the plugin is the first start. It is
474 * considered a first start if the configuration file does not exist.
477 * {@code true} if no configuration file existed when Sone was
478 * loaded, {@code false} otherwise
480 public void setFirstStart(boolean firstStart) {
482 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
483 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
484 notificationManager.addNotification(firstStartNotification);
489 * Sets whether Sone was started with a fresh configuration file.
492 * {@code true} if Sone was started with a fresh configuration,
493 * {@code false} if the existing configuration could be read
495 public void setNewConfig(boolean newConfig) {
496 if (newConfig && !hasFirstStartNotification()) {
497 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
498 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
499 notificationManager.addNotification(configNotReadNotification);
508 * Returns whether the first start notification is currently displayed.
510 * @return {@code true} if the first-start notification is currently
511 * displayed, {@code false} otherwise
513 private boolean hasFirstStartNotification() {
514 return notificationManager.getNotification("first-start-notification") != null;
522 * Starts the web interface and registers all toadlets.
524 public void start() {
527 /* notification templates. */
528 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
530 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
531 notificationManager.addNotification(startupNotification);
533 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
537 startupNotification.dismiss();
539 }, "Sone Startup Notification Remover");
541 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
542 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
543 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
546 @SuppressWarnings("synthetic-access")
548 if (getCore().getIdentityManager().isConnected()) {
549 wotMissingNotification.dismiss();
551 notificationManager.addNotification(wotMissingNotification);
553 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
556 }, "Sone WoT Connector Checker");
560 * Stops the web interface and unregisters all toadlets.
563 unregisterToadlets();
564 Ticker.getInstance().stop();
572 * Register all toadlets.
574 private void registerToadlets() {
575 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
576 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
577 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
578 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
579 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
580 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
581 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
582 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
583 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
584 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
585 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
586 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
587 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
588 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
589 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
590 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
591 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
592 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
593 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
594 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
595 Template deleteImageTemplate = TemplateParser.parse(createReader("/templates/deleteImage.html"));
596 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
597 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
598 Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html"));
599 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
600 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
601 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
602 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
603 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
605 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
606 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
607 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
608 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
609 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
610 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
611 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
612 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
613 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
614 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
615 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
616 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
617 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
618 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
619 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
620 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
621 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
622 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
623 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
624 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
625 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
626 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
627 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
628 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
629 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
630 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
631 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
632 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
633 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
634 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
635 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
639 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationAjaxPage(this)));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this)));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
684 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
685 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
686 for (PageToadlet toadlet : pageToadlets) {
687 String menuName = toadlet.getMenuName();
688 if (menuName != null) {
689 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
691 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
697 * Unregisters all toadlets.
699 private void unregisterToadlets() {
700 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
701 for (PageToadlet pageToadlet : pageToadlets) {
702 toadletContainer.unregister(pageToadlet);
704 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
708 * Creates a {@link Reader} from the {@link InputStream} for the resource
709 * with the given name.
711 * @param resourceName
712 * The name of the resource
713 * @return A {@link Reader} for the resource
715 private Reader createReader(String resourceName) {
717 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
718 } catch (UnsupportedEncodingException uee1) {
724 * Returns all {@link Core#isLocalSone(Sone) local Sone}s that are
725 * referenced by {@link SonePart}s in the given text (after parsing it using
726 * {@link SoneTextParser}).
730 * @return All mentioned local Sones
732 private Set<Sone> getMentionedSones(String text) {
733 /* we need no context to find mentioned Sones. */
734 Set<Sone> mentionedSones = new HashSet<Sone>();
736 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
737 if (part instanceof SonePart) {
738 mentionedSones.add(((SonePart) part).getSone());
741 } catch (IOException ioe1) {
742 logger.log(Level.WARNING, "Could not parse post text: " + text, ioe1);
744 return Filters.filteredSet(mentionedSones, Sone.LOCAL_SONE_FILTER);
748 * Returns the Sone insert notification for the given Sone. If no
749 * notification for the given Sone exists, a new notification is created and
753 * The Sone to get the insert notification for
754 * @return The Sone insert notification
756 private TemplateNotification getSoneInsertNotification(Sone sone) {
757 synchronized (soneInsertNotifications) {
758 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
759 if (templateNotification == null) {
760 templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
761 templateNotification.set("insertSone", sone);
762 soneInsertNotifications.put(sone, templateNotification);
764 return templateNotification;
769 // CORELISTENER METHODS
776 public void newSoneFound(Sone sone) {
777 newSoneNotification.add(sone);
778 if (!hasFirstStartNotification()) {
779 notificationManager.addNotification(newSoneNotification);
787 public void newPostFound(Post post) {
788 boolean isLocal = getCore().isLocalSone(post.getSone());
790 localPostNotification.add(post);
792 newPostNotification.add(post);
794 if (!hasFirstStartNotification()) {
795 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
796 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
797 mentionNotification.add(post);
798 notificationManager.addNotification(mentionNotification);
801 getCore().markPostKnown(post);
809 public void newReplyFound(PostReply reply) {
810 boolean isLocal = getCore().isLocalSone(reply.getSone());
812 localReplyNotification.add(reply);
814 newReplyNotification.add(reply);
816 if (!hasFirstStartNotification()) {
817 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
818 if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null) && (reply.getTime() <= System.currentTimeMillis())) {
819 mentionNotification.add(reply.getPost());
820 notificationManager.addNotification(mentionNotification);
823 getCore().markReplyKnown(reply);
831 public void markSoneKnown(Sone sone) {
832 newSoneNotification.remove(sone);
839 public void markPostKnown(Post post) {
840 newPostNotification.remove(post);
841 localPostNotification.remove(post);
842 mentionNotification.remove(post);
849 public void markReplyKnown(PostReply reply) {
850 newReplyNotification.remove(reply);
851 localReplyNotification.remove(reply);
852 mentionNotification.remove(reply.getPost());
859 public void soneRemoved(Sone sone) {
860 newSoneNotification.remove(sone);
867 public void postRemoved(Post post) {
868 newPostNotification.remove(post);
869 localPostNotification.remove(post);
876 public void replyRemoved(PostReply reply) {
877 newReplyNotification.remove(reply);
878 localReplyNotification.remove(reply);
885 public void soneLocked(final Sone sone) {
886 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
889 @SuppressWarnings("synthetic-access")
891 lockedSonesNotification.add(sone);
892 lockedSonesTickerObjects.remove(sone);
893 notificationManager.addNotification(lockedSonesNotification);
895 }, "Sone Locked Notification");
896 lockedSonesTickerObjects.put(sone, tickerObject);
903 public void soneUnlocked(Sone sone) {
904 lockedSonesNotification.remove(sone);
905 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
912 public void soneInserting(Sone sone) {
913 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
914 soneInsertNotification.set("soneStatus", "inserting");
915 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
916 notificationManager.addNotification(soneInsertNotification);
924 public void soneInserted(Sone sone, long insertDuration) {
925 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
926 soneInsertNotification.set("soneStatus", "inserted");
927 soneInsertNotification.set("insertDuration", insertDuration / 1000);
928 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
929 notificationManager.addNotification(soneInsertNotification);
937 public void soneInsertAborted(Sone sone, Throwable cause) {
938 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
939 soneInsertNotification.set("soneStatus", "insert-aborted");
940 soneInsertNotification.set("insert-error", cause);
941 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
942 notificationManager.addNotification(soneInsertNotification);
950 public void updateFound(Version version, long releaseTime, long latestEdition) {
951 newVersionNotification.getTemplateContext().set("latestVersion", version);
952 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
953 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
954 notificationManager.addNotification(newVersionNotification);
961 public void imageInsertStarted(Image image) {
962 insertingImagesNotification.add(image);
963 notificationManager.addNotification(insertingImagesNotification);
970 public void imageInsertAborted(Image image) {
971 insertingImagesNotification.remove(image);
978 public void imageInsertFinished(Image image) {
979 insertingImagesNotification.remove(image);
980 insertedImagesNotification.add(image);
981 notificationManager.addNotification(insertedImagesNotification);
988 public void imageInsertFailed(Image image, Throwable cause) {
989 insertingImagesNotification.remove(image);
990 imageInsertFailedNotification.add(image);
991 notificationManager.addNotification(imageInsertFailedNotification);
995 * Template provider implementation that uses
996 * {@link WebInterface#createReader(String)} to load templates for
999 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1001 private class ClassPathTemplateProvider implements Provider {
1003 /** Cache for templates. */
1004 private final Cache<String, Template> templateCache = new MemoryCache<String, Template>(new ValueRetriever<String, Template>() {
1007 @SuppressWarnings("synthetic-access")
1008 public CacheItem<Template> retrieve(String key) throws CacheException {
1009 Template template = findTemplate(key);
1010 if (template != null) {
1011 return new DefaultCacheItem<Template>(template);
1021 @SuppressWarnings("synthetic-access")
1022 public Template getTemplate(TemplateContext templateContext, String templateName) {
1024 return templateCache.get(templateName);
1025 } catch (CacheException ce1) {
1026 logger.log(Level.WARNING, "Could not get template for " + templateName + "!", ce1);
1032 * Locates a template in the class path.
1034 * @param templateName
1035 * The name of the template to load
1036 * @return The loaded template, or {@code null} if no template could be
1039 @SuppressWarnings("synthetic-access")
1040 private Template findTemplate(String templateName) {
1041 Reader templateReader = createReader("/templates/" + templateName);
1042 if (templateReader == null) {
1045 Template template = null;
1047 template = TemplateParser.parse(templateReader);
1048 } catch (TemplateException te1) {
1049 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);