Example usage for javax.servlet.http HttpServletResponse addCookie

List of usage examples for javax.servlet.http HttpServletResponse addCookie

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse addCookie.

Prototype

public void addCookie(Cookie cookie);

Source Link

Document

Adds the specified cookie to the response.

Usage

From source file:net.nan21.dnet.core.web.controller.ui.extjs.AbstractUiExtjsController.java

/**
 * Resolve the user's current language from the cookie.
 * //from ww w  .  j  a v a 2 s. c o  m
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
private String resolveLang(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Cookie[] cookies = request.getCookies();
    Cookie c = this.getCookie(cookies, Constants.COOKIE_NAME_LANG);
    if (c == null) {

        String value = this.getSettings().getParam(SysParams_Core.CORE_DEFAULT_LANGUAGE);

        c = this.createCookie(Constants.COOKIE_NAME_LANG, value, 60 * 60 * 24 * 365);
        response.addCookie(c);
    }

    String lang = request.getParameter(Constants.REQUEST_PARAM_LANG);
    if (lang == null || lang.equals("")) {
        lang = c.getValue();
    } else {
        c.setMaxAge(0);
        c = this.createCookie(Constants.COOKIE_NAME_LANG, lang, 60 * 60 * 24 * 365);
        response.addCookie(c);
    }
    return lang;
}

From source file:org.examproject.tweet.controller.TweetController.java

@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String doLogout(HttpServletResponse response, SessionStatus sessionStatus, Model model) {
    Cookie cookie = new Cookie(TweetCookie.REQUEST_TOKEN.getName(), "");
    response.addCookie(cookie);
    cookie = new Cookie(TweetCookie.ACCESS_TOKEN.getName(), "");
    response.addCookie(cookie);/*from ww w .  j ava  2s.c o  m*/
    cookie = new Cookie(TweetCookie.TOKEN_SECRET.getName(), "");
    response.addCookie(cookie);
    cookie = new Cookie(TweetCookie.USER_ID.getName(), "");
    response.addCookie(cookie);
    cookie = new Cookie(TweetCookie.SCREEN_NAME.getName(), "");
    response.addCookie(cookie);
    cookie = new Cookie(TweetCookie.USER_LIST_NAME.getName(), "");
    response.addCookie(cookie);
    cookie = new Cookie(TweetCookie.RESPONSE_LIST_MODE.getName(), "");
    response.addCookie(cookie);

    sessionStatus.setComplete();
    return "redirect:/";
}

From source file:net.nan21.dnet.core.web.controller.ui.extjs.AbstractUiExtjsController.java

/**
 * Resolve the user's current theme from the cookie.
 * //from w w w .j  a  v a  2s .c  o  m
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
private String resolveTheme(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Cookie[] cookies = request.getCookies();
    Cookie c = this.getCookie(cookies, Constants.COOKIE_NAME_THEME);
    if (c == null) {

        String value = this.getSettings().getParam(SysParams_Core.CORE_DEFAULT_THEME_EXTJS);
        c = this.createCookie(Constants.COOKIE_NAME_THEME, value, 60 * 60 * 24 * 365);
        response.addCookie(c);
    }

    String theme = request.getParameter(Constants.REQUEST_PARAM_THEME);
    if (theme == null || theme.equals("")) {
        theme = c.getValue();
    } else {
        c.setMaxAge(0);
        c = this.createCookie(Constants.COOKIE_NAME_THEME, theme, 60 * 60 * 24 * 365);
        response.addCookie(c);
    }
    return theme;
}

From source file:cn.org.once.cstack.config.SecurityConfiguration.java

/**
 * Filter CRSF to add XSFR-TOKEN between exchange
 *
 * @return/*from  w ww.j a  va2  s .  co  m*/
 */
private Filter csrfHeaderFilter() {
    return new OncePerRequestFilter() {
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                FilterChain filterChain) throws ServletException, IOException {
            CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
            if (csrf != null) {
                Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
                String token = csrf.getToken();
                if (cookie == null || token != null && !token.equals(cookie.getValue())) {
                    cookie = new Cookie("XSRF-TOKEN", token);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                }
            }
            filterChain.doFilter(request, response);
        }
    };
}

From source file:de.hska.ld.core.config.security.AjaxLogoutSuccessHandler.java

@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {

    ////from  w  w w  .ja  v  a 2s . c  o  m
    // To delete a cookie, we need to create a cookie that has the same
    // name with the cookie that we want to delete. We also need to set
    // the max age of the cookie to 0 and then add it to the Servlet's
    // response method.
    //
    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);

    // TODO destory session in etherpad

    response.setStatus(HttpServletResponse.SC_OK);
}

From source file:com.mmj.app.common.cookie.parser.CookieNameHelper.java

/**
 * ????CookieName?Response//from ww w.ja  v a 2 s  .  c  o m
 * 
 * <pre>
 * cookie<code>null</code>blankCookie
 * </pre>
 */
public void saveIfModified(HttpServletResponse response) {
    if (!isModified) {
        return;
    }
    String value = config.isSimpleValue() ? simpleValue : CookieUtils.mapToStr(allCookieKeyValues);
    if (config.isEncrypt()) {
        value = EncryptBuilder.getInstance().encrypt(value);
    }
    Cookie cookie = new Cookie(cookieName, value);
    if (StringUtils.isBlank(value)) {
        cookie.setMaxAge(CookieMaxAge.OUT_OF_DATE);
    } else {
        cookie.setMaxAge(config.getMaxAge());
    }
    cookie.setDomain(config.getDomain().getDomain());
    cookie.setPath(config.getPath().getPath());
    response.addCookie(cookie);

    // ?????
    this.isModified = false;
}

From source file:io.cfp.auth.api.AuthController.java

/**
 * Return JWT token and eventually persist user according to providerId and
 * provider/*from ww w .  ja va 2  s  .com*/
 */
protected String processUser(HttpServletResponse response, String email, String returnTo) {

    if (email == null) {
        logger.warn("[LOGIN_NO_MAIL] Unable to log user with [{}] because no e-mail was provided",
                getProvider());
        return "redirect:/noEmail";
    }

    User user = userService.findByemail(email);

    if (user == null) {
        logger.info("[USER_CREATION] Trying to login with a not existent user, creating it");
        user = new User();
        user.setEmail(email);
        user = userService.save(user);
    }

    // add a token for the user
    String token = tokenService.create(email, user.isSuperAdmin());
    response.addCookie(cookieService.getTokenCookie(token));

    if (returnTo == null || returnTo.isEmpty()) {
        returnTo = "http://www.cfp.io";
    }

    logger.info("[LOGGED_IN] User logged in with [{}] and redirected to [{}]", getProvider(), returnTo);

    return "redirect:" + returnTo;
}

From source file:net.prasenjit.auth.config.CsrfCookieGeneratorFilter.java

/**
 * {@inheritDoc}//from w  w  w .  j  a  v  a2  s.co m
 */
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    // Spring put the CSRF token in session attribute "_csrf"
    CsrfToken csrfToken = (CsrfToken) request.getAttribute("_csrf");

    // Send the cookie only if the token has changed
    String actualToken = request.getHeader("X-XSRF-TOKEN");
    if (actualToken == null || !actualToken.equals(csrfToken.getToken())) {
        // Session cookie that will be used by AngularJS
        String pCookieName = "XSRF-TOKEN";
        Cookie cookie = new Cookie(pCookieName, csrfToken.getToken());
        cookie.setMaxAge(-1);
        cookie.setHttpOnly(false);
        cookie.setPath("/");
        response.addCookie(cookie);
    }
    filterChain.doFilter(request, response);
}

From source file:nl.strohalm.cyclos.controls.access.LoginAction.java

private void storeCookie(final HttpServletRequest request, final HttpServletResponse response,
        final boolean force) {
    final String queryString = StringHelper.removeMarkupTags(request.getQueryString());
    if (force || StringUtils.isNotEmpty(queryString)) {
        // Store the query string as a cookie in order to be restored on logout
        final Cookie queryStringCookie = new Cookie("loginQueryString", StringHelper.encodeUrl(queryString));
        queryStringCookie.setPath(request.getContextPath());
        response.addCookie(queryStringCookie);

    }//from ww w.  java2  s  .c o m
    if (force) {
        // Remove the after logout cookie (received on external login)
        final Cookie afterLogoutCookie = new Cookie("afterLogout", null);
        afterLogoutCookie.setPath(request.getContextPath());
        response.addCookie(afterLogoutCookie);
    }
}

From source file:iddb.web.security.service.CommonUserService.java

protected void invalidateUserSession(HttpServletRequest request, HttpServletResponse response) {
    context.removeSubject();//from  w  ww  .j  a  v  a  2s . co m
    String sessionKey = null;
    HttpSession session = request.getSession(false);
    if (session != null) {
        session.removeAttribute(UserService.SUBJECT);
        sessionKey = (String) session.getAttribute(UserService.SESSION_KEY);
        session.removeAttribute(UserService.SESSION_KEY);
    }
    // remove cookie
    Cookie cookie = new Cookie("iddb-u", "");
    cookie.setPath(request.getContextPath() + "/");
    cookie.setMaxAge(0);
    response.addCookie(cookie);

    cookie = new Cookie("iddb-k", "");
    cookie.setPath(request.getContextPath() + "/");
    cookie.setMaxAge(0);
    response.addCookie(cookie);

    if (sessionKey != null) {
        removeSession(sessionKey);
    }
}