Example usage for javax.servlet.http Cookie setVersion

List of usage examples for javax.servlet.http Cookie setVersion

Introduction

In this page you can find the example usage for javax.servlet.http Cookie setVersion.

Prototype

public void setVersion(int v) 

Source Link

Document

Sets the version of the cookie protocol that this Cookie complies with.

Usage

From source file:com.anjz.util.CookieUtils.java

private static void setCookie(String key, String value, int maxAge, String path, String domainName,
        final boolean httpOnly, final boolean secure, HttpServletResponse response) {
    if (response != null) {
        Cookie cookie = new Cookie(key, value);
        cookie.setMaxAge(maxAge);/*from  www . j  a va 2s  . c  om*/
        if (StringUtils.isNotBlank(path)) {
            cookie.setPath(path);
        } else {
            cookie.setPath(PATH);
        }
        if (StringUtils.isNotBlank(domainName)) {
            cookie.setDomain(domainName);
        }
        cookie.setVersion(0);
        cookie.setSecure(secure);
        if (httpOnly) {
            final StringBuffer buf = new StringBuffer();
            getCookieHeaderValue(cookie, buf, httpOnly);
            response.addHeader(getCookieHeaderName(cookie), buf.toString());
        } else {
            response.addCookie(cookie);
        }
    }
}

From source file:com.liferay.portal.util.CookieKeys.java

public static void addCookie(HttpServletRequest request, HttpServletResponse response, Cookie cookie,
        boolean secure) {

    if (!PropsValues.SESSION_ENABLE_PERSISTENT_COOKIES || PropsValues.TCK_URL) {

        return;/*www  . j  a v  a  2s  .c  o m*/
    }

    // LEP-5175

    String name = cookie.getName();

    String originalValue = cookie.getValue();
    String encodedValue = originalValue;

    if (isEncodedCookie(name)) {
        encodedValue = new String(Hex.encodeHex(originalValue.getBytes()));

        if (_log.isDebugEnabled()) {
            _log.debug("Add encoded cookie " + name);
            _log.debug("Original value " + originalValue);
            _log.debug("Hex encoded value " + encodedValue);
        }
    }

    cookie.setSecure(secure);
    cookie.setValue(encodedValue);
    cookie.setVersion(VERSION);

    // Setting a cookie will cause the TCK to lose its ability to track
    // sessions

    response.addCookie(cookie);
}

From source file:com.reever.humilheme.web.CookieController.java

public Cookie createCookie(HttpServletRequest request, HttpServletResponse response, String conteudo) {
    String path = StringUtils.isEmpty(request.getContextPath()) ? "/" : request.getContextPath();
    try {/*w ww .j av a 2s.com*/
        conteudo = URLEncoder.encode(conteudo, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        _logger.error("Erro no encode do cookie", e);
    }
    Cookie cookie = new Cookie(nomeCookie, conteudo);
    cookie.setMaxAge(expiry);
    cookie.setPath(path);
    cookie.setVersion(1);
    response.addCookie(cookie);
    return cookie;
}

From source file:com.xpn.xwiki.web.XWikiServletResponse.java

public void addCookie(String cookieName, String cookieValue, int age) {
    Cookie cookie = new Cookie(cookieName, cookieValue);
    cookie.setVersion(1);
    cookie.setMaxAge(age);/*from w  w w.  j  a v a  2  s.c  o m*/
    this.response.addCookie(cookie);
}

From source file:com.byd.test.actions.OrderAction.java

License:asdf

@RequestMapping("createCookie")
public void createCookie(HttpServletResponse response) {
    System.out.println("cookie start");
    Cookie cookie = new Cookie("cookie_name", "whatisthis");
    cookie.setHttpOnly(Boolean.TRUE);
    cookie.setDomain("chengangxiong");
    cookie.setVersion(1);
    cookie.setMaxAge(15);//15
    response.addCookie(cookie);/*  w w w.  j  av a 2  s . c  o m*/

}

From source file:eu.semlibproject.annotationserver.managers.CookiesManager.java

/**
 * Generate a new cookie for the annotation server
 * // ww w.  j  av a 2 s  . c  o  m
 * @param accessToken   the accessToken
 * @return              the new generated cookie
 */
public Cookie generateNewASCookie(String accessToken) {

    if (accessToken != null) {
        Cookie cookie = new Cookie(SemlibConstants.COOCKIE_NAME, accessToken);
        cookie.setComment(SemlibConstants.COOCKIE_DESCRIPTION);
        cookie.setPath(SemlibConstants.COOKIE_PATH);
        cookie.setMaxAge(SemlibConstants.COOKIE_TIME);
        cookie.setVersion(1);
        cookie.setSecure(false);

        return cookie;
    }

    return null;
}

From source file:com.alfaariss.oa.util.web.CookieTool.java

/**
 * Set Cookie with optional extra context in application context
 * @param sCookie/*  w w w.ja  v  a  2  s  . com*/
 * @param sValue
 * @param sExtraContext
 * @param oRequest
 * @return
 */
public Cookie createCookie(String sCookie, String sValue, String sExtraContext, HttpServletRequest oRequest) {
    assert sValue != null : "Supplied value == null";
    assert oRequest != null : "Supplied request == null";

    Cookie cookie = new Cookie(sCookie, sValue);
    if (_sCookieDomain != null) {
        cookie.setDomain(_sCookieDomain);
        _logger.debug("Created domain cookie on " + _sCookieDomain);
    }

    if (_iCookieVersion != -1) {
        cookie.setVersion(_iCookieVersion);
        _logger.debug("Setting cookie version: " + _iCookieVersion);
    }

    /* format sExtraContext */
    if (sExtraContext == null) {
        sExtraContext = "";
    } else {
        if (!sExtraContext.startsWith("/")) {
            sExtraContext = "/" + sExtraContext;
        }
    }

    String path = oRequest.getContextPath();
    if (path != null && path.length() > 0) {//only set path if path not is empty (when hosted as server root, getContextPath() will return an empty string)
        cookie.setPath(path + sExtraContext);// /openaselect
    } else {//if no contextpath available then setting the cookie path on '/' instead of on the default path (which is for the sso cookie: /openaselect/sso)
        cookie.setPath("/" + sExtraContext);
    }

    cookie.setSecure(_bSecureCookie);

    StringBuffer sbDebug = new StringBuffer("Created '");
    sbDebug.append(sCookie);
    sbDebug.append("' on path=");
    sbDebug.append(cookie.getPath());
    _logger.debug(sbDebug.toString());

    return cookie;
}

From source file:com.nominanuda.web.http.ServletHelper.java

public Cookie servletCookie(HttpCookie c) {
    Cookie _c = new Cookie(c.getName(), c.getValue());
    if (c.getComment() != null) {
        _c.setComment(c.getComment());//from  www.  j a  v  a  2s. c  o m
    }
    if (c.getDomain() != null) {
        _c.setDomain(c.getDomain());
    }
    if (c.getPath() != null) {
        _c.setPath(c.getPath());
    }
    _c.setSecure(c.getSecure());
    _c.setVersion(c.getVersion());
    _c.setHttpOnly(c.getDiscard());
    _c.setMaxAge((int) c.getMaxAge());
    return _c;
}

From source file:io.mapzone.controller.vm.http.HttpResponseForwarder.java

/**
 * Copy cookie from the proxy to the servlet client. Replaces cookie path to
 * local path and renames cookie to avoid collisions.
 *///  w ww  .  jav  a 2  s .c om
protected void copyProxyCookie(HttpServletRequest servletRequest, HttpServletResponse servletResponse,
        Header header) {
    List<HttpCookie> cookies = HttpCookie.parse(header.getValue());
    String path = servletRequest.getContextPath(); // path starts with / or is empty string
    path += servletRequest.getServletPath(); // servlet path starts with / or is empty string

    for (HttpCookie cookie : cookies) {
        // set cookie name prefixed w/ a proxy value so it won't collide w/ other cookies
        String proxyCookieName = requestForwarder.cookieNamePrefix.get() + cookie.getName();
        Cookie servletCookie = new Cookie(proxyCookieName, cookie.getValue());
        servletCookie.setComment(cookie.getComment());
        servletCookie.setMaxAge((int) cookie.getMaxAge());
        servletCookie.setPath(path); // set to the path of the proxy servlet
        // don't set cookie domain
        servletCookie.setSecure(cookie.getSecure());
        servletCookie.setVersion(cookie.getVersion());
        servletResponse.addCookie(servletCookie);
    }
}

From source file:com.codename1.corsproxy.CORSProxy.java

@Override
protected void copyProxyCookie(HttpServletRequest servletRequest, HttpServletResponse servletResponse,
        Header header) {// w  w  w .  ja  v a2  s. co  m
    List<HttpCookie> cookies = HttpCookie.parse(header.getValue());
    String path = servletRequest.getContextPath(); // path starts with / or is empty string
    path += servletRequest.getServletPath(); // servlet path starts with / or is empty string

    for (HttpCookie cookie : cookies) {
        //set cookie name prefixed w/ a proxy value so it won't collide w/ other cookies
        String proxyCookieName = getCookieNamePrefix() + cookie.getName();
        Cookie servletCookie = new Cookie(proxyCookieName, cookie.getValue());
        servletCookie.setComment(cookie.getComment());
        servletCookie.setMaxAge((int) cookie.getMaxAge());
        servletCookie.setPath(path); //set to the path of the proxy servlet
        // don't set cookie domain
        //servletCookie.setSecure(cookie.getSecure());
        servletCookie.setSecure(false);
        servletCookie.setVersion(cookie.getVersion());
        servletResponse.addCookie(servletCookie);
    }
}