Skip to content

BREAKING CHANGE: upgrade to JDK17, httpclinet 5, jnuit5, spring 6 #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

<parent>
<groupId>com.gooddata</groupId>
<artifactId>gdc-parent</artifactId>
<version>6.0.1</version>
<artifactId>gooddata-parent</artifactId>
<version>3.1.0</version>
</parent>

<developers>
Expand Down Expand Up @@ -83,6 +83,12 @@
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down Expand Up @@ -158,7 +164,7 @@
<version>3.2.5</version>
<configuration>
<useModulePath>false</useModulePath>
<skip>true</skip>
<!-- <skip>true</skip> -->
</configuration>
</plugin>
</plugins>
Expand All @@ -182,7 +188,7 @@
<version>3.2.5</version>
<configuration>
<useModulePath>false</useModulePath>
<skip>true</skip>
<!-- <skip>true</skip>-->
</configuration>
</plugin>
<plugin>
Expand Down
213 changes: 171 additions & 42 deletions src/main/java/com/gooddata/http/client/GoodDataHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.http.Header;

Expand All @@ -26,6 +27,7 @@

import java.io.IOException;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
Expand All @@ -41,6 +43,11 @@ public class GoodDataHttpClient {
public static final String COOKIE_GDC_AUTH_TT = "cookie=GDCAuthTT";
public static final String COOKIE_GDC_AUTH_SST = "cookie=GDCAuthSST";

private volatile boolean tokenRefreshing = false;
private final Object tokenRefreshMonitor = new Object();



static final String TT_HEADER = "X-GDC-AuthTT";
static final String SST_HEADER = "X-GDC-AuthSST";

Expand Down Expand Up @@ -95,62 +102,136 @@ private GoodDataChallengeType identifyGoodDataChallenge(final ClassicHttpRespons
return GoodDataChallengeType.UNKNOWN;
}


/**
* Handles the authentication challenge and returns a refreshed response.
*/
/**
* Handles the authentication challenge and returns a refreshed response.
*/


private ClassicHttpResponse handleResponse(
final HttpHost httpHost,
final ClassicHttpRequest request,
final ClassicHttpRequest originalRequest,
final ClassicHttpResponse originalResponse,
final HttpContext context) throws IOException {
final HttpContext context) throws IOException, InterruptedException {


if (originalResponse == null) {
throw new IllegalStateException("httpClient.execute returned null! Check your mock configuration.");
}

final GoodDataChallengeType challenge = identifyGoodDataChallenge(originalResponse);

if (challenge == GoodDataChallengeType.UNKNOWN) {
return originalResponse;
}

EntityUtils.consume(originalResponse.getEntity());

synchronized (tokenRefreshMonitor) {
if (tokenRefreshing) {
while (tokenRefreshing) {
try {
tokenRefreshMonitor.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Interrupted while waiting for token refresh", e);
}
}
final ClassicHttpRequest retryRequest = cloneRequestWithNewTT(originalRequest, tt);
return this.httpClient.execute(httpHost, retryRequest, context, response -> response);
} else {
tokenRefreshing = true;
}
}

try {
if (authLock.tryLock()) {
final Lock writeLock = rwLock.writeLock();
writeLock.lock();
final Lock writeLock = rwLock.writeLock();
writeLock.lock();
try {
boolean doSST = true;
try {
if (challenge == GoodDataChallengeType.TT && sst != null) {
if (refreshTt()) {
doSST = false;
}
if (challenge == GoodDataChallengeType.TT && sst != null) {
boolean refreshed = refreshTt();
if (refreshed) {
doSST = false;
}
if (doSST) {
sst = sstStrategy.obtainSst(httpClient, authHost);
if (!refreshTt()) {
throw new GoodDataAuthException("Unable to obtain TT after successfully obtained SST");
}
}
if (doSST) {
sst = sstStrategy.obtainSst(httpClient, authHost);
if (!refreshTt()) {
throw new GoodDataAuthException("Unable to obtain TT after successfully obtained SST");
}
} finally {
writeLock.unlock();
}
} else {
authLock.lock();
} finally {
writeLock.unlock();
}
} finally {
authLock.unlock();
synchronized (tokenRefreshMonitor) {
tokenRefreshing = false;
tokenRefreshMonitor.notifyAll();
}
}

final ClassicHttpRequest retryRequest = cloneRequestWithNewTT(originalRequest, tt);
ClassicHttpResponse retryResponse = this.httpClient.execute(httpHost, retryRequest, context, response -> response);


if (retryResponse.getCode() == HttpStatus.SC_UNAUTHORIZED &&
identifyGoodDataChallenge(retryResponse) != GoodDataChallengeType.UNKNOWN) {
return retryResponse;
}

return retryResponse;
}




/*
*
*/

private ClassicHttpRequest cloneRequestWithNewTT(ClassicHttpRequest original, String newTT) {
ClassicHttpRequest copy;


// Clone basic types (extend if needed)
switch (original.getMethod()) {
case "GET":
copy = new HttpGet(original.getRequestUri());
break;
case "DELETE":
copy = new org.apache.hc.client5.http.classic.methods.HttpDelete(original.getRequestUri());
break;
default:
throw new UnsupportedOperationException("Unsupported HTTP method: " + original.getMethod());
}

// Copy original headers
for (Header header : original.getHeaders()) {
copy.addHeader(header.getName(), header.getValue());
}
// New style: use response handler, response will be automatically released
return this.httpClient.execute(httpHost, request, context, response -> response);

// Set the new TT
copy.addHeader(TT_HEADER, newTT);
return copy;
}





/**
* Refreshes the temporary token (TT) using SST.
*/
/*
private boolean refreshTt() throws IOException {
log.debug("Obtaining TT");
final HttpGet request = new HttpGet(TOKEN_URL);
try {
request.setHeader(SST_HEADER, sst);
request.addHeader(SST_HEADER, sst);
// Use response handler for token extraction
return httpClient.execute(authHost, request, (HttpContext) null, response -> {
int status = response.getCode();
Expand All @@ -168,48 +249,96 @@ private boolean refreshTt() throws IOException {
request.reset();
}
}
*/

private boolean refreshTt() throws IOException {
log.debug("Obtaining TT");
final HttpGet request = new HttpGet(TOKEN_URL);
try {

request.addHeader(SST_HEADER, sst);

return httpClient.execute(authHost, request, (HttpContext) null, response -> {
int status = response.getCode();

switch (status) {
case HttpStatus.SC_OK:
tt = TokenUtils.extractTT(response);
return true;
case HttpStatus.SC_UNAUTHORIZED:
return false;
default:
throw new GoodDataAuthException("Unable to obtain TT, HTTP status: " + status);
}
});
} finally {
request.reset();
}
}


/**
* Main public execute method: new style, always uses response handler.
*/
/**
* Main public execute method: new style, always uses response handler.
*/
public ClassicHttpResponse execute(HttpHost target, ClassicHttpRequest request, HttpContext context) throws IOException {
notNull(request, "Request can't be null");
final boolean logoutRequest = isLogoutRequest(target, request);
final Lock lock = logoutRequest ? rwLock.writeLock() : rwLock.readLock();
// FIX: use only writeLock to avoid deadlock during handleResponse
final Lock lock = rwLock.writeLock();
lock.lock();

try {
if (tt != null) {
request.setHeader(TT_HEADER, tt);

if (logoutRequest) {
try {
sstStrategy.logout(httpClient, target, request.getRequestUri(), sst, tt);
tt = null;
sst = null;
// Return a dummy response for logout success
return new org.apache.hc.core5.http.message.BasicClassicHttpResponse(HttpStatus.SC_NO_CONTENT, "Logout successful");
} catch (GoodDataLogoutException e) {
return new org.apache.hc.core5.http.message.BasicClassicHttpResponse(e.getStatusCode(), e.getStatusText());
}
// --- PATCH: Always check logout even if TT is null, if it's a logout request ---
if (isLogoutRequest(target, request)) {
try {

sstStrategy.logout(httpClient, target, request.getRequestUri(), sst, tt);
tt = null;
sst = null;
// Return a dummy response for logout success
return new BasicClassicHttpResponse(HttpStatus.SC_NO_CONTENT, "Logout successful");
} catch (GoodDataLogoutException e) {
throw new GoodDataHttpStatusException(e.getStatusCode(), e.getStatusText());
}
}
// Always use response handler: never return or expect CloseableHttpResponse anymore!
// --- END PATCH ---

if (tt != null) {
request.addHeader(TT_HEADER, tt);
}

ClassicHttpResponse resp = this.httpClient.execute(
target,
request,
context,
response -> response // just return the response
response -> response
);
return handleResponse(target, request, resp, context);

if (resp.getCode() == HttpStatus.SC_UNAUTHORIZED) {
// 👇 Proper handling of InterruptedException
try {
return handleResponse(target, request, resp, context);
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // Preserve interrupt status
throw new IOException("Interrupted while handling authentication challenge", e);
}
}

return resp;

} finally {
lock.unlock();
}
}




public ClassicHttpResponse execute(HttpHost target, ClassicHttpRequest request) throws IOException {
return httpClient.execute(target, request, (HttpContext) null, response -> response);
// return httpClient.execute(target, request, (HttpContext) null, response -> response);
return execute(target, request, null);
}


Expand Down
Loading