Example usage for javax.servlet.http Cookie setPath

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

Introduction

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

Prototype

public void setPath(String uri) 

Source Link

Document

Specifies a path for the cookie to which the client should return the cookie.

Usage

From source file:com.rantop.web.util.web.ServletUtils.java

/**
 * Convenience method for deleting a cookie by name
 *
 * @param response the current web response
 * @param cookie the cookie to delete/*from w  ww  .j a v a2s  .  c  o  m*/
 * @param path the path on which the cookie was set (i.e. /appfuse)
 */
public static void deleteCookie(HttpServletResponse response, Cookie cookie, String path) {
    if (cookie != null) {
        // Delete the cookie by setting its maximum age to zero
        cookie.setMaxAge(0);
        cookie.setPath(path);
        response.addCookie(cookie);
    }
}

From source file:com.iterzp.momo.utils.WebUtils.java

/**
 * cookie//from   ww  w  .  ja v a2 s . c  o m
 * 
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 * @param name
 *            cookie??
 * @param path
 *            
 * @param domain
 *            
 */
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String path, String domain) {
    Assert.notNull(request);
    Assert.notNull(response);
    Assert.hasText(name);
    try {
        name = URLEncoder.encode(name, "UTF-8");
        Cookie cookie = new Cookie(name, null);
        cookie.setMaxAge(0);
        if (StringUtils.isNotEmpty(path)) {
            cookie.setPath(path);
        }
        if (StringUtils.isNotEmpty(domain)) {
            cookie.setDomain(domain);
        }
        response.addCookie(cookie);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

From source file:com.vmware.identity.samlservice.Shared.java

/**
 * Adding a browser session cookie to browser.
 * @param cookieName//w w  w . j  a  va 2  s.co  m
 * @param cookieValue
 * @param response
 */
public static void addSessionCookie(String cookieName, String cookieValue, HttpServletResponse response) {
    Validate.notNull(response);
    if (cookieName == null || cookieName.isEmpty() || cookieValue == null || cookieValue.isEmpty()) {
        log.warn("Cookie name/value is null or empty. Ignoring.");
        return;
    }
    log.debug("Setting cookie " + cookieName + " value " + cookieValue);
    Cookie sessionCookie = new Cookie(cookieName, cookieValue);
    sessionCookie.setPath("/");
    sessionCookie.setSecure(true);
    sessionCookie.setHttpOnly(true);
    response.addCookie(sessionCookie);
}

From source file:com.activecq.api.utils.CookieUtil.java

/**
 * <p>/*from ww  w. j  a  v  a2 s  . com*/
 * Extend the cookie life.
 * <p></p>
 * This can be used when a cookie should be valid for X minutes from the last point of activity.
 * <p></p>
 * This method will leave expired or deleted cookies alone.
 * </p>
 * @param request Request to get the Cookie from
 * @param response Response to write the extended Cookie to
 * @param cookieName Name of Cookie to extend the life of
 * @param expiry New Cookie expiry
 */
public static boolean extendCookieLife(HttpServletRequest request, HttpServletResponse response,
        String cookieName, int expiry) {
    Cookie cookie = getCookie(request, cookieName);
    if (cookie == null) {
        return false;
    }

    if (cookie.getMaxAge() <= 0) {
        return false;
    }

    cookie.setMaxAge(expiry);
    cookie.setPath(cookie.getPath());

    addCookie(cookie, response);

    return true;
}

From source file:com.persistent.cloudninja.controller.AuthFilterUtils.java

/**
 * //from   w  ww . j  av a  2 s.  c  om
 * @param cloudNinjaUser
 * @param cookieName
 * @return
 */
public static Cookie createNewCookieForACSAuthenticatedUser(CloudNinjaUser cloudNinjaUser, String cookieName) {
    Collection<GrantedAuthority> authorities = cloudNinjaUser.getUser().getAuthorities();
    if (authorities != null) {
        GrantedAuthority[] grantedAuthorities = new GrantedAuthority[authorities.size()];
        authorities.toArray(grantedAuthorities);
    }

    StringBuffer sb = new StringBuffer(5);

    sb.append(CloudNinjaConstants.COOKIE_TENANTID_PREFIX)
            .append(CloudNinjaConstants.COOKIE_FIELD_AND_VALUE_SEPARATOR).append(cloudNinjaUser.getTenantId())
            .append(CloudNinjaConstants.COOKIE_FIELDS_SEPARATOR);

    sb.append(CloudNinjaConstants.COOKIE_USERNAME_PREFIX)
            .append(CloudNinjaConstants.COOKIE_FIELD_AND_VALUE_SEPARATOR)
            .append(cloudNinjaUser.getUser().getUsername()).append(CloudNinjaConstants.COOKIE_FIELDS_SEPARATOR);

    sb.append(CloudNinjaConstants.COOKIE_AUTHORITIES_PREFIX)
            .append(CloudNinjaConstants.COOKIE_FIELD_AND_VALUE_SEPARATOR).append(authorities.toString())
            .append(CloudNinjaConstants.COOKIE_FIELDS_SEPARATOR);

    sb.append(CloudNinjaConstants.COOKIE_AUTH_SESSION_START_PREFIX)
            .append(CloudNinjaConstants.COOKIE_FIELD_AND_VALUE_SEPARATOR)
            .append(cloudNinjaUser.getAuthenticatedSessionStartTime().getTime())
            .append(CloudNinjaConstants.COOKIE_FIELDS_SEPARATOR);

    sb.append(CloudNinjaConstants.COOKIE_AUTH_SESSION_END_PREFIX)
            .append(CloudNinjaConstants.COOKIE_FIELD_AND_VALUE_SEPARATOR)
            .append(cloudNinjaUser.getAuthenticatedSessionExpiryTime().getTime())
            .append(CloudNinjaConstants.COOKIE_FIELDS_SEPARATOR);

    String newCookieValue = sb.toString();

    Cookie newCookie = new Cookie(cookieName, newCookieValue);
    newCookie.setPath("/");
    return newCookie;
}

From source file:com.iterzp.momo.utils.WebUtils.java

/**
 * cookie//from   w  w w  .j av a 2  s .  co m
 * 
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 * @param name
 *            cookie??
 * @param value
 *            cookie
 * @param maxAge
 *            (??: )
 * @param path
 *            
 * @param domain
 *            
 * @param secure
 *            ??
 */
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, Integer maxAge, String path, String domain, Boolean secure) {
    Assert.notNull(request);
    Assert.notNull(response);
    Assert.hasText(name);
    try {
        name = URLEncoder.encode(name, "UTF-8");
        value = URLEncoder.encode(value, "UTF-8");
        Cookie cookie = new Cookie(name, value);
        if (maxAge != null) {
            cookie.setMaxAge(maxAge);
        }
        if (StringUtils.isNotEmpty(path)) {
            cookie.setPath(path);
        }
        if (StringUtils.isNotEmpty(domain)) {
            cookie.setDomain(domain);
        }
        if (secure != null) {
            cookie.setSecure(secure);
        }
        response.addCookie(cookie);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

From source file:com.lushapp.common.web.utils.WebUtils.java

/**
 * cookie.//from w  w  w  .  j  av  a2 s  .c  o m
 * @param response
 * @param name
 * @param value
 * @param path
 */
public static void setCookie(HttpServletResponse response, String name, String value, String path) {
    if (logger.isDebugEnabled()) {
        logger.debug("Cookie '" + name + "',?: '" + path + "'");
    }

    Cookie cookie = new Cookie(name, value);
    cookie.setSecure(false);
    cookie.setPath(path);
    cookie.setMaxAge(2592000);

    response.addCookie(cookie);
}

From source file:cn.vlabs.umt.ui.servlet.login.LoginMethod.java

/**
 * ??cookie//from  w  w w  .j  ava  2  s  . com
 * */
public static void generateSsoCookie(HttpServletResponse response, HttpServletRequest request,
        LoginInfo loginInfo) throws UnsupportedEncodingException {
    PCookie pcookie = (PCookie) ServiceFactory.getBean(request, "PCookie");
    // Pcookie
    String encrypted = pcookie
            .encrypt(loginInfo.getUser().getCstnetId() + "/" + RequestUtil.getRemoteIP(request) + "/"
                    + loginInfo.getPasswordType() + "/" + System.currentTimeMillis());
    Cookie cookie = new Cookie(Attributes.COOKIE_NAME, encrypted);
    cookie.setPath("/");
    cookie.setMaxAge(MAX_COOKIE_AGE);
    response.addCookie(cookie);
    Cookie umtIdCookie = new Cookie(Attributes.SSO_FLAG, SessionUtils.getUserId(request) + "");
    umtIdCookie.setDomain(Attributes.SSO_FLAG_DOMAIN);
    umtIdCookie.setPath("/");
    umtIdCookie.setMaxAge(LoginMethod.MAX_COOKIE_AGE);
    response.addCookie(umtIdCookie);
}

From source file:com.lushapp.common.web.utils.WebUtils.java

/**
 * Cookie.//from w w w  . ja va2  s. c  o  m
 * @param response
 * @param cookie
 * @param path
 */
public static void deleteCookie(HttpServletResponse response, Cookie cookie, String path) {
    if (cookie != null) {
        cookie.setMaxAge(0);
        cookie.setPath(path);
        response.addCookie(cookie);
    }
}

From source file:com.o2o.util.WebUtils.java

public static void clearCookie(HttpServletRequest request, HttpServletResponse response, String path,
        String domain) {//w  ww .  j  a v a 2s  .  c om
    Assert.notNull(request);
    Assert.notNull(response);
    try {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie_old : cookies) {
            String name = URLEncoder.encode(cookie_old.getName(), "UTF-8");
            Cookie cookie = new Cookie(name, null);
            cookie.setMaxAge(0);
            if (StringUtils.isNotEmpty(path)) {
                cookie.setPath(path);
            }
            if (StringUtils.isNotEmpty(domain)) {
                cookie.setDomain(domain);
            }
            response.addCookie(cookie);
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}