abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r441309 - in /incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol: Response.java util/AbstractRequest.java util/AbstractResponse.java util/CacheControlParser.java util/CacheControlUtil.java
Date Fri, 08 Sep 2006 00:47:30 GMT
Author: jmsnell
Date: Thu Sep  7 17:47:30 2006
New Revision: 441309

URL: http://svn.apache.org/viewvc?view=rev&rev=441309
Log:
Improvements to the base protocol classes

Use an enum of cache control directives instead of two hashsets for 
more efficient processing.

Modified:
    incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Response.java
    incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java
    incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractResponse.java
    incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/CacheControlParser.java
    incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/CacheControlUtil.java

Modified: incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Response.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Response.java?view=diff&rev=441309&r1=441308&r2=441309
==============================================================================
--- incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Response.java
(original)
+++ incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Response.java
Thu Sep  7 17:47:30 2006
@@ -31,7 +31,16 @@
 public interface Response extends ProtocolConstants {
 
   public static enum ResponseType {
-    SUCCESS, REDIRECTION, CLIENT_ERROR, SERVER_ERROR, UNKNOWN
+    SUCCESS, REDIRECTION, CLIENT_ERROR, SERVER_ERROR, UNKNOWN;
+    
+    public static ResponseType select(int status) {
+      if (status >= 200 && status < 300) return SUCCESS;
+      if (status >= 300 && status < 400) return REDIRECTION;
+      if (status >= 400 && status < 500) return CLIENT_ERROR;
+      if (status >= 500 && status < 600) return SERVER_ERROR;
+      return UNKNOWN;
+    }
+    
   }
   
   public ResponseType getType();

Modified: incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java?view=diff&rev=441309&r1=441308&r2=441309
==============================================================================
--- incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java
(original)
+++ incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java
Thu Sep  7 17:47:30 2006
@@ -93,19 +93,19 @@
   }
 
   public boolean isNoCache() {
-    return (flags & NOCACHE) == NOCACHE; 
+    return check(NOCACHE); 
   }
 
   public boolean isNoStore() {
-    return (flags & NOSTORE) == NOSTORE;
+    return check(NOSTORE);
   }
 
   public boolean isNoTransform() {
-    return (flags & NOTRANSFORM) == NOTRANSFORM;
+    return check(NOTRANSFORM);
   }
 
   public boolean isOnlyIfCached() {
-    return (flags & ONLYIFCACHED) == ONLYIFCACHED;
+    return check(ONLYIFCACHED);
   }
 
   public void setMaxAge(long max_age) {
@@ -119,24 +119,29 @@
   public void setMinFresh(long min_fresh) {
     this.min_fresh = min_fresh;
   }
+
+  private boolean check(int flag) {
+    return (flags & flag) == flag;
+  }
+  
+  private void toggle(boolean val, int flag) {
+    if (val) flags |= flag;
+    else flags &= ~flag;
+  }
   
   public void setNoCache(boolean val) {
-    if (val) flags |= NOCACHE;
-    else if (isNoCache()) flags ^= NOCACHE;
+    toggle(val, NOCACHE);
   }
   
   public void setNoStore(boolean val) {
-    if (val) flags |= NOSTORE;
-    else if (isNoStore()) flags ^= NOSTORE;
+    toggle(val, NOSTORE);
   }
   
   public void setNoTransform(boolean val) {
-    if (val) flags |= NOTRANSFORM;
-    else if (isNoTransform()) flags ^= NOTRANSFORM;
+    toggle(val, NOTRANSFORM);
   }
   
   public void setOnlyIfCached(boolean val) {
-    if (val) flags |= ONLYIFCACHED;
-    else if (isOnlyIfCached()) flags ^= ONLYIFCACHED;
+    toggle(val, ONLYIFCACHED);
   }
 }

Modified: incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractResponse.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractResponse.java?view=diff&rev=441309&r1=441308&r2=441309
==============================================================================
--- incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractResponse.java
(original)
+++ incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractResponse.java
Thu Sep  7 17:47:30 2006
@@ -103,12 +103,7 @@
   }
 
   public ResponseType getType() {
-    int status = getStatus();
-    if (status >= 200 && status < 300) return ResponseType.SUCCESS;
-    if (status >= 300 && status < 400) return ResponseType.REDIRECTION;
-    if (status >= 400 && status < 500) return ResponseType.CLIENT_ERROR;
-    if (status >= 500 && status < 600) return ResponseType.SERVER_ERROR;
-    return ResponseType.UNKNOWN;
+    return ResponseType.select(getStatus());
   }
 
   public URI getUriHeader(String name) throws URISyntaxException {
@@ -117,31 +112,31 @@
   }
 
   public boolean isMustRevalidate() {
-    return (flags & REVALIDATE) == REVALIDATE;
+    return check(REVALIDATE);
   }
 
   public boolean isNoCache() {
-    return (flags & NOCACHE) == NOCACHE;
+    return check(NOCACHE);
   }
 
   public boolean isNoStore() {
-    return (flags & NOSTORE) == NOSTORE;
+    return check(NOSTORE);
   }
 
   public boolean isNoTransform() {
-    return (flags & NOTRANSFORM) == NOTRANSFORM;
+    return check(NOTRANSFORM);
   }
 
   public boolean isPrivate() {
-    return (flags & PRIVATE) == PRIVATE;
+    return check(PRIVATE);
   }
 
   public boolean isProxyRevalidate() {
-    return (flags & PROXYREVALIDATE) == PROXYREVALIDATE;
+    return check(PROXYREVALIDATE);
   }
 
   public boolean isPublic() {
-    return (flags & PUBLIC) == PUBLIC;
+    return check(PUBLIC);
   }
 
   public void setMaxAge(long max_age) {
@@ -149,33 +144,27 @@
   }
   
   public void setMustRevalidate(boolean val) {
-    if (val) flags |= REVALIDATE;
-    else if (isMustRevalidate()) flags ^= REVALIDATE;
+    toggle(val, REVALIDATE);
   }
   
   public void setProxyRevalidate(boolean val) {
-    if (val) flags |= PROXYREVALIDATE;
-    else if (isProxyRevalidate()) flags ^= PROXYREVALIDATE;
+    toggle(val, PROXYREVALIDATE);
   }
   
   public void setNoCache(boolean val) {
-    if (val) flags |= NOCACHE;
-    else if (isNoCache()) flags ^= NOCACHE;
+    toggle(val, NOCACHE);
   }
   
   public void setNoStore(boolean val) {
-    if (val) flags |= NOSTORE;
-    else if (isNoStore()) flags ^= NOSTORE;
+    toggle(val, NOSTORE);
   }
   
   public void setNoTransform(boolean val) {
-    if (val) flags |= NOTRANSFORM;
-    else if (isNoTransform()) flags ^= NOTRANSFORM;
+    toggle(val, NOTRANSFORM);
   }
   
   public void setPublic(boolean val) {
-    if (val) flags |= PUBLIC;
-    else if (isPublic()) flags ^= PUBLIC;
+    toggle(val, PUBLIC);
   }
   
   public void setPrivate(boolean val) {
@@ -191,4 +180,13 @@
     this.nocache_headers = headers;
   }
 
+  private boolean check(int flag) {
+    return (flags & flag) == flag;
+  }
+  
+  private void toggle(boolean val, int flag) {
+    if (val) flags |= flag;
+    else flags &= ~flag;
+  }
+  
 }

Modified: incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/CacheControlParser.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/CacheControlParser.java?view=diff&rev=441309&r1=441308&r2=441309
==============================================================================
--- incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/CacheControlParser.java
(original)
+++ incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/CacheControlParser.java
Thu Sep  7 17:47:30 2006
@@ -17,74 +17,79 @@
 */
 package org.apache.abdera.protocol.util;
 
-import java.util.Arrays;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-public class CacheControlParser implements Iterable<String> {
-
-  public static final HashSet<String> cacheRequestDirectives = 
-    new HashSet<String>(
-      Arrays.asList(
-        new String[] { 
-          "no-cache", 
-          "no-store", 
-          "max-age", 
-          "max-stale", 
-          "min-fresh", 
-          "no-transform", 
-          "only-if-cached" }));
-
-  public static final HashSet<String> cacheResponseDirectives = 
-    new HashSet<String>(
-      Arrays.asList(
-        new String[] { 
-          "public", 
-          "private", 
-          "no-cache", 
-          "no-store", 
-          "no-transform", 
-          "must-revalidate", 
-          "proxy-revalidate",
-          "max-age", 
-          "s-maxage" }));
-
+public class CacheControlParser implements Iterable<CacheControlParser.Directive> {
+  
+  public enum Directive {
+    MAXAGE, MAXSTALE, MINFRESH, NOCACHE, NOSTORE, NOTRANSFORM, ONLYIFCACHED,
+    MUSTREVALIDATE, PRIVATE, PROXYREVALIDATE, PUBLIC, SMAXAGE, UNKNOWN;
+    
+    public static Directive select(String d) {
+      try {
+        d = d.toUpperCase().replace("-", "");
+        return Directive.valueOf(d);
+      } catch (Exception e) {}
+      return UNKNOWN;
+    }
+    
+  }
+  
   private static final String REGEX = 
     "\\s*([\\w\\-]+)\\s*(=)?\\s*(\\d+|\\\"([^\"\\\\]*(\\\\.[^\"\\\\]*)*)+\\\")?\\s*";
 
   private static final Pattern pattern = Pattern.compile(REGEX);
 
-  private HashMap<String, String> values = new HashMap<String, String>();
+  private HashMap<Directive, String> values = new HashMap<Directive, String>();
 
   public CacheControlParser(String value) {
     Matcher matcher = pattern.matcher(value);
     while (matcher.find()) {
-      String directive = matcher.group(1);
-      if (isDirective(directive)) {
+      String d = matcher.group(1);
+      Directive directive = Directive.select(d);
+      if (directive != Directive.UNKNOWN) {
         values.put(directive, matcher.group(3));
       }
     }
   }
 
-  private boolean isDirective(String directive) {
-    return cacheRequestDirectives.contains(directive) || 
-           cacheResponseDirectives.contains(directive);
-  }
-
-  public Map<String,String> getValues() {
+  public Map<Directive,String> getValues() {
     return values;
   }
 
-  public String getValue(String directive) {
+  public String getValue(Directive directive) {
     return values.get(directive);
   }
   
-  public Iterator<String> iterator() {
+  public Iterator<Directive> iterator() {
     return values.keySet().iterator();
   }
   
+  public String[] getValues(Directive directive) {
+    String value = getValue(directive);
+    if (value != null) {
+      return splitAndTrim(value, ",", true);
+    }
+    return null;
+  }
+  
+  private static String unquote(String s) {
+    if (s == null || s.length() == 0) return s;
+    if (s.startsWith("\"")) s = s.substring(1);
+    if (s.endsWith("\"")) s = s.substring(0, s.length() - 1);
+    return s;
+  }
+    
+  private static String[] splitAndTrim(String value, String delim, boolean unquote) {
+    String[] headers = (unquote) ? unquote(value).split(delim) : value.split(delim);
+    for (int n = 0; n < headers.length; n++) {
+      headers[n] = headers[n].trim();
+    }
+    return headers;
+  }
+
 }

Modified: incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/CacheControlUtil.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/CacheControlUtil.java?view=diff&rev=441309&r1=441308&r2=441309
==============================================================================
--- incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/CacheControlUtil.java
(original)
+++ incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/CacheControlUtil.java
Thu Sep  7 17:47:30 2006
@@ -58,21 +58,16 @@
       request.setMaxAge(-1);
       request.setMaxStale(-1);
       request.setMinFresh(-1);
-      for (String directive : parser) {
-        if (directive.equalsIgnoreCase("no-cache")) {
-          request.setNoCache(true);
-        } else if (directive.equalsIgnoreCase("no-store"))
-          request.setNoStore(true);
-        else if (directive.equalsIgnoreCase("no-transform"))
-          request.setNoTransform(true);
-        else if (directive.equalsIgnoreCase("only-if-cached"))
-          request.setOnlyIfCached(true);
-        else if (directive.equalsIgnoreCase("max-age"))
-          request.setMaxAge(value(parser.getValue(directive)));
-        else if (directive.equalsIgnoreCase("max-stale"))
-          request.setMaxStale(value(parser.getValue(directive)));
-        else if (directive.equalsIgnoreCase("min-fresh"))
-          request.setMinFresh(value(parser.getValue(directive)));
+      for (CacheControlParser.Directive directive : parser) {
+        switch(directive) {
+          case NOCACHE:      request.setNoCache(true); break;
+          case NOSTORE:      request.setNoStore(true); break;
+          case NOTRANSFORM:  request.setNoTransform(true); break;
+          case ONLYIFCACHED: request.setOnlyIfCached(true); break;
+          case MAXAGE:       request.setMaxAge(value(parser.getValue(directive))); break;
+          case MAXSTALE:     request.setMaxStale(value(parser.getValue(directive))); break;
+          case MINFRESH:     request.setMinFresh(value(parser.getValue(directive))); break;
+        }
       }
   }
   
@@ -88,46 +83,29 @@
       response.setPrivate(false);
       response.setPublic(false);
       response.setMaxAge(-1);
-      for (String directive : parser) {
-        if (directive.equalsIgnoreCase("no-cache")) {
-          response.setNoCache(true);
-          String value = parser.getValue(directive);
-          if (value != null) {
-            String[] headers = splitAndTrim(value, ",", true);
-            response.setNoCacheHeaders(headers);
-          }
-        } else if (directive.equalsIgnoreCase("no-store"))
-          response.setNoStore(true);
-        else if (directive.equalsIgnoreCase("no-transform"))
-          response.setNoTransform(true);
-        else if (directive.equalsIgnoreCase("must-revalidate"))
-          response.setMustRevalidate(true);
-        else if (directive.equalsIgnoreCase("public"))
-          response.setPublic(true);
-        else if (directive.equalsIgnoreCase("private")) {
-          response.setPrivate(true);
-          String value = parser.getValue(directive);
-          if (value != null) {
-            String[] headers = splitAndTrim(value, ",", true);
-            response.setPrivateHeaders(headers);
-          }
-        } else if (directive.equalsIgnoreCase("max-age"))
-          response.setMaxAge(value(parser.getValue(directive)));
+      for (CacheControlParser.Directive directive : parser) {
+        switch(directive) {
+          case NOCACHE:
+            response.setNoCache(true);
+            response.setNoCacheHeaders(parser.getValues(directive));
+            break;
+          case NOSTORE:
+            response.setNoStore(true); break;
+          case NOTRANSFORM:
+            response.setNoTransform(true); break;
+          case MUSTREVALIDATE:
+            response.setMustRevalidate(true); break;
+          case PUBLIC:
+            response.setPublic(true); break;
+          case PRIVATE:
+            response.setPrivate(true);
+            response.setPrivateHeaders(parser.getValues(directive));
+            break;
+          case MAXAGE:
+            response.setMaxAge(value(parser.getValue(directive)));
+            break;
+        }
       }
   }
-  
-  private static String unquote(String s) {
-    if (s == null || s.length() == 0) return s;
-    if (s.startsWith("\"")) s = s.substring(1);
-    if (s.endsWith("\"")) s = s.substring(0, s.length() - 1);
-    return s;
-  }
-  
-  public static String[] splitAndTrim(String value, String delim, boolean unquote) {
-    String[] headers = (unquote) ? unquote(value).split(delim) : value.split(delim);
-    for (int n = 0; n < headers.length; n++) {
-      headers[n] = headers[n].trim();
-    }
-    return headers;
-  }
+
 }



Mime
View raw message