Example usage for javax.servlet.http Cookie setDomain

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

Introduction

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

Prototype

public void setDomain(String domain) 

Source Link

Document

Specifies the domain within which this cookie should be presented.

Usage

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// w w w  .  j av a 2  s  .  c om
 * @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:com.xidu.framework.common.util.CookieUtils.java

/**
 * set the name/value entry to the cookie
 * //from w  w w  .  java2 s.  c  o m
 * @Date : 2011-3-23
 * @param response
 *            - HttpServletResponse's instance
 * @param name
 *            - Cookie's Entry key
 * @param value
 *            - Cookie's Entry value
 * @param path
 *            - Cookie's path
 * @param domain
 *            - Cookie' domain
 * @param maxAge
 *            - Cookie's max age
 */
public static void setCookie(HttpServletResponse response, String name, String value, String path,
        String domain, int maxAge) {
    logger.debug("cookie value:" + value);
    Cookie cookie = new Cookie(name, value);
    cookie.setSecure(false);
    if (StringUtils.isNotBlank(path)) {
        cookie.setPath(path);
    }
    cookie.setMaxAge(maxAge);
    if (StringUtils.isNotBlank(domain)) {
        cookie.setDomain(domain);
    }
    response.addCookie(cookie);
}

From source file:com.aurel.track.master.ModuleBL.java

public static Cookie cretaeCookie(String cookieValue, String path, String url) {
    Cookie myCookie = new Cookie("JSESSIONID", cookieValue);
    myCookie.setPath(path);//from w  w w .j a  v a  2s .  c o  m
    URI uri;
    try {
        uri = new URI(url);
        String domain = uri.getHost();
        myCookie.setDomain(domain);
    } catch (URISyntaxException e) {
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
    }
    return myCookie;
}

From source file:net.shopxx.util.CookieUtils.java

/**
 * cookie/* w ww .  j  av  a 2 s  . c o  m*/
 * 
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 * @param name
 *            cookie??
 * @param path
 *            
 * @param domain
 *            
 */
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String path, String domain) {
    Assert.notNull(request);
    Assert.notNull(response);
    Assert.hasText(name);
    Cookie cookie = new Cookie(name, null);
    cookie.setMaxAge(0);
    if (StringUtils.isNotEmpty(path)) {
        cookie.setPath(path);
    }
    if (StringUtils.isNotEmpty(domain)) {
        cookie.setDomain(domain);
    }
    response.addCookie(cookie);
}

From source file:com.kingcore.framework.util.CookieUtils.java

/** 
 * Creates a Cookie with the specified name, value and max age,
 * and adds it to the response.//from  w  ww .  java  2s. c o  m
 * cookies  cookie?Base64 ?
 *    The form of the domain name is specified by RFC 2109. A domain name begins with a dot (.foo.com) 
 *       and means that the cookie is visible to servers in a specified Domain Name System (DNS) zone 
 *       (for example, www.foo.com, but not a.b.foo.com). By default, cookies are only returned to 
 *       the server that sent them.
 * @param name cookie's name.
 * @param value cookie's value.
 * @param maxAge the time cookie been keeped. the unit is second.
 *      age of the cookie in seconds,??Cookie.
 *      an integer specifying the maximum age of the cookie in seconds; 
 *      if negative, means the cookie is not stored; if zero, deletes the cookie.
 * @param res response Object.
 * @param needEncode ??Cookie?Base64?true
 * @param domain Cookie's domain
 */
public static void sendCookie(String name, String value, int maxAge, HttpServletResponse response,
        boolean needEncode, String domain) {

    try {
        if (needEncode) {
            value = Base64.encode(value.getBytes("utf-8")); //?
            //              value = new String(Base64.encode( value.getBytes("utf-8")), "utf-8" );   //utf-8
        }
        //System.out.println("value = " + value);
        Cookie cookie = new Cookie(name, value);//Hex.encode(value.getBytes()) );
        cookie.setMaxAge(maxAge);
        cookie.setPath("/");
        if (domain != null) {
            cookie.setDomain(domain); // domain
        }
        response.addCookie(cookie);

    } catch (UnsupportedEncodingException e) {
        log.debug("debug", e);
        /// e.pri ntStackTrace();
    }

}

From source file:com.identityconcepts.shibboleth.WSFedLoginHandler.java

/**
 * set cookie for pass-through//w  w w. j av a2 s . c  om
 * cookieDomain can be configured in the handler config
 *
 * @param  path   path to which the client should return the cookie
 */
public static Cookie createCookie(String path) {
    Cookie cookie = new Cookie(COOKIE_NAME, "1");
    cookie.setMaxAge(60 * 60 * 24 * 365);
    cookie.setPath(path);
    cookie.setSecure(true);
    // use cookieDomain if set
    if (!((cookieDomain == null) || (cookieDomain == ""))) {
        cookie.setDomain(cookieDomain);
    }
    return cookie;
}

From source file:com.ax.utils.CookieUtils.java

/**
 * Stores a value in a cookie. This cookie will persist for the amount
 * specified in the <tt>saveTime</tt> parameter.
 * /*from  w w w  .j  a v a2s .co  m*/
 * @see #setCookie(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,String,String)
 * @param request
 *            the servlet request.
 * @param response
 *            the servlet response.
 * @param name
 *            a name to identify the cookie.
 * @param value
 *            the value to store in the cookie.
 * @param maxAge
 *            the time (in seconds) this cookie should live.
 * @param domain
 *            the domain.
 * @param path
 *            the path.
 */
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, int maxAge, String domain, String path) {
    // Check to make sure the new value is not null (appservers like Tomcat
    // 4 blow up if the value is null).
    if (value == null) {
        value = "";
    }
    if (StringUtils.isEmpty(path)) {
        path = "/";
    }
    Cookie cookie = new Cookie(name, value);
    // maxAge0cookiemaxAge
    // if (maxAge > 0)
    // {
    cookie.setMaxAge(maxAge);
    // }
    cookie.setPath(path);
    // domain?cookiedomain
    if (!StringUtils.isEmpty(domain)) {
        cookie.setDomain(domain);
    }
    response.addCookie(cookie);
}

From source file:net.shopxx.util.CookieUtils.java

/**
 * cookie/* www . j  a  v  a  2  s.c om*/
 * 
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 * @param name
 *            cookie??
 * @param value
 *            cookie
 * @param path
 *            
 * @param maxAge
 *            (??: )
 * @param domain
 *            
 * @param secure
 *            ??
 */
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, String path, Integer maxAge, String domain, Boolean secure) {
    Assert.notNull(request);
    Assert.notNull(response);
    Assert.hasText(name);
    try {
        value = URLEncoder.encode(value, "UTF-8");
        Cookie cookie = new Cookie(name, value);
        if (StringUtils.isNotEmpty(path)) {
            cookie.setPath(path);
        }
        if (maxAge != null) {
            cookie.setMaxAge(maxAge);
        }
        if (StringUtils.isNotEmpty(domain)) {
            cookie.setDomain(domain);
        }
        if (secure != null) {
            cookie.setSecure(secure);
        }
        response.addCookie(cookie);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

From source file:com.liusoft.dlog4j.util.RequestUtils.java

/**
 * COOKIE/*from ww w.ja  va  2 s  .  c  o  m*/
 * 
 * @param name
 * @param value
 * @param maxAge
 */
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, int maxAge) {
    Cookie cookie = new Cookie(name, value);
    cookie.setMaxAge(maxAge);
    String serverName = request.getServerName();
    String domain = getDomainOfServerName(serverName);
    if (domain != null && domain.indexOf('.') != -1) {
        cookie.setDomain('.' + domain);
    }
    cookie.setPath("/");
    response.addCookie(cookie);
}

From source file:com.iterzp.momo.utils.WebUtils.java

/**
 * cookie/*from  w w w  .  j a  va2 s .  co m*/
 * 
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 * @param name
 *            cookie??
 * @param path
 *            
 * @param domain
 *            
 */
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String path, String domain) {
    Assert.notNull(request);
    Assert.notNull(response);
    Assert.hasText(name);
    try {
        name = URLEncoder.encode(name, "UTF-8");
        Cookie cookie = new Cookie(name, null);
        cookie.setMaxAge(0);
        if (StringUtils.isNotEmpty(path)) {
            cookie.setPath(path);
        }
        if (StringUtils.isNotEmpty(domain)) {
            cookie.setDomain(domain);
        }
        response.addCookie(cookie);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}