All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.infusers.core.user.RemoteUserService Maven / Gradle / Ivy

package com.infusers.core.user;

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.audit.AuditService;
import com.infusers.core.user.dto.UserDetailsDto;
import com.infusers.core.util.InfusersUtility;

@Service
public class RemoteUserService {
	final Logger log = LogManager.getLogger(RemoteUserService.class);
	
	@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(required = true)
	//private AuditService auditService;	
		
	public UserDetailsDto getUser(String userName) {
		return getUserRemote(userName);
	}
	
	private UserDetailsDto getUserRemote(String userName) {
		log.debug("RemoteUserService.getUserRemote() :: user name : " + userName);

		if(userName==null || userName.trim().length()<1) {
			log.error("RemoteUserService.getUserRemote() :: User Name is empty/invalid. user name : " + userName);
			return null;
		}
		String urlString = infusersAuthUrl + requestMapping + userEndpoint + userName;		
		//auditService.logAudit(userName, "RemoteUserService", "", "getUserRemote", "urlString = "+urlString);							
		
		String endpoint = "";
		try {
			URL url = new URL(urlString);
			endpoint = url.toString();
	        String bearerToken = InfusersUtility.getInstance().getToken(userName);
	        
	        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("RemoteUserService.getUserRemote()" + e.getMessage()+" urlString = "+urlString+" :: endpoint = "+endpoint);
			//auditService.logAudit(userName, "RemoteUserService", "", "getUserRemote", e.getMessage()+" urlString = "+urlString+" :: endpoint = "+endpoint);		
			//e.printStackTrace();
		}
		return null;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy