use of javax.ws.rs.core.NewCookie in project camel by apache.
the class CxfRsProducer method saveCookies.
private void saveCookies(Exchange exchange, Client client, CookieHandler cookieHandler) {
if (cookieHandler != null) {
CookieStore cookieStore = cookieHandler.getCookieStore(exchange);
for (NewCookie newCookie : client.getResponse().getCookies().values()) {
HttpCookie cookie = new HttpCookie(newCookie.getName(), newCookie.getValue());
cookie.setComment(newCookie.getComment());
cookie.setDomain(newCookie.getDomain());
cookie.setHttpOnly(newCookie.isHttpOnly());
cookie.setMaxAge(newCookie.getMaxAge());
cookie.setPath(newCookie.getPath());
cookie.setSecure(newCookie.isSecure());
cookie.setVersion(newCookie.getVersion());
cookieStore.add(client.getCurrentURI(), cookie);
}
}
}
use of javax.ws.rs.core.NewCookie in project camel by apache.
the class BonitaAuthFilter method filter.
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
if (requestContext.getCookies().get("JSESSIONID") == null) {
String username = bonitaApiConfig.getUsername();
String password = bonitaApiConfig.getPassword();
String bonitaApiToken = null;
if (ObjectHelper.isEmpty(username)) {
throw new IllegalArgumentException("Username provided is null or empty.");
}
if (ObjectHelper.isEmpty(password)) {
throw new IllegalArgumentException("Password provided is null or empty.");
}
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
Client client = clientBuilder.build();
WebTarget webTarget = client.target(bonitaApiConfig.getBaseBonitaURI()).path("loginservice");
MultivaluedMap<String, String> form = new MultivaluedHashMap<String, String>();
form.add("username", username);
form.add("password", password);
form.add("redirect", "false");
Response response = webTarget.request().accept(MediaType.APPLICATION_FORM_URLENCODED).post(Entity.form(form));
Map<String, NewCookie> cr = response.getCookies();
ArrayList<Object> cookies = new ArrayList<>();
for (NewCookie cookie : cr.values()) {
if ("X-Bonita-API-Token".equals(cookie.getName())) {
bonitaApiToken = cookie.getValue();
requestContext.getHeaders().add("X-Bonita-API-Token", bonitaApiToken);
}
cookies.add(cookie.toCookie());
}
requestContext.getHeaders().put("Cookie", cookies);
}
}
use of javax.ws.rs.core.NewCookie in project jersey by jersey.
the class InboundMessageContext method getResponseCookies.
/**
* Get any new cookies set on the response message.
*
* @return a read-only map of cookie name (String) to a {@link javax.ws.rs.core.NewCookie new cookie}.
*/
public Map<String, NewCookie> getResponseCookies() {
List<String> cookies = this.headers.get(HttpHeaders.SET_COOKIE);
if (cookies == null || cookies.isEmpty()) {
return Collections.emptyMap();
}
Map<String, NewCookie> result = new HashMap<String, NewCookie>();
for (String cookie : cookies) {
if (cookie != null) {
NewCookie newCookie = HttpHeaderReader.readNewCookie(cookie);
result.put(newCookie.getName(), newCookie);
}
}
return result;
}
use of javax.ws.rs.core.NewCookie in project jersey by jersey.
the class CookieImplTest method testNewCookieValueOf.
@Test
public void testNewCookieValueOf() {
NewCookie cookie = NewCookie.valueOf("fred=flintstone;Version=2");
assertEquals("fred", cookie.getName());
assertEquals("flintstone", cookie.getValue());
assertEquals(2, cookie.getVersion());
cookie = NewCookie.valueOf("fred=flintstone;Version=1;Max-Age=60");
assertEquals("fred", cookie.getName());
assertEquals("flintstone", cookie.getValue());
assertEquals(1, cookie.getVersion());
assertEquals(60, cookie.getMaxAge());
cookie = NewCookie.valueOf("fred=flintstone;Version=1;Comment=\"a modern stonage family\";Max-Age=60;Secure");
assertEquals("fred", cookie.getName());
assertEquals("flintstone", cookie.getValue());
assertEquals("a modern stonage family", cookie.getComment());
assertEquals(1, cookie.getVersion());
assertEquals(60, cookie.getMaxAge());
assertTrue(cookie.isSecure());
}
use of javax.ws.rs.core.NewCookie in project jersey by jersey.
the class ResponseTest method cloneTest.
/*
* Create an instance of Response using
* Response.ResponseBuilder.clone()
* verify that correct status code is returned
*/
@Test
public void cloneTest() throws CloneNotSupportedException {
StringBuilder sb = new StringBuilder();
int status = 200;
List<String> type = Arrays.asList("text/plain", "text/html");
List<String> encoding = Arrays.asList("gzip", "compress");
List<String> lang = Arrays.asList("en-US", "en-GB", "zh-CN");
String name = "name_1";
String value = "value_1";
Cookie ck1 = new Cookie(name, value);
NewCookie nck1 = new NewCookie(ck1);
List<String> cookies = Arrays.asList(nck1.toString().toLowerCase());
Response.ResponseBuilder respb1 = Response.status(status).header("Content-type", "text/plain").header("Content-type", "text/html").header("Content-Language", "en-US").header("Content-Language", "en-GB").header("Content-Language", "zh-CN").header("Cache-Control", "no-transform").header("Set-Cookie", "name_1=value_1;version=1");
Response.ResponseBuilder respb2 = respb1.clone();
Response resp2 = respb2.build();
String tmp = verifyResponse(resp2, null, status, encoding, lang, type, null, null, cookies);
if (tmp.endsWith("false")) {
System.out.println("### " + sb.toString());
fail();
}
sb.append(tmp).append(newline);
String content = "TestOnly";
Response resp1 = respb1.entity(content).cookie((NewCookie) null).build();
tmp = verifyResponse(resp1, content, status, encoding, lang, type, null, null, null);
if (tmp.endsWith("false")) {
System.out.println("### " + sb.toString());
fail();
}
MultivaluedMap<java.lang.String, java.lang.Object> mvp = resp1.getMetadata();
if (mvp.containsKey("Set-Cookie")) {
sb.append("Response contains unexpected Set-Cookie: ").append(mvp.getFirst("Set-Cookie").toString()).append(newline);
System.out.println("### " + sb.toString());
fail();
}
sb.append(tmp).append(newline);
}
Aggregations