2 * Sone - TextFilter.java - Copyright © 2011 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.text;
20 import java.util.logging.Logger;
22 import net.pterodactylus.util.logging.Logging;
25 * Filter for newly inserted text. This filter strips HTTP links to the local
26 * node of identifying marks, e.g. a link to “http://localhost:8888/KSK@gpl.txt”
27 * will be converted to “KSK@gpl.txt”. This will only work for links that point
28 * to the same address Sone is accessed by, so if you access Sone using
29 * localhost:8888, links to 127.0.0.1:8888 will not be removed.
31 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33 public class TextFilter {
36 private static final Logger logger = Logging.getLogger(TextFilter.class);
39 * Filters the given text, stripping the host header part for links to the
43 * The host header from the request
46 * @return The filtered text
48 public static String filter(String hostHeader, String text) {
50 /* filter http(s) links to own node. */
51 if (hostHeader != null) {
53 for (String toRemove : new String[] { "http://" + hostHeader + "/", "https://" + hostHeader + "/", "http://" + hostHeader, "https://" + hostHeader }) {
54 while (line.indexOf(toRemove) != -1) {
55 line = line.replace(toRemove, "");