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 documentName = null;
65 String requestUri = null;
66 String insertUri = null;
67 if (request.getMethod() == Method.POST) {
68 name = request.getHttpRequest().getPartAsStringFailsafe("name", 100);
69 documentName = request.getHttpRequest().getPartAsStringFailsafe("document-name", 96);
70 if (documentName.trim().length() == 0) {
71 documentName = "Sone-" + name;
73 if (request.getHttpRequest().getParam("create-from-uri").length() > 0) {
74 requestUri = request.getHttpRequest().getPartAsStringFailsafe("request-uri", 256);
75 insertUri = request.getHttpRequest().getPartAsStringFailsafe("insert-uri", 256);
79 Sone sone = webInterface.core().createSone(name, documentName, requestUri, insertUri);
81 /* log in the new Sone. */
82 setCurrentSone(request.getToadletContext(), sone);
83 throw new RedirectException("index.html");
84 } catch (SoneException se1) {
85 logger.log(Level.FINE, "Could not create Sone “%s” at (“%s”, “%s”), %s!", new Object[] { name, requestUri, insertUri, se1.getType() });
86 if (se1.getType() == Type.INVALID_SONE_NAME) {
87 template.set("errorName", true);
88 } else if (se1.getType() == Type.INVALID_URI) {
89 template.set("errorUri", true);
93 template.set("name", name);
94 template.set("requestUri", requestUri);
95 template.set("insertUri", insertUri);
102 public boolean isEnabled(ToadletContext toadletContext) {
103 return getCurrentSone(toadletContext) == null;