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:io.lavagna.web.helper.UserSession.java

private static void addRememberMeCookie(int userId, HttpServletRequest req, HttpServletResponse resp,
        UserRepository userRepository) {

    String token = userRepository.createRememberMeToken(userId);
    ///*from  ww w  .j a  va2  s  . c  om*/
    Cookie c = new Cookie("LAVAGNA_REMEMBER_ME", userId + "," + token);
    c.setPath(req.getContextPath() + "/");
    c.setHttpOnly(true);
    c.setMaxAge(60 * 60 * 24 * 365); // 1 year
    if (req.getServletContext().getSessionCookieConfig().isSecure()) {
        c.setSecure(true);
    }
    resp.addCookie(c);
}

From source file:org.b3log.symphony.util.Sessions.java

/**
 * Logins the specified user from the specified request.
 *
 * <p>/* w  ww  .  ja  va2 s .  c o  m*/
 * If no session of the specified request, do nothing.
 * </p>
 *
 * @param request the specified request
 * @param response the specified response
 * @param user the specified user, for example,      <pre>
 * {
 *     "oId": "",
 *     "userPassword": ""
 * }
 * </pre>
 */
public static void login(final HttpServletRequest request, final HttpServletResponse response,
        final JSONObject user) {
    final HttpSession session = request.getSession(false);

    if (null == session) {
        LOGGER.warn("The session is null");

        return;
    }

    session.setAttribute(User.USER, user);
    session.setAttribute(Common.CSRF_TOKEN, RandomStringUtils.randomAlphanumeric(12));

    try {
        final JSONObject cookieJSONObject = new JSONObject();

        cookieJSONObject.put(Keys.OBJECT_ID, user.optString(Keys.OBJECT_ID));
        cookieJSONObject.put(Common.TOKEN, user.optString(User.USER_PASSWORD));

        final Cookie cookie = new Cookie("b3log-latke", cookieJSONObject.toString());

        cookie.setPath("/");
        cookie.setMaxAge(COOKIE_EXPIRY);
        cookie.setHttpOnly(true); // HTTP Only

        response.addCookie(cookie);
    } catch (final Exception e) {
        LOGGER.log(Level.WARN, "Can not write cookie", e);
    }
}

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

public static void addSupportCookie(HttpServletRequest request, HttpServletResponse response) {

    Cookie cookieSupportCookie = new Cookie(COOKIE_SUPPORT, "true");

    cookieSupportCookie.setPath(StringPool.SLASH);
    cookieSupportCookie.setMaxAge(MAX_AGE);

    addCookie(request, response, cookieSupportCookie);
}

From source file:com.sniper.springmvc.utils.CookieUtils.java

/**
 *  Cookie/*from   w  w  w.  j  a  v  a 2s . com*/
 * 
 * @param name
 *            ??
 * @param value
 *            
 * @param maxAge
 *            ??
 */
public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
    Cookie cookie = new Cookie(name, null);
    if (StringUtils.isNotBlank(SpringContextHolder.getApplicationContext().getApplicationName())) {
        cookie.setPath(SpringContextHolder.getApplicationContext().getApplicationName());
    } else {
        cookie.setPath("/");
    }
    cookie.setMaxAge(maxAge);
    try {
        cookie.setValue(URLEncoder.encode(value, "utf-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    response.addCookie(cookie);
}

From source file:com.xidu.framework.common.util.CookieUtils.java

/**
 * set the name/value entry to the cookie
 * /*w  w w.j a v a  2s .c  o m*/
 * @Date : 2011-3-23
 * @param response
 *            - HttpServletResponse's instance
 * @param name
 *            - Cookie's Entry key
 * @param value
 *            - Cookie's Entry value
 * @param path
 *            - Cookie's path
 * @param domain
 *            - Cookie' domain
 * @param maxAge
 *            - Cookie's max age
 */
public static void setCookie(HttpServletResponse response, String name, String value, String path,
        String domain, int maxAge) {
    logger.debug("cookie value:" + value);
    Cookie cookie = new Cookie(name, value);
    cookie.setSecure(false);
    if (StringUtils.isNotBlank(path)) {
        cookie.setPath(path);
    }
    cookie.setMaxAge(maxAge);
    if (StringUtils.isNotBlank(domain)) {
        cookie.setDomain(domain);
    }
    response.addCookie(cookie);
}

From source file:cn.vlabs.duckling.vwb.VWBFilter.java

public static void removeGlobalCookie(HttpServletRequest request, HttpServletResponse response,
        HttpSession session) {/*from   w w  w .  ja v a 2 s  .  c o  m*/
    Cookie oldCookie = new Cookie(COOKIE_NAME, session.getId());
    oldCookie.setPath(request.getContextPath());
    oldCookie.setMaxAge(0);
    response.addCookie(oldCookie);
}

From source file:com.baidu.rigel.biplatform.ma.auth.resource.RandomValidateCode.java

/**
 * /*www.  j  a va 2s  .  c  om*/
 * @param request
 * @param response
 * @param cacheManagerForResource 
 */
public static void getRandcode(HttpServletRequest request, HttpServletResponse response,
        CacheManagerForResource cacheManagerForResource) {
    // BufferedImageImage,Image????
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
    Graphics g = image.getGraphics(); // ImageGraphics,?????
    g.fillRect(0, 0, width, height);
    g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));
    g.setColor(getRandColor(110, 133));
    // 
    for (int i = 0; i <= lineSize; i++) {
        drowLine(g);
    }
    // ?
    String randomString = "";
    for (int i = 1; i <= stringNum; i++) {
        randomString = drowString(g, randomString, i);
    }
    String key = null;
    if (request.getCookies() != null) {
        for (Cookie tmp : request.getCookies()) {
            if (tmp.getName().equals(Constants.RANDOMCODEKEY)) {
                key = tmp.getName();
                cacheManagerForResource.removeFromCache(key);
                break;
            }
        }
    }
    if (StringUtils.isEmpty(key)) {
        key = String.valueOf(System.nanoTime());
    }
    cacheManagerForResource.setToCache(key, randomString);
    final Cookie cookie = new Cookie(Constants.RANDOMCODEKEY, key);
    cookie.setPath(Constants.COOKIE_PATH);
    response.addCookie(cookie);
    g.dispose();
    try {
        ImageIO.write(image, "JPEG", response.getOutputStream()); // ??
    } catch (Exception e) {
        LOG.info(e.getMessage());
    }
}

From source file:org.commonfarm.security.util.CookieUtils.java

/**
 * Sets a cookie//  w  ww.j a va  2  s.com
 *
 * This will also put the cookie in a list of cookies to send with this request's response
 * (so that in case of a redirect occurring down the chain, the first filter
 * will always try to set this cookie again)
 */
public static Cookie setCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, int maxAge, String path) {
    log.debug("CookieUtils.setCookie " + name + ":" + value);
    Cookie cookie = new Cookie(name, value);
    cookie.setMaxAge(maxAge);
    cookie.setPath(path);
    response.addCookie(cookie);

    return cookie;
}

From source file:com.shirokumacafe.archetype.common.utilities.Servlets.java

/**
 * cookie//from  ww  w  . j a v a  2s  . c  o m
 */
public static void setCookie(String name, String value, HttpServletResponse response) {
    Cookie cookie = new Cookie(name, value);
    cookie.setMaxAge((int) ONE_YEAR_SECONDS);
    cookie.setPath("/");
    response.addCookie(cookie);
}

From source file:org.b3log.latke.util.Sessions.java

/**
 * Logins the specified user from the specified request.
 * //  www. j a  v  a2  s  .  c om
 * <p>
 * If no session of the specified request, do nothing.
 * </p>
 *
 * @param request the specified request
 * @param response the specified response
 * @param user the specified user, for example,
 * <pre>
 * {
 *     "userEmail": "",
 *     "userPassword": ""
 * }
 * </pre>
 */
public static void login(final HttpServletRequest request, final HttpServletResponse response,
        final JSONObject user) {
    final HttpSession session = request.getSession(false);

    if (null == session) {
        LOGGER.warning("The session is null");
        return;
    }

    session.setAttribute(User.USER, user);

    try {
        final JSONObject cookieJSONObject = new JSONObject();

        cookieJSONObject.put(User.USER_EMAIL, user.optString(User.USER_EMAIL));
        cookieJSONObject.put(User.USER_PASSWORD, user.optString(User.USER_PASSWORD));

        final Cookie cookie = new Cookie("b3log-latke", cookieJSONObject.toString());

        cookie.setPath("/");
        cookie.setMaxAge(COOKIE_EXPIRY);
        response.addCookie(cookie);
    } catch (final Exception e) {
        LOGGER.log(Level.WARNING, "Can not write cookie", e);
    }
}