Java Code Examples for javax.servlet.http.Cookie#getComment()

The following examples show how to use javax.servlet.http.Cookie#getComment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Response.java    From onedev with MIT License 6 votes vote down vote up
@Override
public void addCookie(Cookie cookie)
{
    if (StringUtil.isBlank(cookie.getName()))
        throw new IllegalArgumentException("Cookie.name cannot be blank/null");

    String comment = cookie.getComment();
    // HttpOnly was supported as a comment in cookie flags before the java.net.HttpCookie implementation so need to check that
    boolean httpOnly = cookie.isHttpOnly() || HttpCookie.isHttpOnlyInComment(comment);
    SameSite sameSite = HttpCookie.getSameSiteFromComment(comment);
    comment = HttpCookie.getCommentWithoutAttributes(comment);

    addCookie(new HttpCookie(
        cookie.getName(),
        cookie.getValue(),
        cookie.getDomain(),
        cookie.getPath(),
        (long)cookie.getMaxAge(),
        httpOnly,
        cookie.getSecure(),
        comment,
        cookie.getVersion(),
        sameSite));
}
 
Example 2
Source File: SimpleCookieManager.java    From lastaflute with Apache License 2.0 6 votes vote down vote up
protected Cookie createSnapshotCookie(Cookie src) {
    // not use close() to avoid dependency to ServletContainer
    final Cookie snapshot = new Cookie(src.getName(), src.getValue());
    snapshot.setPath(src.getPath());
    snapshot.setMaxAge(src.getMaxAge());
    final String domain = src.getDomain();
    if (domain != null) { // the setter has filter process
        snapshot.setDomain(domain);
    }
    snapshot.setSecure(src.getSecure());
    final String comment = src.getComment();
    if (comment != null) { // just in case
        snapshot.setComment(comment);
    }
    snapshot.setVersion(src.getVersion());
    snapshot.setHttpOnly(src.isHttpOnly());
    return snapshot;
}
 
Example 3
Source File: CookieUtil.java    From khan-session with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Create Cookie header
 *
 * @param cookie
 * @param isHttpOnly
 * @return
 */
public static String createCookieHeader(Cookie cookie, boolean isHttpOnly) {
    StringBuilder sb = new StringBuilder();
    sb = sb.append(cookie.getName()).append("=").append(cookie.getValue());

    if (cookie.getDomain() != null && !cookie.getDomain().equals("") ) {
        sb.append(";Domain=").append(cookie.getDomain());
    }
    if (cookie.getPath() != null && !cookie.getPath().equals("")) {
        sb.append(";Path=").append(cookie.getPath());
    }
    if (cookie.getComment() != null && !cookie.getComment().equals("")) {
        sb.append(";Comment=").append(cookie.getComment());
    }
    if (cookie.getMaxAge() > -1) {
        sb.append(";Max-Age=").append(cookie.getMaxAge());
    }
    if (cookie.getSecure()) {
        sb.append(";Secure");
    }
    if (isHttpOnly) {
        sb.append(";HttpOnly");
    }

    return sb.toString();
}
 
Example 4
Source File: CookieSerializer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SuppressWarnings("RedundantIfStatement")
static boolean equals(final Cookie thisCookie, final Cookie thatCookie) {

   if (thisCookie.getMaxAge() != thatCookie.getMaxAge()) {
      return false;
   }
   if (thisCookie.getSecure() != thatCookie.getSecure()) {
      return false;
   }
   if (thisCookie.getVersion() != thatCookie.getVersion()) {
      return false;
   }
   if (thisCookie.getName() != null ? !thisCookie.getName().equals(
           thatCookie.getName()) : thatCookie.getName() != null) {
      return false;
   }
   if (thisCookie.getValue() != null ? !thisCookie.getValue().equals(
           thatCookie.getValue()) : thatCookie.getValue() != null) {
      return false;
   }
   if (thisCookie.getComment() != null ? !thisCookie.getComment().equals(
           thatCookie.getComment()) : thatCookie.getComment() != null) {
      return false;
   }
   if (thisCookie.getDomain() != null ? !thisCookie.getDomain().equals(
           thatCookie.getDomain()) : thatCookie.getDomain() != null) {
      return false;
   }
   if (thisCookie.getPath() != null ? !thisCookie.getPath().equals(
           thatCookie.getPath()) : thatCookie.getPath() != null) {
      return false;
   }
   return true;
}
 
Example 5
Source File: ResponseImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void setCookieHeader(Cookie cookie)
{
  if (cookie == null) {
    return;
  }

  final StringBuffer header = new StringBuffer(32);
  String attrValue;
  int maxAge;
  header.append(cookie.getName() + "=" + cookie.getValue());
  if ((attrValue = cookie.getComment()) != null) {
    header.append(";Comment=" + attrValue);
  }
  if ((attrValue = cookie.getDomain()) != null) {
    header.append(";Domain=" + attrValue);
  }
  if ((maxAge = cookie.getMaxAge()) != -1) {
    if (maxAge > 0) {
      appendCookieExpires(header, maxAge);
    }
    header.append(";Max-Age=" + maxAge);
  }
  if ((attrValue = cookie.getPath()) != null) {
    header.append(";Path=" + attrValue);
  } else {
    header.append(";Path=/");
  }
  if (cookie.getSecure()) {
    header.append(";Secure");
  }
  header.append(";Version=" + cookie.getVersion());

  setHeader("Set-Cookie", header.toString());
}
 
Example 6
Source File: LegacyCookieProcessor.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public String generateHeader(Cookie cookie) {
    /*
     * The spec allows some latitude on when to send the version attribute
     * with a Set-Cookie header. To be nice to clients, we'll make sure the
     * version attribute is first. That means checking the various things
     * that can cause us to switch to a v1 cookie first.
     *
     * Note that by checking for tokens we will also throw an exception if a
     * control character is encountered.
     */
    int version = cookie.getVersion();
    String value = cookie.getValue();
    String path = cookie.getPath();
    String domain = cookie.getDomain();
    String comment = cookie.getComment();

    if (version == 0) {
        // Check for the things that require a v1 cookie
        if (needsQuotes(value, 0) || comment != null || needsQuotes(path, 0) || needsQuotes(domain, 0)) {
            version = 1;
        }
    }

    // Now build the cookie header
    StringBuffer buf = new StringBuffer(); // can't use StringBuilder due to DateFormat

    // Just use the name supplied in the Cookie
    buf.append(cookie.getName());
    buf.append("=");

    // Value
    maybeQuote(buf, value, version);

    // Add version 1 specific information
    if (version == 1) {
        // Version=1 ... required
        buf.append ("; Version=1");

        // Comment=comment
        if (comment != null) {
            buf.append ("; Comment=");
            maybeQuote(buf, comment, version);
        }
    }

    // Add domain information, if present
    if (domain != null) {
        buf.append("; Domain=");
        maybeQuote(buf, domain, version);
    }

    // Max-Age=secs ... or use old "Expires" format
    int maxAge = cookie.getMaxAge();
    if (maxAge >= 0) {
        if (version > 0) {
            buf.append ("; Max-Age=");
            buf.append (maxAge);
        }
        // IE6, IE7 and possibly other browsers don't understand Max-Age.
        // They do understand Expires, even with V1 cookies!
        if (version == 0 || getAlwaysAddExpires()) {
            // Wdy, DD-Mon-YY HH:MM:SS GMT ( Expires Netscape format )
            buf.append ("; Expires=");
            // To expire immediately we need to set the time in past
            if (maxAge == 0) {
                buf.append( ANCIENT_DATE );
            } else {
                COOKIE_DATE_FORMAT.get().format(
                        new Date(System.currentTimeMillis() + maxAge * 1000L),
                        buf,
                        new FieldPosition(0));
            }
        }
    }

    // Path=path
    if (path!=null) {
        buf.append ("; Path=");
        maybeQuote(buf, path, version);
    }

    // Secure
    if (cookie.getSecure()) {
      buf.append ("; Secure");
    }

    // HttpOnly
    if (cookie.isHttpOnly()) {
        buf.append("; HttpOnly");
    }

    SameSiteCookies sameSiteCookiesValue = getSameSiteCookies();

    if (!sameSiteCookiesValue.equals(SameSiteCookies.UNSET)) {
        buf.append("; SameSite=");
        buf.append(sameSiteCookiesValue.getValue());
    }

    return buf.toString();
}
 
Example 7
Source File: CrossSubdomainSessionValve.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
protected void replaceCookie(Request request, Response response, Cookie cookie) {

        Delegator delegator = (Delegator) request.getAttribute("delegator");
        // copy the existing session cookie, but use a different domain (only if domain is valid)
        String cookieDomain = null;
        cookieDomain = EntityUtilProperties.getPropertyValue("url", "cookie.domain", "", delegator);

        if (UtilValidate.isEmpty(cookieDomain)) {
            String serverName = request.getServerName();
            String[] domainArray = serverName.split("\\.");
            // check that the domain isn't an IP address
            if (domainArray.length == 4) {
                boolean isIpAddress = true;
                for (String domainSection : domainArray) {
                    if (!UtilValidate.isIntegerInRange(domainSection, 0, 255)) {
                        isIpAddress = false;
                        break;
                    }
                }
                if (isIpAddress) {
                    return;
                }
            }
            if (domainArray.length > 2) {
                cookieDomain = "." + domainArray[domainArray.length - 2] + "." + domainArray[domainArray.length - 1];
            }
        }


        if (UtilValidate.isNotEmpty(cookieDomain)) {
            Cookie newCookie = new Cookie(cookie.getName(), cookie.getValue());
            if (cookie.getPath() != null) {
                newCookie.setPath(cookie.getPath());
            }
            newCookie.setDomain(cookieDomain);
            newCookie.setMaxAge(cookie.getMaxAge());
            newCookie.setVersion(cookie.getVersion());
            if (cookie.getComment() != null) {
                newCookie.setComment(cookie.getComment());
            }
            newCookie.setSecure(cookie.getSecure());
            newCookie.setHttpOnly(cookie.isHttpOnly());

            // if the response has already been committed, our replacement strategy will have no effect
            if (response.isCommitted()) {
                Debug.logError("CrossSubdomainSessionValve: response was already committed!", module);
            }

            // find the Set-Cookie header for the existing cookie and replace its value with new cookie
            MimeHeaders mimeHeaders = request.getCoyoteRequest().getMimeHeaders();
            for (int i = 0, size = mimeHeaders.size(); i < size; i++) {
                if (mimeHeaders.getName(i).equals("Set-Cookie")) {
                    MessageBytes value = mimeHeaders.getValue(i);
                    if (value.indexOf(cookie.getName()) >= 0) {
                        String newCookieValue = request.getContext().getCookieProcessor().generateHeader(newCookie);
                        if (Debug.verboseOn()) Debug.logVerbose("CrossSubdomainSessionValve: old Set-Cookie value: " + value.toString(), module);
                        if (Debug.verboseOn()) Debug.logVerbose("CrossSubdomainSessionValve: new Set-Cookie value: " + newCookieValue, module);
                        value.setString(newCookieValue);
                    }
                }
            }
        }
    }
 
Example 8
Source File: RequestUtil.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Encode a cookie as per RFC 2109. The resulting string can be used as the value for a <code>Set-Cookie</code> header.
 * 
 * @param cookie
 *          The cookie to encode.
 * @return A string following RFC 2109.
 */
public static String encodeCookie(Cookie cookie) {

	StringBuilder buf = new StringBuilder(cookie.getName());
	buf.append("=");
	buf.append(cookie.getValue());

	if (cookie.getComment() != null) {
		buf.append("; Comment=\"");
		buf.append(cookie.getComment());
		buf.append("\"");
	}

	if (cookie.getDomain() != null) {
		buf.append("; Domain=\"");
		buf.append(cookie.getDomain());
		buf.append("\"");
	}

	long age = cookie.getMaxAge();
	if (cookie.getMaxAge() >= 0) {
		buf.append("; Max-Age=\"");
		buf.append(age);
		buf.append("\"");
	}

	if (cookie.getPath() != null) {
		buf.append("; Path=\"");
		buf.append(cookie.getPath());
		buf.append("\"");
	}

	if (cookie.getSecure()) {
		buf.append("; Secure");
	}

	if (cookie.getVersion() > 0) {
		buf.append("; Version=\"");
		buf.append(cookie.getVersion());
		buf.append("\"");
	}

	return (buf.toString());
}
 
Example 9
Source File: RequestUtil.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Encode a cookie as per RFC 2109. The resulting string can be used as the value for a <code>Set-Cookie</code> header.
 * 
 * @param cookie
 *            The cookie to encode.
 * @return A string following RFC 2109.
 */
public static String encodeCookie(Cookie cookie) {

    StringBuilder buf = new StringBuilder(cookie.getName());
    buf.append("=");
    buf.append(cookie.getValue());

    if (cookie.getComment() != null) {
        buf.append("; Comment=\"");
        buf.append(cookie.getComment());
        buf.append("\"");
    }

    if (cookie.getDomain() != null) {
        buf.append("; Domain=\"");
        buf.append(cookie.getDomain());
        buf.append("\"");
    }

    if (cookie.getMaxAge() >= 0) {
        buf.append("; Max-Age=\"");
        buf.append(cookie.getMaxAge());
        buf.append("\"");
    }

    if (cookie.getPath() != null) {
        buf.append("; Path=\"");
        buf.append(cookie.getPath());
        buf.append("\"");
    }

    if (cookie.getSecure()) {
        buf.append("; Secure");
    }

    if (cookie.getVersion() > 0) {
        buf.append("; Version=\"");
        buf.append(cookie.getVersion());
        buf.append("\"");
    }

    return (buf.toString());
}
 
Example 10
Source File: RequestUtil.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Encode a cookie as per RFC 2109. The resulting string can be used as the value for a <code>Set-Cookie</code> header.
 * 
 * @param cookie
 *            The cookie to encode.
 * @return A string following RFC 2109.
 */
public static String encodeCookie(Cookie cookie) {

    StringBuilder buf = new StringBuilder(cookie.getName());
    buf.append("=");
    buf.append(cookie.getValue());

    if (cookie.getComment() != null) {
        buf.append("; Comment=\"");
        buf.append(cookie.getComment());
        buf.append("\"");
    }

    if (cookie.getDomain() != null) {
        buf.append("; Domain=\"");
        buf.append(cookie.getDomain());
        buf.append("\"");
    }

    if (cookie.getMaxAge() >= 0) {
        buf.append("; Max-Age=\"");
        buf.append(cookie.getMaxAge());
        buf.append("\"");
    }

    if (cookie.getPath() != null) {
        buf.append("; Path=\"");
        buf.append(cookie.getPath());
        buf.append("\"");
    }

    if (cookie.getSecure()) {
        buf.append("; Secure");
    }

    if (cookie.getVersion() > 0) {
        buf.append("; Version=\"");
        buf.append(cookie.getVersion());
        buf.append("\"");
    }

    return (buf.toString());
}