-
Notifications
You must be signed in to change notification settings - Fork 30
Jwttoken add in header #82
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
Conversation
WalkthroughA new static method was added to Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant HttpUtils
participant RestTemplateUtil
participant CookieUtil
participant UserAgentContext
Caller->>HttpUtils: Call HTTP method (get/post/put/uploadFile)
HttpUtils->>RestTemplateUtil: getJwttokenFromHeaders(headers)
RestTemplateUtil->>CookieUtil: getJwtTokenFromCookie(request)
RestTemplateUtil->>UserAgentContext: getUserAgent()
RestTemplateUtil-->>HttpUtils: Headers enriched with JWT token and user agent
HttpUtils->>Caller: Proceed with HTTP request using updated headers
Possibly related PRs
Suggested reviewers
Poem
π Recent review detailsConfiguration used: CodeRabbit UI π Files selected for processing (2)
π§ Files skipped from review as they are similar to previous changes (2)
β¨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. πͺ§ TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
π Outside diff range comments (4)
src/main/java/com/iemr/admin/utils/http/HttpUtils.java (4)
111-119
:β οΈ Potential issueSame thread safety issue as in GET method.
The shared
headers
instance variable creates thread safety problems.Apply the same fix as suggested for the GET method - use local headers instead of the shared instance variable.
143-151
:β οΈ Potential issueSame thread safety issue applies here.
The PUT method also uses the shared headers instance variable, creating the same thread safety concern.
Apply the same local headers fix as suggested for other methods.
176-215
:β οΈ Potential issuePotential issue with uploadFile method integration.
The JWT token extraction is called inside the multipart conditional block (line 198), but not in the else block (lines 207-211). This means file uploads using non-multipart content types won't include JWT tokens.
Move the JWT token extraction outside the conditional or add it to both branches:
} ResponseEntity<String> responseEntity = new ResponseEntity(HttpStatus.BAD_REQUEST); + RestTemplateUtil.getJwttokenFromHeaders(headers); if (headers.getContentType().toString().equals(MediaType.MULTIPART_FORM_DATA_TYPE.toString())) { HttpEntity<FormDataMultiPart> requestEntity; try { FormDataMultiPart multiPart = new FormDataMultiPart(); FileInputStream is = new FileInputStream(data); FormDataBodyPart filePart = new FormDataBodyPart("content", is, MediaType.APPLICATION_OCTET_STREAM_TYPE); multiPart.bodyPart(filePart); multiPart.field("docPath", data); headers.add("Content-Type", MediaType.APPLICATION_JSON); - RestTemplateUtil.getJwttokenFromHeaders(headers); requestEntity = new HttpEntity<FormDataMultiPart>(multiPart, headers);
80-198
: π οΈ Refactor suggestionConsider error handling for RequestContext unavailability.
All method calls to
RestTemplateUtil.getJwttokenFromHeaders()
could potentially fail if called outside a web request context (e.g., in background threads). Consider adding error handling or documentation about this limitation.Add error handling around the JWT token extraction calls:
try { RestTemplateUtil.getJwttokenFromHeaders(headers); } catch (Exception e) { logger.warn("Unable to extract JWT token from request context: " + e.getMessage()); // Continue with request without JWT token }Would you like me to generate a comprehensive refactoring that addresses the thread safety issues and adds proper error handling across all methods?
π§Ή Nitpick comments (1)
src/main/java/com/iemr/admin/utils/RestTemplateUtil.java (1)
52-52
: Fix method naming convention.The method name should follow Java naming conventions. "JWT" should be capitalized as "Jwt" rather than "Jwttoken".
-public static void getJwttokenFromHeaders(HttpHeaders headers) { +public static void getJwtTokenFromHeaders(HttpHeaders headers) {
π Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
π Files selected for processing (2)
src/main/java/com/iemr/admin/utils/RestTemplateUtil.java
(1 hunks)src/main/java/com/iemr/admin/utils/http/HttpUtils.java
(8 hunks)
π§° Additional context used
𧬠Code Graph Analysis (2)
src/main/java/com/iemr/admin/utils/RestTemplateUtil.java (2)
src/main/java/com/iemr/admin/utils/UserAgentContext.java (1)
UserAgentContext
(3-18)src/main/java/com/iemr/admin/utils/Constants.java (1)
Constants
(3-9)
src/main/java/com/iemr/admin/utils/http/HttpUtils.java (1)
src/main/java/com/iemr/admin/utils/RestTemplateUtil.java (1)
RestTemplateUtil
(15-75)
β° Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: Analyze (java)
π Additional comments (4)
src/main/java/com/iemr/admin/utils/http/HttpUtils.java (4)
40-40
: LGTM! Import statement added correctly.The import for
RestTemplateUtil
is properly added and necessary for the new functionality.
92-109
: LGTM! Consistent integration pattern.The JWT token extraction is properly integrated into the GET method with custom headers. The placement after header setup but before request execution is correct.
121-142
: LGTM! Proper integration with custom headers.The JWT token extraction is correctly integrated after custom headers are set up but before the request is executed.
153-174
: LGTM! Consistent with other parameterized methods.The integration follows the same correct pattern as other methods that accept custom headers.
String jwtTokenFromCookie = null; | ||
try { | ||
jwtTokenFromCookie = CookieUtil.getJwtTokenFromCookie(requestHeader); | ||
|
||
} catch (Exception e) { | ||
logger.error("Error while getting jwtToken from Cookie" + e.getMessage()); | ||
} | ||
if (null != UserAgentContext.getUserAgent()) { | ||
headers.add(HttpHeaders.USER_AGENT, UserAgentContext.getUserAgent()); | ||
} | ||
if (null != jwtTokenFromCookie) { | ||
headers.add(Constants.JWT_TOKEN, jwtTokenFromCookie); | ||
} else if (null != requestHeader.getHeader(Constants.JWT_TOKEN)) { | ||
headers.add(Constants.JWT_TOKEN, requestHeader.getHeader(Constants.JWT_TOKEN)); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π οΈ Refactor suggestion
Consider refactoring to eliminate code duplication.
This method contains nearly identical logic to the existing createRequestEntity
method (lines 28-47). The JWT token extraction, User-Agent handling, and header manipulation logic is duplicated.
Consider extracting the common JWT token logic into a separate private method:
+private static String extractJwtToken(HttpServletRequest request) {
+ try {
+ return CookieUtil.getJwtTokenFromCookie(request);
+ } catch (Exception e) {
+ logger.error("Error while getting jwtToken from Cookie" + e.getMessage());
+ return null;
+ }
+}
+
+private static void enrichHeadersWithTokenAndUserAgent(HttpHeaders headers, HttpServletRequest request) {
+ String jwtTokenFromCookie = extractJwtToken(request);
+
+ if (null != UserAgentContext.getUserAgent()) {
+ headers.add(HttpHeaders.USER_AGENT, UserAgentContext.getUserAgent());
+ }
+ if (null != jwtTokenFromCookie) {
+ headers.add(Constants.JWT_TOKEN, jwtTokenFromCookie);
+ } else if (null != request.getHeader(Constants.JWT_TOKEN)) {
+ headers.add(Constants.JWT_TOKEN, request.getHeader(Constants.JWT_TOKEN));
+ }
+}
Then update both methods to use this shared logic.
Committable suggestion skipped: line range outside the PR's diff.
π€ Prompt for AI Agents
In src/main/java/com/iemr/admin/utils/RestTemplateUtil.java around lines 28 to
73, the JWT token extraction, User-Agent handling, and header manipulation logic
is duplicated between the current method and createRequestEntity. Refactor by
extracting the common JWT token retrieval and header setup logic into a private
helper method. Then update both methods to call this helper to avoid code
duplication and improve maintainability.
|
π Description
JIRA ID: AMM-1561
State district not patch because of jwttoken.
β Type of Change
βΉοΈ Additional Information
Please describe how the changes were tested, and include any relevant screenshots, logs, or other information that provides additional context.
Summary by CodeRabbit