Change all copyright headers to include 2012.
[Sone.git] / src / main / java / net / pterodactylus / sone / template / RequestChangeFilter.java
1 /*
2  * Sone - RequestChangeFilter.java - Copyright © 2010–2012 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.template;
19
20 import java.io.UnsupportedEncodingException;
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.net.URLEncoder;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Map.Entry;
28
29 import net.pterodactylus.sone.web.page.FreenetRequest;
30 import net.pterodactylus.util.template.Filter;
31 import net.pterodactylus.util.template.TemplateContext;
32
33 /**
34  * This filter expects a {@link FreenetRequest} as input and outputs a
35  * {@link URI} that is modified by the parameters. The name of the parameter is
36  * handed in as “name”, the value may either be stored in “value”, or in a
37  * template variable whose key is stored in “key”.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class RequestChangeFilter implements Filter {
42
43         /**
44          * {@inheritDoc}
45          */
46         @Override
47         public Object format(TemplateContext templateContext, Object data, Map<String, String> parameters) {
48                 FreenetRequest request = (FreenetRequest) data;
49                 String name = parameters.get("name");
50                 String nameKey = parameters.get("nameKey");
51                 if (nameKey != null) {
52                         name = String.valueOf(templateContext.get(nameKey));
53                 }
54                 String key = parameters.get("key");
55                 String value = null;
56                 if (key != null) {
57                         value = String.valueOf(templateContext.get(key));
58                 }
59                 if (value == null) {
60                         value = parameters.get("value");
61                 }
62                 if (value == null) {
63                         return request.getUri();
64                 }
65
66                 Map<String, String> values = new HashMap<String, String>();
67                 Collection<String> parameterNames = request.getHttpRequest().getParameterNames();
68                 for (String parameterName : parameterNames) {
69                         values.put(parameterName, request.getHttpRequest().getParam(parameterName));
70                 }
71                 values.put(name, value);
72
73                 StringBuilder query = new StringBuilder();
74                 try {
75                         for (Entry<String, String> parameterEntry : values.entrySet()) {
76                                 query.append((query.length() == 0) ? '?' : '&');
77                                 query.append(URLEncoder.encode(parameterEntry.getKey(), "UTF-8"));
78                                 query.append('=');
79                                 query.append(URLEncoder.encode(parameterEntry.getValue(), "UTF-8"));
80                         }
81                         String oldUri = request.getUri().toString();
82                         int questionMark = oldUri.indexOf('?');
83                         if (questionMark == -1) {
84                                 questionMark = oldUri.length();
85                         }
86                         URI u = new URI(oldUri.substring(0, questionMark) + query.toString());
87                         return u;
88                 } catch (UnsupportedEncodingException uee1) {
89                         /* UTF-8 not supported? I don’t think so. */
90                 } catch (URISyntaxException use1) {
91                         use1.printStackTrace();
92                 }
93                 return null;
94         }
95
96 }