Skip to content
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

Refacftored OAuth2 #1324

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

ritikk112
Copy link

@ritikk112 ritikk112 commented Oct 13, 2024

Purpose

Fixes ballerina-platform/ballerina-library#7237

Refactored SSL context initialization: Improved logic for handling optional SSL keys and certificates.
Enhanced exception handling: Streamlined error catching for IOException and InterruptedException.
Optimized code structure: Removed redundant checks and simplified nested if-else statements.
Improved readability: Cleaned up and made the code more readable across methods.

Examples

Checklist

  • Linked to an issue
  • Updated the changelog
  • Added tests
  • Updated the spec
  • Checked native-image compatibility

@CLAassistant
Copy link

CLAassistant commented Oct 13, 2024

CLA assistant check
All committers have signed the CLA.

Copy link

sonarcloud bot commented Oct 13, 2024

@MohamedSabthar
Copy link
Member

Hi @ritikk112,

Are you working on this issue: ballerina-platform/ballerina-library#7237? If so, could you refactor the Ballerina codebase? The current Java code appears to follow best practices, so changes to it are not necessary.

@@ -70,16 +71,16 @@ public static Object doHttpRequest(BString url, BMap<BString, Object> clientConf
headersList.add(entry.getValue().getValue());
}

BMap<BString, ?> customHeaders = getBMapValueIfPresent(clientConfig, OAuth2Constants.CUSTOM_HEADERS);
if (customHeaders != null) {
Optional<BMap<BString, ?>> customHeaders = getBMapValueIfPresent(clientConfig, OAuth2Constants.CUSTOM_HEADERS);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing null checks with Optional types doesn't add any value here, as we are following an imperative style of coding. The previous code block looks fine, so let's keep the original code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I introduced Optional checks to improve code readability as there were a lot of if else statements prior to changes and made the code less streamlined, should I revert back to if checks for null values?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove.


String httpVersion = getBStringValueIfPresent(clientConfig, OAuth2Constants.HTTP_VERSION).getValue();
BMap<BString, ?> secureSocket = getBMapValueIfPresent(clientConfig, OAuth2Constants.SECURE_SOCKET);
Optional<BMap<BString, ?>> secureSocket = getBMapValueIfPresent(clientConfig, OAuth2Constants.SECURE_SOCKET);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check other places.

Comment on lines +302 to +304
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt(); // Restore interrupted status
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we have already handled the InterruptedException by throwing an error to the Ballerina side via the createError method. It's not necessary to restore the status here. Is there a any particular reason to introduce this change ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this just for additional security measures and to handle errors more gracefully

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this as there's no need to propagate this interruption.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor the OAuth2 module codebase
3 participants