9e3587c6c7b3260e2238d87bc0885880caaf6707
[Sone.git] / src / main / java / net / pterodactylus / sone / text / TextFilter.java
1 /*
2  * Sone - TextFilter.java - Copyright © 2011 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.text;
19
20 /**
21  * Filter for newly inserted text. This filter strips HTTP links to the local
22  * node of identifying marks, e.g. a link to “http://localhost:8888/KSK@gpl.txt”
23  * will be converted to “KSK@gpl.txt”. This will only work for links that point
24  * to the same address Sone is accessed by, so if you access Sone using
25  * localhost:8888, links to 127.0.0.1:8888 will not be removed.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class TextFilter {
30
31         /**
32          * Filters the given text, stripping the host header part for links to the
33          * local node.
34          *
35          * @param hostHeader
36          *            The host header from the request
37          * @param text
38          *            The text to filter
39          * @return The filtered text
40          */
41         public static String filter(String hostHeader, String text) {
42
43                 /* filter http(s) links to own node. */
44                 if (hostHeader != null) {
45                         String line = text;
46                         for (String toRemove : new String[] { "http://" + hostHeader + "/", "https://" + hostHeader + "/", "http://" + hostHeader, "https://" + hostHeader }) {
47                                 while (line.indexOf(toRemove) != -1) {
48                                         line = line.replace(toRemove, "");
49                                 }
50                         }
51                         return line;
52                 }
53
54                 /* not modified. */
55                 return text;
56         }
57
58 }