Skip to content

Conversation

@wendigo
Copy link
Contributor

@wendigo wendigo commented Oct 30, 2025

This is a refactor that will allow to validate extra header in #15826

Description

Additional context and related issues

Release notes

(x) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
( ) Release notes are required, with the following suggested text:

## Section
* Fix some things. ({issue}`issuenumber`)

Summary by Sourcery

Refactor ProtocolHeaders by consolidating header definitions into a Headers enum and simplifying header name construction

Enhancements:

  • Extract protocol header names into a Headers enum with a withProtocolName method
  • Replace manual string concatenation in ProtocolHeaders constructor with enum-based header generation

@cla-bot cla-bot bot added the cla-signed label Oct 30, 2025
@sourcery-ai
Copy link

sourcery-ai bot commented Oct 30, 2025

Reviewer's Guide

This PR refactors ProtocolHeaders by introducing a new Headers enum to encapsulate all protocol header names and updating the ProtocolHeaders constructor to generate header strings via the enum’s withProtocolName method, replacing manual concatenation.

Class diagram for refactored ProtocolHeaders and Headers enum

classDiagram
    class ProtocolHeaders {
        - String name
        - String requestUser
        - String requestOriginalUser
        - String requestOriginalRole
        - String requestSource
        - String requestCatalog
        - String requestSchema
        - String requestPath
        - String requestTimeZone
        - String requestLanguage
        - String requestTraceToken
        - String requestSession
        - String requestRole
        - String requestPreparedStatement
        - String requestTransactionId
        - String requestClientInfo
        - String requestClientTags
        - String requestClientCapabilities
        - String requestResourceEstimate
        - String requestExtraCredential
        - String requestQueryDataEncoding
        - String responseSetCatalog
        - String responseSetSchema
        - String responseSetPath
        - String responseSetSession
        - String responseClearSession
        - String responseSetRole
        - String responseQueryDataEncoding
        - String responseAddedPrepare
        - String responseDeallocatedPrepare
        - String responseStartedTransactionId
        - String responseClearTransactionId
        - String responseSetAuthorizationUser
        - String responseResetAuthorizationUser
        - String responseOriginalRole
        + ProtocolHeaders(String name)
        + String getProtocolName()
    }
    class Headers {
        - String headerName
        + Headers(String headerName)
        + String withProtocolName(String protocolName)
        <<enum>>
        REQUEST_USER
        REQUEST_ORIGINAL_USER
        REQUEST_ORIGINAL_ROLES
        REQUEST_SOURCE
        REQUEST_CATALOG
        REQUEST_SCHEMA
        REQUEST_PATH
        REQUEST_TIME_ZONE
        REQUEST_LANGUAGE
        REQUEST_TRACE_TOKEN
        REQUEST_SESSION
        REQUEST_ROLE
        REQUEST_PREPARED_STATEMENT
        REQUEST_TRANSACTION_ID
        REQUEST_CLIENT_INFO
        REQUEST_CLIENT_TAGS
        REQUEST_CLIENT_CAPABILITIES
        REQUEST_RESOURCE_ESTIMATE
        REQUEST_EXTRA_CREDENTIAL
        REQUEST_QUERY_DATA_ENCODING
        RESPONSE_SET_CATALOG
        RESPONSE_SET_SCHEMA
        RESPONSE_SET_PATH
        RESPONSE_SET_SESSION
        RESPONSE_CLEAR_SESSION
        RESPONSE_SET_ROLE
        RESPONSE_SET_ORIGINAL_ROLES
        RESPONSE_QUERY_DATA_ENCODING
        RESPONSE_ADDED_PREPARE
        RESPONSE_DEALLOCATED_PREPARE
        RESPONSE_STARTED_TRANSACTION_ID
        RESPONSE_CLEAR_TRANSACTION_ID
        RESPONSE_SET_AUTHORIZATION_USER
        RESPONSE_RESET_AUTHORIZATION_USER
    }
    ProtocolHeaders o-- Headers : uses
Loading

File-Level Changes

Change Details Files
Introduce Headers enum to encapsulate header definitions
  • Define Headers enum constants with display names
  • Add headerName field and constructor for null-check
  • Implement withProtocolName(protocolName) for header generation
  • Add static PREFIX constant and import enum values
client/trino-client/src/main/java/io/trino/client/ProtocolHeaders.java
Refactor ProtocolHeaders constructor to use Headers enum for header string generation
  • Remove manual prefix variable and string concatenations
  • Replace each request/response header assignment with Headers.XYZ.withProtocolName(name)
  • Simplify responseOriginalRole to use the enum constant
client/trino-client/src/main/java/io/trino/client/ProtocolHeaders.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `client/trino-client/src/main/java/io/trino/client/ProtocolHeaders.java:106-109` </location>
<code_context>
+            this.headerName = requireNonNull(headerName, "headerName is null");
+        }
+
+        public String withProtocolName(String protocolName)
+        {
+            return PREFIX + protocolName + "-" + headerName;
+        }
+    }
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Validate protocolName in withProtocolName to avoid malformed headers.

Add a null or empty check for protocolName to prevent malformed headers.

```suggestion
        public String withProtocolName(String protocolName)
        {
            if (protocolName == null || protocolName.isEmpty()) {
                throw new IllegalArgumentException("protocolName is null or empty");
            }
            return PREFIX + protocolName + "-" + headerName;
        }
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@wendigo wendigo force-pushed the serafin/client-headers branch from 4a37cb2 to 441310e Compare October 30, 2025 20:45
@wendigo wendigo requested review from chenjian2664 and ebyhr October 30, 2025 23:28
@wendigo wendigo merged commit baf8f0a into trinodb:master Oct 31, 2025
194 of 197 checks passed
@wendigo wendigo deleted the serafin/client-headers branch October 31, 2025 06:15
@github-actions github-actions bot added this to the 479 milestone Oct 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants