This repository has been archived by the owner on Feb 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
tinymce.gzip.jsp
246 lines (201 loc) · 7.02 KB
/
tinymce.gzip.jsp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<%@page import="java.io.*,java.util.zip.*,java.net.URI" %>
<%
/**
* tinymce.gzip.jsp
*
* Copyright, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://tinymce.moxiecode.com/license
* Contributing: http://tinymce.moxiecode.com/contributing
*
* This file compresses the TinyMCE JavaScript using GZip and
* enables the browser to do two requests instead of one for each .js file.
*
* It's a good idea to use the diskcache option since it reduces the servers workload.
*/
String cacheKey = "", cacheFile = "", content = "", enc, suffix, cachePath;
String plugins[], languages[], themes[];
boolean diskCache, supportsGzip, isJS, compress, core;
int i, x, bytes, expiresOffset;
ServletOutputStream outStream = response.getOutputStream();
OutputStreamWriter bow;
ByteArrayOutputStream bos;
GZIPOutputStream gzipStream;
FileOutputStream fout;
FileInputStream fin;
byte buff[];
// Get input
plugins = getParam(request, "plugins", "").split(",");
languages = getParam(request, "languages", "").split(",");
themes = getParam(request, "themes", "").split(",");
diskCache = getParam(request, "diskcache", "").equals("true");
isJS = getParam(request, "js", "").equals("true");
compress = getParam(request, "compress", "true").equals("true");
core = getParam(request, "core", "true").equals("true");
suffix = getParam(request, "suffix", "").length() == 0 ? ".min" : "";
cachePath = mapPath(request, "."); // Cache path, this is where the .gz files will be stored
expiresOffset = 3600 * 24 * 10; // Cache for 10 days in browser cache
// Custom extra javascripts to pack
String custom[] = {/*
"some custom .js file",
"some custom .js file"
*/};
// Headers
response.setContentType("text/javascript");
response.addHeader("Vary", "Accept-Encoding"); // Handle proxies
response.setDateHeader("Expires", System.currentTimeMillis() + (expiresOffset * 1000));
// Is called directly then auto init with default settings
if (!isJS) {
out.print(getFileContents(mapPath(request, "tiny_mce_gzip.js")));
return;
}
// Setup cache info
if (diskCache) {
cacheKey = getParam(request, "plugins", "") + getParam(request, "languages", "") + getParam(request, "themes", "");
for (i = 0; i < custom.length; i++) {
cacheKey += custom[i];
}
cacheKey = md5(cacheKey);
if (compress) {
cacheFile = cachePath + File.separatorChar + "tinymce." + cacheKey + ".gz";
} else {
cacheFile = cachePath + File.separatorChar + "tinymce." + cacheKey + ".js";
}
}
// Check if it supports gzip
supportsGzip = false;
enc = request.getHeader("Accept-Encoding");
if (enc != null) {
enc.replaceAll("\\s+", "").toLowerCase();
supportsGzip = enc.indexOf("gzip") != -1 || request.getHeader("---------------") != null;
enc = enc.indexOf("x-gzip") != -1 ? "x-gzip" : "gzip";
}
// Use cached file disk cache
if (diskCache && supportsGzip && new File(cacheFile).exists()) {
if (compress)
response.addHeader("Content-Encoding", enc);
fin = new FileInputStream(cacheFile);
buff = new byte[1024];
while ((bytes = fin.read(buff, 0, buff.length)) != -1) {
outStream.write(buff, 0, bytes);
}
fin.close();
outStream.close();
return;
}
// Add core
if (core) {
// Set base URL for where tinymce is loaded from
String uri = request.getRequestURI();
uri = uri.substring(0, uri.lastIndexOf('/'));
content += "var tinymce={base:'" + uri + "',suffix:'.min'};";
content += getFileContents(mapPath(request, "tinymce" + suffix + ".js"));
}
// Add core languages
for (x = 0; x < languages.length; x++) {
content += getFileContents(mapPath(request, "langs/" + languages[x] + ".js"));
}
// Add themes
for (i = 0; i < themes.length; i++) {
content += getFileContents(mapPath(request, "themes/" + themes[i] + "/theme" + suffix + ".js"));
for (x = 0; x < languages.length; x++) {
content += getFileContents(mapPath(request, "themes/" + themes[i] + "/langs/" + languages[x] + ".js"));
}
}
// Add plugins
for (i = 0; i < plugins.length; i++) {
content += getFileContents(mapPath(request, "plugins/" + plugins[i] + "/plugin" + suffix + ".js"));
for (x = 0; x < languages.length; x++) {
content += getFileContents(mapPath(request, "plugins/" + plugins[i] + "/langs/" + languages[x] + ".js"));
}
}
// Add custom files
for (i = 0; i < custom.length; i++) {
content += getFileContents(mapPath(request, custom[i]));
}
// Generate GZIP'd content
if (supportsGzip) {
if (compress) {
response.addHeader("Content-Encoding", enc);
}
if (diskCache && cacheKey != "") {
bos = new ByteArrayOutputStream();
// Gzip compress
if (compress) {
gzipStream = new GZIPOutputStream(bos);
gzipStream.write(content.getBytes("iso-8859-1"));
gzipStream.close();
} else {
bow = new OutputStreamWriter(bos);
bow.write(content);
bow.close();
}
// Write to file
try {
fout = new FileOutputStream(cacheFile);
fout.write(bos.toByteArray());
fout.close();
} catch (IOException e) {
// Ignore
}
// Write to stream
outStream.write(bos.toByteArray());
} else {
gzipStream = new GZIPOutputStream(outStream);
gzipStream.write(content.getBytes("iso-8859-1"));
gzipStream.close();
}
} else {
out.write(content);
}
%><%!
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
public String getParam(HttpServletRequest request, String name, String def) {
String value = request.getParameter(name) != null ? "" + request.getParameter(name) : def;
return value.replaceAll("[^0-9a-zA-Z\\-_,]+", "");
}
public String getFileContents(String path) {
try {
if (!new File(path).exists()) {
return "";
}
FileInputStream fis = new FileInputStream(path);
int x = fis.available();
byte b[] = new byte[x];
fis.read(b);
return new String(b);
} catch (IOException e) {
// Ignore
}
return "";
}
public String mapPath(HttpServletRequest request, String path) {
String absPath = getServletContext().getRealPath(request.getRequestURI());
absPath = absPath.substring(0, absPath.lastIndexOf(File.separatorChar) + 1);
return absPath + path.replace('/', File.separatorChar);
}
public String md5(String str) {
try {
java.security.MessageDigest md5 = java.security.MessageDigest.getInstance("MD5");
char[] charArray = str.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++) {
byteArray[i] = (byte) charArray[i];
}
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i] ) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
} catch (java.security.NoSuchAlgorithmException e) {
// Ignore
}
return "";
}
%>