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:com.mobileman.projecth.web.util.PersistentCookieHelper.java

public void removeUser(HttpServletRequest request, HttpServletResponse response) {
    //remove from request
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie c : cookies) {
            if (COOKIE_NAME.equals(c.getName())) {
                c.setValue("deleted");
                break;
            }/*from ww  w  .  j  ava  2  s.  c  om*/
        }
    }

    //remove from browser
    Cookie cookie = new Cookie(COOKIE_NAME, "1");
    cookie.setPath(PATH);
    cookie.setMaxAge(0); //0 = remove cookie
    response.setContentType("text/html"); //else delete cookie not works
    response.addCookie(cookie);
}

From source file:net.e2.bw.idreg.client.keycloak.KeycloakClient.java

/** {@inheritDoc} */
public void redirectToAuthServer(HttpServletResponse response, String callbackUrl) throws IOException {

    // Create a state code used for Cross-Site Request Forgery (CSRF, XSRF) prevention
    String state = OIDCUtils.getStateCode();

    // Set up cookie used for Cross-Site Request Forgery (CSRF, XSRF) prevention
    Cookie cookie = new Cookie(OAUTH_TOKEN_REQUEST_STATE, state);
    //cookie.setSecure(isSecure);
    cookie.setPath("/");
    response.addCookie(cookie);

    // Redirect to the authentication request
    String url = config.getAuthRequest(callbackUrl, state);
    log.log(Level.FINE, "Redirecting to auth request: " + url);
    response.sendRedirect(url);/*from  ww  w  .  j  a  v a 2s. co  m*/
}

From source file:controllers.UrlController.java

@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout(HttpServletRequest request, HttpServletResponse response) {
    Cookie[] cookies2 = request.getCookies();
    user_detail = null;//from  www . j  a  v a2 s.c  om
    if (cookies2 != null) {
        for (Cookie cookie : cookies2) {

            cookie.setValue(null);
            cookie.setMaxAge(0);

            response.addCookie(cookie);

        }
    }
    return "index";
}

From source file:uk.co.caprica.bootlace.security.web.filter.AngularJsCsrfHeaderFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    logger.debug("doFilterInternal()");
    CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
    if (csrf != null) {
        Cookie cookie = WebUtils.getCookie(request, CSRF_COOKIE_NAME);
        String token = csrf.getToken();
        if (cookie == null || token != null && !token.equals(cookie.getValue())) {
            logger.debug("Setting new CSRF cookie");
            cookie = new Cookie(CSRF_COOKIE_NAME, token);
            cookie.setPath(request.getServletContext().getContextPath() + "/");
            response.addCookie(cookie);
        }//from  w  w  w . j a  va  2 s  .  c  o m
    }
    filterChain.doFilter(request, response);
}

From source file:cn.vlabs.duckling.vwb.CPSFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    int advSiteId = Integer.parseInt(StringUtils.defaultIfEmpty(request.getParameter("as"), "-1"));

    if (advSiteId <= 0) {
        chain.doFilter(request, response);
        return;//w  ww . j a v a 2 s .  c  om
    }

    HttpServletResponse rep = (HttpServletResponse) response;
    HttpServletRequest req = (HttpServletRequest) request;
    request.setAttribute("cps", advSiteId);

    Cookie[] cookies = req.getCookies();

    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (StringUtils.equals(CPS_ADV, cookie.getName())) {
                cookie.setPath(req.getContextPath());
                cookie.setMaxAge(0);
            }
        }
    }

    Cookie myCookie = new Cookie(CPS_ADV, advSiteId + "");
    myCookie.setMaxAge(60 * 60 * 24);//
    myCookie.setPath(req.getContextPath());
    rep.addCookie(myCookie);
    chain.doFilter(request, response);
}

From source file:am.ik.categolj2.app.authentication.AuthenticationHelper.java

void saveAccessTokenInCookie(OAuth2AccessToken accessToken, HttpServletResponse response)
        throws UnsupportedEncodingException {
    Cookie accessTokenValueCookie = new Cookie(Categolj2Cookies.ACCESS_TOKEN_VALUE_COOKIE,
            URLEncoder.encode(accessToken.getValue(), "UTF-8"));
    accessTokenValueCookie.setMaxAge(accessToken.getExpiresIn());
    Cookie accessTokenExpireCookie = new Cookie(Categolj2Cookies.ACCESS_TOKEN_EXPIRATION_COOKIE,
            URLEncoder.encode(String.valueOf(accessToken.getExpiration().getTime()), "UTF-8"));
    accessTokenExpireCookie.setMaxAge(accessToken.getExpiresIn());

    response.addCookie(accessTokenValueCookie);
    response.addCookie(accessTokenExpireCookie);

    OAuth2RefreshToken refreshToken = accessToken.getRefreshToken();
    if (refreshToken != null) {
        Cookie refreshTokenCookie = new Cookie(Categolj2Cookies.REFRESH_TOKEN_VALUE_COOKIE,
                URLEncoder.encode(refreshToken.getValue(), "UTF-8"));
        refreshTokenCookie.setMaxAge(getRefreshTokenMaxAge(accessToken));
        response.addCookie(refreshTokenCookie);
    }/*  ww  w. j a v  a2 s  . com*/
}

From source file:com.ctc.storefront.controllers.pages.CartPageController.java

private void setCookie(final HttpServletResponse response, final CartData cartData) {
    final Cookie cookie = new Cookie("cartQuantity", String.valueOf(cartData.getTotalUnitCount()));
    cookie.setMaxAge(60 * 60);//from  w w w.jav a2  s  .  c om
    cookie.setPath("/");
    cookie.setDomain(getSiteConfigService().getString(CART_COUNT_COOKIE_DOMAIN_NAME, ".ctc.com"));
    response.addCookie(cookie);
}

From source file:com.ms.commons.cookie.parser.CookieNameHelper.java

/**
 * ????CookieName?Response/*w  w  w. java2 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 = CookieUtils.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:fr.paris.lutece.plugins.mylutece.modules.openam.service.OpenamService.java

/**
 * set a paris connect cokkie in the HttpServletResponse
 *
 * @param strPCUID/*w  ww  .j a v  a  2 s  .c  o m*/
 *            the user PCUID
 * @param response
 *            The HTTP response
 */
public void removeConnectionCookie(HttpServletResponse response) {
    // remove  openam cookie using the setMaxAgeParameters
    Cookie openamCookie = new Cookie(COOKIE_OPENAM_NAME, null);
    openamCookie.setDomain(COOKIE_OPENAM_DOMAIN);
    openamCookie.setSecure(COOKIE_OPENAM_SECURE);
    openamCookie.setMaxAge(0);
    openamCookie.setPath(COOKIE_OPENAM_PATH);
    response.addCookie(openamCookie);
}

From source file:io.cfp.auth.MainCtrl.java

@RequestMapping("/")
public String main(HttpServletResponse response, @CookieValue(required = false) String token,
        @RequestParam(required = false, value = "target") String targetParam,
        @CookieValue(required = false) String returnTo,
        @RequestHeader(required = false, value = REFERER) String referer) {
    response.setHeader(CACHE_CONTROL, "no-cache,no-store,must-revalidate");
    response.setHeader(PRAGMA, "no-cache");
    response.setDateHeader(EXPIRES, 0);/*  w w w  . j  ava  2  s .  c  o  m*/

    String target = "http://www.cfp.io";
    if (targetParam != null) {
        target = targetParam;
    } else if (returnTo != null) {
        target = returnTo;
    } else if (referer != null) {
        target = referer;
    }

    response.addCookie(new Cookie("returnTo", target));

    if (token == null || !tokenSrv.isValid(token)) {
        return "login";
    }

    // token is valid
    return "redirect:" + target;
}