Java Code Examples for javax.servlet.http.Cookie#setVersion()

The following examples show how to use javax.servlet.http.Cookie#setVersion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApiProxyServlet.java    From onboard with Apache License 2.0 6 votes vote down vote up
/**
 * Copy cookie from the proxy to the servlet client. Replaces cookie path to local path and renames cookie to avoid
 * collisions.
 */
protected void copyProxyCookie(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Header header) {
    List<HttpCookie> cookies = HttpCookie.parse(header.getValue());
    String path = getServletContext().getServletContextName();
    if (path == null) {
        path = "";
    }
    path += servletRequest.getServletPath();

    for (HttpCookie cookie : cookies) {
        // set cookie name prefixed w/ a proxy value so it won't collide w/ other cookies
        String proxyCookieName = getCookieNamePrefix() + cookie.getName();
        Cookie servletCookie = new Cookie(proxyCookieName, cookie.getValue());
        servletCookie.setComment(cookie.getComment());
        servletCookie.setMaxAge((int) cookie.getMaxAge());
        servletCookie.setPath(path); // set to the path of the proxy servlet
        // don't set cookie domain
        servletCookie.setSecure(cookie.getSecure());
        servletCookie.setVersion(cookie.getVersion());
        servletResponse.addCookie(servletCookie);
    }
}
 
Example 2
Source File: SimpleCookieManager.java    From lastaflute with Apache License 2.0 6 votes vote down vote up
protected Cookie createSnapshotCookie(Cookie src) {
    // not use close() to avoid dependency to ServletContainer
    final Cookie snapshot = new Cookie(src.getName(), src.getValue());
    snapshot.setPath(src.getPath());
    snapshot.setMaxAge(src.getMaxAge());
    final String domain = src.getDomain();
    if (domain != null) { // the setter has filter process
        snapshot.setDomain(domain);
    }
    snapshot.setSecure(src.getSecure());
    final String comment = src.getComment();
    if (comment != null) { // just in case
        snapshot.setComment(comment);
    }
    snapshot.setVersion(src.getVersion());
    snapshot.setHttpOnly(src.isHttpOnly());
    return snapshot;
}
 
Example 3
Source File: PreservingCookiePathProxyServlet.java    From bonita-ui-designer with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Copy cookie from the proxy to the servlet client.
 * Replaces cookie path to local path and renames cookie to avoid collisions.
 */
@Override
protected void copyProxyCookie(HttpServletRequest servletRequest,
                               HttpServletResponse servletResponse, String headerValue) {
    List<HttpCookie> cookies = HttpCookie.parse(headerValue);
    String path = servletRequest.getContextPath(); // path starts with / or is empty string
    path += servletRequest.getServletPath(); // servlet path starts with / or is empty string
    if (path.isEmpty()) {
        path = "/";
    }

    for (HttpCookie cookie : cookies) {
        //set cookie name prefixed w/ a proxy value so it won't collide w/ other cookies
        String cookieName = doPreserveCookies ? cookie.getName() : getCookieNamePrefix(cookie.getName()) + cookie.getName();
        Cookie servletCookie = new Cookie(cookieName, cookie.getValue());
        servletCookie.setComment(cookie.getComment());
        servletCookie.setMaxAge((int) cookie.getMaxAge());
        //fix: preserve path when preserving cookies
        String cookiePath = doPreserveCookies ? cookie.getPath() : path;
        servletCookie.setPath(cookiePath); //set to the path of the proxy servlet
        // don't set cookie domain
        servletCookie.setSecure(cookie.getSecure());
        servletCookie.setVersion(cookie.getVersion());
        servletResponse.addCookie(servletCookie);
    }
}
 
Example 4
Source File: TestCookies.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void doTestBug60788(boolean useRfc6265) {
    Cookie expected = new Cookie("userId", "foo");
    expected.setVersion(1);
    if (useRfc6265) {
        expected.setDomain("\"www.example.org\"");
        expected.setPath("\"/\"");
    } else {
        // The legacy processor removes the quotes for domain and path
        expected.setDomain("www.example.org");
        expected.setPath("/");
    }

    test(useRfc6265, "$Version=\"1\"; userId=\"foo\";$Path=\"/\";$Domain=\"www.example.org\"",
            expected);
}
 
Example 5
Source File: LanguageBean.java    From oxAuth with MIT License 5 votes vote down vote up
private void setCookieValue(String value) {
	FacesContext ctx = FacesContext.getCurrentInstance();

	if (ctx == null)
		return;
	HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
	Cookie cookie = new Cookie(COOKIE_NAME, value);
	cookie.setMaxAge(DEFAULT_MAX_AGE);
	cookie.setPath(COOKIE_PATH);
	cookie.setSecure(true);
	cookie.setVersion(1);
	response.addCookie(cookie);
}
 
Example 6
Source File: NettyToServletCookieConvertor.java    From netty-servlet with Apache License 2.0 5 votes vote down vote up
public static Cookie convert(org.jboss.netty.handler.codec.http.Cookie nettyCookie){
    Cookie servletCookie = new Cookie(nettyCookie.getName(),nettyCookie.getValue());
    servletCookie.setDomain(nettyCookie.getDomain());
    servletCookie.setMaxAge(nettyCookie.getMaxAge());
    servletCookie.setHttpOnly(nettyCookie.isHttpOnly());
    servletCookie.setPath(nettyCookie.getPath());
    servletCookie.setSecure(nettyCookie.isSecure());
    servletCookie.setVersion(nettyCookie.getVersion());
    servletCookie.setComment(nettyCookie.getComment());
    return servletCookie;
}
 
Example 7
Source File: JWTUtil.java    From yes-cart with Apache License 2.0 5 votes vote down vote up
/**
 * Send JWT log off response.
 *
 * @param response     response to render
 *
 * @throws IOException error
 */
static void sendLogOffJWT(final HttpServletResponse response) throws IOException {

    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");

    final Cookie xAuth = new Cookie(JWTUtil.COOKIE_HEADER, "");
    xAuth.setMaxAge(0);
    xAuth.setVersion(1);
    response.addCookie(xAuth);

    response.getWriter().write("{\"token\": null }");

}
 
Example 8
Source File: JaxRsResponseHandler.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private static Cookie mapCookie ( final Map.Entry<String, NewCookie> entry )
{
    final String name = entry.getKey ();
    final NewCookie nc = entry.getValue ();

    final Cookie cookie = new Cookie ( name, nc.getValue () );
    cookie.setComment ( nc.getComment () );
    cookie.setDomain ( nc.getDomain () );
    cookie.setHttpOnly ( nc.isHttpOnly () );
    cookie.setMaxAge ( nc.getMaxAge () );
    cookie.setPath ( nc.getPath () );
    cookie.setSecure ( nc.isSecure () );
    cookie.setVersion ( nc.getVersion () );
    return cookie;
}
 
Example 9
Source File: WebTokenTuplizerImpl.java    From yes-cart with Apache License 2.0 5 votes vote down vote up
private Cookie createNewCookie(final String name, final String value, final int maxAgeInSeconds, final String path) {
    final Cookie cookie = new Cookie(name, value);
    cookie.setMaxAge(maxAgeInSeconds);
    cookie.setPath(path);
    cookie.setVersion(1); // allow to have base64 encoded value in cookie
    return cookie;
}
 
Example 10
Source File: TestCookieProcessorGeneration.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void v1ValueUTF8() {
    String value = "\u2300";
    Cookie cookie = new Cookie("foo", value);
    cookie.setVersion(1);
    doTest(cookie, (String) null, "foo=" + value);
}
 
Example 11
Source File: ProxyServlet.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
protected void addCookie(final HttpServletResponse resp, final Map.Entry<String, NewCookie> cookie) {
    final NewCookie nc = cookie.getValue();
    final Cookie servletCookie = new Cookie(cookie.getKey(), nc.getValue());
    servletCookie.setComment(nc.getComment());
    if (nc.getDomain() != null) {
        servletCookie.setDomain(nc.getDomain());
    }
    servletCookie.setHttpOnly(nc.isHttpOnly());
    servletCookie.setSecure(nc.isSecure());
    servletCookie.setMaxAge(nc.getMaxAge());
    servletCookie.setPath(nc.getPath());
    servletCookie.setVersion(nc.getVersion());
    resp.addCookie(servletCookie);
}
 
Example 12
Source File: TestCookieProcessorGeneration.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void v1ValueContainsQuote() {
    Cookie cookie = new Cookie("foo", "a\"b");
    cookie.setVersion(1);
    doTest(cookie, "foo=\"a\\\"b\"; Version=1", null);
}
 
Example 13
Source File: TestCookieProcessorGeneration.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void v1ValueContainsBackslash() {
    Cookie cookie = new Cookie("foo", "a\\b");
    cookie.setVersion(1);
    doTest(cookie, "foo=\"a\\\\b\"; Version=1", null);
}
 
Example 14
Source File: TestCookieProcessorGeneration.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void v1ValueContainsBackslashAndQuote() {
    Cookie cookie = new Cookie("foo", "a\"b\\c");
    cookie.setVersion(1);
    doTest(cookie, "foo=\"a\\\"b\\\\c\"; Version=1", null);
}
 
Example 15
Source File: TestCookieProcessorGeneration.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void v1ValueContainsEquals() {
    Cookie cookie = new Cookie("foo", "a=b");
    cookie.setVersion(1);
    doTest(cookie, "foo=\"a=b\"; Version=1", "foo=a=b");
}
 
Example 16
Source File: TestCookieProcessorGeneration.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void v1ValueContainsComma() {
    Cookie cookie = new Cookie("foo", "a,b");
    cookie.setVersion(1);
    doTest(cookie, "foo=\"a,b\"; Version=1", null);
}
 
Example 17
Source File: TestCookieProcessorGeneration.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void v1QuotedValue() {
    Cookie cookie = new Cookie("foo", "\"bar\"");
    cookie.setVersion(1);
    doTest(cookie, "foo=\"bar\"; Version=1", "foo=\"bar\"");
}
 
Example 18
Source File: TestCookieProcessorGeneration.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void v1NullValue() {
    Cookie cookie = new Cookie("foo", null);
    cookie.setVersion(1);
    doTest(cookie, "foo=\"\"; Version=1", "foo=");
}
 
Example 19
Source File: Request.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Parse cookies.
 */
protected void parseCookies() {

    cookiesParsed = true;

    Cookies serverCookies = coyoteRequest.getCookies();
    int count = serverCookies.getCookieCount();
    if (count <= 0) {
        return;
    }

    cookies = new Cookie[count];

    int idx=0;
    for (int i = 0; i < count; i++) {
        ServerCookie scookie = serverCookies.getCookie(i);
        try {
            /*
            we must unescape the '\\' escape character
            */
            Cookie cookie = new Cookie(scookie.getName().toString(),null);
            int version = scookie.getVersion();
            cookie.setVersion(version);
            cookie.setValue(unescape(scookie.getValue().toString()));
            cookie.setPath(unescape(scookie.getPath().toString()));
            String domain = scookie.getDomain().toString();
            if (domain!=null)
             {
                cookie.setDomain(unescape(domain));//avoid NPE
            }
            String comment = scookie.getComment().toString();
            cookie.setComment(version==1?unescape(comment):null);
            cookies[idx++] = cookie;
        } catch(IllegalArgumentException e) {
            // Ignore bad cookie
        }
    }
    if( idx < count ) {
        Cookie [] ncookies = new Cookie[idx];
        System.arraycopy(cookies, 0, ncookies, 0, idx);
        cookies = ncookies;
    }

}
 
Example 20
Source File: CrossSubdomainSessionValve.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
protected void replaceCookie(Request request, Response response, Cookie cookie) {

        Delegator delegator = (Delegator) request.getAttribute("delegator");
        // copy the existing session cookie, but use a different domain (only if domain is valid)
        String cookieDomain = null;
        cookieDomain = EntityUtilProperties.getPropertyValue("url", "cookie.domain", "", delegator);

        if (UtilValidate.isEmpty(cookieDomain)) {
            String serverName = request.getServerName();
            String[] domainArray = serverName.split("\\.");
            // check that the domain isn't an IP address
            if (domainArray.length == 4) {
                boolean isIpAddress = true;
                for (String domainSection : domainArray) {
                    if (!UtilValidate.isIntegerInRange(domainSection, 0, 255)) {
                        isIpAddress = false;
                        break;
                    }
                }
                if (isIpAddress) {
                    return;
                }
            }
            if (domainArray.length > 2) {
                cookieDomain = "." + domainArray[domainArray.length - 2] + "." + domainArray[domainArray.length - 1];
            }
        }


        if (UtilValidate.isNotEmpty(cookieDomain)) {
            Cookie newCookie = new Cookie(cookie.getName(), cookie.getValue());
            if (cookie.getPath() != null) {
                newCookie.setPath(cookie.getPath());
            }
            newCookie.setDomain(cookieDomain);
            newCookie.setMaxAge(cookie.getMaxAge());
            newCookie.setVersion(cookie.getVersion());
            if (cookie.getComment() != null) {
                newCookie.setComment(cookie.getComment());
            }
            newCookie.setSecure(cookie.getSecure());
            newCookie.setHttpOnly(cookie.isHttpOnly());

            // if the response has already been committed, our replacement strategy will have no effect
            if (response.isCommitted()) {
                Debug.logError("CrossSubdomainSessionValve: response was already committed!", module);
            }

            // find the Set-Cookie header for the existing cookie and replace its value with new cookie
            MimeHeaders mimeHeaders = request.getCoyoteRequest().getMimeHeaders();
            for (int i = 0, size = mimeHeaders.size(); i < size; i++) {
                if (mimeHeaders.getName(i).equals("Set-Cookie")) {
                    MessageBytes value = mimeHeaders.getValue(i);
                    if (value.indexOf(cookie.getName()) >= 0) {
                        String newCookieValue = request.getContext().getCookieProcessor().generateHeader(newCookie);
                        if (Debug.verboseOn()) Debug.logVerbose("CrossSubdomainSessionValve: old Set-Cookie value: " + value.toString(), module);
                        if (Debug.verboseOn()) Debug.logVerbose("CrossSubdomainSessionValve: new Set-Cookie value: " + newCookieValue, module);
                        value.setString(newCookieValue);
                    }
                }
            }
        }
    }