Allow the resolver page to be excepted from the link filter.
[WoTNS.git] / src / main / java / net / pterodactylus / wotns / ui / web / ResolverPage.java
1 /*
2  * WoTNS - ResolverPage.java - Copyright © 2011–2017 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.wotns.ui.web;
19
20 import java.net.MalformedURLException;
21 import java.net.URI;
22
23 import net.pterodactylus.util.template.Template;
24 import net.pterodactylus.util.template.TemplateContext;
25 import net.pterodactylus.wotns.main.Resolver;
26 import net.pterodactylus.wotns.web.FreenetRequest;
27 import freenet.keys.FreenetURI;
28
29 /**
30  * TODO
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class ResolverPage extends BasicPage {
35
36         private final Resolver resolver;
37
38         public ResolverPage(Template unknownNameTemplate, WebInterface webInterface, Resolver resolver) {
39                 super(webInterface, "", "Error - Web of Trust Name Service", unknownNameTemplate);
40                 this.resolver = resolver;
41         }
42
43         //
44         // PAGE METHODS
45         //
46
47         /**
48          * {@inheritDoc}
49          */
50         @Override
51         public boolean isPrefixPage() {
52                 return true;
53         }
54
55         //
56         // FREENETTEMPLATEPAGE METHODS
57         //
58
59         /**
60          * {@inheritDoc}
61          */
62         @Override
63         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
64                 String uri = request.getUri().getPath();
65                 String path = uri.substring(uri.indexOf('/', 1) + 1);
66
67                 /* look for path after target. */
68                 int firstSlash = path.indexOf('/');
69                 int secondSlash = path.indexOf('/', firstSlash + 1);
70                 String filePath = "";
71                 if (secondSlash != -1) {
72                         filePath = path.substring(secondSlash);
73                         path = path.substring(0, secondSlash);
74                 }
75                 try {
76                         FreenetURI targetUri = resolver.resolveURI(path);
77                         if (targetUri != null) {
78                                 throw new RedirectException("/" + targetUri.toString() + filePath);
79                         }
80                 } catch (MalformedURLException mue1) {
81                         /* TODO - do something. */
82                 }
83                 templateContext.set("shortName", path);
84         }
85
86         /**
87          * {@inheritDoc}
88          */
89         @Override
90         public boolean isLinkExcepted(URI link) {
91                 return true;
92         }
93
94 }