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.lushapp.common.web.utils.WebUtils.java

/**
 * cookie./*from  ww  w . j av  a  2 s.co 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:com.vmware.identity.SharedUtils.java

/**
 * Build mock HttpServletResponse object with a StringWriter
 *  which receives actual response//from  w w w  .  j a v  a2 s. c o m
 * @param sw
 * @param contentType
 * @param expectCookie
 * @return
 * @throws IOException
 */
public static HttpServletResponse buildMockResponseSuccessObject(StringWriter sw, String contentType,
        boolean expectCookie, String contentDispositionValue) throws IOException {
    HttpServletResponse response = createMock(HttpServletResponse.class);
    if (expectCookie) {
        response.addCookie(isA(Cookie.class));
    }
    response.setContentType(contentType);
    if (contentDispositionValue != null && !contentDispositionValue.isEmpty()) {
        response.setHeader("Content-Disposition", contentDispositionValue);
    }
    expect(response.getWriter()).andReturn(new PrintWriter(sw)).anyTimes();
    replay(response);
    return response;
}

From source file:com.netsteadfast.greenstep.base.sys.UserCurrentCookie.java

public static void setCurrentId(HttpServletResponse response, String currentId, String sessionId,
        String account, String language) {
    try {/*from w  ww  .  j ava  2 s.  co  m*/
        String value = currentId + Constants.ID_DELIMITER + sessionId + Constants.ID_DELIMITER + account
                + Constants.ID_DELIMITER + language;
        String encValue = EncryptorUtils.encrypt(Constants.getEncryptorKey1(), Constants.getEncryptorKey2(),
                value);
        encValue = SimpleUtils.toHex(encValue);
        Cookie cookie = new Cookie(Constants.APP_SITE_CURRENTID_COOKIE_NAME, encValue);
        cookie.setPath("/");
        cookie.setValue(encValue);
        cookie.setMaxAge(60 * 60 * 24); // 1-day
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.shishu.utility.string.StringUtil.java

public static void setCookie(HttpServletResponse response, String CookieName, String CookieVal, int CookieAge)
        throws UnsupportedEncodingException {
    Cookie cookie = new Cookie(CookieName, URLEncoder.encode(CookieVal, "utf-8"));
    cookie.setMaxAge(CookieAge);//from   ww w .  j a  va 2 s .  c o m
    cookie.setPath("/");
    response.addCookie(cookie);
}

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

/**
 * cookie/*from w  w w . ja  va  2s .  c om*/
 * 
 * @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.aaasec.sigserv.csspserver.SpServlet.java

private static SpSession getSession(HttpServletRequest request, HttpServletResponse response) {
    BigInteger sessionID = new BigInteger(32, new Random(System.currentTimeMillis()));
    Cookie[] cookies = request.getCookies();
    try {/*from  w ww  .j  a  v  a 2  s  .c o  m*/
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("SigSpSession")) {
                sessionID = new BigInteger(cookie.getValue());
            }
        }
    } catch (Exception ex) {
    }
    response.addCookie(new Cookie("SigSpSession", sessionID.toString()));
    return getSessionFromID(sessionID);
}

From source file:com.ax.utils.CookieUtils.java

/**
 * Stores a value in a cookie. This cookie will persist for the amount
 * specified in the <tt>saveTime</tt> parameter.
 * /*from   ww  w  . ja  va 2  s  .  co m*/
 * @see #setCookie(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,String,String)
 * @param request
 *            the servlet request.
 * @param response
 *            the servlet response.
 * @param name
 *            a name to identify the cookie.
 * @param value
 *            the value to store in the cookie.
 * @param maxAge
 *            the time (in seconds) this cookie should live.
 * @param domain
 *            the domain.
 * @param path
 *            the path.
 */
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, int maxAge, String domain, String path) {
    // Check to make sure the new value is not null (appservers like Tomcat
    // 4 blow up if the value is null).
    if (value == null) {
        value = "";
    }
    if (StringUtils.isEmpty(path)) {
        path = "/";
    }
    Cookie cookie = new Cookie(name, value);
    // maxAge0cookiemaxAge
    // if (maxAge > 0)
    // {
    cookie.setMaxAge(maxAge);
    // }
    cookie.setPath(path);
    // domain?cookiedomain
    if (!StringUtils.isEmpty(domain)) {
        cookie.setDomain(domain);
    }
    response.addCookie(cookie);
}

From source file:com.kingcore.framework.util.CookieUtils.java

/** 
 * Creates a Cookie with the specified name, value and max age,
 * and adds it to the response.//from w  w  w. j  av a  2  s .  co m
 * cookies  cookie?Base64 ?
 *    The form of the domain name is specified by RFC 2109. A domain name begins with a dot (.foo.com) 
 *       and means that the cookie is visible to servers in a specified Domain Name System (DNS) zone 
 *       (for example, www.foo.com, but not a.b.foo.com). By default, cookies are only returned to 
 *       the server that sent them.
 * @param name cookie's name.
 * @param value cookie's value.
 * @param maxAge the time cookie been keeped. the unit is second.
 *      age of the cookie in seconds,??Cookie.
 *      an integer specifying the maximum age of the cookie in seconds; 
 *      if negative, means the cookie is not stored; if zero, deletes the cookie.
 * @param res response Object.
 * @param needEncode ??Cookie?Base64?true
 * @param domain Cookie's domain
 */
public static void sendCookie(String name, String value, int maxAge, HttpServletResponse response,
        boolean needEncode, String domain) {

    try {
        if (needEncode) {
            value = Base64.encode(value.getBytes("utf-8")); //?
            //              value = new String(Base64.encode( value.getBytes("utf-8")), "utf-8" );   //utf-8
        }
        //System.out.println("value = " + value);
        Cookie cookie = new Cookie(name, value);//Hex.encode(value.getBytes()) );
        cookie.setMaxAge(maxAge);
        cookie.setPath("/");
        if (domain != null) {
            cookie.setDomain(domain); // domain
        }
        response.addCookie(cookie);

    } catch (UnsupportedEncodingException e) {
        log.debug("debug", e);
        /// e.pri ntStackTrace();
    }

}

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

/**
 * Logins the specified user from the specified request.
 * //ww w  .  ja va 2 s.  co  m
 * <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);
    }
}

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

/**
 * Adding a browser session cookie to browser.
 * @param cookieName/*  w w  w  . j a v  a  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);
}