List of usage examples for javax.servlet.http Cookie setMaxAge
public void setMaxAge(int expiry)
From source file:com.persistent.cloudninja.controller.CloudNinjaAuthFilter.java
private void removeCookie(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String authCookieName) {//from w ww . j ava 2 s . c o m // Cookie cookieToBeRemoved = AuthFilterUtils.checkForPreExistentCookie(httpServletRequest, authCookieName); Cookie cookie = new Cookie(authCookieName, null); cookie.setMaxAge(0); cookie.setPath("/"); httpServletResponse.addCookie(cookie); }
From source file:com.persistent.cloudninja.controller.TenantProfileController.java
@RequestMapping(value = "{tenantId}/showTenantProfilePage.htm", method = RequestMethod.POST) public ModelAndView showProfilePage(HttpServletRequest request, HttpServletResponse response, @CookieValue(value = "CLOUDNINJAAUTH", required = false) String cookie, @ModelAttribute("logoFileDTO") LogoFileDTO logoFileDTO, BindingResult result) { // validate the file uploaded for logo logoFileDTOValidator.validate(logoFileDTO, result); // if no errors in validation then only process the request if (!result.hasErrors()) { if (cookie == null) { cookie = request.getAttribute("cookieNameAttr").toString(); }//from w ww .j av a 2 s.co m String tenantId = AuthFilterUtils .getFieldValueFromCookieString(CloudNinjaConstants.COOKIE_TENANTID_PREFIX, cookie); String logoFileName = fileUploadService.fileUploadService(logoFileDTO.getFile(), tenantId); String logoCookieName = "CLOUDNINJALOGO"; // update the logo cookie with the new logo file Cookie cookies[] = request.getCookies(); Cookie logoCookie = null; if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals(logoCookieName)) { logoCookie = cookies[i]; logoCookie.setValue(logoFileName); logoCookie.setMaxAge(-1); logoCookie.setPath("/"); response.addCookie(logoCookie); break; } } } } return new ModelAndView("tenantProfilePage", "logoFileDTO", logoFileDTO); }
From source file:org.craftercms.commons.http.CookieManager.java
/** * Add a new cookie, using the configured domain, path and max age, to the response. * * @param name the name of the cookie//from w w w.j a va 2 s . c o m * @param value the value of the cookie */ public void addCookie(String name, String value, HttpServletResponse response) { Cookie cookie = new Cookie(name, value); cookie.setHttpOnly(httpOnly); cookie.setSecure(secure); if (StringUtils.isNotEmpty(domain)) { cookie.setDomain(domain); } if (StringUtils.isNotEmpty(path)) { cookie.setPath(path); } if (maxAge != null) { cookie.setMaxAge(maxAge); } response.addCookie(cookie); logger.debug(LOG_KEY_ADDED_COOKIE, name); }
From source file:net.sourceforge.subsonic.service.PlayerService.java
/** * Returns the player associated with the given HTTP request. If no such player exists, a new * one is created.//from ww w . j a v a 2 s. c o m * * @param request The HTTP request. * @param response The HTTP response. * @param remoteControlEnabled Whether this method should return a remote-controlled player. * @param isStreamRequest Whether the HTTP request is a request for streaming data. * @return The player associated with the given HTTP request. */ public synchronized Player getPlayer(HttpServletRequest request, HttpServletResponse response, boolean remoteControlEnabled, boolean isStreamRequest) { // Find by 'player' request parameter. Player player = getPlayerById(request.getParameter("player")); // Find in session context. if (player == null && remoteControlEnabled) { String playerId = (String) request.getSession().getAttribute("player"); if (playerId != null) { player = getPlayerById(playerId); } } // Find by cookie. String username = securityService.getCurrentUsername(request); if (player == null && remoteControlEnabled) { player = getPlayerById(getPlayerIdFromCookie(request, username)); } // Make sure we're not hijacking the player of another user. if (player != null && player.getUsername() != null && username != null && !player.getUsername().equals(username)) { player = null; } // Look for player with same IP address and user name. if (player == null) { player = getPlayerByIpAddressAndUsername(request.getRemoteAddr(), username); // Don't use this player if it's used by REST API. if (player != null && player.getClientId() != null) { player = null; } } // If no player was found, create it. if (player == null) { player = new Player(); createPlayer(player); // LOG.debug("Created player " + player.getId() + " (remoteControlEnabled: " + remoteControlEnabled + // ", isStreamRequest: " + isStreamRequest + ", username: " + username + // ", ip: " + request.getRemoteAddr() + ")."); } // Update player data. boolean isUpdate = false; if (username != null && player.getUsername() == null) { player.setUsername(username); isUpdate = true; } if (player.getIpAddress() == null || isStreamRequest || (!isPlayerConnected(player) && player.isDynamicIp() && !request.getRemoteAddr().equals(player.getIpAddress()))) { player.setIpAddress(request.getRemoteAddr()); isUpdate = true; } String userAgent = request.getHeader("user-agent"); if (isStreamRequest) { player.setType(userAgent); player.setLastSeen(new Date()); isUpdate = true; } if (isUpdate) { updatePlayer(player); } // Set cookie in response. if (response != null) { String cookieName = COOKIE_NAME + "-" + StringUtil.utf8HexEncode(username); Cookie cookie = new Cookie(cookieName, player.getId()); cookie.setMaxAge(COOKIE_EXPIRY); String path = request.getContextPath(); if (StringUtils.isEmpty(path)) { path = "/"; } cookie.setPath(path); response.addCookie(cookie); } // Save player in session context. if (remoteControlEnabled) { request.getSession().setAttribute("player", player.getId()); } return player; }
From source file:de.hska.ld.core.controller.HomeController.java
@RequestMapping("/logout") public String logout(HttpServletRequest request, HttpServletResponse response, Principal p) throws ServletException { request.logout();/*from w w w .ja v a 2s .com*/ javax.servlet.http.Cookie cookie = new Cookie("sessionID", ""); cookie.setPath("/"); if (!"localhost".equals(env.getProperty("module.core.oidc.server.endpoint.main.domain"))) { cookie.setDomain(env.getProperty("module.core.oidc.server.endpoint.main.domain")); } cookie.setMaxAge(0); response.addCookie(cookie); return "redirect:"; }
From source file:de.eorganization.hoopla.server.services.LoginServiceImpl.java
@Override public LoginInfo login(String requestUri) { LoginInfo loginInfo = new LoginInfo(); loginInfo.setLoggedIn(false);//from w w w .j a va 2 s .co m loginInfo.setLoginUrl(userService.createLoginURL(requestUri)); Map<String, String> cookies = CookiesUtil.getCookiesStringMap(getThreadLocalRequest().getCookies()); log.info("Got cookies " + cookies); String oauthService = cookies.get("oauth.service"); log.info("Logging in with OAuth service " + oauthService); if (oauthService != null) { try { String accessTokenString = cookies.get("oauth.accessToken"); String accessSecret = cookies.get("oauth.secret"); if (accessTokenString == null) return loginInfo; log.info("Retrieved access token " + accessTokenString); Token accessToken = new Token(accessTokenString, accessSecret); log.info("Token object " + accessToken.getToken() + ", " + accessToken.getSecret()); OAuth2Provider provider = OAuth2Provider.valueOf(oauthService); OAuthService service = provider.getOAuthService(); Cookie serviceTokenCookie = new Cookie("oauth.service", provider.toString()); serviceTokenCookie.setMaxAge(14 * 24 * 60 * 60); serviceTokenCookie.setPath("/"); getThreadLocalResponse().addCookie(serviceTokenCookie); Cookie accessTokenCookie = new Cookie("oauth.accessToken", accessTokenString); accessTokenCookie.setMaxAge(14 * 24 * 60 * 60); accessTokenCookie.setPath("/"); getThreadLocalResponse().addCookie(accessTokenCookie); Cookie accessSecretCookie = new Cookie("oauth.secret", accessSecret); accessSecretCookie.setMaxAge(14 * 24 * 60 * 60); accessSecretCookie.setPath("/"); getThreadLocalResponse().addCookie(accessSecretCookie); if (OAuth2Provider.GOOGLE.equals(provider)) { OAuthRequest req = new OAuthRequest(Verb.GET, "https://www.googleapis.com/oauth2/v1/userinfo"); service.signRequest(accessToken, req); Response response = req.send(); log.info("Requested user info from google: " + response.getBody()); JSONObject googleUserInfo = new JSONObject(response.getBody()); log.info("got user info: " + googleUserInfo.getString("given_name") + ", " + googleUserInfo.getString("family_name")); Member tempMember = new HooplaServiceImpl() .findMemberBySocialId(googleUserInfo.getString("id")); if (tempMember == null) { tempMember = new Member(); tempMember.setSocialId(googleUserInfo.getString("id")); tempMember.setFirstname(googleUserInfo.getString("given_name")); tempMember.setLastname(googleUserInfo.getString("family_name")); tempMember.setNickname(googleUserInfo.getString("name")); tempMember.setProfilePic(googleUserInfo.getString("picture")); req = new OAuthRequest(Verb.GET, "https://www.googleapis.com/plus/v1/people/me"); service.signRequest(accessToken, req); response = req.send(); log.info("Requested more user info from google: " + response.getBody()); JSONObject googleUserInfo2 = new JSONObject(response.getBody()); log.info("got user info: " + googleUserInfo2.getString("nickname") + ", " + googleUserInfo2.getString("displayName")); if (googleUserInfo2 != null && googleUserInfo2.getJSONArray("emails") != null) for (int i = 0; i < googleUserInfo2.getJSONArray("emails").length(); i++) { JSONObject emailInfo = googleUserInfo2.getJSONArray("emails").optJSONObject(i); if (emailInfo != null && emailInfo.getBoolean("primary")) { tempMember.setEmail(emailInfo.getString("value")); tempMember = new HooplaServiceImpl().registerMember(tempMember); loginInfo.setLoggedIn(true); break; } } } else loginInfo.setLoggedIn(true); loginInfo.setMember(tempMember); } else if (OAuth2Provider.TWITTER.equals(provider)) { OAuthRequest req = new OAuthRequest(Verb.GET, "https://api.twitter.com/1/account/verify_credentials.json"); service.signRequest(accessToken, req); log.info("Requesting from twitter " + req.getCompleteUrl()); Response response = req.send(); log.info("Requested user info from twitter: " + response.getBody()); JSONObject twitterUserInfo = new JSONObject(response.getBody()); log.info("got user info: " + twitterUserInfo.getString("name") + ", " + twitterUserInfo.getString("screen_name")); Member tempMember = new HooplaServiceImpl() .findMemberBySocialId(new Integer(twitterUserInfo.getInt("id")).toString()); if (tempMember == null) { tempMember = new Member(); tempMember.setSocialId(new Integer(twitterUserInfo.getInt("id")).toString()); tempMember.setFirstname(twitterUserInfo.getString("name").split(" ")[0]); tempMember.setLastname(twitterUserInfo.getString("name").split(" ", 2)[1]); tempMember.setNickname(twitterUserInfo.getString("screen_name")); tempMember.setProfilePic(twitterUserInfo.getString("profile_image_url")); } else loginInfo.setLoggedIn(true); loginInfo.setMember(tempMember); } else if (OAuth2Provider.FACEBOOK.equals(provider)) { OAuthRequest req = new OAuthRequest(Verb.GET, "https://graph.facebook.com/me"); service.signRequest(accessToken, req); log.info("Requesting from facebook " + req.getCompleteUrl()); Response response = req.send(); log.info("Requested user info from facebook: " + response.getBody()); JSONObject facebookUserInfo = new JSONObject(response.getBody()); log.info("got user info: " + facebookUserInfo.getString("name") + ", " + facebookUserInfo.getString("username")); Member tempMember = new HooplaServiceImpl() .findMemberBySocialId(facebookUserInfo.getString("id")); if (tempMember == null) { tempMember = new Member(); tempMember.setSocialId(new Integer(facebookUserInfo.getString("id")).toString()); tempMember.setFirstname(facebookUserInfo.getString("first_name")); tempMember.setLastname(facebookUserInfo.getString("last_name")); tempMember.setNickname(facebookUserInfo.getString("username")); tempMember.setProfilePic("https://graph.facebook.com/" + facebookUserInfo.getString("id") + "/picture?type=large"); tempMember.setEmail(facebookUserInfo.getString("email")); tempMember = new HooplaServiceImpl().registerMember(tempMember); } loginInfo.setLoggedIn(true); loginInfo.setMember(tempMember); } loginInfo.setLogoutUrl("/logout/oauth"); log.info("Set loginInfo to " + loginInfo); return loginInfo; } catch (Exception e) { log.log(Level.WARNING, e.getLocalizedMessage(), e); } } else { User user = userService.getCurrentUser(); if (userService.isUserLoggedIn() && user != null) { loginInfo.setLoggedIn(true); loginInfo.setMember(new HooplaServiceImpl().saveOrGetMember(user)); loginInfo.setLogoutUrl(userService.createLogoutURL(requestUri)); } log.info("Logged in with google services " + loginInfo); } return loginInfo; }
From source file:com.xwiki.authentication.AbstractSSOAuthServiceImpl.java
protected XWikiUser checkAuthSSO(String username, String password, XWikiContext context) throws XWikiException { Cookie cookie; LOG.debug("checkAuth"); LOG.debug("Action: " + context.getAction()); if (context.getAction().startsWith("logout")) { cookie = getCookie(COOKIE_NAME, context); if (cookie != null) { cookie.setMaxAge(0); context.getResponse().addCookie(cookie); }//from ww w .j a v a 2 s . c o m return null; } Principal principal = null; if (LOG.isDebugEnabled()) { Cookie[] cookies = context.getRequest().getCookies(); if (cookies != null) { for (Cookie c : cookies) { LOG.debug("CookieList: " + c.getName() + " => " + c.getValue()); } } } cookie = getCookie(COOKIE_NAME, context); if (cookie != null) { LOG.debug("Found Cookie"); String uname = decryptText(cookie.getValue(), context); if (uname != null) { principal = new SimplePrincipal(uname); } } XWikiUser user; // Authenticate if (principal == null) { principal = authenticate(username, password, context); if (principal == null) { return null; } LOG.debug("Saving auth cookie"); String encuname = encryptText(principal.getName().contains(":") ? principal.getName() : context.getDatabase() + ":" + principal.getName(), context); Cookie usernameCookie = new Cookie(COOKIE_NAME, encuname); usernameCookie.setMaxAge(-1); usernameCookie.setPath("/"); context.getResponse().addCookie(usernameCookie); user = new XWikiUser(principal.getName()); } else { user = new XWikiUser(principal.getName().startsWith(context.getDatabase()) ? principal.getName().substring(context.getDatabase().length() + 1) : principal.getName()); } return user; }
From source file:com.yahoo.yos.YahooFilter.java
public void clearSession(HttpServletRequest req, HttpServletResponse res) { if (logger.isDebugEnabled()) { logger.debug("clear session requested"); }/*from ww w. j a v a2 s. c o m*/ Cookie at = new Cookie("yosdk_at", ""); at.setMaxAge(0); Cookie rt = new Cookie("yosdk_rt", ""); rt.setMaxAge(0); res.addCookie(at); res.addCookie(rt); req.setAttribute("yahooSession", null); req.setAttribute("yahooRedirect", null); }
From source file:de.eorganization.crawler.server.services.LoginServiceImpl.java
@Override public LoginInfo login(String requestUri) throws Exception { LoginInfo loginInfo = new LoginInfo(); loginInfo.setLoggedIn(false);/*from w ww. ja v a 2 s . c o m*/ loginInfo.setLoginUrl(userService.createLoginURL(requestUri)); Map<String, String> cookies = CookiesUtil.getCookiesStringMap(getThreadLocalRequest().getCookies()); log.info("Got cookies " + cookies); String oauthService = cookies.get("oauth.service"); log.info("Logging in with OAuth service " + oauthService); if (oauthService != null) { try { String accessTokenString = cookies.get("oauth.accessToken"); String accessSecret = cookies.get("oauth.secret"); if (accessTokenString == null) return loginInfo; log.info("Retrieved access token " + accessTokenString); Token accessToken = new Token(accessTokenString, accessSecret); log.info("Token object " + accessToken.getToken() + ", " + accessToken.getSecret()); OAuth2Provider provider = OAuth2Provider.valueOf(oauthService); OAuthService service = provider.getOAuthService(); Cookie serviceTokenCookie = new Cookie("oauth.service", provider.toString()); serviceTokenCookie.setMaxAge(14 * 24 * 60 * 60); serviceTokenCookie.setPath("/"); getThreadLocalResponse().addCookie(serviceTokenCookie); Cookie accessTokenCookie = new Cookie("oauth.accessToken", accessTokenString); accessTokenCookie.setMaxAge(14 * 24 * 60 * 60); accessTokenCookie.setPath("/"); getThreadLocalResponse().addCookie(accessTokenCookie); Cookie accessSecretCookie = new Cookie("oauth.secret", accessSecret); accessSecretCookie.setMaxAge(14 * 24 * 60 * 60); accessSecretCookie.setPath("/"); getThreadLocalResponse().addCookie(accessSecretCookie); if (OAuth2Provider.GOOGLE.equals(provider)) { OAuthRequest req = new OAuthRequest(Verb.GET, "https://www.googleapis.com/oauth2/v1/userinfo"); service.signRequest(accessToken, req); Response response = req.send(); log.info("Requested user info from google: " + response.getBody()); JSONObject googleUserInfo = new JSONObject(response.getBody()); log.info("got user info: " + googleUserInfo.getString("given_name") + ", " + googleUserInfo.getString("family_name")); Member tempMember = AmiManager.findMemberBySocialId(googleUserInfo.getString("id")); if (tempMember == null) { tempMember = new Member(); tempMember.setSocialId(googleUserInfo.getString("id")); tempMember.setFirstname(googleUserInfo.getString("given_name")); tempMember.setLastname(googleUserInfo.getString("family_name")); tempMember.setNickname(googleUserInfo.getString("name")); tempMember.setProfilePic(googleUserInfo.getString("picture")); req = new OAuthRequest(Verb.GET, "https://www.googleapis.com/plus/v1/people/me"); service.signRequest(accessToken, req); response = req.send(); log.info("Requested more user info from google: " + response.getBody()); JSONObject googleUserInfo2 = new JSONObject(response.getBody()); log.info("got user info: " + googleUserInfo2.getString("nickname") + ", " + googleUserInfo2.getString("displayName")); if (googleUserInfo2 != null && googleUserInfo2.getJSONArray("emails") != null) for (int i = 0; i < googleUserInfo2.getJSONArray("emails").length(); i++) { JSONObject emailInfo = googleUserInfo2.getJSONArray("emails").optJSONObject(i); if (emailInfo != null && emailInfo.getBoolean("primary")) { tempMember.setEmail(emailInfo.getString("value")); tempMember = AmiManager.registerMember(tempMember); loginInfo.setLoggedIn(true); break; } } } else loginInfo.setLoggedIn(true); loginInfo.setMember(tempMember); } else if (OAuth2Provider.TWITTER.equals(provider)) { OAuthRequest req = new OAuthRequest(Verb.GET, "https://api.twitter.com/1/account/verify_credentials.json"); service.signRequest(accessToken, req); log.info("Requesting from twitter " + req.getCompleteUrl()); Response response = req.send(); log.info("Requested user info from twitter: " + response.getBody()); JSONObject twitterUserInfo = new JSONObject(response.getBody()); log.info("got user info: " + twitterUserInfo.getString("name") + ", " + twitterUserInfo.getString("screen_name")); Member tempMember = AmiManager .findMemberBySocialId(new Integer(twitterUserInfo.getInt("id")).toString()); if (tempMember == null) { tempMember = new Member(); tempMember.setSocialId(new Integer(twitterUserInfo.getInt("id")).toString()); tempMember.setFirstname(twitterUserInfo.getString("name").split(" ")[0]); tempMember.setLastname(twitterUserInfo.getString("name").split(" ", 2)[1]); tempMember.setNickname(twitterUserInfo.getString("screen_name")); tempMember.setProfilePic(twitterUserInfo.getString("profile_image_url")); } else loginInfo.setLoggedIn(true); loginInfo.setMember(tempMember); } else if (OAuth2Provider.FACEBOOK.equals(provider)) { OAuthRequest req = new OAuthRequest(Verb.GET, "https://graph.facebook.com/me"); service.signRequest(accessToken, req); log.info("Requesting from facebook " + req.getCompleteUrl()); Response response = req.send(); log.info("Requested user info from facebook: " + response.getBody()); JSONObject facebookUserInfo = new JSONObject(response.getBody()); log.info("got user info: " + facebookUserInfo.getString("name") + ", " + facebookUserInfo.getString("username")); Member tempMember = AmiManager.findMemberBySocialId(facebookUserInfo.getString("id")); if (tempMember == null) { tempMember = new Member(); tempMember.setSocialId(new Integer(facebookUserInfo.getString("id")).toString()); tempMember.setFirstname(facebookUserInfo.getString("first_name")); tempMember.setLastname(facebookUserInfo.getString("last_name")); tempMember.setNickname(facebookUserInfo.getString("username")); tempMember.setProfilePic("https://graph.facebook.com/" + facebookUserInfo.getString("id") + "/picture?type=large"); tempMember.setEmail(facebookUserInfo.getString("email")); tempMember = AmiManager.registerMember(tempMember); } loginInfo.setLoggedIn(true); loginInfo.setMember(tempMember); } loginInfo.setLogoutUrl("/logout/oauth"); log.info("Set loginInfo to " + loginInfo); return loginInfo; } catch (OverQuotaException oqe) { log.log(Level.WARNING, oqe.getLocalizedMessage(), oqe); throw new OutOfQuotaException("Out of Quota!", oqe); } catch (Exception e) { log.log(Level.WARNING, e.getLocalizedMessage(), e); } } else { User user = userService.getCurrentUser(); if (userService.isUserLoggedIn() && user != null) { loginInfo.setLoggedIn(true); loginInfo.setMember(AmiManager.saveOrGetMember(user)); loginInfo.setLogoutUrl(userService.createLogoutURL(requestUri)); } log.info("Logged in with google services " + loginInfo); } return loginInfo; }
From source file:org.craftercms.cstudio.share.servlet.CookieManagerImpl.java
public void putCookieValue(HttpServletRequest request, HttpServletResponse response, String path, String key, int age, Serializable value) throws CStudioException { String cookieValue = null;//from ww w . j ava 2s .c o m if (_encryptCookies) { cookieValue = getEncryptedObjectAsString(value); } else { cookieValue = value.toString(); } Cookie cookie = new Cookie(key, cookieValue); if (!StringUtils.isEmpty(path)) { cookie.setPath(path); } else { cookie.setPath("/"); } cookie.setMaxAge(age); //if(request.getServerName().indexOf(".") != -1) { // String validForDomain = request.getServerName().substring(request.getServerName().indexOf(".")); // cookie.setDomain(validForDomain); //} if (_cookieDomain != null) { cookie.setDomain(_cookieDomain); } response.addCookie(cookie); }