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.mentor.questa.vrm.jenkins.QuestaVrmHostAction.java

public void doFlipMode(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {

    // flip!//  w  w  w .j  av a  2  s .c  o  m
    boolean showActions = !getActionCookie(req);

    // set the updated value
    Cookie cookie = new Cookie("HostAction_showActions", String.valueOf(showActions));
    List anc = req.getAncestors();
    Ancestor a = (Ancestor) anc.get(anc.size() - 2);
    cookie.setPath(a.getUrl()); // just for this project
    cookie.setMaxAge(60 * 60 * 24 * 365); // 1 year

    rsp.addCookie(cookie);

    // back to the same page
    rsp.sendRedirect(".");
}

From source file:fr.paris.lutece.plugins.mylutece.modules.openam.service.OpenamService.java

/**
 * set a paris connect cokkie in the HttpServletResponse
 *
 * @param strPCUID/*from ww w. jav a2s .c o m*/
 *            the user PCUID
 * @param response
 *            The HTTP response
 */
public void setConnectionCookie(String strPCUID, HttpServletResponse response) {
    // set a connexion cookie to let the user access other PC Services
    // without sign in
    Cookie openamCookie = new Cookie(COOKIE_OPENAM_NAME, strPCUID);
    openamCookie.setDomain(COOKIE_OPENAM_DOMAIN);
    openamCookie.setSecure(COOKIE_OPENAM_SECURE);
    openamCookie.setMaxAge(COOKIE_OPENAM_MAX_AGE);
    openamCookie.setPath(COOKIE_OPENAM_PATH);

    response.addCookie(openamCookie);
}

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

/**
 * Inizializza il cookie per l’autenticazione persistente.
 * //  www .j  ava2s  .com
 * @param  value  Valore.
 * @param  maxAge Scadenza.
 * @return        Oggetto.
 */
private Cookie buildCookie(String value, int maxAge) {
    Cookie cookie;

    cookie = new Cookie(getCookieName(), value);
    if (myCookieDomain != null) {
        cookie.setDomain(myCookieDomain);
    }
    cookie.setPath(myCookiePath);
    cookie.setMaxAge(maxAge);

    return cookie;
}

From source file:org.ngrinder.user.controller.UserController.java

/**
 * Switch user identity.//from ww w.j  ava  2s.c  om
 *
 * @param model    model
 * @param to       the user to whom a user will switch
 * @param response response
 * @return redirect:/perftest/
 */
@RequestMapping("/switch")
public String switchUser(@RequestParam(required = false, defaultValue = "") String to,
        HttpServletRequest request, HttpServletResponse response, ModelMap model) {
    Cookie cookie = new Cookie("switchUser", to);
    cookie.setPath("/");
    // Delete Cookie if empty switchUser
    if (StringUtils.isEmpty(to)) {
        cookie.setMaxAge(0);
    }

    response.addCookie(cookie);
    model.clear();
    final String referer = request.getHeader("referer");
    return "redirect:" + StringUtils.defaultIfBlank(referer, "/");
}

From source file:com.skilrock.lms.embedded.roleMgmt.common.PrivsInterceptor.java

public void createCookie() {
    boolean found = false;
    Cookie userSessionId = null;
    Cookie[] cookies = request.getCookies();
    for (Cookie element : cookies) {
        userSessionId = element;//from ww w . ja va  2s.  c o  m
        if (userSessionId.getName().equals("LMSCookie")) {
            found = true;
            break;
        }
        if (!found) {
            userSessionId = new Cookie("LMSCookie", "");
            userSessionId.setMaxAge(24 * 60 * 60);
            userSessionId.setPath("/");
            response.addCookie(userSessionId);
        } else {
            userSessionId.setMaxAge(24 * 60 * 60);
            userSessionId.setPath("/");
            response.addCookie(userSessionId);
        }

    }

}

From source file:org.craftercms.cstudio.share.servlet.CookieManagerImpl.java

public void destroyCookie(HttpServletRequest request, HttpServletResponse response, String key, String path) {
    Cookie[] cookieArray = request.getCookies();
    if (cookieArray != null) {
        for (Cookie cookie : cookieArray) {
            String name = cookie.getName();
            if (name != null && name.equals(key)) {
                if (!StringUtils.isEmpty(path)) {
                    cookie.setPath(path);
                } else {
                    cookie.setPath("/");
                }//from   ww  w.  ja  v a 2 s . com
                cookie.setMaxAge(0);
                cookie.setValue(null);

                if (_cookieDomain != null) {
                    cookie.setDomain(_cookieDomain);
                }

                response.addCookie(cookie);
            }
        }
    }
}

From source file:io.seldon.api.controller.JsPortholeController.java

/**
 *
 * @param request .../* www .  ja va 2s .  co  m*/
 * @param response ...
 * @param localId if non-null, use this local id instead of generating a {@link UUID}.
 *                Typically this will be used to propagate client-specific cookies where browser privacy issues have
 *                blocked the server-side setting.
 * @return
 */
private String ensureCookie(HttpServletRequest request, HttpServletResponse response, String localId) {
    final Cookie[] cookies = request.getCookies();
    String uuid = null;
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(RL_COOKIE_ID)) {
                uuid = cookie.getValue();
            }
        }
    }
    if (uuid == null) {
        if (localId != null) {
            logger.info("Using local ID for porthole session: " + localId);
            uuid = localId;
        } else {
            uuid = UUID.randomUUID().toString();
        }
        final Cookie cookie = new Cookie(RL_COOKIE_ID, uuid);
        cookie.setMaxAge(COOKIE_MAX_AGE);
        response.addCookie(cookie);
        response.addHeader("P3P", "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");
    }
    return uuid;
}

From source file:com.utest.webservice.impl.v2.UserWebServiceImpl.java

@PUT
@Path("/login")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Override//from   w  w w.ja  v  a 2  s. co m
public Boolean login(@Context MessageContext context) throws Exception {
    Integer auth = UserUtil.getCurrentUserId();
    if (auth == null) {
        throw new org.apache.cxf.interceptor.security.AccessDeniedException("No logged in user!");
    }
    final AuthenticatedUserInfo authInfo = (AuthenticatedUserInfo) SecurityContextHolder.getContext()
            .getAuthentication().getPrincipal();
    String sessionId = SessionUtil.extractSession(context, true);
    userService.login(authInfo, sessionId);

    String token = Base64.encodeObject(SecurityContextHolder.getContext().getAuthentication(),
            Base64.GZIP | Base64.DONT_BREAK_LINES);
    Cookie loginCookie = new Cookie(SessionUtil.AUTH_TOKEN, token);
    // time to live
    loginCookie.setMaxAge(getLoginExpirationSeconds());
    context.getHttpServletResponse().addCookie(loginCookie);
    return Boolean.TRUE;
}

From source file:com.vmm.storefront.controllers.pages.ProductPageController.java

@RequestMapping(value = PRODUCT_CODE_PATH_VARIABLE_PATTERN, method = RequestMethod.GET)
public String productDetail(@PathVariable("productCode") final String productCode, final Model model,
        final HttpServletRequest request, final HttpServletResponse response,
        @CookieValue(value = "lastBrowsedProducts", defaultValue = "") String lastBrowsedProducts)
        throws CMSItemNotFoundException, UnsupportedEncodingException {

    // Count of products to be maintained in Cookie
    final int countOfProducts = 20;

    System.out.println("praveen cookie value======" + lastBrowsedProducts);

    if (lastBrowsedProducts.equalsIgnoreCase("")) {
        lastBrowsedProducts = productCode;
    } else {//w ww .ja  va2s  .c  o m
        lastBrowsedProducts = listLatestBrowsedProducts(lastBrowsedProducts, productCode, countOfProducts);
    }
    final Cookie foo = new Cookie("lastBrowsedProducts", lastBrowsedProducts);
    foo.setMaxAge(9999999);
    foo.setPath("/");
    response.addCookie(foo);

    System.out.println("praveen cookie added value------------------" + lastBrowsedProducts);

    final List<ProductOption> extraOptions = Arrays.asList(ProductOption.VARIANT_MATRIX_BASE,
            ProductOption.VARIANT_MATRIX_URL, ProductOption.VARIANT_MATRIX_MEDIA);

    final ProductData productData = productFacade.getProductForCodeAndOptions(productCode, extraOptions);

    final String redirection = checkRequestUrl(request, response, productDataUrlResolver.resolve(productData));
    if (StringUtils.isNotEmpty(redirection)) {
        return redirection;
    }

    updatePageTitle(productCode, model);

    populateProductDetailForDisplay(productCode, model, request, extraOptions);

    model.addAttribute(new ReviewForm());
    model.addAttribute("pageType", PageType.PRODUCT.name());
    model.addAttribute("futureStockEnabled", Boolean.valueOf(Config.getBoolean(FUTURE_STOCK_ENABLED, false)));

    final String metaKeywords = MetaSanitizerUtil.sanitizeKeywords(productData.getKeywords());
    final String metaDescription = MetaSanitizerUtil.sanitizeDescription(productData.getDescription());
    setUpMetaData(model, metaKeywords, metaDescription);
    return getViewForPage(model);
}

From source file:com.skilrock.lms.web.roleMgmt.common.PrivsInterceptor.java

public void createCookie() {
    boolean found = false;
    Cookie userSessionId = null;
    Cookie[] cookies = request.getCookies();
    for (Cookie element : cookies) {
        userSessionId = element;/*  ww  w .ja  v a 2s . c o m*/
        if (userSessionId.getName().equals("LMSCookie")) {
            found = true;
            break;
        }
    }
    if (!found) {
        userSessionId = new Cookie("LMSCookie", "");
        userSessionId.setMaxAge(24 * 60 * 60);
        userSessionId.setPath("/");
        response.addCookie(userSessionId);
    } else {
        userSessionId.setMaxAge(24 * 60 * 60);
        userSessionId.setPath("/");
        response.addCookie(userSessionId);
    }

}