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:com.qut.middleware.esoe.authn.servlet.AuthnServlet.java

/**
 * Sets the session cookie for this principal
 * //from  w ww.ja  va 2  s .  c om
 * @param data
 */
private void setSessionCookie(AuthnProcessorData data) {
    Cookie sessionCookie = new Cookie(this.sessionTokenName, data.getSessionID());
    sessionCookie.setDomain(this.sessionDomain);
    sessionCookie.setMaxAge(-1); // negative indicates session scope cookie
    sessionCookie.setPath("/");

    data.getHttpResponse().addCookie(sessionCookie);
}

From source file:com.mockey.model.ResponseFromService.java

private void setCookiesFromHeader(Header[] headers) {
    for (Header header : headers) {

        if (header.getName().equals("Set-Cookie")) {
            String headerValue = header.getValue();
            // Parse cookie
            String[] fields = headerValue.split(";\\s*");

            //String cookieValue = fields[0];
            //String expires = null;
            String path = null;/*from   w  ww.  j a v a 2  s .  c  o  m*/
            String domain = null;
            boolean secure = false;

            // Parse each field
            for (int j = 1; j < fields.length; j++) {
                if ("secure".equalsIgnoreCase(fields[j])) {
                    secure = true;
                } else if (fields[j].indexOf('=') > 0) {
                    String[] f = fields[j].split("=");
                    if ("expires".equalsIgnoreCase(f[0])) {
                        //expires = f[1];
                    } else if ("domain".equalsIgnoreCase(f[0])) {
                        domain = f[1];
                    } else if ("path".equalsIgnoreCase(f[0])) {
                        path = f[1];
                    }
                }
            }
            String[] cookieParts = headerValue.split("=", 2);
            String cookieBody = cookieParts[1];
            String[] cookieBodyParts = cookieBody.split("; ");
            Cookie cookie = new Cookie(cookieParts[0], cookieBodyParts[0]);
            cookie.setDomain(domain);
            cookie.setPath(path);
            cookie.setSecure(secure);
            //            if(expires!=null){
            //            Date expiresTime = null;
            //            try {
            //               expiresTime = HttpCookieDateUtil.parseDate(expires);
            //               Date nowTime = new Date();
            //               long maxAge = nowTime.getTime() - expiresTime.getTime();
            //               cookie.setMaxAge((int) maxAge/1000);
            //            }catch(Exception e){
            //               log.error("Unable to calculate maxAge with expiration date "+expiresTime, e);
            //            }
            //            }
            this.cookieList.add(cookie);
        }

    }
}

From source file:org.mule.transport.http.servlet.MuleHttpServletRequest.java

public Cookie[] getCookies() {
    org.apache.commons.httpclient.Cookie[] cookies = message
            .getInboundProperty(HttpConnector.HTTP_COOKIES_PROPERTY);
    if (cookies == null)
        return null;

    Cookie[] servletCookies = new Cookie[cookies.length];
    for (org.apache.commons.httpclient.Cookie c : cookies) {
        Cookie servletCookie = new Cookie(c.getName(), c.getValue());

        servletCookie.setComment(c.getComment());
        servletCookie.setDomain(c.getDomain());

    }/*from ww w . j  a va2s  . com*/
    return servletCookies;
}

From source file:net.yacy.cora.protocol.ResponseHeader.java

/**
 * Sets Cookie on the client machine.//from   ww w.  ja va 2s  .  c o  m
 *
 * @param name Cookie name
 * @param value Cookie value
 * @param maxage time to live in seconds, none negative number, according to https://tools.ietf.org/html/rfc2109, 0=discard in https://tools.ietf.org/html/rfc2965
 * @param path Path the cookie belongs to. Default - "/". Can be <b>null</b>.
 * @param domain Domain this cookie belongs to. Default - domain name. Can be <b>null</b>.
 * @param secure If true cookie will be send only over safe connection such as https
 * @see further documentation: <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a>
 */
public void setCookie(final String name, final String value, final Integer maxage, final String path,
        final String domain, final boolean secure) {
    /*
    * TODO:Here every value can be validated for correctness if needed
    * For example semicolon should be not in any of the values
    * However an exception in this case would be an overhead IMHO.
    */
    if (!name.isEmpty()) {
        if (this.cookieStore == null)
            this.cookieStore = new ArrayList<Cookie>();
        Cookie c = new Cookie(name, value);
        if (maxage != null && maxage >= 0)
            c.setMaxAge(maxage);
        if (path != null)
            c.setPath(path);
        if (domain != null)
            c.setDomain(domain);
        if (secure)
            c.setSecure(secure);
        this.cookieStore.add(c);
    }
}

From source file:com.hypersocket.session.json.SessionUtils.java

public void setLocale(HttpServletRequest request, HttpServletResponse response, String locale) {

    request.getSession().setAttribute(USER_LOCALE, locale);

    Cookie cookie = new Cookie(HYPERSOCKET_LOCALE, locale);
    cookie.setMaxAge(Integer.MAX_VALUE);
    cookie.setPath("/");
    cookie.setSecure(request.getProtocol().equalsIgnoreCase("https"));
    cookie.setDomain(request.getServerName());
    response.addCookie(cookie);//from  w  w  w .j  av a 2 s. c o m

}

From source file:org.orcid.core.manager.impl.InternalSSOManagerImpl.java

@Override
public void getAndUpdateCookie(String orcid, HttpServletRequest request, HttpServletResponse response) {
    InternalSSOEntity existingCookie = internalSSODao.find(orcid);
    if (existingCookie != null) {
        internalSSODao.update(existingCookie.getId(), existingCookie.getToken());
        HashMap<String, String> cookieValues = new HashMap<String, String>();
        cookieValues.put(COOKIE_KEY_ORCID, orcid);
        cookieValues.put(COOKIE_KEY_TOKEN, existingCookie.getToken());

        String jsonCookie = JsonUtils.convertToJsonString(cookieValues);
        Cookie tokenCookie = new Cookie(COOKIE_NAME, jsonCookie);
        tokenCookie.setMaxAge(maxAgeMinutes * 60);
        tokenCookie.setPath("/");
        tokenCookie.setSecure(true);/*from  www .j a va2 s. c  om*/
        tokenCookie.setHttpOnly(true);
        tokenCookie.setDomain(allowedDomain.trim());
        //Add new cookie to response
        response.addCookie(tokenCookie);
    }
}

From source file:com.nominanuda.web.http.ServletHelper.java

public Cookie servletCookie(HttpCookie c) {
    Cookie _c = new Cookie(c.getName(), c.getValue());
    if (c.getComment() != null) {
        _c.setComment(c.getComment());//  w w  w. j a  va 2s  .  c om
    }
    if (c.getDomain() != null) {
        _c.setDomain(c.getDomain());
    }
    if (c.getPath() != null) {
        _c.setPath(c.getPath());
    }
    _c.setSecure(c.getSecure());
    _c.setVersion(c.getVersion());
    _c.setHttpOnly(c.getDiscard());
    _c.setMaxAge((int) c.getMaxAge());
    return _c;
}

From source file:com.ctc.storefront.controllers.misc.AddToCartController.java

private void setCookie(final HttpServletResponse response, final CartData cartData) {
    final Cookie cookie = new Cookie("cartQuantity", String.valueOf(cartData.getTotalUnitCount()));
    cookie.setMaxAge(60 * 60);/*from w ww  .jav  a 2s  . com*/
    cookie.setPath("/");
    cookie.setDomain(siteConfigService.getString(CART_COUNT_COOKIE_DOMAIN_NAME, ".ctc.com"));
    response.addCookie(cookie);
}

From source file:com.demandware.vulnapp.challenge.impl.XSSChallenge.java

/**
 * create a new xss admin cookie based on a given domain
 *///from  ww w  .j  av  a 2 s.c  o  m
private javax.servlet.http.Cookie makeAdminCookie(String domain) {
    javax.servlet.http.Cookie adminCookie = new javax.servlet.http.Cookie(DIVA_ADMIN_NAME,
            this.adminCookieValue);
    adminCookie.setDomain(domain);
    return adminCookie;
}

From source file:com.jredrain.session.HttpSessionFilter.java

private Cookie generateCookie(HttpServletRequest request, HttpServletResponse response) {
    Cookie sessionIdCookie;
    String sid = null;/*from ww  w  . jav  a 2 s  .co m*/
    if (StringUtils.isBlank(sid)) {
        sid = CommonUtils.uuid();
    }
    sessionIdCookie = new Cookie(sessionIdCookieName, sid);

    String domain = request.getServerName();

    if (domain != null) {
        sessionIdCookie.setDomain(domain);
    }

    sessionIdCookie.setPath("/");
    response.addCookie(sessionIdCookie);
    return sessionIdCookie;
}