List of usage examples for javax.servlet.http HttpServletResponse addCookie
public void addCookie(Cookie cookie);
From source file:net.shopxx.util.CookieUtils.java
/** * cookie/* www . j a va 2 s .c o m*/ * * @param request * HttpServletRequest * @param response * HttpServletResponse * @param name * cookie?? * @param value * cookie * @param path * * @param maxAge * (??: ) * @param domain * * @param secure * ?? */ public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, String path, Integer maxAge, String domain, Boolean secure) { Assert.notNull(request); Assert.notNull(response); Assert.hasText(name); try { value = URLEncoder.encode(value, "UTF-8"); Cookie cookie = new Cookie(name, value); if (StringUtils.isNotEmpty(path)) { cookie.setPath(path); } if (maxAge != null) { cookie.setMaxAge(maxAge); } if (StringUtils.isNotEmpty(domain)) { cookie.setDomain(domain); } if (secure != null) { cookie.setSecure(secure); } response.addCookie(cookie); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
From source file:io.lavagna.web.helper.UserSession.java
public static void authenticateUserIfRemembered(HttpServletRequest req, HttpServletResponse resp, UserRepository userRepository) { Cookie c;/*from ww w .j a va 2 s. com*/ if (isUserAuthenticated(req) || (c = getCookie(req, "LAVAGNA_REMEMBER_ME")) == null) { return; } ImmutablePair<Integer, String> uIdToken = extractUserIdAndToken(c.getValue()); if (uIdToken != null && userRepository.rememberMeTokenExists(uIdToken.getLeft(), uIdToken.getRight())) { userRepository.deleteRememberMeToken(uIdToken.getLeft(), uIdToken.getRight()); User user = userRepository.findById(uIdToken.getLeft()); setUser(user.getId(), user.isAnonymous(), req, resp, userRepository, true); } else { // delete cookie because it's invalid c.setMaxAge(0); c.setValue(null); resp.addCookie(c); } }
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 v a2 s . 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.o2o.util.WebUtils.java
public static void clearCookie(HttpServletRequest request, HttpServletResponse response, String path, String domain) {//from ww w . j ava2 s .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(); } }
From source file:cn.vlabs.umt.ui.servlet.login.LoginMethod.java
/** * ??cookie/*from w w w . jav a 2 s. c om*/ * */ 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.iterzp.momo.utils.WebUtils.java
/** * cookie/* w w w . ja v a2 s .c om*/ * * @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:org.carewebframework.ui.FrameworkWebSupport.java
/** * Sets a cookie into the response. Cookies are URLEncoded for consistency (Version 0+ of * Cookies)//from w ww.j av a2s . com * * @param cookieName Name of cookie. * @param value Value of cookie. If null, the cookie is removed from the client if it exists. * @param httpResponse Response object. * @param httpRequest Request object. * @return Newly created cookie. * @throws IllegalArgumentException if cookieName, httpResponse, or httpRequest arguments are * null */ public static Cookie setCookie(final String cookieName, String value, final HttpServletResponse httpResponse, final HttpServletRequest httpRequest) { Validate.notNull(httpResponse, "The httpResponse must not be null"); Cookie cookie = getCookie(cookieName, httpRequest); if (value != null) { value = encodeCookieValue(value); } if (cookie == null) { if (value == null) { return null; } cookie = new Cookie(cookieName, value); } else if (value == null) { cookie.setMaxAge(0); } else { cookie.setValue(value); } if (httpRequest.isSecure()) { cookie.setSecure(true); } httpResponse.addCookie(cookie); return cookie; }
From source file:spring.travel.site.controllers.LogoutController.java
@RequestMapping(method = RequestMethod.GET) public View logout(HttpServletResponse response) { Cookie cookie = new Cookie(cookieName, ""); cookie.setMaxAge(0);/* w w w . j a v a 2s .c o m*/ response.addCookie(cookie); return new RedirectView("/"); }
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;/*from w w w. j a v a2 s. c om*/ } // 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.ofbizcn.securityext.login.LoginEvents.java
public static void setUsername(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); Delegator delegator = (Delegator) request.getAttribute("delegator"); String domain = EntityUtilProperties.getPropertyValue("url.properties", "cookie.domain", delegator); // first try to get the username from the cookie synchronized (session) { if (UtilValidate.isEmpty(getUsername(request))) { // create the cookie and send it back Cookie cookie = new Cookie(usernameCookieName, request.getParameter("USERNAME")); cookie.setMaxAge(60 * 60 * 24 * 365); cookie.setPath("/"); cookie.setDomain(domain);/* w w w .j ava 2s . c om*/ response.addCookie(cookie); } } }