KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > utils > 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.utils;
11
12 import org.openlaszlo.server.LPS;
13
14 import javax.servlet.*;
15 import javax.servlet.http.*;
16
17 import java.util.StringTokenizer JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.apache.log4j.*;
23
24 /**
25  * ContentEncoding is representation of an HTTP 1.1 Content Encoding
26  * according to RFC 2616:
27  * <a HREF="http://www.w3.org/Protocols/rfc2616/rfc2616.html"
28  * >http://www.w3.org/Protocols/rfc2616/rfc2616.html</a>
29  * This spec is less than perfect.
30  *
31  * @author <a HREF="mailto:bloch@laszlosystems.com">bloch@laszlosystems.com</a>
32  */

33 public class ContentEncoding {
34
35     private static Logger mLogger = Logger.getLogger(ContentEncoding.class);
36
37     public String JavaDoc name = null;
38     public float q = 0;
39
40     /**
41      * @param req an HTTP request
42      * @return ContentEncoding [] an array of encodings acceptable by
43      * client making this request
44      */

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

81     public static String JavaDoc chooseEncoding(HttpServletRequest req) {
82
83         // This case is annoying
84
String JavaDoc ua = req.getHeader(LZHttpUtils.USER_AGENT);
85         if (ua == null) {
86             mLogger.warn("request has no user-agent header");
87             return null;
88         }
89
90         if (!LPS.configuration.optionAllows("content-encoding-user-agent", ua)) {
91             return null;
92         }
93
94         List JavaDoc encs = parseEncodings(req);
95         if (encs == null) {
96             return null;
97         }
98
99         // First try gzip(transduce x-gzip to gzip) and then
100
// try deflate. Otherwise try null.
101
//
102
// FIXME: [2002-12-17] The spec says we should use a more complicated
103
// algorithm but this will probably work in general. Time will tell.
104

105         Iterator JavaDoc iter;
106
107         iter = encs.iterator();
108         while (iter.hasNext()) {
109             ContentEncoding e = (ContentEncoding)iter.next();
110             if (e.name.equals("gzip") || e.name.equals("x-gzip")) {
111                 return "gzip";
112             }
113         }
114
115         // FIXME: [2002-12-17 bloch]deflate as used in CompilationManager
116
// doesn't seem to produce bits that mozilla or ie can cope with. Hmmm...
117
/*
118         iter = encs.iterator();
119         while (iter.hasNext()) {
120             ContentEncoding e = (ContentEncoding)iter.next();
121             if (e.name.equals("deflate")) {
122                 return "deflate";
123             }
124         }
125         */

126         
127         return null;
128    }
129 }
130
Popular Tags