KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > servlets > ContentEncoding


1 /* *****************************************************************************
2  * ContentEncoding.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.servlets;
11
12 import javax.servlet.*;
13 import javax.servlet.http.*;
14
15 import java.util.StringTokenizer JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 /**
21  * ContentEncoding is representation of an HTTP 1.1 Content Encoding
22  * according to RFC 2616:
23  * <a HREF="http://www.w3.org/Protocols/rfc2616/rfc2616.html"
24  * >http://www.w3.org/Protocols/rfc2616/rfc2616.html</a>
25  * This spec is less than perfect.
26  *
27  * @author <a HREF="mailto:bloch@laszlosystems.com">bloch@laszlosystems.com</a>
28  */

29 public class ContentEncoding {
30
31     public String JavaDoc name = null;
32     public float q = 0;
33
34     /**
35      * @param req an HTTP request
36      * @return ContentEncoding [] an array of encodings acceptable by
37      * client making this request
38      */

39     private static List JavaDoc parseEncodings(HttpServletRequest req) {
40
41         String JavaDoc acceptHeader = req.getHeader("Accept-Encoding");
42         if (acceptHeader == null) {
43             return null;
44         }
45
46         StringTokenizer JavaDoc toker = new StringTokenizer JavaDoc(acceptHeader, ",");
47         
48         int numEncodings = toker.countTokens();
49
50         ArrayList JavaDoc encs = new ArrayList JavaDoc(numEncodings);
51         int i = 0;
52
53         while (toker.hasMoreElements()) {
54             String JavaDoc token = toker.nextToken();
55             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(token, ";");
56             ContentEncoding enc = new ContentEncoding();
57             enc.name = t.nextToken().trim();
58             if (t.countTokens() > 1) {
59                 enc.q = Float.parseFloat(t.nextToken().trim());
60             } else {
61                 enc.q = 1;
62             }
63             encs.add(enc);
64         }
65
66         return encs;
67     }
68
69     /**
70      * @return the encoding that is "best" among the array for
71      * a response. We support gzip and deflate
72      * @param req HttpServlet request
73      */

74     public static String JavaDoc chooseEncoding(HttpServletRequest req) {
75
76         List JavaDoc encs = parseEncodings(req);
77         if (encs == null) {
78             return null;
79         }
80
81         // First try gzip(transduce x-gzip to gzip) and then
82
// try deflate. Otherwise try null.
83
//
84
// FIXME: [2002-12-17] The spec says we should use a more complicated
85
// algorithm but this will probably work in general. Time will tell.
86

87         Iterator JavaDoc iter;
88
89         iter = encs.iterator();
90         while (iter.hasNext()) {
91             ContentEncoding e = (ContentEncoding)iter.next();
92             if (e.name.equals("gzip") || e.name.equals("x-gzip")) {
93                 return "gzip";
94             }
95         }
96
97         // FIXME: [2002-12-17 bloch]deflate as used in CompilationManager
98
// doesn't seem to produce bits that mozilla or ie can cope with. Hmmm...
99
/*
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 }
112
Popular Tags