-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedirects.java
More file actions
131 lines (106 loc) · 4.83 KB
/
Copy pathRedirects.java
File metadata and controls
131 lines (106 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package javaxt.express.cms;
//******************************************************************************
//** Redirects Class
//******************************************************************************
/**
* Used to parse a plain text file with URL redirects. This is useful when
* moving/renaming content files. Each line maps a key to a replacement,
* separated by one or more tabs like this:
<pre>
path/to/old-file path/to/new-file
</pre>
* An incoming URL is matched against the keys using a case-insensitive
* substring search (longest key first, so more specific rules win), and the
* matched portion is swapped for the replacement. Relative replacements keep
* the original scheme/host, while an absolute replacement (http/https) is
* used as the full destination for cross-site redirects. The file is
* re-parsed automatically whenever it changes on disk. Blank lines and lines
* starting with "#" or "//" are ignored.
*
******************************************************************************/
public class Redirects {
private java.util.HashMap<String, String> redirects = new java.util.HashMap<String, String>();
private java.util.List<String> sortedKeys = new java.util.ArrayList<String>();
private javaxt.io.File file;
private long lastUpdate;
//**************************************************************************
//** Constructor
//**************************************************************************
public Redirects(javaxt.io.File file) {
this.file = file;
parseRedirects();
}
//**************************************************************************
//** parseRedirects
//**************************************************************************
/** Used to parse the "redirects.txt" file and updates the redirects hashmap.
* Also updates the lastUpdate timestamp used in the getRedirect() method.
*/
private void parseRedirects(){
redirects.clear();
sortedKeys.clear();
if (file.exists()){
lastUpdate = file.getDate().getTime();
for (String entry : file.getText().split("\r\n")){
entry = entry.trim();
if (entry.length()==0 || entry.startsWith("#") || entry.startsWith("//")){
//skip line
}
else{
while(entry.contains("\t\t")) entry = entry.replace("\t\t", "\t");
String[] arr = entry.split("\t");
if (arr.length>1){
redirects.put(arr[0], arr[1]);
}
}
}
sortedKeys = new java.util.ArrayList<String>(redirects.keySet());
java.util.Collections.sort(sortedKeys, new StringComparator());
}
}
//**************************************************************************
//** getRedirect
//**************************************************************************
/** Returns a redirected/updated url. Returns null if there is no redirect.
*/
public String getRedirect(java.net.URL url){
return getRedirect(url.toString());
}
//**************************************************************************
//** getRedirect
//**************************************************************************
/** Returns a redirected/updated url. Returns null if there is no redirect.
*/
private String getRedirect(String url){
//Check timestamp of the redirect file. Reparse as needed.
if (file.exists() && file.getDate().getTime()!=lastUpdate) parseRedirects();
//Loop through all the redirects and find a suitable match
java.util.Iterator<String> it = sortedKeys.iterator();
while (it.hasNext()){
String key = it.next();
if (url.toUpperCase().contains(key.toUpperCase())){
String replacement = redirects.get(key);
int x = url.toUpperCase().indexOf(key.toUpperCase());
int y = key.length();
String a = url.substring(0, x);
String b = url.substring(x+y);
//If the replacement is an absolute URL, use the full path
String t = replacement.toLowerCase();
if (t.startsWith("http://") || t.startsWith("https://")){
return replacement + b;
}
return a + replacement + b;
}
}
//If not match is found, return a null
return null;
}
/** Used to compare to strings based on their length. Longer strings will
* appear first.
*/
private class StringComparator implements java.util.Comparator<String> {
public int compare(String t1, String t2) {
return new Integer(t2.length()).compareTo(new Integer(t1.length()));
}
}
}