Skip to content

Commit

Permalink
to preserve session reuse same http client with same cookie manager
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Lamy <olamy@apache.org>
  • Loading branch information
olamy authored and markt-asf committed Oct 19, 2023
1 parent 0185fd7 commit 2e3ffe8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
Expand Up @@ -476,9 +476,9 @@ protected void invoke() {
_testCase = new WebTestCase();
setTestProperties(_testCase);
logger.trace("[BaseUrlClient] EXECUTING");
// if (_useSavedState && _state != null) {
// _testCase.getRequest().setState(_state);
// }
if (_useSavedState || _saveState) {
_testCase.getRequest().setState(true);
}
if (_redirect) {
logger.trace("##########Call setFollowRedirects");
_testCase.getRequest().setFollowRedirects(_redirect);
Expand Down
Expand Up @@ -106,6 +106,8 @@ public class HttpExchange {

List<HttpHeaders> headers = null;

private boolean state;

private String uri;

private String query;
Expand Down Expand Up @@ -326,15 +328,8 @@ public boolean getFollowRedirects() {
return this.followRedirect;
}

/**
* <code>setState</code> will set the HTTP state for the current request (i.e.
* session tracking). This has the side affect
*/
// public void setState(HttpState state) {
// _state = state;
// _useCookies = true;
// }

private static final ThreadLocal<HttpClient> httpClientThreadLocal = new ThreadLocal<>();
/**
* <code>execute</code> will dispatch the current request to the target
* server.
Expand All @@ -347,14 +342,9 @@ public HttpResponse execute() throws IOException {
String method;
int defaultPort;

CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
//CookieHandler.setDefault(cookieManager); //.getDefault()
// FIXME do this later on requestt
HttpClient.Builder builder = HttpClient.newBuilder().followRedirects(followRedirect?HttpClient.Redirect.ALWAYS:HttpClient.Redirect.NEVER)
.version(HttpClient.Version.HTTP_1_1) // not supporting 1.0?
// TODO _useCookies ?
.cookieHandler(cookieManager);
.version(HttpClient.Version.HTTP_1_1)
.cookieHandler(new CookieManager());

StringBuilder url = new StringBuilder();

Expand Down Expand Up @@ -384,8 +374,23 @@ protected PasswordAuthentication getPasswordAuthentication() {
throw new UnsupportedOperationException("Digest Authentication is not currently " + "supported");
}

HttpClient httpClient = null;

if(state) {
httpClient = httpClientThreadLocal.get();
} else {
httpClientThreadLocal.set(null);
}

if (httpClient == null) {
httpClient = builder.build();
}

if (state) {
httpClientThreadLocal.set(httpClient);
}


HttpClient httpClient = builder.build();

HttpRequest.Builder httpRequestBuilder = HttpRequest.newBuilder().uri(URI.create(url.toString()));

Expand Down Expand Up @@ -490,18 +495,6 @@ protected PasswordAuthentication getPasswordAuthentication() {
*/
}

/**
* Returns the current state for this request.
*
* @return HttpState current state
*/
// public HttpState getState() {
// if (_state == null) {
// _state = new HttpState();
// }
// return _state;
// }

public String toString() {
StringBuilder sb = new StringBuilder(255);
sb.append("[REQUEST LINE] -> ").append(_requestLine).append('\n');
Expand All @@ -521,6 +514,14 @@ public String toString() {

}

public boolean isState() {
return state;
}

public void setState(boolean state) {
this.state = state;
}

/*
* private methods
* ========================================================================
Expand Down

0 comments on commit 2e3ffe8

Please sign in to comment.