home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / java / net / CookieManager.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  3.3 KB  |  121 lines

  1. package java.net;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import sun.net.www.protocol.http.InMemoryCookieStore;
  10.  
  11. public class CookieManager extends CookieHandler {
  12.    private CookiePolicy policyCallback;
  13.    private CookieStore cookieJar;
  14.  
  15.    public CookieManager() {
  16.       this((CookieStore)null, (CookiePolicy)null);
  17.    }
  18.  
  19.    public CookieManager(CookieStore var1, CookiePolicy var2) {
  20.       this.cookieJar = null;
  21.       this.policyCallback = var2 == null ? CookiePolicy.ACCEPT_ORIGINAL_SERVER : var2;
  22.       if (var1 == null) {
  23.          this.cookieJar = new InMemoryCookieStore();
  24.       } else {
  25.          this.cookieJar = var1;
  26.       }
  27.  
  28.    }
  29.  
  30.    public void setCookiePolicy(CookiePolicy var1) {
  31.       if (var1 != null) {
  32.          this.policyCallback = var1;
  33.       }
  34.  
  35.    }
  36.  
  37.    public CookieStore getCookieStore() {
  38.       return this.cookieJar;
  39.    }
  40.  
  41.    public Map<String, List<String>> get(URI var1, Map<String, List<String>> var2) throws IOException {
  42.       if (var1 != null && var2 != null) {
  43.          HashMap var3 = new HashMap();
  44.          if (this.cookieJar == null) {
  45.             return Collections.unmodifiableMap(var3);
  46.          } else {
  47.             ArrayList var4 = new ArrayList();
  48.  
  49.             for(HttpCookie var6 : this.cookieJar.get(var1)) {
  50.                if (this.pathMatches(var1.getPath(), var6.getPath())) {
  51.                   var4.add(var6);
  52.                }
  53.             }
  54.  
  55.             List var7 = this.sortByPath(var4);
  56.             var3.put("Cookie", var7);
  57.             return Collections.unmodifiableMap(var3);
  58.          }
  59.       } else {
  60.          throw new IllegalArgumentException("Argument is null");
  61.       }
  62.    }
  63.  
  64.    public void put(URI var1, Map<String, List<String>> var2) throws IOException {
  65.       if (var1 != null && var2 != null) {
  66.          if (this.cookieJar != null) {
  67.             for(String var4 : var2.keySet()) {
  68.                if (var4 != null && (var4.equalsIgnoreCase("Set-Cookie2") || var4.equalsIgnoreCase("Set-Cookie"))) {
  69.                   for(String var6 : (List)var2.get(var4)) {
  70.                      try {
  71.                         for(HttpCookie var9 : HttpCookie.parse(var6)) {
  72.                            if (this.shouldAcceptInternal(var1, var9)) {
  73.                               this.cookieJar.add(var1, var9);
  74.                            }
  75.                         }
  76.                      } catch (IllegalArgumentException var10) {
  77.                      }
  78.                   }
  79.                }
  80.             }
  81.  
  82.          }
  83.       } else {
  84.          throw new IllegalArgumentException("Argument is null");
  85.       }
  86.    }
  87.  
  88.    private boolean shouldAcceptInternal(URI var1, HttpCookie var2) {
  89.       try {
  90.          return this.policyCallback.shouldAccept(var1, var2);
  91.       } catch (Exception var4) {
  92.          return false;
  93.       }
  94.    }
  95.  
  96.    private boolean pathMatches(String var1, String var2) {
  97.       if (var1 == var2) {
  98.          return true;
  99.       } else if (var1 != null && var2 != null) {
  100.          return var1.startsWith(var2);
  101.       } else {
  102.          return false;
  103.       }
  104.    }
  105.  
  106.    private List<String> sortByPath(List<HttpCookie> var1) {
  107.       Collections.sort(var1, new CookiePathComparator());
  108.       ArrayList var2 = new ArrayList();
  109.  
  110.       for(HttpCookie var4 : var1) {
  111.          if (var1.indexOf(var4) == 0 && var4.getVersion() > 0) {
  112.             var2.add("$Version=\"1\"");
  113.          }
  114.  
  115.          var2.add(var4.toString());
  116.       }
  117.  
  118.       return var2;
  119.    }
  120. }
  121.