protected Cookie toServletCookie(org.apache.commons.httpclient.Cookie commonsCookie) {

    Cookie cookie = new Cookie(commonsCookie.getName(), commonsCookie.getValue());

    String domain = commonsCookie.getDomain();

    if (Validator.isNotNull(domain)) {
      cookie.setDomain(domain);
    }

    Date expiryDate = commonsCookie.getExpiryDate();

    if (expiryDate != null) {
      int maxAge = (int) (expiryDate.getTime() - System.currentTimeMillis());

      maxAge = maxAge / 1000;

      if (maxAge > -1) {
        cookie.setMaxAge(maxAge);
      }
    }

    String path = commonsCookie.getPath();

    if (Validator.isNotNull(path)) {
      cookie.setPath(path);
    }

    cookie.setSecure(commonsCookie.getSecure());
    cookie.setVersion(commonsCookie.getVersion());

    return cookie;
  }
Exemplo n.º 2
0
  /**
   * Create a RFC 2965 compliant <tt>"Cookie"</tt> header value containing all {@link
   * org.apache.commons.httpclient.Cookie}s suitable for sending in a <tt>"Cookie"</tt> header
   *
   * @param cookies an array of {@link org.apache.commons.httpclient.Cookie}s to be formatted
   * @return a string suitable for sending in a Cookie header.
   */
  public String formatCookies(final Cookie[] cookies) {
    LOG.trace("enter RFC2965Spec.formatCookieHeader(Cookie[])");

    if (cookies == null) {
      throw new IllegalArgumentException("Cookies may not be null");
    }
    // check if cookies array contains a set-cookie (old style) cookie
    boolean hasOldStyleCookie = false;
    int version = -1;
    for (int i = 0; i < cookies.length; i++) {
      Cookie cookie = cookies[i];
      if (!(cookie instanceof Cookie2)) {
        hasOldStyleCookie = true;
        break;
      }
      if (cookie.getVersion() > version) {
        version = cookie.getVersion();
      }
    }
    if (version < 0) {
      version = 0;
    }
    if (hasOldStyleCookie || version < 1) {
      // delegate old-style cookie formatting to rfc2109Spec
      return this.rfc2109.formatCookies(cookies);
    }
    // Arrange cookies by path
    Arrays.sort(cookies, PATH_COMPOARATOR);

    final StringBuffer buffer = new StringBuffer();
    // format cookie version
    this.formatter.format(buffer, new NameValuePair("$Version", Integer.toString(version)));
    for (int i = 0; i < cookies.length; i++) {
      buffer.append("; ");
      Cookie2 cookie = (Cookie2) cookies[i];
      // format cookie attributes
      doFormatCookie2(cookie, buffer);
    }
    return buffer.toString();
  }
 private void logCookie(Cookie cookie) {
   Log_OC.d(TAG, "Cookie name: " + cookie.getName());
   Log_OC.d(TAG, "       value: " + cookie.getValue());
   Log_OC.d(TAG, "       domain: " + cookie.getDomain());
   Log_OC.d(TAG, "       path: " + cookie.getPath());
   Log_OC.d(TAG, "       version: " + cookie.getVersion());
   Log_OC.d(
       TAG,
       "       expiryDate: "
           + (cookie.getExpiryDate() != null ? cookie.getExpiryDate().toString() : "--"));
   Log_OC.d(TAG, "       comment: " + cookie.getComment());
   Log_OC.d(TAG, "       secure: " + cookie.getSecure());
 }