Example usage for javax.servlet.http Cookie setMaxAge

List of usage examples for javax.servlet.http Cookie setMaxAge

Introduction

In this page you can find the example usage for javax.servlet.http Cookie setMaxAge.

Prototype

public void setMaxAge(int expiry) 

Source Link

Document

Sets the maximum age in seconds for this Cookie.

Usage

From source file:org.shareok.data.webserv.HomeController.java

@RequestMapping("/home")
public ModelAndView home(HttpServletRequest req, HttpServletResponse res) {

    ModelAndView model = new ModelAndView();
    model.setViewName("home");
    HttpSession session = (HttpSession) req.getSession(false);
    if (null != session) {
        RedisUser user = (RedisUser) session
                .getAttribute(ShareokdataManager.getSessionRedisUserAttributeName());
        if (null != user) {
            Cookie userCookie = new Cookie("userId", String.valueOf(user.getUserId()));
            userCookie.setMaxAge(30 * 60);
            res.addCookie(userCookie);//from   w ww .j  a  v  a  2 s . co m
            model.addObject("user", user);
            model.addObject("loginTime", session.getCreationTime());
        }
    }
    return model;
}

From source file:org.eclipse.dirigible.runtime.content.ThemesServlet.java

private void setCookieUser(HttpServletResponse resp, String themeName) {
    Cookie cookie = new Cookie(ICommonConstants.COOKIE_THEME, themeName);
    cookie.setMaxAge(30 * 24 * 60 * 60);
    cookie.setPath(SLASH);//from ww  w .j  ava  2  s .c  o m
    resp.addCookie(cookie);
}

From source file:org.uaa.security.core.LoginSuccessHandler.java

public void handle(UsernamePasswordToken token, HttpServletRequest request, HttpServletResponse response) {
    // set cookie
    Map<String, Object> session = new LinkedHashMap<String, Object>();
    session.put("uid", token.getUid());
    session.put("username", token.getUsername());
    session.put("password", token.getPassword());
    session.put("roles", token.getRoles());
    session.put("isAuthenticated", token.isAuthenticated());

    try {/*  w w w .j  ava2  s. c o m*/
        String sessionId = Crypto.encrypt(JsonFactory.toJson(session));
        log.debug(sessionId);
        Cookie cookie = new Cookie(ConfigUtil.getValue("SESSION_ID"), sessionId);
        cookie.setMaxAge(7 * 24 * 3600);
        response.addCookie(cookie);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:csns.web.controller.IndexController.java

@RequestMapping({ "/department/{dept}/", "/department/{dept}" })
public String index(@PathVariable String dept, ModelMap models, HttpServletResponse response) {
    Department department = departmentDao.getDepartment(dept);
    if (department == null)
        return "redirect:/";

    Cookie cookie = new Cookie("default-dept", dept);
    cookie.setPath("/");
    cookie.setMaxAge(100000000);
    response.addCookie(cookie);/*  w w  w.  ja v  a 2  s.  c o  m*/

    models.addAttribute("department", department);
    models.addAttribute("newses", newsDao.getNews(department));
    return "department/index";
}

From source file:org.codice.ddf.security.servlet.logout.LocalLogoutServlet.java

private void deleteJSessionId(HttpServletResponse response) {
    Cookie cookie = new Cookie("JSESSIONID", "");
    cookie.setMaxAge(0);
    cookie.setPath("/");
    cookie.setComment("EXPIRING COOKIE at " + System.currentTimeMillis());
    response.addCookie(cookie);/* ww  w.  j a  va 2  s.  c o m*/
}

From source file:de.berlios.jhelpdesk.web.preferences.LookAndFeelEditController.java

private Cookie createCookie(HttpServletRequest req, Locale loc) {
    Cookie cookie = new Cookie("jhd_locale", loc.getLanguage());
    cookie.setMaxAge(SECONDS_BY_WEEK);
    cookie.setPath(req.getContextPath());
    return cookie;
}

From source file:de.sainth.recipe.backend.rest.controller.LogoutController.java

@RequestMapping()
@ResponseStatus(HttpStatus.NO_CONTENT)/*from w w  w .  j a  v a2 s  .  c om*/
void logout(HttpServletRequest request, HttpServletResponse response) {
    if ("/logout".equals(request.getServletPath())) {
        Optional<Cookie> cookie = Arrays.stream(request.getCookies())
                .filter(c -> "recipe_bearer".equals(c.getName())).findFirst();
        if (cookie.isPresent()) {
            Cookie c = cookie.get();
            c.setValue("");
            c.setPath("/");
            c.setMaxAge(0);
            response.addCookie(c);
        }
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    }
}

From source file:com.test.servlet.LoginController.java

@Override
public void setResponse(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter writer = response.getWriter();
    String email = getParam(RequestParam.email.toString());
    String password = getParam(RequestParam.password.toString());
    if (Utility.isStringEmpty(email)) {
        writer.print(/*from  w w  w .  j av  a2  s  . co m*/
                Utility.generalErrorMessage(ResponseCode.email_not_provided.toString(), "Email is required!"));
        return;
    }
    if (Utility.isStringEmpty(password)) {
        writer.print(Utility.generalErrorMessage(ResponseCode.password_not_provided.toString(),
                "Password is required!"));
        return;
    }

    DBUtility dbUtil = new DBUtility(servlet);
    User user = dbUtil.getUser(email, password);
    if (user != null) {
        HttpSession session = request.getSession();
        session.setAttribute("user_id", user.getId());
        String sessionKey = SessionGenerator.getInstance().nextSessionId();
        Cookie cookie = new Cookie("auth_key", sessionKey);
        cookie.setMaxAge(Constants.COOKIE_AGE);
        response.addCookie(cookie);

        dbUtil.insertSession(sessionKey, user.getId());

        JSONObject jResponse = new JSONObject();
        jResponse.put(JSONKey.status.toString(), 0);
        //jResponse.put(JSONKey.auth_key.toString(), sessionKey);
        jResponse.put(JSONKey.user_info.toString(), user.toJSONObject());

        writer.print(jResponse.toString());
    } else {
        writer.print(Utility.generalErrorMessage(ResponseCode.email_doesnt_exist.toString(),
                "Email address not found"));
    }
}

From source file:org.cyclop.service.common.CookieStorage.java

protected void storeCookie(@NotNull CookieName name, @NotNull String value) {
    RequestCycle requestCycle = RequestCycle.get();
    if (requestCycle == null) {
        LOG.warn("RequestCycle is null - cannot read cookies");
        return;/*from   ww w.j a va 2 s. co m*/
    }
    WebResponse response = (WebResponse) requestCycle.getResponse();
    Cookie cookie = new Cookie(name.name(), value);
    cookie.setMaxAge(appConfig.cookie.expirySeconds);
    response.addCookie(cookie);
}

From source file:org.unidle.social.SignInAdapterImpl.java

@Override
public String signIn(final String userId, final Connection<?> connection, final NativeWebRequest request) {

    final Authentication authentication = new UsernamePasswordAuthenticationToken(userId, null);

    SecurityContextHolder.getContext().setAuthentication(authentication);

    final Cookie cookie = new Cookie(LAST_LOGIN_SOURCE.getName(), connection.createData().getProviderId());
    cookie.setMaxAge(LAST_LOGIN_SOURCE.getMaxAgeAs(SECONDS));
    cookie.setPath("/");

    request.getNativeResponse(HttpServletResponse.class).addCookie(cookie);

    return null;//ww w  . jav a 2 s. com
}