From 7d380ba312a2c9e4e820c83f8d00a81894bbc6f5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 29 Apr 2011 07:22:11 +0200 Subject: [PATCH] Add text filter for inserts. --- .../net/pterodactylus/sone/text/TextFilter.java | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/text/TextFilter.java diff --git a/src/main/java/net/pterodactylus/sone/text/TextFilter.java b/src/main/java/net/pterodactylus/sone/text/TextFilter.java new file mode 100644 index 0000000..5744368 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/text/TextFilter.java @@ -0,0 +1,65 @@ +/* + * Sone - TextFilter.java - Copyright © 2011 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.text; + +import java.util.logging.Logger; + +import net.pterodactylus.util.logging.Logging; + +/** + * Filter for newly inserted text. This filter strips HTTP links to the local + * node of identifying marks, e.g. a link to “http://localhost:8888/KSK@gpl.txt” + * will be converted to “KSK@gpl.txt”. This will only work for links that point + * to the same address Sone is accessed by, so if you access Sone using + * localhost:8888, links to 127.0.0.1:8888 will not be removed. + * + * @author David ‘Bombe’ Roden + */ +public class TextFilter { + + /** The logger. */ + private static final Logger logger = Logging.getLogger(TextFilter.class); + + /** + * Filters the given text, stripping the host header part for links to the + * local node. + * + * @param hostHeader + * The host header from the request + * @param text + * The text to filter + * @return The filtered text + */ + public static String filter(String hostHeader, String text) { + + /* filter http(s) links to own node. */ + if (hostHeader != null) { + String line = text; + for (String toRemove : new String[] { "http://" + hostHeader + "/", "https://" + hostHeader + "/", "http://" + hostHeader, "https://" + hostHeader }) { + while (line.indexOf(toRemove) != -1) { + line = line.replace(toRemove, ""); + } + } + return line; + } + + /* not modified. */ + return text; + } + +} -- 2.7.4