Skip to content

Commit

Permalink
[Minor] don't add operations log message for StrolchAccessDeniedExcep…
Browse files Browse the repository at this point in the history
…tion in REST
  • Loading branch information
eitch committed Aug 6, 2024
1 parent ebc8e86 commit 2f5a4ce
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@
import li.strolch.model.log.LogMessage;
import li.strolch.model.log.LogMessageState;
import li.strolch.model.log.LogSeverity;
import li.strolch.privilege.base.AccessDeniedException;
import li.strolch.privilege.model.CertificateThreadLocal;
import li.strolch.rest.helper.ResponseUtil;
import li.strolch.utils.helper.ExceptionHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;
import java.util.ResourceBundle;

import static li.strolch.model.Tags.AGENT;
import static li.strolch.utils.helper.ExceptionHelper.*;

@Provider
public class StrolchRestfulExceptionMapper implements ExceptionMapper<Exception> {
Expand All @@ -46,10 +48,12 @@ public class StrolchRestfulExceptionMapper implements ExceptionMapper<Exception>
@Override
public Response toResponse(Exception ex) {

logger.error(MessageFormat.format("Handling exception {0}", ex.getClass()), ex);
logger.error("Handling exception {}", ex.getClass(), ex);

RestfulStrolchComponent instance = RestfulStrolchComponent.getInstance();
if (instance.hasComponent(OperationsLog.class)) {
boolean isNotAccessDeniedException = !hasCause(ex, AccessDeniedException.class) && !hasCause(ex,
StrolchAccessDeniedException.class);
if (isNotAccessDeniedException && instance.hasComponent(OperationsLog.class)) {
try {
String username = CertificateThreadLocal.hasCert() ? CertificateThreadLocal.getCert().getUsername() :
"anonymous";
Expand All @@ -68,6 +72,7 @@ public Response toResponse(Exception ex) {

return switch (ex) {
case NotFoundException ignored -> ResponseUtil.toResponse(Status.NOT_FOUND, ex);
case AccessDeniedException e -> ResponseUtil.toResponse(Status.FORBIDDEN, e.getMessage());
case StrolchAccessDeniedException e -> ResponseUtil.toResponse(Status.FORBIDDEN, e.getI18n());
case StrolchNotAuthenticatedException e -> {
logger.error("User tried to access resource, but was not authenticated: {}", ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;
import jakarta.ws.rs.core.Response.Status;
import li.strolch.exception.StrolchAccessDeniedException;
import li.strolch.exception.StrolchException;
import li.strolch.exception.StrolchNotAuthenticatedException;
import li.strolch.privilege.base.AccessDeniedException;
Expand Down Expand Up @@ -179,7 +180,7 @@ private static Response evaluateResponseByCause(Exception e, String msg) {
Status status;
if (hasCause(e, InvalidCredentialsException.class)) {
status = Status.UNAUTHORIZED;
} else if (hasCause(e, AccessDeniedException.class)) {
} else if (hasCause(e, AccessDeniedException.class) || hasCause(e, StrolchAccessDeniedException.class)) {
status = Status.UNAUTHORIZED;
} else if (hasCause(e, StrolchException.class)) {
status = Status.FORBIDDEN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import li.strolch.exception.StrolchElementNotFoundException;
import li.strolch.exception.StrolchException;
import li.strolch.exception.StrolchNotAuthenticatedException;
import li.strolch.exception.StrolchUserMessageException;
import li.strolch.exception.*;
import li.strolch.model.i18n.I18nMessageToJsonVisitor;
import li.strolch.privilege.base.AccessDeniedException;
import li.strolch.privilege.base.PrivilegeException;
Expand Down Expand Up @@ -148,6 +145,7 @@ public static Response toResponse(ServiceResult svcResult) {
Throwable rootCause = getRootCause(t);
status = switch (rootCause) {
case AccessDeniedException ignored -> Status.FORBIDDEN;
case StrolchAccessDeniedException ignored -> Status.FORBIDDEN;
case PrivilegeException ignored -> Status.UNAUTHORIZED;
case StrolchElementNotFoundException ignored -> Status.NOT_FOUND;
case null, default -> Status.INTERNAL_SERVER_ERROR;
Expand All @@ -162,6 +160,7 @@ public static Response toResponse(Throwable t) {
return switch (rootCause) {
case StrolchNotAuthenticatedException ignored -> toResponse(Status.UNAUTHORIZED, rootCause);
case AccessDeniedException ignored -> toResponse(Status.FORBIDDEN, rootCause);
case StrolchAccessDeniedException ignored -> toResponse(Status.FORBIDDEN, rootCause);
case StrolchElementNotFoundException ignored -> toResponse(Status.NOT_FOUND, rootCause);
case PrivilegeException ignored -> toResponse(Status.FORBIDDEN, rootCause);
case null, default -> toResponse(Status.INTERNAL_SERVER_ERROR, rootCause);
Expand Down

0 comments on commit 2f5a4ce

Please sign in to comment.