Author: olegk
Date: Wed Apr 26 02:26:13 2006
New Revision: 397155
URL: http://svn.apache.org/viewcvs?rev=397155&view=rev
Log:
Refactored the internal management of the cookie attribute handlers
Modified:
jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
Modified:
jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java?rev=397155&r1=397154&r2=397155&view=diff
==============================================================================
---
jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
(original)
+++
jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
Wed Apr 26 02:26:13 2006
@@ -58,10 +58,15 @@
private final ParameterFormatter formatter;
/**
+ * Stores the list of attribute handlers
+ */
+ private final List attribHandlerList;
+
+ /**
* Stores attribute name -> attribute handler mappings
*/
- private static Map attributeHandlerMap = null;
-
+ private final Map attribHandlerMap;
+
/**
* Default constructor
* */
@@ -69,24 +74,64 @@
super();
this.formatter = new ParameterFormatter();
this.formatter.setAlwaysUseQuotes(true);
- initializeAttributeHandlerMap();
+ this.attribHandlerMap = new HashMap(10);
+ this.attribHandlerList = new ArrayList(10);
+
+ registerAttribHandler(Cookie2.COOKIE_NAME_KEY, new
Cookie2NameAttributeHandler());
+ registerAttribHandler(Cookie2.PATH, new Cookie2PathAttributeHandler());
+ registerAttribHandler(Cookie2.DOMAIN, new
Cookie2DomainAttributeHandler());
+ registerAttribHandler(Cookie2.PORT, new Cookie2PortAttributeHandler());
+ registerAttribHandler(Cookie2.VERSION, new
Cookie2VersionAttributeHandler());
+ registerAttribHandler(Cookie2.MAXAGE, new
Cookie2MaxageAttributeHandler());
}
+ protected void registerAttribHandler(
+ final String name, final CookieAttributeHandler handler) {
+ if (name == null) {
+ throw new IllegalArgumentException("Attribute name may not be
null");
+ }
+ if (handler == null) {
+ throw new IllegalArgumentException("Attribute handler may not be
null");
+ }
+ if (!this.attribHandlerList.contains(handler)) {
+ this.attribHandlerList.add(handler);
+ }
+ this.attribHandlerMap.put(name, handler);
+ }
+
/**
- * initializes attribute name -> attribute handler mappings.
- * Called from constructor.
- */
- private void initializeAttributeHandlerMap() {
- if (attributeHandlerMap == null) {
- attributeHandlerMap = new HashMap();
- attributeHandlerMap.put(Cookie2.COOKIE_NAME_KEY, new
Cookie2NameAttributeHandler());
- attributeHandlerMap.put(Cookie2.PATH, new
Cookie2PathAttributeHandler());
- attributeHandlerMap.put(Cookie2.DOMAIN, new
Cookie2DomainAttributeHandler());
- attributeHandlerMap.put(Cookie2.PORT, new
Cookie2PortAttributeHandler());
- attributeHandlerMap.put(Cookie2.VERSION, new
Cookie2VersionAttributeHandler());
- attributeHandlerMap.put(Cookie2.MAXAGE, new
Cookie2MaxageAttributeHandler());
+ * Finds an attribute handler [EMAIL PROTECTED] CookieAttributeHandler}
for the
+ * given attribute. Returns <tt>null</tt> if no attribute handler is
+ * found for the specified attribute.
+ *
+ * @param name attribute name. e.g. Domain, Path, etc.
+ * @return an attribute handler or <tt>null</tt>
+ */
+ protected CookieAttributeHandler findAttribHandler(final String name) {
+ return (CookieAttributeHandler) this.attribHandlerMap.get(name);
+ }
+
+ /**
+ * Gets attribute handler [EMAIL PROTECTED] CookieAttributeHandler} for the
+ * given attribute.
+ *
+ * @param name attribute name. e.g. Domain, Path, etc.
+ * @throws IllegalStateException if handler not found for the
+ * specified attribute.
+ */
+ protected CookieAttributeHandler getAttribHandler(final String name) {
+ CookieAttributeHandler handler = findAttribHandler(name);
+ if (handler == null) {
+ throw new IllegalStateException("Handler not registered for " +
+ name + " attribute.");
+ } else {
+ return handler;
}
}
+
+ protected Iterator getAttribHandlerIterator() {
+ return this.attribHandlerList.iterator();
+ }
/**
* Parses the Set-Cookie2 value into an array of <tt>Cookie</tt>s.
@@ -251,11 +296,9 @@
final String paramName = attribute.getName().toLowerCase();
final String paramValue = attribute.getValue();
- try {
- CookieAttributeHandler handler = getAttributeHandler(paramName);
+ CookieAttributeHandler handler = findAttribHandler(paramName);
+ if (handler != null) {
handler.parse(cookie, paramValue);
- } catch (IllegalStateException canIgnore) {
- // handler not registered for this paramName
}
// handle other cookie attributes
@@ -303,7 +346,7 @@
return;
}
CookieSource cookiesource = new CookieSource(host, port, path,
secure);
- for (Iterator i = attributeHandlerMap.values().iterator();
i.hasNext(); ) {
+ for (Iterator i = getAttribHandlerIterator(); i.hasNext(); ) {
CookieAttributeHandler handler = (CookieAttributeHandler)
i.next();
handler.validate(cookie, cookiesource);
}
@@ -335,7 +378,7 @@
return rfc2109Spec.match(host, port, path, secure, cookie);
}
CookieSource cookiesource = new CookieSource(host, port, path,
secure);
- for (Iterator i = attributeHandlerMap.values().iterator();
i.hasNext(); ) {
+ for (Iterator i = getAttribHandlerIterator(); i.hasNext(); ) {
CookieAttributeHandler handler = (CookieAttributeHandler)
i.next();
if (!handler.match(cookie, cookiesource)) {
return false;
@@ -376,7 +419,7 @@
Cookie2 cookie = (Cookie2) cookieParam;
final StringBuffer buffer = new StringBuffer();
// format cookie version
- CookieAttributeHandler handler = getAttributeHandler(Cookie2.VERSION);
+ CookieAttributeHandler handler = getAttribHandler(Cookie2.VERSION);
handler.format(buffer, cookie);
// format cookie attributes
formatCookieAttributes(buffer, cookie);
@@ -414,7 +457,7 @@
/* format cookie2 cookies */
final StringBuffer buffer = new StringBuffer();
// format cookie version
- CookieAttributeHandler handler = getAttributeHandler(Cookie2.VERSION);
+ CookieAttributeHandler handler = getAttribHandler(Cookie2.VERSION);
handler.format(buffer, null);
for (int i = 0; i < cookies.length; i++) {
@@ -433,16 +476,16 @@
*/
private void formatCookieAttributes(final StringBuffer buffer, final
Cookie2 cookie) {
// format cookie name and value
- CookieAttributeHandler handler =
getAttributeHandler(Cookie2.COOKIE_NAME_KEY);
+ CookieAttributeHandler handler =
getAttribHandler(Cookie2.COOKIE_NAME_KEY);
handler.format(buffer, cookie);
// format domain attribute
- handler = getAttributeHandler(Cookie2.DOMAIN);
+ handler = getAttribHandler(Cookie2.DOMAIN);
handler.format(buffer, cookie);
// format path attribute
- handler = getAttributeHandler(Cookie2.PATH);
+ handler = getAttribHandler(Cookie2.PATH);
handler.format(buffer, cookie);
// format port attribute
- handler = getAttributeHandler(Cookie2.PORT);
+ handler = getAttribHandler(Cookie2.PORT);
handler.format(buffer, cookie);
}
@@ -490,26 +533,6 @@
+ "attribute: " +
e.getMessage());
}
return ports;
- }
-
- /**
- * Gets attribute handler [EMAIL PROTECTED] CookieAttributeHandler} for the
- * given attribute.
- *
- * @param name attribute name. e.g. Domain, Path, etc.
- * @throws IllegalStateException if handler not found for the
- * specified attribute.
- */
- private CookieAttributeHandler getAttributeHandler(final String name)
- throws IllegalStateException {
- CookieAttributeHandler handler =
- (CookieAttributeHandler)(attributeHandlerMap.get(name));
- if (handler == null) {
- throw new IllegalStateException("Handler not registered for " +
- name + " attribute.");
- } else {
- return handler;
- }
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]