List of usage examples for javax.servlet.http Cookie setMaxAge
public void setMaxAge(int expiry)
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); cookie.setPath("/"); response.addCookie(cookie);//from w w w . jav a 2s. com }
From source file:org.exoplatform.social.webui.Utils.java
/** * /* w ww . j a va 2 s . com*/ * @param value */ public static void setCookies(String key, String value) { // removeCookie(key); // PortalRequestContext request = Util.getPortalRequestContext(); Cookie cookie = new Cookie(key, value); cookie.setPath(request.getRequest().getContextPath()); cookie.setMaxAge(Integer.MAX_VALUE); request.getResponse().addCookie(cookie); }
From source file:org.itracker.web.util.LoginUtilities.java
public static User setupSession(User user, String encPassword, HttpServletRequest request, HttpServletResponse response) {/*w ww .j a v a 2s.c o m*/ if (user == null) { logger.warn("setupSession: null user", (logger.isDebugEnabled() ? new RuntimeException() : null)); throw new IllegalArgumentException("null user"); } UserService userService = ServletContextUtils.getItrackerServices().getUserService(); if (logger.isDebugEnabled()) { logger.debug("Creating new session"); } HttpSession session = request.getSession(true); if (logger.isDebugEnabled()) { logger.debug("Setting session timeout to " + getConfiguredSessionTimeout() + " minutes"); } session.setMaxInactiveInterval(getConfiguredSessionTimeout() * 60); if (logger.isDebugEnabled()) { logger.debug("Setting session tracker"); } session.setAttribute(Constants.SESSION_TRACKER_KEY, new SessionTracker(user.getLogin(), session.getId())); if (logger.isDebugEnabled()) { logger.debug("Setting user information"); } session.setAttribute(Constants.USER_KEY, user); if (logger.isDebugEnabled()) { logger.debug("Setting preferences for user " + user.getLogin()); } UserPreferences userPrefs = user.getPreferences(); // TODO : this is a hack, remove when possible if (userPrefs == null) { logger.warn("setupSession: got user with no preferences!: " + user + " (prefs: " + user.getPreferences() + ")"); userPrefs = new UserPreferences(); } session.setAttribute(Constants.PREFERENCES_KEY, userPrefs); if (logger.isDebugEnabled()) { logger.debug("Setting user " + user + " locale to " + ITrackerResources.getLocale(userPrefs.getUserLocale())); } session.setAttribute(Constants.LOCALE_KEY, ITrackerResources.getLocale(userPrefs.getUserLocale())); // TODO: cookie could be removed Cookie cookie = new Cookie(Constants.COOKIE_NAME, ""); cookie.setPath(request.getContextPath()); cookie.setValue(""); cookie.setMaxAge(0); response.addCookie(cookie); if (logger.isDebugEnabled()) { logger.debug("Setting permissions for user " + user.getLogin()); } Map<Integer, Set<PermissionType>> usersMapOfProjectIdsAndSetOfPermissionTypes = userService .getUsersMapOfProjectIdsAndSetOfPermissionTypes(user, AuthenticationConstants.REQ_SOURCE_WEB); session.setAttribute(Constants.PERMISSIONS_KEY, usersMapOfProjectIdsAndSetOfPermissionTypes); // Reset some session forms session.setAttribute(Constants.SEARCH_QUERY_KEY, null); SessionManager.clearSessionNeedsReset(user.getLogin()); if (logger.isDebugEnabled()) { logger.debug("User session data updated."); } return user; }
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 w w . ja v a 2s . co m * * @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:com.tc.utils.XSPUtils.java
public static void logout(String url) { HttpSession httpSession = XSPUtils.getHttpSession(); if (httpSession == null) { return;/*from w w w . j ava 2 s. c om*/ } String sessionId = XSPUtils.getHttpSession().getId(); XSPUtils.getRequest().getSession(false).invalidate(); //wipe out the cookies for (Cookie cookie : getCookies()) { cookie.setValue(StringCache.EMPTY); cookie.setPath("/"); cookie.setMaxAge(0); XSPUtils.getResponse().addCookie(cookie); } try { NotesContext notesContext = NotesContext.getCurrent(); notesContext.getModule().removeSession(sessionId); XSPUtils.externalContext().redirect(url); } catch (IOException e) { logger.log(Level.SEVERE, null, e); } }
From source file:com.tc.utils.XSPUtils.java
public static void logout() { HttpSession httpSession = XSPUtils.getHttpSession(); if (httpSession == null) { return;/*from w w w. j a v a 2 s . c o m*/ } String sessionId = XSPUtils.getHttpSession().getId(); String url = XSPUtils.externalContext().getRequestContextPath() + "?logout&redirectto=" + externalContext().getRequestContextPath(); XSPUtils.getRequest().getSession(false).invalidate(); //wipe out the cookies for (Cookie cookie : getCookies()) { cookie.setValue(StringCache.EMPTY); cookie.setPath("/"); cookie.setMaxAge(0); XSPUtils.getResponse().addCookie(cookie); } try { NotesContext notesContext = NotesContext.getCurrent(); notesContext.getModule().removeSession(sessionId); XSPUtils.externalContext().redirect(url); } catch (IOException e) { logger.log(Level.SEVERE, null, e); } }
From source file:com.vmware.identity.openidconnect.sample.RelyingPartyController.java
private static Cookie logoutSessionCookie() { Cookie sessionCookie = new Cookie(SESSION_COOKIE_NAME, ""); sessionCookie.setPath("/openidconnect-sample-rp"); sessionCookie.setSecure(true);/*from ww w . j a va 2 s.c om*/ sessionCookie.setHttpOnly(true); sessionCookie.setMaxAge(0); return sessionCookie; }
From source file:io.lavagna.web.helper.UserSession.java
public static void authenticateUserIfRemembered(HttpServletRequest req, HttpServletResponse resp, UserRepository userRepository) { Cookie c; if (isUserAuthenticated(req) || (c = getCookie(req, "LAVAGNA_REMEMBER_ME")) == null) { return;//from w ww.ja v a 2 s.c om } 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.google.gsa.valve.modules.utils.CookieManagement.java
/** * Transforms Apache cookies into Servlet Cookies * /*from w ww. j ava2 s.c o m*/ * @param apacheCookie apache cookie * * @return servlet cookie */ public static javax.servlet.http.Cookie transformApacheCookie( org.apache.commons.httpclient.Cookie apacheCookie) { javax.servlet.http.Cookie newCookie = null; if (apacheCookie != null) { Date expire = apacheCookie.getExpiryDate(); int maxAge = -1; if (expire == null) { maxAge = -1; } else { Date now = Calendar.getInstance().getTime(); // Convert milli-second to second Long second = new Long((expire.getTime() - now.getTime()) / 1000); maxAge = second.intValue(); } newCookie = new javax.servlet.http.Cookie(apacheCookie.getName(), apacheCookie.getValue()); //Hardcoding the domain newCookie.setDomain(apacheCookie.getDomain()); newCookie.setPath(apacheCookie.getPath()); newCookie.setMaxAge(maxAge); newCookie.setSecure(apacheCookie.getSecure()); } return newCookie; }
From source file:ai.susi.server.AbstractAPIHandler.java
/** * Checks a request for valid login data, either a existing session, a cookie or an access token * @return user identity if some login is active, anonymous identity otherwise *///from w w w . ja v a 2 s. co m public static ClientIdentity getIdentity(HttpServletRequest request, HttpServletResponse response, Query query) { if (getLoginCookie(request) != null) { // check if login cookie is set Cookie loginCookie = getLoginCookie(request); ClientCredential credential = new ClientCredential(ClientCredential.Type.cookie, loginCookie.getValue()); Authentication authentication = new Authentication(credential, DAO.authentication); if (authentication.getIdentity() != null && authentication.checkExpireTime()) { //reset cookie validity time authentication.setExpireTime(defaultCookieTime); loginCookie.setMaxAge(defaultCookieTime.intValue()); loginCookie.setPath("/"); // bug. The path gets reset response.addCookie(loginCookie); return authentication.getIdentity(); } authentication.delete(); // delete cookie if set deleteLoginCookie(response); Log.getLog().info("Invalid login try via cookie from host: " + query.getClientHost()); } else if (request.getSession().getAttribute("identity") != null) { // check session is set return (ClientIdentity) request.getSession().getAttribute("identity"); } else if (request.getParameter("access_token") != null) { // access tokens can be used by api calls, somehow the stateless equivalent of sessions for browsers ClientCredential credential = new ClientCredential(ClientCredential.Type.access_token, request.getParameter("access_token")); Authentication authentication = new Authentication(credential, DAO.authentication); // check if access_token is valid if (authentication.getIdentity() != null) { ClientIdentity identity = authentication.getIdentity(); if (authentication.checkExpireTime()) { Log.getLog().info("login for user: " + identity.getName() + " via access token from host: " + query.getClientHost()); if ("true".equals(request.getParameter("request_session"))) { request.getSession().setAttribute("identity", identity); } if (authentication.has("one_time") && authentication.getBoolean("one_time")) { authentication.delete(); } return identity; } } Log.getLog().info("Invalid access token from host: " + query.getClientHost()); return getAnonymousIdentity(query.getClientHost()); } return getAnonymousIdentity(query.getClientHost()); }