Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey3-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>${jersey3-version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
Expand Down
4 changes: 4 additions & 0 deletions smart-connector-rest-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
</dependency>

<!-- Add XML suport -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package eu.knowledge.engine.rest.api;

import com.fasterxml.jackson.core.JsonProcessingException;

import eu.knowledge.engine.rest.model.ResponseMessage;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

import com.fasterxml.jackson.core.JsonProcessingException;

/**
* Since apparently Jersey gives a status 500 response when it encounters
* unexpected input in request bodies, this exception mapper maps those
Expand All @@ -16,10 +17,11 @@
public class JsonExceptionMapper implements ExceptionMapper<JsonProcessingException> {
@Override
public Response toResponse(JsonProcessingException exception) {
return Response
.status(Response.Status.BAD_REQUEST)
.entity(exception.getOriginalMessage())
.type(MediaType.TEXT_PLAIN)
.build();

var response = new ResponseMessage();
response.setMessageType("error");
response.setMessage(exception.getClass().getSimpleName() + ": " + exception.getOriginalMessage());

return Response.status(Response.Status.BAD_REQUEST).entity(response).type(MediaType.APPLICATION_JSON).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package eu.knowledge.engine.rest.api;

import eu.knowledge.engine.rest.model.ResponseMessage;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

/**
* Validation errors by default are returned as plain text and this class maps
* those errors to our {@link ResponseMessage}.
*/
@Provider
public class ValidationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {
@Override
public Response toResponse(ConstraintViolationException exception) {

var response = new ResponseMessage();
response.setMessageType("error");
response.setMessage(exception.getClass().getSimpleName() + ": " + prepareMessage(exception));

return Response.status(Response.Status.BAD_REQUEST).entity(response).type(MediaType.APPLICATION_JSON).build();
}

private String prepareMessage(ConstraintViolationException exception) {
StringBuilder message = new StringBuilder();
for (ConstraintViolation<?> cv : exception.getConstraintViolations()) {
message.append(cv.getPropertyPath() + " " + cv.getMessage() + "\n ");
}
return message.toString();
}
}
Loading