%@
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.ByteArrayOutputStream"%><%@
page import="java.io.BufferedInputStream"%><%@
page import="java.io.BufferedReader"%><%@
page import="java.io.InputStream"%><%@
page import="java.io.PipedInputStream"%><%@
page import="java.io.PipedOutputStream"%><%@
page import="java.io.Reader"%><%@
page import="java.util.zip.GZIPOutputStream "%><%@
page import="java.io.IOException"%><%!
/*
* JavaServer Pages content negotiating filter for javascript files
* Copyright (c) 2001 Jeroen Pulles
* mailto:jeroenp@gmx.net
*/
// 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 class HtmlResource extends Object
{
private String mURL;
private long mLastModified;
private long mExpires;
//private int[] mCharArray;
private byte[] mCharArray;
private byte[] mGzipArray;
private String mContentType = "text/html";
// list of browsers that are supported, sort with most
// common on top:
private final String[] mAcceptedAgents = {
"Mozilla/4.0 (compatible; MSIE 5.5",
"Mozilla/5.0",
};
public HtmlResource(String url)
throws MalformedURLException, IOException
{
if (url == null)
throw new IllegalArgumentException("HtmlResource(): no URL");
mURL = url;
initResource();
}
/**
* Returns true if the User-Agent header from the request
* is found in the mAcceptedAgents array.
*/
private boolean acceptGzip(HttpServletRequest request)
{
if (request == null) {
throw new IllegalArgumentException("acceptGzip: 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, IOException
{
final URL httpresource = new URL(mURL);
final URLConnection conn = httpresource.openConnection();
mContentType = conn.getContentType();
mLastModified = conn.getLastModified();
mExpires = conn.getExpiration();
final Object obj = conn.getContent();
if (!(obj instanceof InputStream)) {
throw new RuntimeException(
"initResource(): Not a valid resource, of type " +
obj.getClass().getName());
}
mCharArray = new byte[conn.getContentLength()];
BufferedInputStream in = new BufferedInputStream((InputStream)obj);
int c;
for (int i = 0;(c = in.read()) != -1; i++) {
mCharArray[i] = (byte)c;
}
mGzipArray = compress(mCharArray);
}
private byte[] compress(byte[] chararray)
throws IOException
{
ByteArrayOutputStream os = new ByteArrayOutputStream();
GZIPOutputStream gzos = new GZIPOutputStream(os);
for (int i = 0; i < chararray.length; i++) {
// TODO use a buffer and gzos own write...
gzos.write(chararray[i]);
}
gzos.close();
os.close();
byte[] data = os.toByteArray(); // ERROR?
if (true) { // DEBUG
throw new RuntimeException("AAP-" + chararray.length +
"=>" + data.length);
}
return data;
}
/**
* Send the response to the client and close
* the JspWriter connection stream.
*/
public void write(
JspWriter out,
HttpServletRequest request,
HttpServletResponse response)
throws IOException
{
//if (!acceptGzip(request)) {
if (false) { // DEBUG
// ****
// Design Note:
// Use redirects to prevent proxy caches to mix up
// responses for various clients.
// ****
response.sendRedirect(response.encodeURL(mURL));
}
response.setStatus(200);
response.setContentType(mContentType);
response.setContentType("text/aap");
if (mLastModified > 0l) {
response.addDateHeader("Last-Modified", mLastModified);
}
if (mExpires > 0l) {
response.addDateHeader("Expires", mExpires);
}
//response.addHeader("Content-Encoding", "gzip");
for (int i = 0; i < mGzipArray.length; i++) {
out.write(mGzipArray[i]);
}
out.close();
}
} // class
%><%
// Create resource and return it:
HtmlResource h = new HtmlResource(request.getParameter("resource"));
h.write(out, request, response);
%>