@Override
 public void process(HttpResponse response, HttpContext context)
     throws HttpException, IOException {
   CookieStore cookieStore = (CookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
   for (Header header : response.getAllHeaders()) {
     if (!header.getName().equalsIgnoreCase("Set-Cookie")) {
       continue;
     }
     Matcher matcher = pattern.matcher(header.getValue());
     if (!matcher.find()) {
       continue;
     }
     for (Cookie cookie : cookieStore.getCookies()) {
       if (cookie.getName().equalsIgnoreCase(matcher.group(1))) {
         if (cookie instanceof BasicClientCookie) {
           ((BasicClientCookie) cookie).setValue('"' + cookie.getValue() + '"');
         } else if (cookie instanceof BasicClientCookie2) {
           ((BasicClientCookie2) cookie).setValue('"' + cookie.getValue() + '"');
         } else {
           Log.w(TAG, "unhandled cookie implementation " + cookie.getClass().getName());
         }
         break;
       }
     }
   }
 }
Example #2
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;
  }
 @Override
 public Object clone() throws CloneNotSupportedException {
   BasicClientCookie2 clone = (BasicClientCookie2) super.clone();
   clone.ports = this.ports.clone();
   return clone;
 }