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.streamsets.lib.security.http.SSOUserAuthenticator.java

void setAuthCookieIfNecessary(HttpServletRequest req, HttpServletResponse res, String authToken,
        long expiresMillis) {
    if (!authToken.equals(getAuthTokenFromCookie(req))) {
        res.addCookie(createAuthCookie(req, authToken, expiresMillis));
    }/*from  w w  w  .  jav  a2 s. com*/
}

From source file:com.streamsets.lib.security.http.SSOUserAuthenticator.java

@Override
protected Authentication returnUnauthorized(HttpServletRequest httpReq, HttpServletResponse httpRes,
        String principalId, String logMessageTemplate) throws ServerAuthException {
    Authentication ret;/*from  ww  w .  j a  va 2  s .  com*/
    httpRes.addCookie(createAuthCookie(httpReq, "", 0));
    if (httpReq.getHeader(SSOConstants.X_REST_CALL) != null) {
        ret = super.returnUnauthorized(httpReq, httpRes, null, logMessageTemplate);
    } else {
        redirectToLogin(httpReq, httpRes);
        ret = Authentication.SEND_FAILURE;
    }
    return ret;
}

From source file:org.kievguide.controller.UserController.java

@RequestMapping(value = "/signinaction", method = RequestMethod.POST)
public ModelAndView signInAction(@RequestParam("email") String email, @RequestParam("password") String password,
        HttpServletResponse response) {
    ModelAndView modelAndView = new ModelAndView();

    User user = null;//from w  ww. j  a v  a 2  s .  co m
    user = userService.searchUser(email);

    if (user.getPassword().equals(password)) {

        Cookie userCookie = new Cookie("userstatus", email);
        response.addCookie(userCookie);
        String userStatus = Util.userPanel(email);
        modelAndView.addObject("userstatus", userStatus);
        modelAndView.addObject("user", user);
        modelAndView.setViewName("redirect:" + "firstrequest");
        return modelAndView;
    } else {
        modelAndView.setViewName("error");
        return modelAndView;
    }

}

From source file:com.atlassian.jira.security.xsrf.SimpleXsrfTokenGenerator.java

private void addNewCookie(HttpServletRequest httpServletRequest, String token,
        HttpServletResponse httpServletResponse) {
    final Cookie cookie = new Cookie(TOKEN_HTTP_SESSION_KEY, token);
    cookie.setPath(getRequestContext(httpServletRequest));
    cookie.setMaxAge(-1); // expire with the browser exit
    cookie.setSecure(httpServletRequest.isSecure());
    httpServletResponse.addCookie(cookie);
    httpServletRequest.setAttribute(SET_COOKIE_PENDING, token);
}

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

@RequestMapping()
@ResponseStatus(HttpStatus.NO_CONTENT)//from   w w w  .j  av 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:nl.strohalm.cyclos.utils.ResponseHelper.java

/**
 * Adds a cookie with the given name and value set for the context path root
 *//*from  w w w  .ja  v a  2s.co m*/
public Cookie addRootCookie(final HttpServletRequest request, final HttpServletResponse response,
        final String name, final Object value) {
    final Cookie cookie = new Cookie(name, value == null ? "" : value.toString());
    cookie.setPath(request.getContextPath());
    response.addCookie(cookie);
    return cookie;
}

From source file:it.scoppelletti.programmerpower.web.security.SsoRememberMeServices.java

/**
 * Rimuove il cookie per l&rsquo;autenticazione persistente.
 *
 * @param req  Richiesta.//from  ww  w  .j  a v a2  s.  c om
 * @param resp Risposta.
 */
@Override
protected void cancelCookie(HttpServletRequest req, HttpServletResponse resp) {
    Cookie cookie;

    cookie = buildCookie(null, 0);
    resp.addCookie(cookie);
}

From source file:io.soabase.web.filters.LanguageFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (request instanceof HttpServletRequest) {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        AtomicReference<String> fixedQueryString = new AtomicReference<>();
        String queryStringCode = getFromQueryString(httpRequest.getQueryString(), fixedQueryString);
        String expectedLanguageCode = MoreObjects.firstNonNull(queryStringCode,
                getLanguageCode(null, getCookie(httpRequest)));
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        Optional<String> foundCookie = getCookie(httpRequest);
        if (!foundCookie.isPresent() || !foundCookie.get().equals(expectedLanguageCode)) {
            Cookie cookie = new Cookie(cookieName, expectedLanguageCode);
            httpResponse.addCookie(cookie);
        }/*from ww w .  ja v a  2 s. c  om*/

        if (queryStringCode != null) {
            StringBuffer redirectUrl = httpRequest.getRequestURL();
            if (!fixedQueryString.get().isEmpty()) {
                redirectUrl.append("?").append(fixedQueryString.get());
            }
            ((HttpServletResponse) response).sendRedirect(redirectUrl.toString());
            return;
        }
    }
    chain.doFilter(request, response);
}

From source file:cn.vlabs.umt.ui.servlet.LogoutServlet.java

private void removeCookie(HttpServletResponse response, String domain, String path, String name) {
    Cookie cookie = new Cookie(name, "");
    if (!CommonUtils.isNull(domain)) {
        cookie.setDomain(domain);/*from   w  w w. j av  a2 s .  co m*/
    }
    if (!CommonUtils.isNull(path)) {
        cookie.setPath(path);
    }
    cookie.setMaxAge(0);
    response.addCookie(cookie);
}

From source file:com.shenit.commons.utils.HttpUtils.java

/**
 * ?cookie/*from  w  ww . j  a  v  a 2 s.c  o m*/
 * 
 * @param resp
 * @param copyToSession
 *            ??session
 * @param cookies
 */
public static void save(HttpServletRequest req, HttpServletResponse resp, boolean copyToSession,
        Cookie... cookies) {
    HttpSession session = req.getSession(true);
    for (Cookie cookie : cookies) {
        resp.addCookie(cookie);
        if (copyToSession)
            session.setAttribute(cookie.getName(), cookie.getValue());
    }
}