c39bd8a0f02b8d7c4759c7e4d1a2ddc2351e5961
[Sone.git] / src / main / java / net / pterodactylus / sone / web / BackupProfilePage.java
1 /*
2  * Sone - BackupProfilePage.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.StringWriter;
21
22 import net.pterodactylus.util.io.Closer;
23 import net.pterodactylus.util.template.Template;
24
25 /**
26  * This page lets the user store a backup file containing the settings of the
27  * logged in Sone.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class BackupProfilePage extends SoneTemplatePage {
32
33         /**
34          * Creates a new “backup profile” page.
35          *
36          * @param template
37          *            The template to render
38          * @param webInterface
39          *            The Sone web interface
40          */
41         public BackupProfilePage(Template template, WebInterface webInterface) {
42                 super("backupProfile.html", template, "Page.BackupProfile.Title", webInterface);
43         }
44
45         //
46         // TEMPLATEPAGE METHODS
47         //
48
49         /**
50          * {@inheritDoc}
51          */
52         @Override
53         public Response handleRequest(Request request) {
54                 StringWriter stringWriter = new StringWriter();
55                 try {
56                         template.render(stringWriter);
57                 } finally {
58                         Closer.close(stringWriter);
59                 }
60
61                 Response response = new Response(200, "OK", "text/xml; charset=utf-8", stringWriter.toString());
62                 response.setHeader("Content-Disposition", "attachment; filename=Sone_" + getCurrentSone(request.getToadletContext()).getName() + ".xml");
63                 return response;
64         }
65
66         //
67         // SONETEMPLATEPAGE METHODS
68         //
69
70         /**
71          * {@inheritDoc}
72          */
73         @Override
74         protected boolean requiresLogin() {
75                 return true;
76         }
77
78 }