Remove @author tags
[Sone.git] / src / main / java / net / pterodactylus / sone / text / TextFilter.java
1 /*
2  * Sone - TextFilter.java - Copyright © 2011–2016 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 public class TextFilter {
28
29         /**
30          * Filters the given text, stripping the host header part for links to the
31          * local node.
32          *
33          * @param hostHeader
34          *            The host header from the request
35          * @param text
36          *            The text to filter
37          * @return The filtered text
38          */
39         public static String filter(String hostHeader, String text) {
40
41                 /* filter http(s) links to own node. */
42                 if (hostHeader != null) {
43                         String line = text;
44                         for (String toRemove : new String[] { "http://" + hostHeader + "/", "https://" + hostHeader + "/", "http://" + hostHeader, "https://" + hostHeader }) {
45                                 while (line.indexOf(toRemove) != -1) {
46                                         line = line.replace(toRemove, "");
47                                 }
48                         }
49                         return line;
50                 }
51
52                 /* not modified. */
53                 return text;
54         }
55
56 }