<%@page import="java.io.*,java.util.zip.*" %> <% /** * $Id: tiny_mce_gzip.jsp 535 2008-01-14 15:01:34Z spocke $ * * @author Moxiecode * @copyright Copyright © 2006, Moxiecode Systems AB, All rights reserved. * * 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", "").equals("_src") ? "_src" : ""; 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"))); out.print("tinyMCE_GZ.init({});"); return; } // Setup cache info if (diskCache) { cacheKey = getParam(request, "plugins", "") + getParam(request, "languages", "") + getParam(request, "themes", ""); for (i=0; i<%! /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 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