Add Sone creation page.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / CreateSonePage.java
1 /*
2  * FreenetSone - CreateSonePage.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import java.util.logging.Level;
21 import java.util.logging.Logger;
22
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;
30
31 /**
32  * The “create Sone” page lets the user create a new Sone.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class CreateSonePage extends SoneTemplatePage {
37
38         /** The logger. */
39         private static final Logger logger = Logging.getLogger(CreateSonePage.class);
40
41         /**
42          * Creates a new “create Sone” page.
43          *
44          * @param template
45          *            The template to render
46          * @param webInterface
47          *            The Sone web interface
48          */
49         public CreateSonePage(Template template, WebInterface webInterface) {
50                 super("createSone.html", template, "Page.CreateSone.Title", webInterface);
51         }
52
53         //
54         // TEMPLATEPAGE METHODS
55         //
56
57         /**
58          * {@inheritDoc}
59          */
60         @Override
61         protected void processTemplate(Request request, Template template) throws RedirectException {
62                 String name = "";
63                 String requestUri = null;
64                 String insertUri = null;
65                 if (request.getMethod() == Method.POST) {
66                         name = request.getHttpRequest().getPartAsStringFailsafe("name", 100);
67                         if (request.getHttpRequest().getParam("create-from-uri").length() > 0) {
68                                 requestUri = request.getHttpRequest().getPartAsStringFailsafe("request-uri", 256);
69                                 insertUri = request.getHttpRequest().getPartAsStringFailsafe("insert-uri", 256);
70                         }
71                         try {
72                                 /* create Sone. */
73                                 Sone sone = webInterface.core().createSone(name, requestUri, insertUri);
74
75                                 /* log in the new Sone. */
76                                 setCurrentSone(request.getToadletContext(), sone);
77                                 throw new RedirectException("index.html");
78                         } catch (SoneException se1) {
79                                 logger.log(Level.FINE, "Could not create Sone “%s” at (“%s”, “%s”), %s!", new Object[] { name, requestUri, insertUri, se1.getType() });
80                                 if (se1.getType() == Type.INVALID_SONE_NAME) {
81                                         template.set("errorName", true);
82                                 } else if (se1.getType() == Type.INVALID_URI) {
83                                         template.set("errorUri", true);
84                                 }
85                         }
86                 }
87                 template.set("name", name);
88                 template.set("requestUri", requestUri);
89                 template.set("insertUri", insertUri);
90         }
91
92         /**
93          * {@inheritDoc}
94          */
95         @Override
96         public boolean isEnabled(ToadletContext toadletContext) {
97                 return true;
98         }
99
100 }