io.convergence_platform.services.controllers.BaseController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of service-lib Show documentation
Show all versions of service-lib Show documentation
Holds the common functionality needed by all Convergence Platform-based services written in Java.
The newest version!
package io.convergence_platform.services.controllers;
import io.convergence_platform.common.controllers.IApiControllerMethod;
import io.convergence_platform.common.controllers.IApiControllerMethodReturningList;
import io.convergence_platform.common.dto.FailureInfoDTO;
import io.convergence_platform.common.exceptions.ManagedApiException;
import io.convergence_platform.common.responses.ApiResponse;
import io.convergence_platform.common.responses.Errors;
import io.convergence_platform.common.responses.IApiResponseBody;
import io.convergence_platform.common.responses.ListApiResponse;
import io.convergence_platform.services.observability.ILogSerializer;
import io.convergence_platform.services.observability.RequestLog;
import io.convergence_platform.services.security.ServiceAuthenticationToken;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.List;
import java.util.UUID;
public abstract class BaseController {
@Value("${application.debug}")
private boolean isDebug;
@Autowired
private ILogSerializer logSerializer;
protected ApiResponse createInternalErrorResponse(HttpServletRequest request, RequestLog requestLog, String code, Exception ex) {
String message = ex.getMessage();
if (ex instanceof ManagedApiException) {
FailureInfoDTO failure = new FailureInfoDTO();
failure.setCode(((ManagedApiException) ex).getCode());
failure.setHttpStatusCode(((ManagedApiException) ex).getHttpStatusCode());
failure.setMessage(message);
failure.setRequestId(UUID.fromString(requestLog.getRawRequestID()));
failure.setParentRequestId(requestLog.parentRequestIdentifier == null ? null : UUID.fromString(requestLog.parentRequestIdentifier));
return ApiResponse.from(failure);
} else if (isDebug) {
// TODO: Store information on the exception in DB
ex.printStackTrace();
message = "An unexpected error happened during API execution";
} else {
ex.printStackTrace();
}
FailureInfoDTO failure = new FailureInfoDTO();
failure.setCode(code);
failure.setMessage(message);
failure.setHttpStatusCode(500);
failure.setRequestId(UUID.fromString(requestLog.getRawRequestID()));
failure.setParentRequestId(requestLog.parentRequestIdentifier == null ? null : UUID.fromString(requestLog.parentRequestIdentifier));
return ApiResponse.from(failure);
}
protected ResponseEntity wrap(HttpServletRequest request, RequestLog requestLog, ApiResponse response) {
response.header.setRequestId(UUID.fromString(requestLog.getRawRequestID()));
response.header.setParentRequestId(requestLog.parentRequestIdentifier == null ? null : UUID.fromString(requestLog.parentRequestIdentifier));
requestLog.finish(response);
logSerializer.save(requestLog);
return ResponseEntity.status(response.header.getHttpStatusCode())
.body(response);
}
protected ResponseEntity wrap(HttpServletRequest request, RequestLog requestLog, ListApiResponse response) {
response.header.setRequestId(UUID.fromString(requestLog.getRawRequestID()));
response.header.setParentRequestId(requestLog.parentRequestIdentifier == null ? null : UUID.fromString(requestLog.parentRequestIdentifier));
requestLog.finish(response);
logSerializer.save(requestLog);
return ResponseEntity.status(response.header.getHttpStatusCode())
.body(response);
}
protected ResponseEntity> runApiMethod(HttpServletRequest request, RequestLog requestLog, IApiControllerMethod method) {
try {
return wrap(request, requestLog, ApiResponse.from(method.execute()));
} catch (ManagedApiException ex) {
throw ex;
} catch (Exception ex) {
return wrap(request, requestLog, createInternalErrorResponse(request, requestLog, Errors.API_INTERNAL_ERROR, ex));
}
}
protected ResponseEntity runApiMethodReturningList(HttpServletRequest request, RequestLog requestLog, IApiControllerMethodReturningList method) {
try {
Object result = method.execute();
if (result instanceof FailureInfoDTO) {
return wrap(request, requestLog, ApiResponse.from((FailureInfoDTO) result));
} else {
return wrap(request, requestLog, ListApiResponse.from((List) result));
}
} catch (ManagedApiException ex) {
throw ex;
} catch (Exception ex) {
return wrap(request, requestLog, createInternalErrorResponse(request, requestLog, Errors.API_INTERNAL_ERROR, ex));
}
}
protected ResponseEntity unauthorizedResponse(HttpServletRequest request, RequestLog requestLog) {
int statusCode = HttpStatus.FORBIDDEN.value();
FailureInfoDTO info = new FailureInfoDTO();
info.setCode(Errors.ERR_ACCESS_DENIED);
info.setHttpStatusCode(statusCode);
info.setMessage("You don't have access to perform this operation. Please contact your administrator.");
info.setRequestId(UUID.fromString(requestLog.getRawRequestID()));
info.setParentRequestId(requestLog.parentRequestIdentifier == null ? null : UUID.fromString(requestLog.parentRequestIdentifier));
ApiResponse response = ApiResponse.from(info);
requestLog.finish(response);
logSerializer.save(requestLog);
return ResponseEntity.status(statusCode)
.body(response);
}
protected UUID getCurrentSignedInUserUUID() {
try {
ServiceAuthenticationToken token = (ServiceAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
return token.userUuid;
} catch (Exception ignored) {
return null;
}
}
}