Use the identity manager.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ImportSonePage.java
1 /*
2  * Sone - ImportSonePage.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.io.IOException;
21 import java.io.InputStream;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24
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.support.api.Bucket;
30 import freenet.support.io.Closer;
31
32 /**
33  * The “import Sone” page lets the user import a Sone from a previous backup.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class ImportSonePage extends SoneTemplatePage {
38
39         /** The logger. */
40         private static final Logger logger = Logging.getLogger(ImportSonePage.class);
41
42         /**
43          * Creates a new “import Sone” page.
44          *
45          * @param template
46          *            The template to render
47          * @param webInterface
48          *            The Sone web interface
49          */
50         public ImportSonePage(Template template, WebInterface webInterface) {
51                 super("importSone.html", template, "Page.ImportSone.Title", webInterface, false);
52         }
53
54         //
55         // TEMPLATEPAGE METHODS
56         //
57
58         /**
59          * {@inheritDoc}
60          */
61         @Override
62         protected void processTemplate(net.pterodactylus.sone.web.page.Page.Request request, Template template) throws RedirectException {
63                 super.processTemplate(request, template);
64                 template.set("errorParsingSone", false);
65                 if (request.getMethod() == Method.POST) {
66                         Bucket soneBucket = request.getHttpRequest().getPart("sone-file");
67                         InputStream soneInputStream = null;
68                         try {
69                                 soneInputStream = soneBucket.getInputStream();
70                                 Sone sone = webInterface.core().loadSone(soneInputStream);
71                                 if (sone != null) {
72                                         throw new RedirectException("viewSone.html?sone=" + sone.getId());
73                                 }
74                         } catch (IOException ioe1) {
75                                 logger.log(Level.INFO, "Could not load sone from posted XML file.", ioe1);
76                         } finally {
77                                 Closer.close(soneInputStream);
78                                 soneBucket.free();
79                         }
80                         template.set("errorParsingSone", true);
81                 }
82         }
83
84 }