1 4 5 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 ; 18 import java.util.List ; 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 22 import org.apache.log4j.*; 23 24 33 public class ContentEncoding { 34 35 private static Logger mLogger = Logger.getLogger(ContentEncoding.class); 36 37 public String name = null; 38 public float q = 0; 39 40 45 private static List parseEncodings(HttpServletRequest req) { 46 47 String acceptHeader = req.getHeader("Accept-Encoding"); 48 if (acceptHeader == null) { 49 return null; 50 } 51 52 StringTokenizer toker = new StringTokenizer (acceptHeader, ","); 53 54 int numEncodings = toker.countTokens(); 55 56 ArrayList encs = new ArrayList (numEncodings); 57 int i = 0; 58 59 while (toker.hasMoreElements()) { 60 String token = toker.nextToken(); 61 StringTokenizer t = new StringTokenizer (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 81 public static String chooseEncoding(HttpServletRequest req) { 82 83 String 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 encs = parseEncodings(req); 95 if (encs == null) { 96 return null; 97 } 98 99 105 Iterator 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 126 127 return null; 128 } 129 } 130 | Popular Tags |