1 4 5 9 10 package org.openlaszlo.servlets; 11 12 import javax.servlet.*; 13 import javax.servlet.http.*; 14 15 import java.util.StringTokenizer ; 16 import java.util.List ; 17 import java.util.ArrayList ; 18 import java.util.Iterator ; 19 20 29 public class ContentEncoding { 30 31 public String name = null; 32 public float q = 0; 33 34 39 private static List parseEncodings(HttpServletRequest req) { 40 41 String acceptHeader = req.getHeader("Accept-Encoding"); 42 if (acceptHeader == null) { 43 return null; 44 } 45 46 StringTokenizer toker = new StringTokenizer (acceptHeader, ","); 47 48 int numEncodings = toker.countTokens(); 49 50 ArrayList encs = new ArrayList (numEncodings); 51 int i = 0; 52 53 while (toker.hasMoreElements()) { 54 String token = toker.nextToken(); 55 StringTokenizer t = new StringTokenizer (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 74 public static String chooseEncoding(HttpServletRequest req) { 75 76 List encs = parseEncodings(req); 77 if (encs == null) { 78 return null; 79 } 80 81 87 Iterator 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 108 109 return null; 110 } 111 } 112 | Popular Tags |