1 /*
   2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.net.www.protocol.http;
  27 
  28 import java.io.IOException;
  29 import java.net.URL;
  30 import java.util.Hashtable;
  31 import java.util.LinkedList;
  32 import java.util.ListIterator;
  33 import java.util.Enumeration;
  34 import java.util.HashMap;
  35 
  36 /**
  37  * @author Michael McMahon
  38  */
  39 
  40 public class AuthCacheImpl implements AuthCache {
  41     HashMap hashtable;
  42 
  43     public AuthCacheImpl () {
  44         hashtable = new HashMap ();
  45     }
  46 
  47     public void setMap (HashMap map) {
  48         hashtable = map;
  49     }
  50 
  51     // put a value in map according to primary key + secondary key which
  52     // is the path field of AuthenticationInfo
  53 
  54     public synchronized void put (String pkey, AuthCacheValue value) {
  55         LinkedList list = (LinkedList) hashtable.get (pkey);
  56         String skey = value.getPath();
  57         if (list == null) {
  58             list = new LinkedList ();
  59             hashtable.put (pkey, list);
  60         }
  61         // Check if the path already exists or a super-set of it exists
  62         ListIterator iter = list.listIterator();
  63         while (iter.hasNext()) {
  64             AuthenticationInfo inf = (AuthenticationInfo)iter.next();
  65             if (inf.path == null || inf.path.startsWith (skey)) {
  66                 iter.remove ();
  67             }
  68         }
  69         iter.add (value);
  70     }
  71 
  72     // get a value from map checking both primary
  73     // and secondary (urlpath) key
  74 
  75     public synchronized AuthCacheValue get (String pkey, String skey) {
  76         AuthenticationInfo result = null;
  77         LinkedList list = (LinkedList) hashtable.get (pkey);
  78         if (list == null || list.size() == 0) {
  79             return null;
  80         }
  81         if (skey == null) {
  82             // list should contain only one element
  83             return (AuthenticationInfo)list.get (0);
  84         }
  85         ListIterator iter = list.listIterator();
  86         while (iter.hasNext()) {
  87             AuthenticationInfo inf = (AuthenticationInfo)iter.next();
  88             if (skey.startsWith (inf.path)) {
  89                 return inf;
  90             }
  91         }
  92         return null;
  93     }
  94 
  95     public synchronized void remove (String pkey, AuthCacheValue entry) {
  96         LinkedList list = (LinkedList) hashtable.get (pkey);
  97         if (list == null) {
  98             return;
  99         }
 100         if (entry == null) {
 101             list.clear();
 102             return;
 103         }
 104         ListIterator iter = list.listIterator ();
 105         while (iter.hasNext()) {
 106             AuthenticationInfo inf = (AuthenticationInfo)iter.next();
 107             if (entry.equals(inf)) {
 108                 iter.remove ();
 109             }
 110         }
 111     }
 112 }