Skip to content

Commit

Permalink
Merge pull request #1326 from warunalakshitha/java21
Browse files Browse the repository at this point in the history
Fix call endpoint hang for non isolated strands
  • Loading branch information
warunalakshitha authored Oct 29, 2024
2 parents 0e0cfcc + cecac7a commit d7ba9e5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ group=io.ballerina.stdlib
version=2.12.1-SNAPSHOT
puppycrawlCheckstyleVersion=10.12.0
ballerinaGradlePluginVersion=2.0.1
ballerinaLangVersion=2201.10.0-20241007-143200-6b69ca80
ballerinaLangVersion=2201.10.0-20241025-103700-5c9e6a27
githubJohnrengelmanShadowVersion=8.1.1
underCouchDownloadVersion=5.4.0
researchgateReleaseVersion=2.8.0
Expand Down
31 changes: 17 additions & 14 deletions native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package io.ballerina.stdlib.oauth2;

import io.ballerina.runtime.api.Environment;
import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.values.BError;
Expand Down Expand Up @@ -56,8 +57,8 @@ public class OAuth2Client {

private OAuth2Client() {}

public static Object doHttpRequest(BString url, BMap<BString, Object> clientConfig, BMap<BString, BString> headers,
BString payload) {
public static Object doHttpRequest(Environment env, BString url, BMap<BString, Object> clientConfig,
BMap<BString, BString> headers, BString payload) {
BString customPayload = getBStringValueIfPresent(clientConfig, OAuth2Constants.CUSTOM_PAYLOAD);
String textPayload = payload.getValue();
if (customPayload != null) {
Expand Down Expand Up @@ -100,13 +101,13 @@ public static Object doHttpRequest(BString url, BMap<BString, Object> clientConf
try {
SSLContext sslContext = getSslContext(secureSocket);
HttpClient client = buildHttpClient(httpVersion, sslContext);
return callEndpoint(client, request);
return callEndpoint(env, client, request);
} catch (Exception e) {
return createError("Failed to init SSL context. " + e.getMessage());
}
}
HttpClient client = buildHttpClient(httpVersion);
return callEndpoint(client, request);
return callEndpoint(env, client, request);
}

private static URI buildUri(String url, BMap<BString, ?> secureSocket) throws IllegalArgumentException {
Expand Down Expand Up @@ -300,17 +301,19 @@ private static HttpRequest buildHttpRequest(URI uri, String[] headers, String pa
.build();
}

private static Object callEndpoint(HttpClient client, HttpRequest request) {
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() >= 200 && response.statusCode() < 300) {
return StringUtils.fromString(response.body());
private static Object callEndpoint(Environment env, HttpClient client, HttpRequest request) {
return env.yieldAndRun(() -> {
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() >= 200 && response.statusCode() < 300) {
return StringUtils.fromString(response.body());
}
return createError("Failed to get a success response from the endpoint. Response code: '" +
response.statusCode() + "'. Response body: '" + response.body() + "'");
} catch (IOException | InterruptedException e) {
return createError("Failed to send the request to the endpoint. " + e.getMessage());
}
return createError("Failed to get a success response from the endpoint. Response code: '" +
response.statusCode() + "'. Response body: '" + response.body() + "'");
} catch (IOException | InterruptedException e) {
return createError("Failed to send the request to the endpoint. " + e.getMessage());
}
});
}

private static BMap<BString, ?> getBMapValueIfPresent(BMap<BString, ?> config, BString key) {
Expand Down

0 comments on commit d7ba9e5

Please sign in to comment.