Example usage for javax.servlet.http Cookie setPath

List of usage examples for javax.servlet.http Cookie setPath

Introduction

In this page you can find the example usage for javax.servlet.http Cookie setPath.

Prototype

public void setPath(String uri) 

Source Link

Document

Specifies a path for the cookie to which the client should return the cookie.

Usage

From source file:com.pearson.openideas.cq5.components.page.OpenIdeasMasterTemplate.java

/**
 * init method doesn't do a whole lot right now.
 *//*from ww  w .j  a  va  2 s . co  m*/
public void init() {
    log.debug("master page template init called for page {}", getCurrentPage().getPath());
    CookieUtils cookieUtils = new CookieUtils();

    String logout = getRequestParameter("logout");
    log.debug("Should we try to logout? " + logout);

    if (StringUtils.isNotBlank(logout)) {
        Cookie cookie = cookieUtils.getCookie(getSlingRequest(), "login-token");
        cookie.setPath("/");
        cookie.setMaxAge(0);

        Cookie nameCookie = cookieUtils.getCookie(getSlingRequest(), "login-name");
        if (nameCookie != null) {
            nameCookie.setPath("/");
            nameCookie.setMaxAge(0);
            getSlingResponse().addCookie(nameCookie);
        }
        getSlingResponse().addCookie(cookie);

        UrlUtils.logoutRedirect(getCurrentPage(), getSlingScriptHelper(), getSlingRequest(), getSlingResponse(),
                true);

        loggedIn = false;
    } else {
        log.debug(
                "cookie value for this page: " + cookieUtils.getCookieValue(getSlingRequest(), "login-token"));
        if (StringUtils.isNotBlank(cookieUtils.getCookieValue(getSlingRequest(), "login-token"))) {
            loggedIn = true;
        }

        log.debug("Are we logged in? " + loggedIn);
    }

    pageTitle = PAGETITLEPREFIX + getProperties().get("jcr:title", "");
    accounts = getPageProperties().getInherited("accounts", String[].class);

    if (loggedIn) {
        UserProperties props = getSlingRequest().adaptTo(UserProperties.class);

        try {
            loggedInName = props.getProperty("givenName");
            log.debug("Who is logged in? " + loggedInName);

        } catch (RepositoryException e) {
            log.error("error pulling first name", e);
        }
    } else {
        UrlUtils.logoutRedirect(getCurrentPage(), getSlingScriptHelper(), getSlingRequest(), getSlingResponse(),
                false);
    }

    if (StringUtils.isNotBlank(getRequestParameter("j_reason"))) {
        error = "Login failed, please try again";
    }
}

From source file:org.opencms.ui.login.CmsLoginHelper.java

/**
 * Sets the cookie in the response.<p>
 *
 * @param cookie the cookie to set//  www.  j av a 2 s  . c  om
 * @param delete flag to determine if the cookir should be deleted
 * @param request the current request
 * @param response the current response
 */
protected static void setCookie(Cookie cookie, boolean delete, HttpServletRequest request,
        HttpServletResponse response) {

    if (request.getAttribute(PARAM_PREDEF_OUFQN) != null) {
        // prevent the use of cookies if using a direct ou login url
        return;
    }
    int maxAge = 0;
    if (!delete) {
        // set the expiration date of the cookie to six months from today
        GregorianCalendar cal = new GregorianCalendar();
        cal.add(Calendar.MONTH, 6);
        maxAge = (int) ((cal.getTimeInMillis() - System.currentTimeMillis()) / 1000);
    }
    cookie.setMaxAge(maxAge);
    // set the path
    cookie.setPath(CmsStringUtil.joinPaths(OpenCms.getStaticExportManager().getVfsPrefix(),
            CmsWorkplaceLoginHandler.LOGIN_HANDLER));
    // set the cookie
    response.addCookie(cookie);
}

From source file:com.companyname.services.PlatCookieService.java

public void invalidateCookie(HttpServletRequest request, HttpServletResponse response, String _cookieName) {
    logger.info("cancelling cookie named: " + _cookieName);

    // if cookie does not exist, do nothing
    Cookie cookie = getCookie(request, _cookieName);

    if (cookie == null) {
        return;// w w  w .  jav a  2  s. co  m
    }

    cookie = new Cookie(_cookieName, null);
    cookie.setValue("");
    cookie.setMaxAge(0);
    cookie.setPath(getCookiePath());
    cookie.setDomain(getCookieDomain());
    response.addCookie(cookie);
}

From source file:de.knightsoftnet.validators.server.security.CsrfCookieHandler.java

/**
 * set csrf/xsrf cookie.//from  ww  w  .j a va2  s.  c  o m
 */
public void setCookie(final HttpServletRequest prequest, final HttpServletResponse presponse)
        throws IOException {
    final CsrfToken csrf = (CsrfToken) prequest.getAttribute(CsrfToken.class.getName());
    if (csrf != null) {
        Cookie cookie = WebUtils.getCookie(prequest, ResourcePaths.XSRF_COOKIE);
        final String token = csrf.getToken();
        if (cookie == null || token != null && !token.equals(cookie.getValue())) {
            cookie = new Cookie(ResourcePaths.XSRF_COOKIE, token);
            cookie.setPath(StringUtils.defaultString(StringUtils.trimToNull(prequest.getContextPath()), "/"));
            presponse.addCookie(cookie);
        }
    }
}

From source file:org.opencms.jsp.CmsJspLoginPersistingBean.java

/**
 * @see org.opencms.jsp.CmsJspLoginBean#login(java.lang.String, java.lang.String, java.lang.String)
 *//*from w  w  w . j a  v a 2 s.  c  o m*/
@Override
public void login(String userName, String password, String projectName) {

    super.login(userName, password, projectName);
    if (isLoginSuccess()) {
        CmsObject cms = getCmsObject();
        CmsPersistentLoginTokenHandler tokenHandler = new CmsPersistentLoginTokenHandler();
        tokenHandler.setTokenLifetime(m_tokenLifetime);
        try {
            final String token = tokenHandler.createToken(cms);
            Cookie cookie = new Cookie(CmsPersistentLoginAuthorizationHandler.COOKIE_NAME, token);
            cookie.setMaxAge((int) (m_tokenLifetime / 1000));
            cookie.setPath(getCookiePath(true));
            getResponse().addCookie(cookie);
            m_isTokenSet = true;
        } catch (CmsException e) {
            LOG.error(e.getMessage(), e);
        }
    }
}

From source file:com.glaf.core.util.RequestUtils.java

public static void removeLoginUser(HttpServletRequest request, HttpServletResponse response) {
    Cookie[] cookies = request.getCookies();
    if (cookies != null && cookies.length > 0) {
        for (Cookie cookie : cookies) {
            if (StringUtils.equals(cookie.getName(), Constants.COOKIE_NAME)) {
                cookie.setMaxAge(0);/*from   w w w. j  a va2  s  .  c o  m*/
                cookie.setPath("/");
                cookie.setValue(UUID32.getUUID());
                response.addCookie(cookie);
                logger.debug("remove user from cookie");
            }
        }
    }

    HttpSession session = request.getSession(false);
    if (session != null) {
        session.removeAttribute(Constants.LOGIN_INFO);
        session.invalidate();
    }
}

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);//www  . j  a  va 2 s .co  m
    }
    if (!CommonUtils.isNull(path)) {
        cookie.setPath(path);
    }
    cookie.setMaxAge(0);
    response.addCookie(cookie);
}

From source file:com.mawujun.util.web.CookieGenerator.java

/**
 * Create a cookie with the given value, using the cookie descriptor
 * settings of this generator (except for "cookieMaxAge").
 * @param cookieValue the value of the cookie to crate
 * @return the cookie/*from   w ww.j a v a  2s .c om*/
 * @see #setCookieName
 * @see #setCookieDomain
 * @see #setCookiePath
 */
protected Cookie createCookie(String cookieValue) {
    Cookie cookie = new Cookie(getCookieName(), cookieValue);
    if (getCookieDomain() != null) {
        cookie.setDomain(getCookieDomain());
    }
    cookie.setPath(getCookiePath());
    return cookie;
}

From source file:org.nuxeo.ecm.platform.ui.web.auth.cleartrust.ClearTrustAuthenticator.java

private void expireCookie(String cookieName, HttpServletRequest request, HttpServletResponse response) {
    log.debug("expiring cookie [" + cookieName + "]  ...");
    Cookie cookie = new Cookie(cookieName, "");
    // A zero value causes the cookie to be deleted
    cookie.setMaxAge(0);/*from w w  w.ja v a2  s  .co  m*/
    cookie.setPath("/");
    response.addCookie(cookie);
}

From source file:com.thoughtworks.go.http.mocks.MockHttpServletResponseAssert.java

public SELF hasCookie(String path, String name, String value, int maxAge, boolean secured, boolean httpOnly) {
    Cookie actualCookie = actual.getCookie(name);

    Cookie expectedCookie = new Cookie(name, value);
    expectedCookie.setDomain("");
    expectedCookie.setPath(path);
    expectedCookie.setMaxAge(maxAge);//  w  ww  .  ja  v a2 s  .c om
    expectedCookie.setSecure(secured);
    expectedCookie.setHttpOnly(httpOnly);

    if (!EqualsBuilder.reflectionEquals(expectedCookie, actualCookie)) {
        this.as("cookie");

        throw Failures.instance().failure(info,
                shouldBeEqual(ReflectionToStringBuilder.toString(actualCookie, ToStringStyle.MULTI_LINE_STYLE),
                        ReflectionToStringBuilder.toString(expectedCookie, ToStringStyle.MULTI_LINE_STYLE),
                        info.representation()));
    }
    return myself;
}