2 * FreenetSone - CreateSonePage.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.util.logging.Level;
21 import java.util.logging.Logger;
23 import net.pterodactylus.sone.core.SoneException;
24 import net.pterodactylus.sone.core.SoneException.Type;
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.web.page.Page.Request.Method;
27 import net.pterodactylus.util.logging.Logging;
28 import net.pterodactylus.util.template.Template;
29 import freenet.clients.http.ToadletContext;
32 * The “create Sone” page lets the user create a new Sone.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class CreateSonePage extends SoneTemplatePage {
39 private static final Logger logger = Logging.getLogger(CreateSonePage.class);
42 * Creates a new “create Sone” page.
45 * The template to render
47 * The Sone web interface
49 public CreateSonePage(Template template, WebInterface webInterface) {
50 super("createSone.html", template, "Page.CreateSone.Title", webInterface);
54 // TEMPLATEPAGE METHODS
61 protected void processTemplate(Request request, Template template) throws RedirectException {
62 super.processTemplate(request, template);
64 String requestUri = null;
65 String insertUri = null;
66 if (request.getMethod() == Method.POST) {
67 name = request.getHttpRequest().getPartAsStringFailsafe("name", 100);
68 if (request.getHttpRequest().getParam("create-from-uri").length() > 0) {
69 requestUri = request.getHttpRequest().getPartAsStringFailsafe("request-uri", 256);
70 insertUri = request.getHttpRequest().getPartAsStringFailsafe("insert-uri", 256);
74 Sone sone = webInterface.core().createSone(name, requestUri, insertUri);
76 /* log in the new Sone. */
77 setCurrentSone(request.getToadletContext(), sone);
78 throw new RedirectException("index.html");
79 } catch (SoneException se1) {
80 logger.log(Level.FINE, "Could not create Sone “%s” at (“%s”, “%s”), %s!", new Object[] { name, requestUri, insertUri, se1.getType() });
81 if (se1.getType() == Type.INVALID_SONE_NAME) {
82 template.set("errorName", true);
83 } else if (se1.getType() == Type.INVALID_URI) {
84 template.set("errorUri", true);
88 template.set("name", name);
89 template.set("requestUri", requestUri);
90 template.set("insertUri", insertUri);
97 public boolean isEnabled(ToadletContext toadletContext) {
98 return getCurrentSone(toadletContext) == null;