com.infusers.core.user.remote.RemoteUserService Maven / Gradle / Ivy
package com.infusers.core.user.remote;
import java.net.URL;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.infusers.core.user.dto.UserDetailsDto;
import com.infusers.core.user.util.UserUtility;
@Service
public class RemoteUserService {
private final Logger log = LogManager.getLogger(RemoteUserService.class);
private static final String CLASS_NAME = "RemoteUserService";
@Value("${infusers.auth.getuser.endpoint}")
private String userEndpoint;
@Value("${infusers.auth.url}")
private String infusersAuthUrl;
@Value("${myapp.rom.request.mapping}")
private String requestMapping;
@Value("${infusers.token.inter.api.communication}")
private String infuserTokenInterApiCommunication;
@Autowired
private UserUtility userUtility;
public UserDetailsDto getUser(String userName) {
log.debug(CLASS_NAME+".getUser() :: user name : " + userName);
if(userName==null || userName.trim().length()<1) {
log.error(CLASS_NAME+".getUser() :: User Name is empty/invalid. user name : " + userName);
return null;
}
String urlString = infusersAuthUrl + requestMapping + userEndpoint + userName;
String endpoint = "";
try {
URL url = new URL(urlString);
endpoint = url.toString();
String bearerToken = userUtility.getToken();
RestTemplate restTemplate = new RestTemplate();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setBearerAuth(bearerToken);
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity requestEntity = new HttpEntity<>(null,
requestHeaders);
ResponseEntity userDto = restTemplate.exchange(endpoint, HttpMethod.GET, requestEntity, UserDetailsDto.class);
return userDto.getBody();
}
catch (Exception e) {
log.error(CLASS_NAME+".getUser()" + e.getMessage()+" urlString = "+urlString+" :: endpoint = "+endpoint);
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy