io.mosip.admin.packetstatusupdater.service.impl.AuditManagerProxyServiceImpl Maven / Gradle / Ivy
package io.mosip.admin.packetstatusupdater.service.impl;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import io.mosip.admin.packetstatusupdater.constant.AdminManagerProxyErrorCode;
import io.mosip.admin.packetstatusupdater.dto.AuditManagerRequestDto;
import io.mosip.admin.packetstatusupdater.dto.AuditManagerResponseDto;
import io.mosip.admin.packetstatusupdater.exception.MasterDataServiceException;
import io.mosip.admin.packetstatusupdater.service.AuditManagerProxyService;
import io.mosip.kernel.core.http.RequestWrapper;
/**
*
* @author Megha Tanga
*
*/
@Service
public class AuditManagerProxyServiceImpl implements AuditManagerProxyService {
@Value("${mosip.kernel.audit.manager.api}")
String auditmanagerapi;
@Autowired
RestTemplate restTemplate;
@Override
public AuditManagerResponseDto logAdminAudit(AuditManagerRequestDto auditManagerRequestDto) {
AuditManagerResponseDto auditManagerResponseDto = new AuditManagerResponseDto();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
RequestWrapper request = new RequestWrapper<>();
try {
auditManagerRequestDto.setHostIp(InetAddress.getLocalHost().getHostAddress());
auditManagerRequestDto.setHostName(InetAddress.getLocalHost().getHostName());
} catch (UnknownHostException ex) {
throw new MasterDataServiceException(AdminManagerProxyErrorCode.ADMIN_UNKNOWNHOST_EXCEPTION.getErrorCode(),
AdminManagerProxyErrorCode.ADMIN_UNKNOWNHOST_EXCEPTION.getErrorMessage(), ex);
}
request.setId("mosip.admin.audit");
request.setVersion("0.1");
request.setRequesttime(LocalDateTime.now());
request.setRequest(auditManagerRequestDto);
HttpEntity> entity = new HttpEntity<>(request, headers);
try {
Object returnEntityt = restTemplate.postForEntity(auditmanagerapi, entity, Object.class).getBody();
} catch (HttpClientErrorException | HttpServerErrorException ex) {
throw new MasterDataServiceException(AdminManagerProxyErrorCode.ADMIN_FETCH_EXCEPTION.getErrorCode(),
AdminManagerProxyErrorCode.ADMIN_FETCH_EXCEPTION.getErrorMessage(), ex);
}
auditManagerResponseDto.setStatus("Success");
auditManagerResponseDto.setMessage("Audit logged successfuly");
return auditManagerResponseDto;
}
}