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 4 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.

@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.

@ritikk112
Copy link
Author

HI @MohamedSabthar made the appropriate changes, can you do a quick review and let me know if any other changes are to made.

@@ -71,7 +71,7 @@ public static Object doHttpRequest(BString url, BMap<BString, Object> clientConf
}

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

Choose a reason for hiding this comment

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

Why are these spaces removed?

@@ -104,20 +103,18 @@ public static Object doHttpRequest(BString url, BMap<BString, Object> clientConf
} catch (Exception e) {
return createError("Failed to init SSL context. " + e.getMessage());
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Remove additional spaces. You can configure your IDE to remove the trailing spaces when saving a file.

Suggested change
}
}

}
if (cert instanceof BMap) {
BMap<BString, BString> trustStore = (BMap<BString, BString>) cert;
if (key != null) {
if(key != null){
Copy link
Member

Choose a reason for hiding this comment

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

Keep these spaces.

I guess if you ran a build these should be identified by the checkstyle plugin. If it doesn't we might have to check what happened there @MohamedSabthar.

Use this styling guide for your IDE.

Suggested change
if(key != null){
if (key != null) {

Copy link
Member

Choose a reason for hiding this comment

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

The build is failing. @ritikk112 Let's fix it.

Copy link
Author

Choose a reason for hiding this comment

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

Where can I find the report for the failed build? I want to pinpoint the error but can't find the report, do I have to build and run on my local machine for it?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, for a detailed report on Checkstyle errors and SpotBugs reports, you should run them locally. Anyway, you should also build and test the project locally before raising the PR. 🙂

Copy link

sonarcloud bot commented Oct 15, 2024

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
4 participants