Package com.genesys.wsclient.impl

Source Code of com.genesys.wsclient.impl.CookieSessionImpl

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;
  }
 
}
TOP

Related Classes of com.genesys.wsclient.impl.CookieSessionImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.