Skip to content

Commit

Permalink
[Minor] Better message on forbidden access
Browse files Browse the repository at this point in the history
  • Loading branch information
eitch committed Sep 26, 2023
1 parent 48d1218 commit 6c09715
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public Response toResponse(Exception ex) {
if (ex instanceof NotFoundException)
return ResponseUtil.toResponse(Status.NOT_FOUND, ex);

if (ex instanceof StrolchAccessDeniedException e)
return ResponseUtil.toResponse(Status.FORBIDDEN, e.getI18n());

if (ex instanceof StrolchNotAuthenticatedException e) {
logger.error("User tried to access resource, but was not authenticated: " + ex.getMessage());
return Response.status(Status.UNAUTHORIZED).entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
}

if (ex instanceof StrolchAccessDeniedException e)
return ResponseUtil.toResponse(Status.FORBIDDEN, e.getI18n());

return ResponseUtil.toResponse(ex);
}
}
43 changes: 22 additions & 21 deletions web-rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
package li.strolch.rest.helper;

import static li.strolch.rest.StrolchRestfulConstants.*;
import static li.strolch.utils.helper.ExceptionHelper.getExceptionMessageWithCauses;
import static li.strolch.utils.helper.ExceptionHelper.getRootCause;

import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
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.StrolchNotAuthenticatedException;
import li.strolch.exception.StrolchUserMessageException;
import li.strolch.model.i18n.I18nMessageToJsonVisitor;
import li.strolch.privilege.base.AccessDeniedException;
Expand All @@ -27,6 +20,14 @@
import li.strolch.utils.collections.Paging;
import li.strolch.utils.helper.StringHelper;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import static li.strolch.rest.StrolchRestfulConstants.*;
import static li.strolch.utils.helper.ExceptionHelper.getExceptionMessageWithCauses;
import static li.strolch.utils.helper.ExceptionHelper.getRootCause;

/**
* Created by eitch on 29.08.16.
*/
Expand Down Expand Up @@ -155,17 +156,17 @@ public static Response toResponse(ServiceResult svcResult) {
}

public static Response toResponse(Throwable t) {
if (t instanceof AccessDeniedException) {
if (t instanceof StrolchNotAuthenticatedException)
return ResponseUtil.toResponse(Status.UNAUTHORIZED, t);
if (t instanceof AccessDeniedException)
return ResponseUtil.toResponse(Status.FORBIDDEN, t);
} else if (t instanceof StrolchElementNotFoundException) {
if (t instanceof StrolchElementNotFoundException)
return ResponseUtil.toResponse(Status.NOT_FOUND, t);
} else if (t instanceof PrivilegeModelException) {
if (t instanceof PrivilegeModelException)
return ResponseUtil.toResponse(Status.INTERNAL_SERVER_ERROR, t);
} else if (t instanceof PrivilegeException) {
return ResponseUtil.toResponse(Status.UNAUTHORIZED, t);
} else {
return toResponse(Status.INTERNAL_SERVER_ERROR, t);
}
if (t instanceof PrivilegeException)
return ResponseUtil.toResponse(Status.FORBIDDEN, t);
return toResponse(Status.INTERNAL_SERVER_ERROR, t);
}

public static Response toResponse(Status status, String msg) {
Expand All @@ -183,8 +184,8 @@ public static Response toResponse(Status status, Throwable t) {
response.add("i18n", ex.getI18n().accept(new I18nMessageToJsonVisitor()));
} else {
Throwable rootCause = getRootCause(t);
if (rootCause instanceof StrolchUserMessageException ex
&& ((StrolchUserMessageException) rootCause).hasI18n()) {
if (rootCause instanceof StrolchUserMessageException ex &&
((StrolchUserMessageException) rootCause).hasI18n()) {
response.add("i18n", ex.getI18n().accept(new I18nMessageToJsonVisitor()));
}
}
Expand Down

0 comments on commit 6c09715

Please sign in to comment.