package com.genesys.wsclient.impl;
import java.util.Enumeration;
import org.cometd.client.transport.HttpClientTransport;
import org.cometd.client.transport.HttpClientTransport.Cookie;
import org.eclipse.jetty.client.CachedExchange;
import org.eclipse.jetty.http.HttpFields;
import com.genesys.wsclient.HttpRequest;
public class CookieSessionImpl implements CookieSession {
private final HttpClientTransport.CookieProvider cookieProvider = new HttpClientTransport.StandardCookieProvider();
@Override
public void setupRequest(HttpRequest request) {
for (Cookie cookie : cookieProvider.getCookies())
request.setHeader("Cookie", cookie.getName() + "=" + cookie.getValue());
}
@Override
public void handleResponse(CachedExchange exchange) {
HttpFields httpFields = exchange.getResponseFields();
if (httpFields != null) {
Enumeration<String> setCookies = httpFields.getValues("Set-Cookie");
while (setCookies.hasMoreElements()) {
String cookie = setCookies.nextElement().split(";", 2)[0];
String[] nameValue = cookie.split("=", 2);
if (nameValue.length == 2) {
String name = nameValue[0].trim();
String value = nameValue[1].trim();
String domain = null;
String path = null;
int maxAge = -1;
boolean secure = false;
int version = 0;
String comment = null;
cookieProvider.setCookie(new Cookie(name, value, domain, path, maxAge, secure, version, comment));
}
}
}
}
@Override
public HttpClientTransport.CookieProvider getCookieProvider() {
return cookieProvider;
}
}