Hardcode document name for Sone SSKs to “Sone”.
[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                 super.processTemplate(request, template);
63                 String name = "";
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);
71                         }
72                         try {
73                                 /* create Sone. */
74                                 Sone sone = webInterface.core().createSone(name, "Sone", requestUri, insertUri);
75
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);
85                                 }
86                         }
87                 }
88                 template.set("name", name);
89                 template.set("requestUri", requestUri);
90                 template.set("insertUri", insertUri);
91         }
92
93         /**
94          * {@inheritDoc}
95          */
96         @Override
97         public boolean isEnabled(ToadletContext toadletContext) {
98                 return getCurrentSone(toadletContext) == null;
99         }
100
101 }