001: /* *****************************************************************************
002: * ContentEncoding.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.servlets;
011:
012: import javax.servlet.*;
013: import javax.servlet.http.*;
014:
015: import java.util.StringTokenizer;
016: import java.util.List;
017: import java.util.ArrayList;
018: import java.util.Iterator;
019:
020: /**
021: * ContentEncoding is representation of an HTTP 1.1 Content Encoding
022: * according to RFC 2616:
023: * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.html"
024: * >http://www.w3.org/Protocols/rfc2616/rfc2616.html</a>
025: * This spec is less than perfect.
026: *
027: * @author <a href="mailto:bloch@laszlosystems.com">bloch@laszlosystems.com</a>
028: */
029: public class ContentEncoding {
030:
031: public String name = null;
032: public float q = 0;
033:
034: /**
035: * @param req an HTTP request
036: * @return ContentEncoding [] an array of encodings acceptable by
037: * client making this request
038: */
039: private static List parseEncodings(HttpServletRequest req) {
040:
041: String acceptHeader = req.getHeader("Accept-Encoding");
042: if (acceptHeader == null) {
043: return null;
044: }
045:
046: StringTokenizer toker = new StringTokenizer(acceptHeader, ",");
047:
048: int numEncodings = toker.countTokens();
049:
050: ArrayList encs = new ArrayList(numEncodings);
051: int i = 0;
052:
053: while (toker.hasMoreElements()) {
054: String token = toker.nextToken();
055: StringTokenizer t = new StringTokenizer(token, ";");
056: ContentEncoding enc = new ContentEncoding();
057: enc.name = t.nextToken().trim();
058: if (t.countTokens() > 1) {
059: enc.q = Float.parseFloat(t.nextToken().trim());
060: } else {
061: enc.q = 1;
062: }
063: encs.add(enc);
064: }
065:
066: return encs;
067: }
068:
069: /**
070: * @return the encoding that is "best" among the array for
071: * a response. We support gzip and deflate
072: * @param req HttpServlet request
073: */
074: public static String chooseEncoding(HttpServletRequest req) {
075:
076: List encs = parseEncodings(req);
077: if (encs == null) {
078: return null;
079: }
080:
081: // First try gzip(transduce x-gzip to gzip) and then
082: // try deflate. Otherwise try null.
083: //
084: // FIXME: [2002-12-17] The spec says we should use a more complicated
085: // algorithm but this will probably work in general. Time will tell.
086:
087: Iterator iter;
088:
089: iter = encs.iterator();
090: while (iter.hasNext()) {
091: ContentEncoding e = (ContentEncoding) iter.next();
092: if (e.name.equals("gzip") || e.name.equals("x-gzip")) {
093: return "gzip";
094: }
095: }
096:
097: // FIXME: [2002-12-17 bloch]deflate as used in CompilationManager
098: // doesn't seem to produce bits that mozilla or ie can cope with. Hmmm...
099: /*
100: iter = encs.iterator();
101: while (iter.hasNext()) {
102: ContentEncoding e = (ContentEncoding)iter.next();
103: if (e.name.equals("deflate")) {
104: return "deflate";
105: }
106: }
107: */
108:
109: return null;
110: }
111: }
|