Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/main/java/com/iemr/admin/utils/RestTemplateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,27 @@ public static HttpEntity<Object> createRequestEntity(Object body, String authori
return new HttpEntity<>(body, headers);
}

public static void getJwttokenFromHeaders(HttpHeaders headers) {
ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes());

HttpServletRequest requestHeader = servletRequestAttributes.getRequest();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
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));
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

}
Comment on lines +57 to +75
Copy link
Copy Markdown

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.


}
8 changes: 8 additions & 0 deletions src/main/java/com/iemr/admin/utils/http/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.iemr.admin.utils.RestTemplateUtil;
import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataMultiPart;

Expand Down Expand Up @@ -76,6 +77,7 @@ public HttpUtils() {

public String get(String uri) {
String body;
RestTemplateUtil.getJwttokenFromHeaders(headers);
HttpEntity<String> requestEntity = new HttpEntity<String>("", headers);
ResponseEntity<String> responseEntity = rest.exchange(uri, HttpMethod.GET, requestEntity, String.class);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
setStatus((HttpStatus) responseEntity.getStatusCode());
Expand All @@ -98,6 +100,7 @@ public String get(String uri, HashMap<String, Object> header) {
} else {
headers.add("Content-Type", MediaType.APPLICATION_JSON);
}
RestTemplateUtil.getJwttokenFromHeaders(headers);
HttpEntity<String> requestEntity = new HttpEntity<String>("", headers);
ResponseEntity<String> responseEntity = rest.exchange(uri, HttpMethod.GET, requestEntity, String.class);
setStatus((HttpStatus) responseEntity.getStatusCode());
Expand All @@ -107,6 +110,7 @@ public String get(String uri, HashMap<String, Object> header) {

public String post(String uri, String json) {
String body;
RestTemplateUtil.getJwttokenFromHeaders(headers);
HttpEntity<String> requestEntity = new HttpEntity<String>(json, headers);
ResponseEntity<String> responseEntity = rest.exchange(uri, HttpMethod.POST, requestEntity, String.class);
setStatus((HttpStatus) responseEntity.getStatusCode());
Expand All @@ -126,6 +130,7 @@ public String post(String uri, String data, HashMap<String, Object> header) {
if (header.containsKey("X-APIkey-Header")) {
headers.add("X-APIkey-Header", header.get("X-APIkey-Header").toString());
}
RestTemplateUtil.getJwttokenFromHeaders(headers);
// headers.add("Content-Type", MediaType.APPLICATION_JSON);
ResponseEntity<String> responseEntity = new ResponseEntity(HttpStatus.BAD_REQUEST);
HttpEntity<String> requestEntity;
Expand All @@ -137,6 +142,7 @@ public String post(String uri, String data, HashMap<String, Object> header) {
}
public String put(String uri, String json) {
String body;
RestTemplateUtil.getJwttokenFromHeaders(headers);
HttpEntity<String> requestEntity = new HttpEntity<String>(json, headers);
ResponseEntity<String> responseEntity = rest.exchange(uri, HttpMethod.PUT, requestEntity, String.class);
setStatus((HttpStatus) responseEntity.getStatusCode());
Expand All @@ -159,6 +165,7 @@ public String put(String uri, String data, HashMap<String, Object> header) {
// headers.add("Content-Type", MediaType.APPLICATION_JSON);
ResponseEntity<String> responseEntity = new ResponseEntity(HttpStatus.BAD_REQUEST);
HttpEntity<String> requestEntity;
RestTemplateUtil.getJwttokenFromHeaders(headers);
requestEntity = new HttpEntity<String>(data, headers);
responseEntity = rest.exchange(uri, HttpMethod.PUT, requestEntity, String.class);
setStatus((HttpStatus) responseEntity.getStatusCode());
Expand Down Expand Up @@ -188,6 +195,7 @@ public String uploadFile(String uri, String data, HashMap<String, Object> header
multiPart.bodyPart(filePart);
multiPart.field("docPath", data);
headers.add("Content-Type", MediaType.APPLICATION_JSON);
RestTemplateUtil.getJwttokenFromHeaders(headers);
requestEntity = new HttpEntity<FormDataMultiPart>(multiPart, headers);// new
// HttpEntity<String>(multiPart,
// headers);
Expand Down
Loading