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:com.orangeleap.webtools.controller.json.LogoutController.java

@RequestMapping("/logout.json")
public void logout(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final String guid = request.getParameter("guid");

    final String sessionId = request.getParameter("sessionId");

    if (StringUtils.isNotBlank(sessionId)) {
        final Cookie sessionCookies[] = request.getCookies();
        Cookie sessionCookie = null;/* w w w.  j  a  v  a  2 s  .  c o m*/

        for (final Cookie aCookie : sessionCookies) {
            if (aCookie.getName().equals("sessionId")) {
                aCookie.setMaxAge(0);
                aCookie.setValue("");
                sessionCookie = aCookie;
                break;
            }
        }
        if (sessionCookie == null) {
            sessionCookie = new Cookie("sessionId", "");
        }
        response.addCookie(sessionCookie);
        sessionCache.remove(sessionId);
    }

    final Widget widget = widgetService.getWidget(guid);
    final String authenticationUrl = widget.getWidgetAuthenticationURL();

    response.sendRedirect(authenticationUrl);
}

From source file:Controller.login.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from  w  w  w. jav a  2s  .  c om
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String email = request.getParameter("inputEmail");
    String password = request.getParameter("inputPassword");
    String remember = request.getParameter("remember");

    System.out.println("remember ne mnow " + email);

    LoginImplementation loginCheckObject = new LoginImplementation();

    JSONObject userData = (loginCheckObject.loginNow(email, password, remember));
    System.out.println(userData);
    if (userData.has("error")) {
        RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
        request.setAttribute("loginError", "error");
        rd.include(request, response);

    } else {

        if ("yes".equals(remember)) {
            System.out.println("apples");
            HttpSession session = request.getSession();
            session.setAttribute("user", userData);
            //setting session to expiry in 30 mins
            session.setMaxInactiveInterval(30 * 60);
            Cookie userName = new Cookie("user", email);
            userName.setMaxAge(30 * 60);
            response.addCookie(userName);
        }
        request.setAttribute("userData", userData);
        RequestDispatcher rd = request.getRequestDispatcher("views/home.jsp");
        rd.forward(request, response);
        return;
    }

    out.close();
}

From source file:au.gov.dto.springframework.security.web.csrf.CookieCsrfTokenRepository.java

@Override
public void saveToken(CsrfToken token, HttpServletRequest request, HttpServletResponse response) {
    Cookie csrfCookie;
    if (token == null) {
        csrfCookie = new Cookie(csrfCookieName, "");
        csrfCookie.setMaxAge(0);
    } else {// ww w  . ja  v a2 s .  c o m
        csrfCookie = new Cookie(csrfCookieName, token.getToken());
        csrfCookie.setMaxAge(csrfCookieMaxAgeSeconds);
    }
    csrfCookie.setHttpOnly(true);
    csrfCookie.setSecure(request.isSecure());
    csrfCookie.setPath(csrfCookiePath);
    response.addCookie(csrfCookie);
}

From source file:de.appsolve.padelcampus.utils.LoginUtil.java

private void deleteCookie(HttpServletRequest request, HttpServletResponse response, String path) {
    Cookie cookie = new Cookie(COOKIE_LOGIN_TOKEN, null);
    cookie.setDomain(request.getServerName());
    cookie.setMaxAge(0);
    if (!StringUtils.isEmpty(path)) {
        cookie.setPath(path);/*from   ww  w  .  ja  v a2  s. c  o m*/
    }
    response.addCookie(cookie);
}

From source file:com.kcs.action.GenerateXmlDfFxmAction.java

private void setCookieDownloadStatus(String status) {
    Cookie cookie = new Cookie("downloadStatus", status);
    cookie.setMaxAge(60 * 60 * 24 * 365); // Make the cookie last a year
    HttpServletResponse response = ServletActionContext.getResponse();
    response.addCookie(cookie);//w w  w.  j a v a 2s .c om
}

From source file:co.id.app.sys.util.StringUtils.java

/**
 * Sets the given cookie values in the servlet response.
 * <p/>/*from   w  w  w .  j  a v  a 2s.  c o  m*/
 * This will also put the cookie in a list of cookies to send with this request's response
 * (so that in case of a redirect occurring down the chain, the first filter
 * will always try to set this cookie again)
 * <p/>
 * The cookie secure flag is set if the request is secure.
 * <p/>
 * This method was derived from Atlassian <tt>CookieUtils</tt> method of
 * the same name, release under the Apache License.
 *
 * @param request the servlet request
 * @param response the servlet response
 * @param name the cookie name
 * @param value the cookie value
 * @param maxAge the maximum age of the cookie in seconds. A negative
 * value will expire the cookie at the end of the session, while 0 will delete
 * the cookie.
 * @param path the cookie path
 * @return the Cookie object created and set in the response
 */
public static Cookie setCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, int maxAge, String path) {

    Cookie cookie = new Cookie(name, value);
    cookie.setMaxAge(maxAge);
    cookie.setPath(path);
    cookie.setSecure(request.isSecure());
    response.addCookie(cookie);

    return cookie;
}

From source file:com.persistent.cloudninja.web.security.CloudNinjaRemembermeService.java

/**
 * On logout, invalidate the cookie and send back in the response
 *///from  w ww  . j  a  va2  s  .co m
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
    String cookieName = getCookieName();
    Cookie cookieToInvalidate = getCoockieFromRequest(request, cookieName);
    if (null != cookieToInvalidate) {
        cookieToInvalidate.setMaxAge(0);
        cookieToInvalidate.setPath("/");
        response.addCookie(cookieToInvalidate);
    }
    cookieToInvalidate = getCoockieFromRequest(request, "CLOUDNINJALOGO");
    if (null != cookieToInvalidate) {
        cookieToInvalidate.setMaxAge(0);
        cookieToInvalidate.setPath("/");
        response.addCookie(cookieToInvalidate);
    }
}

From source file:com.haulmont.cuba.web.sys.AppCookies.java

public void addCookie(String name, String value, int maxAge) {
    if (isCookiesEnabled()) {
        if (StringUtils.isEmpty(value)) {
            removeCookie(name);/*from w ww  . ja va  2  s  .  com*/
        } else {
            Cookie cookie = new Cookie(name, value);
            cookie.setPath(getCookiePath());
            cookie.setMaxAge(maxAge);
            addCookie(cookie);
        }
    }
}

From source file:com.google.youtube.captions.AuthSubLogin.java

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    try {/*  w  w w . java  2  s.c om*/
        String authSubToken = AuthSubUtil.getTokenFromReply(req.getQueryString());
        if (authSubToken == null) {
            throw new IllegalStateException("Could not parse token from AuthSub response.");
        } else {
            authSubToken = URLDecoder.decode(authSubToken, "UTF-8");
        }

        authSubToken = AuthSubUtil.exchangeForSessionToken(authSubToken, null);

        YouTubeService service = new YouTubeService(SystemProperty.applicationId.get(), Util.DEVELOPER_KEY);
        service.setAuthSubToken(authSubToken);

        UserProfileEntry profileEntry = service.getEntry(new URL(PROFILE_URL), UserProfileEntry.class);
        String username = profileEntry.getUsername();

        String authSubCookie = Util.getCookie(req, Util.AUTH_SUB_COOKIE);
        JSONObject cookieAsJSON = new JSONObject();
        if (!Util.isEmptyOrNull(authSubCookie)) {
            try {
                cookieAsJSON = new JSONObject(authSubCookie);
            } catch (JSONException e) {
                LOG.log(Level.WARNING, "Unable to parse JSON from the existing cookie: " + authSubCookie, e);
            }
        }

        try {
            cookieAsJSON.put(username, authSubToken);
        } catch (JSONException e) {
            LOG.log(Level.WARNING,
                    String.format("Unable to add account '%s' and AuthSub token '%s'" + " to the JSON object.",
                            username, authSubToken),
                    e);
        }

        Cookie cookie = new Cookie(Util.AUTH_SUB_COOKIE, cookieAsJSON.toString());
        cookie.setMaxAge(Util.COOKIE_LIFETIME);
        resp.addCookie(cookie);

        cookie = new Cookie(Util.CURRENT_AUTHSUB_TOKEN, authSubToken);
        cookie.setMaxAge(Util.COOKIE_LIFETIME);
        resp.addCookie(cookie);

        cookie = new Cookie(Util.CURRENT_USERNAME, username);
        cookie.setMaxAge(Util.COOKIE_LIFETIME);
        resp.addCookie(cookie);
    } catch (IllegalStateException e) {
        LOG.log(Level.WARNING, "", e);
    } catch (AuthenticationException e) {
        LOG.log(Level.WARNING, "", e);
    } catch (GeneralSecurityException e) {
        LOG.log(Level.WARNING, "", e);
    } catch (ServiceException e) {
        LOG.log(Level.WARNING, "", e);
    }

    resp.sendRedirect("/");
}

From source file:com.mobileman.projecth.web.util.PersistentCookieHelper.java

public void setUser(HttpServletResponse response, User user) {
    String hash = createHash("" + System.currentTimeMillis(), user);
    //save hash to cvookie
    Cookie cookie = new Cookie(COOKIE_NAME, hash);
    cookie.setPath(PATH);// w w  w  .j ava 2s  .c om
    cookie.setMaxAge(365 * 24 * 3600);

    response.addCookie(cookie);
}