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.vmware.identity.openidconnect.sample.RelyingPartyController.java

private static Cookie logoutSessionCookie() {
    Cookie sessionCookie = new Cookie(SESSION_COOKIE_NAME, "");
    sessionCookie.setPath("/openidconnect-sample-rp");
    sessionCookie.setSecure(true);//w w  w  .  j a  va 2 s  .  c  o  m
    sessionCookie.setHttpOnly(true);
    sessionCookie.setMaxAge(0);
    return sessionCookie;
}

From source file:com.vmware.identity.openidconnect.sample.RelyingPartyController.java

private static Cookie loginSessionCookie(SessionID sessionId) {
    Cookie sessionCookie = new Cookie(SESSION_COOKIE_NAME, sessionId.getValue());
    sessionCookie.setPath("/openidconnect-sample-rp");
    sessionCookie.setSecure(true);//w w w.jav a 2s.  c o m
    sessionCookie.setHttpOnly(true);
    return sessionCookie;
}

From source file:com.tc.utils.XSPUtils.java

public static void logout(String url) {
    HttpSession httpSession = XSPUtils.getHttpSession();

    if (httpSession == null) {
        return;/*w w w  .j  ava 2s .c  o  m*/
    }

    String sessionId = XSPUtils.getHttpSession().getId();
    XSPUtils.getRequest().getSession(false).invalidate();

    //wipe out the cookies
    for (Cookie cookie : getCookies()) {
        cookie.setValue(StringCache.EMPTY);
        cookie.setPath("/");
        cookie.setMaxAge(0);
        XSPUtils.getResponse().addCookie(cookie);
    }

    try {
        NotesContext notesContext = NotesContext.getCurrent();
        notesContext.getModule().removeSession(sessionId);
        XSPUtils.externalContext().redirect(url);
    } catch (IOException e) {
        logger.log(Level.SEVERE, null, e);
    }
}

From source file:com.tc.utils.XSPUtils.java

public static void logout() {
    HttpSession httpSession = XSPUtils.getHttpSession();

    if (httpSession == null) {
        return;/* w ww.j a va 2  s. co m*/
    }

    String sessionId = XSPUtils.getHttpSession().getId();
    String url = XSPUtils.externalContext().getRequestContextPath() + "?logout&redirectto="
            + externalContext().getRequestContextPath();
    XSPUtils.getRequest().getSession(false).invalidate();

    //wipe out the cookies
    for (Cookie cookie : getCookies()) {
        cookie.setValue(StringCache.EMPTY);
        cookie.setPath("/");
        cookie.setMaxAge(0);
        XSPUtils.getResponse().addCookie(cookie);
    }

    try {
        NotesContext notesContext = NotesContext.getCurrent();
        notesContext.getModule().removeSession(sessionId);
        XSPUtils.externalContext().redirect(url);
    } catch (IOException e) {
        logger.log(Level.SEVERE, null, e);
    }
}

From source file:com.google.gsa.valve.modules.utils.CookieManagement.java

/**
 * Transforms Apache cookies into Servlet Cookies
 * //from w w  w. ja va 2s  . co m
 * @param apacheCookie apache cookie 
 * 
 * @return servlet cookie
 */
public static javax.servlet.http.Cookie transformApacheCookie(
        org.apache.commons.httpclient.Cookie apacheCookie) {

    javax.servlet.http.Cookie newCookie = null;

    if (apacheCookie != null) {
        Date expire = apacheCookie.getExpiryDate();
        int maxAge = -1;

        if (expire == null) {
            maxAge = -1;
        } else {
            Date now = Calendar.getInstance().getTime();
            // Convert milli-second to second
            Long second = new Long((expire.getTime() - now.getTime()) / 1000);
            maxAge = second.intValue();
        }

        newCookie = new javax.servlet.http.Cookie(apacheCookie.getName(), apacheCookie.getValue());
        //Hardcoding the domain
        newCookie.setDomain(apacheCookie.getDomain());
        newCookie.setPath(apacheCookie.getPath());
        newCookie.setMaxAge(maxAge);
        newCookie.setSecure(apacheCookie.getSecure());
    }
    return newCookie;
}

From source file:com.xpn.xwiki.stats.impl.StatsUtil.java

/**
 * Create a new visit cookie and return it.
 * /* w w  w .j  a  v  a2  s.  c o m*/
 * @param context the XWiki context.
 * @return the newly created cookie.
 * @since 1.4M1
 */
protected static Cookie addCookie(XWikiContext context) {
    Cookie cookie = new Cookie(COOKPROP_VISITID, RandomStringUtils.randomAlphanumeric(32).toUpperCase());
    cookie.setPath("/");

    int time = (int) (getCookieExpirationDate().getTime() - (new Date()).getTime()) / 1000;
    cookie.setMaxAge(time);

    String cookieDomain = null;
    getCookieDomains(context);
    if (cookieDomains != null) {
        String servername = context.getRequest().getServerName();
        for (int i = 0; i < cookieDomains.length; i++) {
            if (servername.indexOf(cookieDomains[i]) != -1) {
                cookieDomain = cookieDomains[i];
                break;
            }
        }
    }

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

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Setting cookie " + cookie.getValue() + " for name " + cookie.getName() + " with domain "
                + cookie.getDomain() + " and path " + cookie.getPath() + " and maxage " + cookie.getMaxAge());
    }

    context.getResponse().addCookie(cookie);

    return cookie;
}

From source file:gr.abiss.calipso.userDetails.util.SecurityUtil.java

/**
 * Writes a cookie to the response. In case of a blank value the method will 
 * set the max age to zero, effectively marking the cookie for immediate 
 * deletion by the client if the <code>allowClear</code> is true or throw an exception if false.
 * Blank value strings mark cookie deletion. If 
 * @param response//from w w w  . j a v  a  2 s  . c  o  m
 * @param cookieName
 * @param cookieValue
 * @param allowClear
 */
private static void addCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
        String cookieValue, boolean allowClear, UserDetailsConfig userDetailsConfig) {
    if (StringUtils.isBlank(cookieValue) && !allowClear) {
        throw new RuntimeException(
                "Was given a blank cookie value but allowClear is false for cookie name: " + cookieName);
    }

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("addCookie, cookieName: " + cookieName + ", cookie value: " + cookieValue + ", domain: "
                + userDetailsConfig.getCookiesDomain() + ", secure: " + userDetailsConfig.isCookiesSecure()
                + ", http-only: " + userDetailsConfig.isCookiesHttpOnly() + ", path: "
                + userDetailsConfig.getCookiesContextPath());
    }
    Cookie cookie = new Cookie(cookieName, cookieValue);

    // set the cookie domain
    if (StringUtils.isNotBlank(userDetailsConfig.getCookiesDomain())) {
        cookie.setDomain('.' + userDetailsConfig.getCookiesDomain());
    }
    // maybe not a good idea unless you can trust the proxy
    //      else if (StringUtils.isNotBlank(request.getHeader("X-Forwarded-Host"))) {
    //         cookie.setDomain('.' + request.getHeader("X-Forwarded-Host"));
    //      }
    //      else{
    //         cookie.setDomain('.' + request.getLocalName());
    //         
    //      }
    // set the cookie path
    if (StringUtils.isNotBlank(userDetailsConfig.getCookiesContextPath())) {
        cookie.setPath(userDetailsConfig.getCookiesContextPath());
    }
    //      else {
    //         cookie.setPath("/");
    //      }

    cookie.setSecure(userDetailsConfig.isCookiesSecure());
    cookie.setHttpOnly(userDetailsConfig.isCookiesHttpOnly());

    if (StringUtils.isBlank(cookieValue)) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("addCookie, setting max-age to 0 to clear cookie: " + cookieName);
        }
        cookie.setMaxAge(0);
    }
    response.addCookie(cookie);
}

From source file:ai.susi.server.AbstractAPIHandler.java

/**
 * Checks a request for valid login data, either a existing session, a cookie or an access token
 * @return user identity if some login is active, anonymous identity otherwise
 *///  www.j  a  va2s  . com
public static ClientIdentity getIdentity(HttpServletRequest request, HttpServletResponse response,
        Query query) {

    if (getLoginCookie(request) != null) { // check if login cookie is set

        Cookie loginCookie = getLoginCookie(request);

        ClientCredential credential = new ClientCredential(ClientCredential.Type.cookie,
                loginCookie.getValue());
        Authentication authentication = new Authentication(credential, DAO.authentication);

        if (authentication.getIdentity() != null && authentication.checkExpireTime()) {

            //reset cookie validity time
            authentication.setExpireTime(defaultCookieTime);
            loginCookie.setMaxAge(defaultCookieTime.intValue());
            loginCookie.setPath("/"); // bug. The path gets reset
            response.addCookie(loginCookie);

            return authentication.getIdentity();
        }

        authentication.delete();

        // delete cookie if set
        deleteLoginCookie(response);

        Log.getLog().info("Invalid login try via cookie from host: " + query.getClientHost());
    } else if (request.getSession().getAttribute("identity") != null) { // check session is set
        return (ClientIdentity) request.getSession().getAttribute("identity");
    } else if (request.getParameter("access_token") != null) { // access tokens can be used by api calls, somehow the stateless equivalent of sessions for browsers
        ClientCredential credential = new ClientCredential(ClientCredential.Type.access_token,
                request.getParameter("access_token"));
        Authentication authentication = new Authentication(credential, DAO.authentication);

        // check if access_token is valid
        if (authentication.getIdentity() != null) {
            ClientIdentity identity = authentication.getIdentity();

            if (authentication.checkExpireTime()) {
                Log.getLog().info("login for user: " + identity.getName() + " via access token from host: "
                        + query.getClientHost());

                if ("true".equals(request.getParameter("request_session"))) {
                    request.getSession().setAttribute("identity", identity);
                }
                if (authentication.has("one_time") && authentication.getBoolean("one_time")) {
                    authentication.delete();
                }
                return identity;
            }
        }
        Log.getLog().info("Invalid access token from host: " + query.getClientHost());
        return getAnonymousIdentity(query.getClientHost());
    }

    return getAnonymousIdentity(query.getClientHost());
}

From source file:org.etudes.jforum.ControllerUtils.java

/**
 * Add or update a cookie. This method adds a cookie, serializing its value using XML.
 *
 * @param name The cookie name.//from  w  ww  . ja v a 2 s .  c o m
 * @param value The cookie value
 */
public static void addCookie(String name, String value) {
    Cookie cookie = new Cookie(name, value);
    cookie.setMaxAge(3600 * 24 * 365);
    cookie.setPath("/");

    JForumBaseServlet.getResponse().addCookie(cookie);
}

From source file:org.jkcsoft.web.struts.http.controllers.HttpHelper.java

/**
 *
 *//*w ww  . java  2s  .  c o m*/
public static void setCookie(HttpServletResponse res, String cookieName, String cookieValue, String path,
        int timeLength) {
    Cookie c = new Cookie(cookieName, cookieValue);
    c.setPath(path);
    c.setMaxAge(timeLength);
    res.addCookie(c);
}