From 8c8acd50782b5d64b2a5a57fe8f9a4c2385203fd Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 21 Oct 2010 23:11:38 +0200 Subject: [PATCH] Add request change filter. --- .../sone/template/RequestChangeFilter.java | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/template/RequestChangeFilter.java diff --git a/src/main/java/net/pterodactylus/sone/template/RequestChangeFilter.java b/src/main/java/net/pterodactylus/sone/template/RequestChangeFilter.java new file mode 100644 index 0000000..d9c453e --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/template/RequestChangeFilter.java @@ -0,0 +1,93 @@ +/* + * Sone - RequestChangeFilter.java - Copyright © 2010 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.template; + +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLEncoder; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import net.pterodactylus.sone.web.page.Page.Request; +import net.pterodactylus.util.template.DataProvider; +import net.pterodactylus.util.template.Filter; + +/** + * This filter expects a {@link Request} as input and outputs a {@link URI} that + * is modified by the parameters. The name of the parameter is handed in as + * “name”, the value may either be stored in “value”, or in a template variable + * whose key is stored in “key”. + * + * @author David ‘Bombe’ Roden + */ +public class RequestChangeFilter implements Filter { + + /** + * {@inheritDoc} + */ + @Override + public Object format(DataProvider dataProvider, Object data, Map parameters) { + Request request = (Request) data; + String name = parameters.get("name"); + String key = parameters.get("key"); + String value = null; + if (key != null) { + value = String.valueOf(dataProvider.getData(key)); + } + if (value == null) { + value = parameters.get("value"); + } + if (value == null) { + return request.getUri(); + } + + Map values = new HashMap(); + Collection parameterNames = request.getHttpRequest().getParameterNames(); + for (String parameterName : parameterNames) { + values.put(parameterName, request.getHttpRequest().getParam(parameterName)); + } + values.put(name, value); + + StringBuilder query = new StringBuilder(); + try { + for (Entry parameterEntry : values.entrySet()) { + query.append((query.length() == 0) ? '?' : '&'); + query.append(URLEncoder.encode(parameterEntry.getKey(), "UTF-8")); + query.append('='); + query.append(URLEncoder.encode(parameterEntry.getValue(), "UTF-8")); + } + String oldUri = request.getUri().toString(); + int questionMark = oldUri.indexOf('?'); + if (questionMark == -1) { + questionMark = oldUri.length(); + } + URI u = new URI(oldUri.substring(0, questionMark) + query.toString()); + System.out.println("u: " + u); + return u; + } catch (UnsupportedEncodingException uee1) { + /* UTF-8 not supported? I don’t think so. */ + } catch (URISyntaxException use1) { + use1.printStackTrace(); + } + return null; + } + +} -- 2.7.4