Add request change filter.
[Sone.git] / src / main / java / net / pterodactylus / sone / template / RequestChangeFilter.java
1 /*
2  * Sone - RequestChangeFilter.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.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.Page.Request;
30 import net.pterodactylus.util.template.DataProvider;
31 import net.pterodactylus.util.template.Filter;
32
33 /**
34  * This filter expects a {@link Request} as input and outputs a {@link URI} that
35  * is modified by the parameters. The name of the parameter is handed in as
36  * “name”, the value may either be stored in “value”, or in a template variable
37  * 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(DataProvider dataProvider, Object data, Map<String, String> parameters) {
48                 Request request = (Request) data;
49                 String name = parameters.get("name");
50                 String key = parameters.get("key");
51                 String value = null;
52                 if (key != null) {
53                         value = String.valueOf(dataProvider.getData(key));
54                 }
55                 if (value == null) {
56                         value = parameters.get("value");
57                 }
58                 if (value == null) {
59                         return request.getUri();
60                 }
61
62                 Map<String, String> values = new HashMap<String, String>();
63                 Collection<String> parameterNames = request.getHttpRequest().getParameterNames();
64                 for (String parameterName : parameterNames) {
65                         values.put(parameterName, request.getHttpRequest().getParam(parameterName));
66                 }
67                 values.put(name, value);
68
69                 StringBuilder query = new StringBuilder();
70                 try {
71                         for (Entry<String, String> parameterEntry : values.entrySet()) {
72                                 query.append((query.length() == 0) ? '?' : '&');
73                                 query.append(URLEncoder.encode(parameterEntry.getKey(), "UTF-8"));
74                                 query.append('=');
75                                 query.append(URLEncoder.encode(parameterEntry.getValue(), "UTF-8"));
76                         }
77                         String oldUri = request.getUri().toString();
78                         int questionMark = oldUri.indexOf('?');
79                         if (questionMark == -1) {
80                                 questionMark = oldUri.length();
81                         }
82                         URI u = new URI(oldUri.substring(0, questionMark) + query.toString());
83                         System.out.println("u: " + u);
84                         return u;
85                 } catch (UnsupportedEncodingException uee1) {
86                         /* UTF-8 not supported? I don’t think so. */
87                 } catch (URISyntaxException use1) {
88                         use1.printStackTrace();
89                 }
90                 return null;
91         }
92
93 }