Merge branch 'release-0.6.4'
[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 import java.util.logging.Logger;
21
22 import net.pterodactylus.util.logging.Logging;
23
24 /**
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.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class TextFilter {
34
35         /** The logger. */
36         private static final Logger logger = Logging.getLogger(TextFilter.class);
37
38         /**
39          * Filters the given text, stripping the host header part for links to the
40          * local node.
41          *
42          * @param hostHeader
43          *            The host header from the request
44          * @param text
45          *            The text to filter
46          * @return The filtered text
47          */
48         public static String filter(String hostHeader, String text) {
49
50                 /* filter http(s) links to own node. */
51                 if (hostHeader != null) {
52                         String line = text;
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, "");
56                                 }
57                         }
58                         return line;
59                 }
60
61                 /* not modified. */
62                 return text;
63         }
64
65 }