Java Code Examples for java.net.HttpCookie#parse()

The following examples show how to use java.net.HttpCookie#parse() . 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: ResponseUtils.java    From XS2A-Sandbox with Apache License 2.0 6 votes vote down vote up
private String cookie(String cookieStringIn, String name) {
	String cookieString = cookieStringIn;
	if(cookieString==null) {
		return null;
	}

	String cookieParamName=name+"=";

	// Fix Java: rfc2965 want cookie to be separated by comma.
	// SOmehow i am receiving some semicolon separated cookies.
	// Quick Fix: First strip the preceeding cookies if not the first.
	if(!StringUtils.startsWithIgnoreCase(cookieString, cookieParamName)) {
		int indexOfIgnoreCase = StringUtils.indexOfIgnoreCase(cookieString, cookieParamName);
		cookieString = cookieString.substring(indexOfIgnoreCase);
	}
	// The proce
	List<HttpCookie> cookies = HttpCookie.parse(cookieString);
	for (HttpCookie httpCookie : cookies) {
		if(StringUtils.equalsIgnoreCase(httpCookie.getName(), name)){
			return httpCookie.getValue();
		}
	}
	return null;
}
 
Example 2
Source File: CookiesUtils.java    From XS2A-Sandbox with Apache License 2.0 6 votes vote down vote up
public String resetCookies(List<String> cookieStrings) {
	String result = null;
	for (String cookieString : cookieStrings) {
		List<HttpCookie> parse = HttpCookie.parse(cookieString);
		for (HttpCookie httpCookie : parse) {
			if(StringUtils.isNoneBlank(httpCookie.getValue())) {
				String cookie = httpCookie.getName()+"="+httpCookie.getValue();
				if(result==null) {
					result = cookie;
				} else {
					result = result + " ; " + cookie;
				}
			}
		}
	}
	return result;
}
 
Example 3
Source File: ResponseOf.java    From takes with MIT License 6 votes vote down vote up
/**
 * Apply header to servlet response.
 * @param sresp Response
 * @param header Header
 */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static void applyHeader(final HttpServletResponse sresp,
    final String header) {
    final Iterator<Text> split = new Split(header, ":").iterator();
    final UncheckedText name = new UncheckedText(new Trimmed(split.next()));
    final UncheckedText val = new UncheckedText(new Trimmed(split.next()));
    if (new Equality<Text>(
        new TextOf("set-cookie"),
        new Lowered(name)
    ).value()
    ) {
        for (final HttpCookie cck : HttpCookie.parse(header)) {
            sresp.addCookie(
                new Cookie(cck.getName(), cck.getValue())
            );
        }
    } else {
        sresp.setHeader(name.asString(), val.asString());
    }
}
 
Example 4
Source File: HttpProxy.java    From haven-platform 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.
 */
private void copyProxyCookie(HttpServletRequest servletRequest,
                             HttpServletResponse servletResponse, Header header) {
    List<HttpCookie> cookies = HttpCookie.parse(header.getValue());
    String path = servletRequest.getContextPath(); // path starts with / or is empty string
    path += servletRequest.getServletPath(); // servlet path starts with / or is empty string
    for (int i = 0, l = cookies.size(); i < l; i++) {
        HttpCookie cookie = cookies.get(i);
        //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 5
Source File: CookieManager.java    From Kalle with Apache License 2.0 6 votes vote down vote up
/**
 * Cookie for the specified URI to save, where path and port will be verified.
 *
 * @param uri        uri.
 * @param cookieList all you want to save the Cookie, does not meet the rules will not be saved.
 */
public void add(URI uri, List<String> cookieList) {
    for (String cookieValue : cookieList) {
        List<HttpCookie> cookies = HttpCookie.parse(cookieValue);
        for (HttpCookie cookie : cookies) {
            if (cookie.getPath() == null) {
                String path = normalizePath(uri.getPath());
                cookie.setPath(path);
            } else if (!pathMatches(uri, cookie)) {
                continue;
            }

            if (cookie.getDomain() == null) cookie.setDomain(uri.getHost());

            String portList = cookie.getPortlist();
            int port = getPort(uri);
            if (TextUtils.isEmpty(portList) || containsPort(portList, port)) {
                cookieJar.add(uri, cookie);
            }
        }
    }
}
 
Example 6
Source File: WebSSOResourceTest.java    From knox with Apache License 2.0 6 votes vote down vote up
@Override
public void setHeader(String name, String value) {
  headers.put(name, value);
  /* if we have Set-Cookie header create a cookie for it */
  if ("Set-Cookie".equalsIgnoreCase(name)) {
    final List<HttpCookie> clientCookies = HttpCookie.parse(value);
    clientCookies.forEach(c -> {
      Cookie cookie = new Cookie(c.getName(), c.getValue());
      cookie.setSecure(c.getSecure());
      if (c.getDomain() != null) {
        cookie.setDomain(c.getDomain());
      }
      if (c.getPath() != null) {
        cookie.setPath(c.getPath());
      }
      cookie.setHttpOnly(c.isHttpOnly());
      cookie.setMaxAge(Math.toIntExact(c.getMaxAge()));
      this.addCookie(cookie);
    });
  }
}
 
Example 7
Source File: TestHttpCookieFlag.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpCookie() throws IOException {
  URL base = new URL("http://" + NetUtils.getHostPortString(server
          .getConnectorAddress(0)));
  HttpURLConnection conn = (HttpURLConnection) new URL(base,
          "/echo").openConnection();

  String header = conn.getHeaderField("Set-Cookie");
  List<HttpCookie> cookies = HttpCookie.parse(header);
  Assert.assertTrue(!cookies.isEmpty());
  Assert.assertTrue(header.contains("; HttpOnly"));
  Assert.assertTrue("token".equals(cookies.get(0).getValue()));
}
 
Example 8
Source File: TestHttpCookie.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
TestHttpCookie(String cHeader) {
    this.cHeader = cHeader;

    try {
        List<HttpCookie> cookies = HttpCookie.parse(cHeader);
        this.cookies = cookies;
    } catch (IllegalArgumentException ignored) {
        cookies = null;
    }
}
 
Example 9
Source File: TestHttpCookie.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
TestHttpCookie(String cHeader) {
    this.cHeader = cHeader;

    try {
        List<HttpCookie> cookies = HttpCookie.parse(cHeader);
        this.cookies = cookies;
    } catch (IllegalArgumentException ignored) {
        cookies = null;
    }
}
 
Example 10
Source File: TestHttpCookie.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
TestHttpCookie(String cHeader) {
    this.cHeader = cHeader;

    try {
        List<HttpCookie> cookies = HttpCookie.parse(cHeader);
        this.cookies = cookies;
    } catch (IllegalArgumentException ignored) {
        cookies = null;
    }
}
 
Example 11
Source File: TestAuthenticationFilter.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private static void parseCookieMap(String cookieHeader, HashMap<String,
        String> cookieMap) {
  List<HttpCookie> cookies = HttpCookie.parse(cookieHeader);
  for (HttpCookie cookie : cookies) {
    if (AuthenticatedURL.AUTH_COOKIE.equals(cookie.getName())) {
      cookieMap.put(cookie.getName(), cookie.getValue());
      if (cookie.getPath() != null) {
        cookieMap.put("Path", cookie.getPath());
      }
      if (cookie.getDomain() != null) {
        cookieMap.put("Domain", cookie.getDomain());
      }
    }
  }
}
 
Example 12
Source File: TestHttpCookieFlag.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpsCookie() throws IOException, GeneralSecurityException {
  URL base = new URL("https://" + NetUtils.getHostPortString(server
          .getConnectorAddress(1)));
  HttpsURLConnection conn = (HttpsURLConnection) new URL(base,
          "/echo").openConnection();
  conn.setSSLSocketFactory(clientSslFactory.createSSLSocketFactory());

  String header = conn.getHeaderField("Set-Cookie");
  List<HttpCookie> cookies = HttpCookie.parse(header);
  Assert.assertTrue(!cookies.isEmpty());
  Assert.assertTrue(header.contains("; HttpOnly"));
  Assert.assertTrue(cookies.get(0).getSecure());
  Assert.assertTrue("token".equals(cookies.get(0).getValue()));
}
 
Example 13
Source File: TestHttpCookie.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
TestHttpCookie(String cHeader) {
    this.cHeader = cHeader;

    try {
        List<HttpCookie> cookies = HttpCookie.parse(cHeader);
        this.cookies = cookies;
    } catch (IllegalArgumentException ignored) {
        cookies = null;
    }
}
 
Example 14
Source File: TestHttpCookie.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
TestHttpCookie(String cHeader) {
    this.cHeader = cHeader;

    try {
        List<HttpCookie> cookies = HttpCookie.parse(cHeader);
        this.cookies = cookies;
    } catch (IllegalArgumentException ignored) {
        cookies = null;
    }
}
 
Example 15
Source File: TestHttpCookie.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
TestHttpCookie(String cHeader) {
    this.cHeader = cHeader;

    try {
        List<HttpCookie> cookies = HttpCookie.parse(cHeader);
        this.cookies = cookies;
    } catch (IllegalArgumentException ignored) {
        cookies = null;
    }
}
 
Example 16
Source File: HttpUtils.java    From karate with MIT License 5 votes vote down vote up
public static Map<String, Cookie> parseCookieHeaderString(String header) {
    List<HttpCookie> list = HttpCookie.parse(header);
    Map<String, Cookie> map = new HashMap(list.size());
    list.forEach((hc) -> {
        String name = hc.getName();
        Cookie c = new Cookie(name, hc.getValue());
        c.putIfValueNotNull(Cookie.DOMAIN, hc.getDomain());
        c.putIfValueNotNull(Cookie.PATH, hc.getPath());
        c.putIfValueNotNull(Cookie.VERSION, hc.getVersion() + "");
        c.putIfValueNotNull(Cookie.MAX_AGE, hc.getMaxAge() + "");
        c.putIfValueNotNull(Cookie.SECURE, hc.getSecure() + "");
        map.put(name, c);
    });
    return map;
}
 
Example 17
Source File: TestHttpCookie.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
TestHttpCookie(String cHeader) {
    this.cHeader = cHeader;

    try {
        List<HttpCookie> cookies = HttpCookie.parse(cHeader);
        this.cookies = cookies;
    } catch (IllegalArgumentException ignored) {
        cookies = null;
    }
}
 
Example 18
Source File: TestHttpCookie.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
TestHttpCookie(String cHeader) {
    this.cHeader = cHeader;

    try {
        List<HttpCookie> cookies = HttpCookie.parse(cHeader);
        this.cookies = cookies;
    } catch (IllegalArgumentException ignored) {
        cookies = null;
    }
}
 
Example 19
Source File: TestHttpCookie.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
TestHttpCookie(String cHeader) {
    this.cHeader = cHeader;

    try {
        List<HttpCookie> cookies = HttpCookie.parse(cHeader);
        this.cookies = cookies;
    } catch (IllegalArgumentException ignored) {
        cookies = null;
    }
}
 
Example 20
Source File: FromServer.java    From http-builder-ng with Apache License 2.0 4 votes vote down vote up
public List<HttpCookie> parse() {
    return HttpCookie.parse(key + ": " + value);
}