[Bug 418732] Swap PathMap with IPAddressMap, also fix PathMap initialization bug

Signed-off-by: Constantine Linnick <theaspect@gmail.com>
This commit is contained in:
Constantine Linnick 2013-10-05 16:10:43 +07:00 committed by Gerrit Code Review @ Eclipse.org
parent b31466839a
commit b4052a2b53
3 changed files with 27 additions and 22 deletions

View File

@ -134,6 +134,7 @@ public class PathMap<O> extends HashMap<String,O>
MappedEntry<O> entry = new MappedEntry<>("",object); MappedEntry<O> entry = new MappedEntry<>("",object);
entry.setMapped(""); entry.setMapped("");
_exactMap.put("", entry); _exactMap.put("", entry);
_prefixDefault = entry;
return super.put("", object); return super.put("", object);
} }
@ -381,6 +382,7 @@ public class PathMap<O> extends HashMap<String,O>
_suffixMap=new ArrayTernaryTrie<>(false); _suffixMap=new ArrayTernaryTrie<>(false);
_default=null; _default=null;
_defaultSingletonList=null; _defaultSingletonList=null;
_prefixDefault=null;
super.clear(); super.clear();
} }

View File

@ -101,9 +101,9 @@ import org.eclipse.jetty.util.log.Logger;
public class IPAccessHandler extends HandlerWrapper public class IPAccessHandler extends HandlerWrapper
{ {
private static final Logger LOG = Log.getLogger(IPAccessHandler.class); private static final Logger LOG = Log.getLogger(IPAccessHandler.class);
// true means nodefault match
IPAddressMap<PathMap> _white = new IPAddressMap<PathMap>(); PathMap<IPAddressMap<Boolean>> _white = new PathMap<IPAddressMap<Boolean>>(true);
IPAddressMap<PathMap> _black = new IPAddressMap<PathMap>(); PathMap<IPAddressMap<Boolean>> _black = new PathMap<IPAddressMap<Boolean>>(true);
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
@ -213,7 +213,7 @@ public class IPAccessHandler extends HandlerWrapper
* @param entry new entry * @param entry new entry
* @param patternMap target address pattern map * @param patternMap target address pattern map
*/ */
protected void add(String entry, IPAddressMap<PathMap> patternMap) protected void add(String entry, PathMap<IPAddressMap<Boolean>> patternMap)
{ {
if (entry != null && entry.length() > 0) if (entry != null && entry.length() > 0)
{ {
@ -237,14 +237,15 @@ public class IPAccessHandler extends HandlerWrapper
if (path!=null && (path.startsWith("|") || path.startsWith("/*."))) if (path!=null && (path.startsWith("|") || path.startsWith("/*.")))
path=path.substring(1); path=path.substring(1);
PathMap pathMap = patternMap.get(addr); IPAddressMap<Boolean> addrMap = patternMap.get(path);
if (pathMap == null) if (addrMap == null)
{ {
pathMap = new PathMap(true); addrMap = new IPAddressMap<Boolean>();
patternMap.put(addr,pathMap); patternMap.put(path,addrMap);
} }
if (path != null && !"".equals(path)) if (addr != null && !"".equals(addr))
pathMap.put(path,path); // MUST NOT BE null
addrMap.put(addr, true);
if (deprecated) if (deprecated)
LOG.debug(toString() +" - deprecated specification syntax: "+entry); LOG.debug(toString() +" - deprecated specification syntax: "+entry);
@ -259,7 +260,7 @@ public class IPAccessHandler extends HandlerWrapper
* @param entries new entries * @param entries new entries
* @param patternMap target address pattern map * @param patternMap target address pattern map
*/ */
protected void set(String[] entries, IPAddressMap<PathMap> patternMap) protected void set(String[] entries, PathMap<IPAddressMap<Boolean>> patternMap)
{ {
patternMap.clear(); patternMap.clear();
@ -286,16 +287,15 @@ public class IPAccessHandler extends HandlerWrapper
if (_white.size()>0) if (_white.size()>0)
{ {
boolean match = false; boolean match = false;
Object whiteObj = _white.getLazyMatches(path);
Object whiteObj = _white.getLazyMatches(addr);
if (whiteObj != null) if (whiteObj != null)
{ {
List whiteList = (whiteObj instanceof List) ? (List)whiteObj : Collections.singletonList(whiteObj); List whiteList = (whiteObj instanceof List) ? (List)whiteObj : Collections.singletonList(whiteObj);
for (Object entry: whiteList) for (Object entry: whiteList)
{ {
PathMap pathMap = ((Map.Entry<String,PathMap>)entry).getValue(); IPAddressMap<Boolean> addrMap = ((Map.Entry<String,IPAddressMap<Boolean>>)entry).getValue();
if (match = (pathMap!=null && (pathMap.size()==0 || pathMap.match(path)!=null))) if (match = (addrMap!=null && (addrMap.size()==0 || addrMap.match(addr)!=null)))
break; break;
} }
} }
@ -306,15 +306,15 @@ public class IPAccessHandler extends HandlerWrapper
if (_black.size() > 0) if (_black.size() > 0)
{ {
Object blackObj = _black.getLazyMatches(addr); Object blackObj = _black.getLazyMatches(path);
if (blackObj != null) if (blackObj != null)
{ {
List blackList = (blackObj instanceof List) ? (List)blackObj : Collections.singletonList(blackObj); List blackList = (blackObj instanceof List) ? (List)blackObj : Collections.singletonList(blackObj);
for (Object entry: blackList) for (Object entry: blackList)
{ {
PathMap pathMap = ((Map.Entry<String,PathMap>)entry).getValue(); IPAddressMap<Boolean> addrMap = ((Map.Entry<String,IPAddressMap<Boolean>>)entry).getValue();
if (pathMap!=null && (pathMap.size()==0 || pathMap.match(path)!=null)) if (addrMap!=null && (addrMap.size()==0 || addrMap.match(addr)!=null))
return false; return false;
} }
} }
@ -348,11 +348,11 @@ public class IPAccessHandler extends HandlerWrapper
* @param buf buffer * @param buf buffer
* @param patternMap pattern map to dump * @param patternMap pattern map to dump
*/ */
protected void dump(StringBuilder buf, IPAddressMap<PathMap> patternMap) protected void dump(StringBuilder buf, PathMap<IPAddressMap<Boolean>> patternMap)
{ {
for (String addr: patternMap.keySet()) for (String path: patternMap.keySet())
{ {
for (Object path: ((PathMap)patternMap.get(addr)).values()) for (String addr: patternMap.get(path).keySet())
{ {
buf.append("# "); buf.append("# ");
buf.append(addr); buf.append(addr);

View File

@ -124,7 +124,10 @@ public class IPAccessHandlerTest
output.flush(); output.flush();
Response response = readResponse(input); Response response = readResponse(input);
assertEquals(_code, response.getCode()); Object[] params = new Object[]{
"Request WBHUC", _white, _black, _host, _uri, _code,
"Response", response.getCode()};
assertEquals(Arrays.deepToString(params), _code, response.getCode());
} }
finally finally
{ {