示例#1
0
  private List<Cookie> createCookies(final HeaderElement[] elems, final CookieOrigin origin)
      throws MalformedCookieException {
    final List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
    for (final HeaderElement headerelement : elems) {
      final String name = headerelement.getName();
      final String value = headerelement.getValue();
      if (name == null || name.length() == 0) {
        throw new MalformedCookieException("Cookie name may not be empty");
      }

      final BasicClientCookie2 cookie = new BasicClientCookie2(name, value);
      cookie.setPath(getDefaultPath(origin));
      cookie.setDomain(getDefaultDomain(origin));
      cookie.setPorts(new int[] {origin.getPort()});
      // cycle through the parameters
      final NameValuePair[] attribs = headerelement.getParameters();

      // Eliminate duplicate attributes. The first occurrence takes precedence
      // See RFC2965: 3.2  Origin Server Role
      final Map<String, NameValuePair> attribmap =
          new HashMap<String, NameValuePair>(attribs.length);
      for (int j = attribs.length - 1; j >= 0; j--) {
        final NameValuePair param = attribs[j];
        attribmap.put(param.getName().toLowerCase(Locale.ENGLISH), param);
      }
      for (final Map.Entry<String, NameValuePair> entry : attribmap.entrySet()) {
        final NameValuePair attrib = entry.getValue();
        final String s = attrib.getName().toLowerCase(Locale.ENGLISH);

        cookie.setAttribute(s, attrib.getValue());

        final CookieAttributeHandler handler = findAttribHandler(s);
        if (handler != null) {
          handler.parse(cookie, attrib.getValue());
        }
      }
      cookies.add(cookie);
    }
    return cookies;
  }