[DEV] update UUID error in ObjectId to be simple

This commit is contained in:
Edouard DUPIN 2025-01-08 19:54:08 +01:00
parent 4adc097c6b
commit 1e05e8361a
10 changed files with 50 additions and 49 deletions

View File

@ -14,7 +14,7 @@ public class ExceptionCatcher implements ExceptionMapper<Exception> {
public Response toResponse(final Exception exception) { public Response toResponse(final Exception exception) {
LOGGER.warn("Catch exception (not managed...):"); LOGGER.warn("Catch exception (not managed...):");
final RestErrorResponse ret = build(exception); final RestErrorResponse ret = build(exception);
LOGGER.error("Error UUID={}", ret.uuid); LOGGER.error("Error OID={}", ret.oid);
exception.printStackTrace(); exception.printStackTrace();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ret).type(MediaType.APPLICATION_JSON) return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ret).type(MediaType.APPLICATION_JSON)
.build(); .build();

View File

@ -15,7 +15,7 @@ public class FailExceptionCatcher implements ExceptionMapper<FailException> {
public Response toResponse(final FailException exception) { public Response toResponse(final FailException exception) {
LOGGER.warn("Catch FailException: {}", exception.getLocalizedMessage()); LOGGER.warn("Catch FailException: {}", exception.getLocalizedMessage());
final RestErrorResponse ret = build(exception); final RestErrorResponse ret = build(exception);
LOGGER.error("Error UUID={}", ret.uuid); LOGGER.error("Error OID={}", ret.oid);
if (exception.exception != null) { if (exception.exception != null) {
exception.exception.printStackTrace(); exception.exception.printStackTrace();
} }

View File

@ -15,7 +15,7 @@ public class InputExceptionCatcher implements ExceptionMapper<InputException> {
public Response toResponse(final InputException exception) { public Response toResponse(final InputException exception) {
LOGGER.warn("Catch InputException:"); LOGGER.warn("Catch InputException:");
final RestErrorResponse ret = build(exception); final RestErrorResponse ret = build(exception);
LOGGER.error("Error UUID={} ==> '{}'=>'{}'", ret.uuid, exception.missingVariable, LOGGER.error("Error OID={} ==> '{}'=>'{}'", ret.oid, exception.missingVariable,
exception.getLocalizedMessage()); exception.getLocalizedMessage());
// exception.printStackTrace(); // exception.printStackTrace();
return Response.status(exception.status).entity(ret).type(MediaType.APPLICATION_JSON).build(); return Response.status(exception.status).entity(ret).type(MediaType.APPLICATION_JSON).build();

View File

@ -16,7 +16,7 @@ public class JacksonExceptionCatcher implements ExceptionMapper<JacksonException
public Response toResponse(final JacksonException exception) { public Response toResponse(final JacksonException exception) {
LOGGER.warn("Catch exception Input data parsing:"); LOGGER.warn("Catch exception Input data parsing:");
final RestErrorResponse ret = build(exception); final RestErrorResponse ret = build(exception);
LOGGER.error("Error UUID={}", ret.uuid); LOGGER.error("Error OID={}", ret.oid);
exception.printStackTrace(); exception.printStackTrace();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ret).type(MediaType.APPLICATION_JSON) return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ret).type(MediaType.APPLICATION_JSON)
.build(); .build();

View File

@ -1,10 +1,9 @@
package org.kar.archidata.catcher; package org.kar.archidata.catcher;
import java.time.Instant; import java.time.Instant;
import java.util.UUID;
import org.bson.types.ObjectId;
import org.kar.archidata.annotation.NoWriteSpecificMode; import org.kar.archidata.annotation.NoWriteSpecificMode;
import org.kar.archidata.tools.UuidUtils;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
@ -12,7 +11,7 @@ import jakarta.ws.rs.core.Response;
@NoWriteSpecificMode @NoWriteSpecificMode
public class RestErrorResponse { public class RestErrorResponse {
public UUID uuid = UuidUtils.nextUUID(); public ObjectId oid = new ObjectId();
@NotNull @NotNull
@Column(length = 0) @Column(length = 0)
public String name; // Mandatory for TS generic error public String name; // Mandatory for TS generic error

View File

@ -15,7 +15,7 @@ public class SystemExceptionCatcher implements ExceptionMapper<SystemException>
public Response toResponse(final SystemException exception) { public Response toResponse(final SystemException exception) {
LOGGER.warn("Catch SystemException:"); LOGGER.warn("Catch SystemException:");
final RestErrorResponse ret = build(exception); final RestErrorResponse ret = build(exception);
LOGGER.error("Error UUID={}", ret.uuid); LOGGER.error("Error OID={}", ret.oid);
exception.printStackTrace(); exception.printStackTrace();
return Response.status(exception.status).entity(ret).type(MediaType.APPLICATION_JSON).build(); return Response.status(exception.status).entity(ret).type(MediaType.APPLICATION_JSON).build();
} }

View File

@ -14,7 +14,7 @@ public class WebApplicationExceptionCatcher implements ExceptionMapper<WebApplic
@Override @Override
public Response toResponse(final WebApplicationException exception) { public Response toResponse(final WebApplicationException exception) {
final RestErrorResponse ret = build(exception); final RestErrorResponse ret = build(exception);
LOGGER.error("Error UUID={}", ret.uuid); LOGGER.error("Error OID={}", ret.oid);
return Response.status(exception.getResponse().getStatusInfo().toEnum()).entity(ret) return Response.status(exception.getResponse().getStatusInfo().toEnum()).entity(ret)
.type(MediaType.APPLICATION_JSON).build(); .type(MediaType.APPLICATION_JSON).build();
} }

View File

@ -1,17 +1,18 @@
package org.kar.archidata.exception; package org.kar.archidata.exception;
import java.util.UUID; import org.bson.types.ObjectId;
public class RESTErrorResponseExeption extends Exception { public class RESTErrorResponseException extends Exception {
public UUID uuid; private static final long serialVersionUID = 1L;
public ObjectId oid;
public String time; public String time;
public String name; public String name;
public String message; public String message;
public int status; public int status;
public String statusMessage; public String statusMessage;
public RESTErrorResponseExeption() { public RESTErrorResponseException() {
this.uuid = null; this.oid = new ObjectId();
this.time = null; this.time = null;
this.name = null; this.name = null;
this.message = null; this.message = null;
@ -19,9 +20,9 @@ public class RESTErrorResponseExeption extends Exception {
this.statusMessage = null; this.statusMessage = null;
} }
public RESTErrorResponseExeption(final UUID uuid, final String time, final String name, final String message, public RESTErrorResponseException(final ObjectId oid, final String time, final String name, final String message,
final int status, final String statusMessage) { final int status, final String statusMessage) {
this.uuid = uuid; this.oid = oid;
this.time = time; this.time = time;
this.name = name; this.name = name;
this.message = message; this.message = message;
@ -31,7 +32,7 @@ public class RESTErrorResponseExeption extends Exception {
@Override @Override
public String toString() { public String toString() {
return "RESTErrorResponseExeption [uuid=" + this.uuid + ", time=" + this.time + ", name=" + this.name return "RESTErrorResponseExeption [oid=" + this.oid + ", time=" + this.time + ", name=" + this.name
+ ", message=" + this.message + ", status=" + this.status + ", statusMessage=" + this.statusMessage + ", message=" + this.message + ", status=" + this.status + ", statusMessage=" + this.statusMessage
+ "]"; + "]";
} }

View File

@ -208,7 +208,7 @@ public class AuthenticationFilter implements ContainerRequestFilter {
// The WWW-Authenticate header is sent along with the response // The WWW-Authenticate header is sent along with the response
LOGGER.warn("abortWithUnauthorized:"); LOGGER.warn("abortWithUnauthorized:");
final RestErrorResponse ret = new RestErrorResponse(Response.Status.UNAUTHORIZED, "Unauthorized", message); final RestErrorResponse ret = new RestErrorResponse(Response.Status.UNAUTHORIZED, "Unauthorized", message);
LOGGER.error("Error UUID={}", ret.uuid); LOGGER.error("Error OID={}", ret.oid);
requestContext.abortWith(Response.status(ret.status) requestContext.abortWith(Response.status(ret.status)
.header(HttpHeaders.WWW_AUTHENTICATE, .header(HttpHeaders.WWW_AUTHENTICATE,
AUTHENTICATION_SCHEME + " base64(HEADER).base64(CONTENT).base64(KEY)") AUTHENTICATION_SCHEME + " base64(HEADER).base64(CONTENT).base64(KEY)")
@ -217,7 +217,7 @@ public class AuthenticationFilter implements ContainerRequestFilter {
private void abortWithForbidden(final ContainerRequestContext requestContext, final String message) { private void abortWithForbidden(final ContainerRequestContext requestContext, final String message) {
final RestErrorResponse ret = new RestErrorResponse(Response.Status.FORBIDDEN, "FORBIDDEN", message); final RestErrorResponse ret = new RestErrorResponse(Response.Status.FORBIDDEN, "FORBIDDEN", message);
LOGGER.error("Error UUID={}", ret.uuid); LOGGER.error("Error OID={}", ret.oid);
requestContext.abortWith(Response.status(ret.status).header(HttpHeaders.WWW_AUTHENTICATE, message).entity(ret) requestContext.abortWith(Response.status(ret.status).header(HttpHeaders.WWW_AUTHENTICATE, message).entity(ret)
.type(MediaType.APPLICATION_JSON).build()); .type(MediaType.APPLICATION_JSON).build());
} }

View File

@ -11,7 +11,7 @@ import java.net.http.HttpResponse;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.kar.archidata.exception.RESTErrorResponseExeption; import org.kar.archidata.exception.RESTErrorResponseException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -37,7 +37,7 @@ public class RESTApi {
} }
public <T> List<T> gets(final Class<T> clazz, final String urlOffset) public <T> List<T> gets(final Class<T> clazz, final String urlOffset)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
final HttpClient client = HttpClient.newHttpClient(); final HttpClient client = HttpClient.newHttpClient();
Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1) Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1)
.uri(URI.create(this.baseUrl + urlOffset)); .uri(URI.create(this.baseUrl + urlOffset));
@ -48,8 +48,8 @@ public class RESTApi {
final HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString()); final HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) { if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) {
try { try {
final RESTErrorResponseExeption out = this.mapper.readValue(httpResponse.body(), final RESTErrorResponseException out = this.mapper.readValue(httpResponse.body(),
RESTErrorResponseExeption.class); RESTErrorResponseException.class);
throw out; throw out;
} catch (final MismatchedInputException ex) { } catch (final MismatchedInputException ex) {
throw new IOException( throw new IOException(
@ -62,57 +62,57 @@ public class RESTApi {
} }
public <T> T get(final Class<T> clazz, final String urlOffset) public <T> T get(final Class<T> clazz, final String urlOffset)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
return modelSendJson("GET", clazz, urlOffset, null); return modelSendJson("GET", clazz, urlOffset, null);
} }
public <T, U> T post(final Class<T> clazz, final String urlOffset, final U data) public <T, U> T post(final Class<T> clazz, final String urlOffset, final U data)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
return modelSend("POST", clazz, urlOffset, data); return modelSend("POST", clazz, urlOffset, data);
} }
public <T, U> T postJson(final Class<T> clazz, final String urlOffset, final String body) public <T, U> T postJson(final Class<T> clazz, final String urlOffset, final String body)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
return modelSendJson("POST", clazz, urlOffset, body); return modelSendJson("POST", clazz, urlOffset, body);
} }
public <T> T postMap(final Class<T> clazz, final String urlOffset, final Map<String, Object> data) public <T> T postMap(final Class<T> clazz, final String urlOffset, final Map<String, Object> data)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
return modelSendMap("POST", clazz, urlOffset, data); return modelSendMap("POST", clazz, urlOffset, data);
} }
public <T, U> T put(final Class<T> clazz, final String urlOffset, final U data) public <T, U> T put(final Class<T> clazz, final String urlOffset, final U data)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
return modelSend("PUT", clazz, urlOffset, data); return modelSend("PUT", clazz, urlOffset, data);
} }
public <T, U> T putJson(final Class<T> clazz, final String urlOffset, final String body) public <T, U> T putJson(final Class<T> clazz, final String urlOffset, final String body)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
return modelSendJson("PUT", clazz, urlOffset, body); return modelSendJson("PUT", clazz, urlOffset, body);
} }
public <T> T putMap(final Class<T> clazz, final String urlOffset, final Map<String, Object> data) public <T> T putMap(final Class<T> clazz, final String urlOffset, final Map<String, Object> data)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
return modelSendMap("PUT", clazz, urlOffset, data); return modelSendMap("PUT", clazz, urlOffset, data);
} }
public <T, U> T patch(final Class<T> clazz, final String urlOffset, final U data) public <T, U> T patch(final Class<T> clazz, final String urlOffset, final U data)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
return modelSend("PATCH", clazz, urlOffset, data); return modelSend("PATCH", clazz, urlOffset, data);
} }
public <T, U> T patchJson(final Class<T> clazz, final String urlOffset, final String body) public <T, U> T patchJson(final Class<T> clazz, final String urlOffset, final String body)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
return modelSendJson("PATCH", clazz, urlOffset, body); return modelSendJson("PATCH", clazz, urlOffset, body);
} }
public <T> T patchMap(final Class<T> clazz, final String urlOffset, final Map<String, Object> data) public <T> T patchMap(final Class<T> clazz, final String urlOffset, final Map<String, Object> data)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
return modelSendMap("PATCH", clazz, urlOffset, data); return modelSendMap("PATCH", clazz, urlOffset, data);
} }
protected <T, U> T modelSend(final String model, final Class<T> clazz, final String urlOffset, final U data) protected <T, U> T modelSend(final String model, final Class<T> clazz, final String urlOffset, final U data)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
if (data == null) { if (data == null) {
return modelSendJson(model, clazz, urlOffset, null); return modelSendJson(model, clazz, urlOffset, null);
} else { } else {
@ -123,7 +123,7 @@ public class RESTApi {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T, U> T modelSendJson(final String model, final Class<T> clazz, final String urlOffset, String body) public <T, U> T modelSendJson(final String model, final Class<T> clazz, final String urlOffset, String body)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
final HttpClient client = HttpClient.newHttpClient(); final HttpClient client = HttpClient.newHttpClient();
// client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true); // client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1) Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1)
@ -138,13 +138,14 @@ public class RESTApi {
} else { } else {
requestBuilding = requestBuilding.header("Content-Type", "application/json"); requestBuilding = requestBuilding.header("Content-Type", "application/json");
} }
LOGGER.trace("publish body: {}", body);
final HttpRequest request = requestBuilding.method(model, BodyPublishers.ofString(body)).build(); final HttpRequest request = requestBuilding.method(model, BodyPublishers.ofString(body)).build();
final HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString()); final HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) { if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) {
LOGGER.trace("Receive Error: {}", httpResponse.body()); LOGGER.trace("Receive Error: {}", httpResponse.body());
try { try {
final RESTErrorResponseExeption out = this.mapper.readValue(httpResponse.body(), final RESTErrorResponseException out = this.mapper.readValue(httpResponse.body(),
RESTErrorResponseExeption.class); RESTErrorResponseException.class);
throw out; throw out;
} catch (final MismatchedInputException ex) { } catch (final MismatchedInputException ex) {
throw new IOException( throw new IOException(
@ -171,7 +172,7 @@ public class RESTApi {
final String model, final String model,
final Class<T> clazz, final Class<T> clazz,
final String urlOffset, final String urlOffset,
final Map<String, Object> data) throws RESTErrorResponseExeption, IOException, InterruptedException { final Map<String, Object> data) throws RESTErrorResponseException, IOException, InterruptedException {
final HttpClient client = HttpClient.newHttpClient(); final HttpClient client = HttpClient.newHttpClient();
String body = null; String body = null;
Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1) Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1)
@ -189,8 +190,8 @@ public class RESTApi {
final HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString()); final HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) { if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) {
try { try {
final RESTErrorResponseExeption out = this.mapper.readValue(httpResponse.body(), final RESTErrorResponseException out = this.mapper.readValue(httpResponse.body(),
RESTErrorResponseExeption.class); RESTErrorResponseException.class);
throw out; throw out;
} catch (final MismatchedInputException ex) { } catch (final MismatchedInputException ex) {
throw new IOException( throw new IOException(
@ -210,7 +211,7 @@ public class RESTApi {
* Call a DELETE on a REST API * Call a DELETE on a REST API
* @param urlOffset Offset to call the API * @param urlOffset Offset to call the API
*/ */
public void delete(final String urlOffset) throws RESTErrorResponseExeption, IOException, InterruptedException { public void delete(final String urlOffset) throws RESTErrorResponseException, IOException, InterruptedException {
delete(Void.class, urlOffset); delete(Void.class, urlOffset);
} }
@ -222,7 +223,7 @@ public class RESTApi {
* @return The parsed object received. * @return The parsed object received.
*/ */
public <T> T delete(final Class<T> clazz, final String urlOffset) public <T> T delete(final Class<T> clazz, final String urlOffset)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
return simpleRequest("DELETE", clazz, urlOffset); return simpleRequest("DELETE", clazz, urlOffset);
} }
@ -230,7 +231,7 @@ public class RESTApi {
* Call an ARCHIVE on a REST API * Call an ARCHIVE on a REST API
* @param urlOffset Offset to call the API * @param urlOffset Offset to call the API
*/ */
public void archive(final String urlOffset) throws RESTErrorResponseExeption, IOException, InterruptedException { public void archive(final String urlOffset) throws RESTErrorResponseException, IOException, InterruptedException {
archive(Void.class, urlOffset); archive(Void.class, urlOffset);
} }
@ -242,7 +243,7 @@ public class RESTApi {
* @return The parsed object received. * @return The parsed object received.
*/ */
public <T> T archive(final Class<T> clazz, final String urlOffset) public <T> T archive(final Class<T> clazz, final String urlOffset)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
return simpleRequest("ARCHIVE", clazz, urlOffset); return simpleRequest("ARCHIVE", clazz, urlOffset);
} }
@ -250,7 +251,7 @@ public class RESTApi {
* Call an RESTORE on a REST API * Call an RESTORE on a REST API
* @param urlOffset Offset to call the API * @param urlOffset Offset to call the API
*/ */
public void restore(final String urlOffset) throws RESTErrorResponseExeption, IOException, InterruptedException { public void restore(final String urlOffset) throws RESTErrorResponseException, IOException, InterruptedException {
restore(Void.class, urlOffset); restore(Void.class, urlOffset);
} }
@ -262,7 +263,7 @@ public class RESTApi {
* @return The parsed object received. * @return The parsed object received.
*/ */
public <T> T restore(final Class<T> clazz, final String urlOffset) public <T> T restore(final Class<T> clazz, final String urlOffset)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
return simpleRequest("RESTORE", clazz, urlOffset); return simpleRequest("RESTORE", clazz, urlOffset);
} }
@ -275,7 +276,7 @@ public class RESTApi {
* @return The parsed object received. * @return The parsed object received.
*/ */
public <T> T simpleRequest(final String model, final Class<T> clazz, final String urlOffset) public <T> T simpleRequest(final String model, final Class<T> clazz, final String urlOffset)
throws RESTErrorResponseExeption, IOException, InterruptedException { throws RESTErrorResponseException, IOException, InterruptedException {
final HttpClient client = HttpClient.newHttpClient(); final HttpClient client = HttpClient.newHttpClient();
Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1) Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1)
.uri(URI.create(this.baseUrl + urlOffset)); .uri(URI.create(this.baseUrl + urlOffset));
@ -286,8 +287,8 @@ public class RESTApi {
final HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString()); final HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) { if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) {
try { try {
final RESTErrorResponseExeption out = this.mapper.readValue(httpResponse.body(), final RESTErrorResponseException out = this.mapper.readValue(httpResponse.body(),
RESTErrorResponseExeption.class); RESTErrorResponseException.class);
throw out; throw out;
} catch (final MismatchedInputException ex) { } catch (final MismatchedInputException ex) {
throw new IOException( throw new IOException(