Example usage for javax.servlet.http HttpServletResponse addCookie

List of usage examples for javax.servlet.http HttpServletResponse addCookie

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse addCookie.

Prototype

public void addCookie(Cookie cookie);

Source Link

Document

Adds the specified cookie to the response.

Usage

From source file:com.erudika.scoold.utils.HttpUtils.java

/**
 * Sets a cookie./* w  ww  . j a  v  a 2s .  co m*/
 * @param name the name
 * @param value the value
 * @param req HTTP request
 * @param res HTTP response
 * @param httpOnly HTTP only flag
 * @param maxAge max age
 */
public static void setRawCookie(String name, String value, HttpServletRequest req, HttpServletResponse res,
        boolean httpOnly, int maxAge) {
    if (StringUtils.isBlank(name) || value == null || req == null || res == null) {
        return;
    }
    Cookie cookie = new Cookie(name, value);
    cookie.setHttpOnly(httpOnly);
    cookie.setMaxAge(maxAge < 0 ? Config.SESSION_TIMEOUT_SEC : maxAge);
    cookie.setPath("/");
    cookie.setSecure(req.isSecure());
    res.addCookie(cookie);
}

From source file:com.leixl.easyframework.web.CookieUtils.java

/**
 * ?cookie?/* w ww  .j  a  va 2  s . c o  m*/
 * 
 * @param request
 * @param response
 * @param name
 * @param value
 * @param expiry
 * @param domain
 * @return
 */
public static Cookie addCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, Integer expiry, String domain) {
    Cookie cookie = new Cookie(name, value);
    if (expiry != null) {
        cookie.setMaxAge(expiry);
    }
    if (StringUtils.isNotBlank(domain)) {
        cookie.setDomain(domain);
    }
    String ctx = request.getContextPath();
    cookie.setPath(StringUtils.isBlank(ctx) ? "/" : ctx);
    response.addCookie(cookie);
    return cookie;
}

From source file:alpha.portal.webapp.util.RequestUtil.java

/**
 * Convenience method to set a cookie.// w w w.  ja  va 2s. co  m
 * 
 * @param response
 *            the current response
 * @param name
 *            the name of the cookie
 * @param value
 *            the value of the cookie
 * @param path
 *            the path to set it on
 */
public static void setCookie(final HttpServletResponse response, final String name, final String value,
        final String path) {
    if (RequestUtil.log.isDebugEnabled()) {
        RequestUtil.log.debug("Setting cookie '" + name + "' on path '" + path + "'");
    }

    final Cookie cookie = new Cookie(name, value);
    cookie.setSecure(false);
    cookie.setPath(path);
    cookie.setMaxAge(3600 * 24 * 30); // 30 days

    response.addCookie(cookie);
}

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

/**
 * set the name/value entry to the cookie
 * //from w  w  w.  j  a v  a  2 s.  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:com.lushapp.common.web.utils.WebUtils.java

/**
 * Cookie.//from  w  w  w.  ja  va 2 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:de.eod.jliki.users.utils.UserDBHelper.java

/**
 * Logs in a user returned from database after the login test was made.<br/>
 * @param dbUser the user from database (session may not be closed!)
 * @param passedLogin did the user pass the login test?
 * @param rememberMe will the user stay logged in?
 * @param userLogin the login object//from w  w w .  j  av  a2s .com
 * @param session the hibernate session for further queries
 * @return true if the user was logged in
 */
private static boolean loginUser(final User dbUser, final boolean passedLogin, final boolean rememberMe,
        final LoginBean userLogin, final Session session) {
    boolean didLogin = false;
    if (passedLogin && dbUser.getActive() == ActiveState.ACTIVE) {
        didLogin = true;
        userLogin.setUserName(dbUser.getName());
        userLogin.setLoggedIn(true);
    } else {
        didLogin = false;
        userLogin.setUserName(userLogin.getUserName());
        userLogin.setLoggedIn(false);
    }

    dbUser.setLastlogin(new Date());

    final UUID loginUUID = UUID.randomUUID();
    Cookie cookie = null;
    final int tenDays = 60 * 60 * 24 * 10;
    if (rememberMe && passedLogin) {
        cookie = new Cookie("login", loginUUID.toString());
        cookie.setMaxAge(tenDays);
        dbUser.setCookieid(loginUUID.toString());
    } else {
        cookie = new Cookie("login", "");
        cookie.setMaxAge(0);
        dbUser.setCookieid("");
    }

    userLogin.clearPermissions();
    dbUser.transferPermissionsToLogin(userLogin);
    for (final UserGroup grp : dbUser.getGroups()) {
        grp.transferPermissionsToLogin(userLogin);
    }

    final HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance()
            .getExternalContext().getResponse();
    httpServletResponse.addCookie(cookie);

    return didLogin;
}

From source file:edu.utah.further.i2b2.hook.further.web.ServletUtil.java

/**
 * Convenience method to set a cookie. The cookie gets max age set to 30 days.
 *
 * @param response//  w w  w  .ja v a 2  s .  com
 *            response that will accept a cookie
 * @param name
 *            name of the cookie to store
 * @param value
 *            value of the cookie
 * @param path
 *            path of the cookie
 */
public static void setCookie(final HttpServletResponse response, final String name, final String value,
        final String path) {
    if (log.isDebugEnabled()) {
        log.debug("Setting cookie " + quote(name) + " on path " + quote(path));
    }

    final Cookie cookie = new Cookie(name, value);
    cookie.setSecure(false);
    cookie.setPath(path);
    cookie.setMaxAge(3600 * 24 * 30); // 30 days

    response.addCookie(cookie);
}

From source file:io.syndesis.rest.v1.handler.credential.CredentialHandler.java

static void removeCredentialCookies(final HttpServletRequest request,
        final HttpServletResponse response) {
    Arrays.stream(request.getCookies())
            .filter(c -> c.getName().startsWith(CredentialFlowState.CREDENTIAL_PREFIX)).forEach(c -> {
                final Cookie removal = new Cookie(c.getName(), "");
                removal.setPath("/");
                removal.setMaxAge(0);/*from   ww w.  j ava2  s  .  c  om*/
                removal.setHttpOnly(true);
                removal.setSecure(true);

                response.addCookie(removal);
            });
}

From source file:ke.alphacba.cms.core.util.NoSessionIdUtils.java

private static void writeCookie(HttpServletResponse response, String loginCookieName, String domain,
        int cookieTime, String jsessionId, boolean httpOnly) {

    String cookieValue = new String(Base64.encodeBase64(new StringBuilder().append(loginCookieName)
            .append(new Date().getTime()).append(jsessionId).toString().getBytes()));
    Cookie cookie1 = new Cookie(loginCookieName, cookieValue);
    cookie1.setHttpOnly(true);//  ww w  .  j a v  a2  s.  c om
    cookie1.setMaxAge(cookieTime);
    cookie1.setPath("/");
    cookie1.setDomain(domain);
    response.addCookie(cookie1);
}

From source file:nl.strohalm.cyclos.utils.ActionHelper.java

/**
 * Sends a message to the next page/*w  ww.  j ava  2s.c o  m*/
 */
public static void sendMessage(final HttpServletRequest request, final HttpServletResponse response,
        final String key, final Object... arguments) {
    final HttpSession session = request.getSession();
    session.setAttribute("messageKey", key);
    session.setAttribute("messageArguments", arguments);
    response.addCookie(new Cookie("showMessage", "true"));
}