FileDocCategorySizeDatePackage
RFC2965PortAttributeHandler.javaAPI DocAndroid 1.5 API6330Wed May 06 22:41:10 BST 2009org.apache.http.impl.cookie

RFC2965PortAttributeHandler

public class RFC2965PortAttributeHandler extends Object implements CookieAttributeHandler
"Port" cookie attribute handler for RFC 2965 cookie spec.

Fields Summary
Constructors Summary
public RFC2965PortAttributeHandler()

        super();
    
Methods Summary
public booleanmatch(org.apache.http.cookie.Cookie cookie, org.apache.http.cookie.CookieOrigin origin)
Match cookie port attribute. If the Port attribute is not specified in header, the cookie can be sent to any port. Otherwise, the request port must be in the cookie's port list.

        if (cookie == null) {
            throw new IllegalArgumentException("Cookie may not be null");
        }
        if (origin == null) {
            throw new IllegalArgumentException("Cookie origin may not be null");
        }
        int port = origin.getPort();
        if (cookie instanceof ClientCookie 
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
            if (cookie.getPorts() == null) {
                // Invalid cookie state: port not specified
                return false;
            }
            if (!portMatch(port, cookie.getPorts())) {
                return false;
            }
        }
        return true;
    
public voidparse(org.apache.http.cookie.SetCookie cookie, java.lang.String portValue)
Parse cookie port attribute.

        if (cookie == null) {
            throw new IllegalArgumentException("Cookie may not be null");
        }
        if (cookie instanceof SetCookie2) {
            SetCookie2 cookie2 = (SetCookie2) cookie;
            if (portValue != null && portValue.trim().length() > 0) {
                int[] ports = parsePortAttribute(portValue);
                cookie2.setPorts(ports);
            }
        }
    
private static int[]parsePortAttribute(java.lang.String portValue)
Parses the given Port attribute value (e.g. "8000,8001,8002") into an array of ports.

param
portValue port attribute value
return
parsed array of ports
throws
MalformedCookieException if there is a problem in parsing due to invalid portValue.

        StringTokenizer st = new StringTokenizer(portValue, ",");
        int[] ports = new int[st.countTokens()];
        try {
            int i = 0;
            while(st.hasMoreTokens()) {
                ports[i] = Integer.parseInt(st.nextToken().trim());
                if (ports[i] < 0) {
                  throw new MalformedCookieException ("Invalid Port attribute.");
                }
                ++i;
            }
        } catch (NumberFormatException e) {
            throw new MalformedCookieException ("Invalid Port "
                                                + "attribute: " + e.getMessage());
        }
        return ports;
    
private static booleanportMatch(int port, int[] ports)
Returns true if the given port exists in the given ports list.

param
port port of host where cookie was received from or being sent to.
param
ports port list
return
true returns true if the given port exists in the given ports list; false otherwise.

        boolean portInList = false;
        for (int i = 0, len = ports.length; i < len; i++) {
            if (port == ports[i]) {
                portInList = true;
                break;
            }
        }
        return portInList;
    
public voidvalidate(org.apache.http.cookie.Cookie cookie, org.apache.http.cookie.CookieOrigin origin)
Validate cookie port attribute. If the Port attribute was specified in header, the request port must be in cookie's port list.

        if (cookie == null) {
            throw new IllegalArgumentException("Cookie may not be null");
        }
        if (origin == null) {
            throw new IllegalArgumentException("Cookie origin may not be null");
        }
        int port = origin.getPort();
        if (cookie instanceof ClientCookie 
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
            if (!portMatch(port, cookie.getPorts())) {
                throw new MalformedCookieException(
                        "Port attribute violates RFC 2965: "
                        + "Request port not found in cookie's port list.");
            }
        }