Post date: 2007-08-10 17:25I have played a little with gzip compression. I am not sure of IBM's status of including it in domino. There are som issues with gzip and Internet Explorer so I can understand IBM's reluctance to support it.
Anyways the servlet is listed below. You can take any domino-resource (in fact any web-resource what so ever) and supply the link as a parameter and the servlet will gzip the contents and deliver it.
Notice that it will retrieve the content using the credentials of the logged in user. It mirrors all the cookies in the request to the server.
This is the url format (you can try the link if you want):
http://www.dominoExperts.com/servlet/gZip?url=/
It supports relative URL's but you could also pick up content from other sites.
I have included the class file for those lazy kids that don't want to compile the servlet.
/*
* Author: Tomas Nielsen
* Created: 2007-08-09
* www.dominoExperts.com
*//*
Servlet to retrieve a URL's content and deliver it compressed using gzip - if the client can handle it.
The servlet is completely transparent if the user is logged in, the page is retrieved with the users credentials.
This is done by mirroring all cookies trough the proxy.Note: URL's can NOT contain spaces. That is not a valid URL. %20 or + is the way to go.
Beware that there is a bug in HttpServletRequest.getParameter that will cause some disturbances with encoding if you
use encoded URL's. Depending on your configuration it can for example misinterpret UTF-8 as ISO8859-1 or vice versa(it does not query the browser settings before converting, and some browsers do not even send encoding, so it is a gamble).
Steps performed:
- Get the URL content
- Determine clients ability to do gzip
- If able do the zipping thing- Deliver content
Invoked trough:
/servlet/gZip?url=xxx
*/import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class gZip extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
long executionTime = System.currentTimeMillis();
try {
String u = req.getParameter("url");
u = (u.charAt(0)=='/' ? "http://" + req.getServerName() + u : u); // Allow relative URL's. URL url = new URL( u );
URLConnection con;
con = url.openConnection();
// Simulate being the user requesting the original page. int i = 0; String c = "";
Cookie cakes[] = req.getCookies();
if (cakes != null) while (i < cakes.length ) {
c += cakes[i].getName() + "=" + cakes[i].getValue() + "; ";
i++;
}
con.setRequestProperty ("Cookie", c );
con.connect();
if (con == null) { // Bad connect. PrintWriter o = res.getWriter();
o.println("Error reading url: " + req.getParameter("url"));
return;
}
String contentType = con.getContentType();
BufferedReader br;
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = "";
String content="";
// Get the page source. while((line = br.readLine()) != null) { // Loop trough all lines. content += line + "\n";
} br.close();
// set the content type to the original content type res.setContentType(contentType);
// See if browser can handle gzip String encoding=req.getHeader("Accept-Encoding");
if (encoding!=null && encoding.indexOf("gzip")>=0) { // gzip broser? res.setHeader("Content-Encoding","gzip");
OutputStream o=res.getOutputStream();
GZIPOutputStream gz=new GZIPOutputStream(o);
gz.write(content.getBytes());
gz.close();
o.close();
} else { // Some old browser -> give them plain text. PrintWriter o = res.getWriter();
o.println(content);
o.flush();
o.close();
}
} catch(Exception e)
{
e.printStackTrace();
}
}
}
gZip.class
|