Unverified Commit 3cd571b3 authored by Ankit Verma's avatar Ankit Verma Committed by GitHub
Browse files

Merge pull request #190 from Radheshhathwar/development

Made changes in RedisUtil for MismatchedInputException
Showing with 67 additions and 21 deletions
+67 -21
package com.tarento.upsmf.examsAndAdmissions.exceptions;
public class UserInfoFetchException extends RuntimeException {
public UserInfoFetchException(String message) {
super(message);
}
public UserInfoFetchException(String message, Throwable cause) {
super(message, cause);
}
}
package com.tarento.upsmf.examsAndAdmissions.util; package com.tarento.upsmf.examsAndAdmissions.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.tarento.upsmf.examsAndAdmissions.exception.InvalidRequestException;
import com.tarento.upsmf.examsAndAdmissions.exceptions.UserInfoFetchException;
import com.tarento.upsmf.examsAndAdmissions.model.User; import com.tarento.upsmf.examsAndAdmissions.model.User;
import org.apache.kafka.common.requests.DeleteAclsResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.HashOperations;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.apache.kafka.common.requests.DeleteAclsResponse.log;
@Component @Component
public class RedisUtil { public class RedisUtil {
...@@ -42,40 +48,68 @@ public class RedisUtil { ...@@ -42,40 +48,68 @@ public class RedisUtil {
* @return * @return
*/ */
public User getUserById(String id) { public User getUserById(String id) {
if(id == null || id.isBlank()) { try {
throw new RuntimeException("Invalid Request"); validateId(id);
// Check in Redis
if (Boolean.TRUE.equals(hashOperations.getOperations().hasKey(id))) {
return hashOperations.get(userRedisHashKey, id);
}
// Fetch user information from the external service
ResponseEntity<String> response = fetchUserInfoFromExternalService(id);
return handleUserInfoResponse(response, id);
} catch (Exception e) {
// Log the exception and rethrow a custom exception
log.error("Error in getting user info for ID: {}", id, e);
throw new UserInfoFetchException("Error in getting user info.", e);
} }
// check in redis }
boolean keyExists = Boolean.TRUE.equals(hashOperations.getOperations().hasKey(id));
if(keyExists) { private void validateId(String id) {
return hashOperations.get(userRedisHashKey, id); if (id == null || id.isBlank()) {
throw new InvalidRequestException("Invalid user ID");
} }
ObjectNode request = mapper.createObjectNode(); }
private ResponseEntity<String> fetchUserInfoFromExternalService(String id) {
ObjectNode request = createUserInfoRequest(id);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return restTemplate.exchange(userInfoUrl, HttpMethod.POST, new HttpEntity<>(request, headers), String.class);
}
private ObjectNode createUserInfoRequest(String id) {
ObjectNode root = mapper.createObjectNode(); ObjectNode root = mapper.createObjectNode();
root.put("userName", id); root.put("userName", id);
ObjectNode request = mapper.createObjectNode();
request.put("request", root); request.put("request", root);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON); return request;
try { }
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<String> response = restTemplate.exchange( private User handleUserInfoResponse(ResponseEntity<String> response, String id) {
userInfoUrl, HttpMethod.POST, if (response.getStatusCode() == HttpStatus.OK) {
new HttpEntity<>(request, headers), String.class); try {
if (response.getStatusCode() == HttpStatus.OK) {
JsonNode responseBody = mapper.readTree(response.getBody()); JsonNode responseBody = mapper.readTree(response.getBody());
User user = mapper.treeToValue(responseBody, User.class); User user = mapper.treeToValue(responseBody, User.class);
if(user != null) {
if (user != null) {
hashOperations.put(userRedisHashKey, user.getId(), user); hashOperations.put(userRedisHashKey, user.getId(), user);
return user; return user;
} }
} catch (IOException e) {
log.error("Error parsing user info response for ID: {}", id, e);
} }
throw new RuntimeException("Error in getting user info.");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Error in getting user info.");
} }
throw new UserInfoFetchException("Error in getting user info. Status: " + response.getStatusCode());
} }
/** /**
* Method to roles from user * Method to roles from user
* @param id * @param id
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment