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;
  }
 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());
 }
 private void validateCookie(Cookie cookie) {
   if ("cookie1".equals(cookie.getName())) {
     assertEquals("value1", cookie.getValue());
     assertEquals("/", cookie.getPath());
     assertEquals("localhost", cookie.getDomain());
     validateDate(cookie.getExpiryDate());
     assertTrue(cookie.getSecure());
   } else {
     assertEquals("cookie2", cookie.getName());
     assertEquals("value2", cookie.getValue());
     assertFalse(cookie.getSecure());
   }
 }
示例#4
0
 private String getHttpCookie() {
   StringBuilder strHeader = new StringBuilder();
   Cookie[] cookies = httpClient.getState().getCookies();
   for (Cookie cookie : cookies) {
     String domain = cookie.getDomain();
     String path = cookie.getPath();
     String name = cookie.getName();
     String value = cookie.getValue();
     Date expired = cookie.getExpiryDate();
     boolean isSecure = cookie.getSecure();
     strHeader.append("domain=" + domain + ";");
     strHeader.append("path=" + path + ";");
     strHeader.append(name + "=" + value + ";");
     if (expired != null) {
       strHeader.append("expired=" + expired.toGMTString() + ";");
     }
     strHeader.append("isSecure=" + isSecure + "/n");
   }
   return strHeader.toString();
 }
    /** Match cookie domain attribute. */
    public boolean match(final Cookie cookie, final CookieOrigin origin) {
      if (cookie == null) {
        throw new IllegalArgumentException("Cookie may not be null");
      }
      if (origin == null) {
        throw new IllegalArgumentException("Cookie origin may not be null");
      }
      String host = origin.getHost().toLowerCase();
      String cookieDomain = cookie.getDomain();

      // The effective host name MUST domain-match the Domain
      // attribute of the cookie.
      if (!domainMatch(host, cookieDomain)) {
        return false;
      }
      // effective host name minus domain must not contain any dots
      String effectiveHostWithoutDomain = host.substring(0, host.length() - cookieDomain.length());
      if (effectiveHostWithoutDomain.indexOf('.') != -1) {
        return false;
      }
      return true;
    }
    /** Validate cookie domain attribute. */
    public void validate(final Cookie cookie, final CookieOrigin origin)
        throws MalformedCookieException {
      if (cookie == null) {
        throw new IllegalArgumentException("Cookie may not be null");
      }
      if (origin == null) {
        throw new IllegalArgumentException("Cookie origin may not be null");
      }
      String host = origin.getHost().toLowerCase();
      if (cookie.getDomain() == null) {
        throw new MalformedCookieException("Invalid cookie state: " + "domain not specified");
      }
      String cookieDomain = cookie.getDomain().toLowerCase();

      if (cookie.isDomainAttributeSpecified()) {
        // Domain attribute must start with a dot
        if (!cookieDomain.startsWith(".")) {
          throw new MalformedCookieException(
              "Domain attribute \""
                  + cookie.getDomain()
                  + "\" violates RFC 2109: domain must start with a dot");
        }

        // Domain attribute must contain atleast one embedded dot,
        // or the value must be equal to .local.
        int dotIndex = cookieDomain.indexOf('.', 1);
        if (((dotIndex < 0) || (dotIndex == cookieDomain.length() - 1))
            && (!cookieDomain.equals(".local"))) {
          throw new MalformedCookieException(
              "Domain attribute \""
                  + cookie.getDomain()
                  + "\" violates RFC 2965: the value contains no embedded dots "
                  + "and the value is not .local");
        }

        // The effective host name must domain-match domain attribute.
        if (!domainMatch(host, cookieDomain)) {
          throw new MalformedCookieException(
              "Domain attribute \""
                  + cookie.getDomain()
                  + "\" violates RFC 2965: effective host name does not "
                  + "domain-match domain attribute.");
        }

        // effective host name minus domain must not contain any dots
        String effectiveHostWithoutDomain =
            host.substring(0, host.length() - cookieDomain.length());
        if (effectiveHostWithoutDomain.indexOf('.') != -1) {
          throw new MalformedCookieException(
              "Domain attribute \""
                  + cookie.getDomain()
                  + "\" violates RFC 2965: "
                  + "effective host minus domain may not contain any dots");
        }
      } else {
        // Domain was not specified in header. In this case, domain must
        // string match request host (case-insensitive).
        if (!cookie.getDomain().equals(host)) {
          throw new MalformedCookieException(
              "Illegal domain attribute: \""
                  + cookie.getDomain()
                  + "\"."
                  + "Domain of origin: \""
                  + host
                  + "\"");
        }
      }
    }