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

Added feature to add custom headers to Service #176

Merged
merged 2 commits into from
Nov 18, 2021

Conversation

BalasubramanyamEvani
Copy link
Contributor

References to other Issues or PRs

Ref #169

Brief description of what is fixed or changed

  1. Added support to add custom headers in Service
  2. Implemented method setHttpHeaders which accepts a headers map
  3. Created method setCustomHeaders in HttpService.java to filter and store the map key-values
  4. Finally in send method of HttpService.java the customHeaders are added

Other comments

Rather than merging the custom map with the default one, I created a new protected variable to store the custom map
values. Please let me know if this is correct or not.

Copy link
Contributor

@fantavlik fantavlik left a comment

Choose a reason for hiding this comment

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

Thanks so much for the contribution @BalasubramanyamEvani - I just had one request to simplify the logic since I believe we're already accounting for the required headers not being overwritten -- I appreciate you going out of the way to prevent these from colliding though,

*/
public void setCustomHeaders(Map<String, String> headers) {
if (Objects.nonNull(headers) && !headers.isEmpty()) {
Map<String, String> fitleredCustomHeaders = headers.entrySet()
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can remove this logic and the filtertHttpHeaderKeys above since the custom header values are only added if the header does not already exist down on line 486:

if (!header.containsKey(key)) {
    cn.setRequestProperty(key, entry.getValue());
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, sure that makes sense. Also, I was thinking about whether the customHeaders map needs to be cleared out before populating it if it's not empty?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh good call that makes, although if we're not filtering I think a simple reassignment would suffice? e.g.

if (Objects.nonNull(headers)) {
    customHeaders = headers;
}

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

That would cover a case where someone needed to clear the customHeaders that were already set (I can't think of a use case for that, but potentially useful)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I think a reassignment should suffice. I have made the necessary changes. Please check.

@fantavlik fantavlik merged commit c6f3008 into splunk:master Nov 18, 2021
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.

2 participants