<%@ page session="false"%><%@ page errorPage="error.jsp"%><%@ page import="java.net.URL"%><%@ page import="java.net.URLConnection"%><%@ page import="java.net.MalformedURLException"%><%@ page import="java.io.BufferedInputStream"%><%@ page import="java.io.InputStream"%><%@ page import="java.io.IOException"%><%! /* * JavaServer Pages content negotiating filter for javascript files * Copyright (c) 2001 Jeroen Pulles */ // WARNING: // Do not add whitespace (space, tab, newline) between JSP tags, // this will destroy the response since it will add the space to the // binary file: corruption awaits. public static final String mDefaultURL = "http://fabtomcat/nl/AH/content/_js/experimental/2.js"; public static final String mURL = "http://fabtomcat/nl/AH/content/_js/experimental/2.js.gz"; public static final String mContentType = "text/html"; public static final String mContentEncoding = "gzip"; private static transient int[] mEncodedStream = null; private static final String[] mAcceptedAgents = { // "Mozilla/4.75 [en] (WinNT; U)", // "Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)", // "Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC)", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)", "Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:0.9.5) Gecko/20011011", "Mozilla/5.0 (Windows; U;", }; public void jspInit() { super.jspInit(); mEncodedStream = null; } /** * Returns true if the User-Agent header from the request * is found in the mAcceptedAgents array. */ private boolean acceptEncodingGzip(HttpServletRequest request) { if (request == null) { throw new IllegalArgumentException( "acceptEncodingGzip: no request"); } final String agent = request.getHeader("User-Agent"); if (agent == null) { return false; } for (int i = 0; i < mAcceptedAgents.length; i++) { if (agent.indexOf(mAcceptedAgents[i]) >= 0) { return true; } } return false; } /** * Loads the contents of the file at the given mURL. */ private void initResource() throws MalformedURLException { if (mEncodedStream != null) { return; } final URL httpresource = new URL(mURL); final URLConnection conn = httpresource.openConnection(); final Object obj = conn.getContent(); if (!(obj instanceof InputStream)) { throw new RuntimeException( "initResource(): Not a valid binary resource of type " + obj.getClass().getName()); } mEncodedStream = new int[conn.getContentLength()]; BufferedInputStream in = new BufferedInputStream((InputStream)obj); int c; for (int i = 0;(c = in.read()) != -1; i++) { mEncodedStream[i] = c; } } /** * Send the response to the client and close * the JspWriter connection stream. */ private static void writeResponse( JspWriter out, HttpServletResponse response) throws IOException { response.setStatus(200); response.setContentType(mContentType); response.addHeader("Content-Encoding", mContentEncoding); if (mEncodedStream == null) { throw new RuntimeException("No resource, " + "use initResource first"); } for (int i = 0; i < mEncodedStream.length; i++) { out.write(mEncodedStream[i]); } out.close(); } %><% if (acceptEncodingGzip(request)) { initResource(out); writeResponse(out, response); } else { response.sendRedirect(response.encodeURL(mDefaultURL)); } %>