From 0852557aaabdc2440e04a534361579a43bcb3357 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Mon, 20 Feb 2017 12:13:51 +0100 Subject: [PATCH] [New] Added ResponseUtil.toResponse(ServiceResult) --- .../strolch/rest/StrolchRestfulConstants.java | 1 + .../li/strolch/rest/helper/ResponseUtil.java | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/li.strolch.rest/src/main/java/li/strolch/rest/StrolchRestfulConstants.java b/li.strolch.rest/src/main/java/li/strolch/rest/StrolchRestfulConstants.java index a433fee24..88dd46485 100644 --- a/li.strolch.rest/src/main/java/li/strolch/rest/StrolchRestfulConstants.java +++ b/li.strolch.rest/src/main/java/li/strolch/rest/StrolchRestfulConstants.java @@ -24,6 +24,7 @@ public class StrolchRestfulConstants { public static final String STROLCH_AUTHORIZATION = "strolch.authorization"; //$NON-NLS-1$ public static final String MSG = "msg"; + public static final String EXCEPTION_MSG = "exceptionMsg"; public static final String DATA = "data"; public static final String LAST_OFFSET = "lastOffset"; public static final String NEXT_OFFSET = "nextOffset"; diff --git a/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java b/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java index 3d451aae6..30e0a5506 100644 --- a/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java +++ b/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java @@ -1,6 +1,7 @@ package li.strolch.rest.helper; import static li.strolch.rest.StrolchRestfulConstants.DATA; +import static li.strolch.rest.StrolchRestfulConstants.EXCEPTION_MSG; import static li.strolch.rest.StrolchRestfulConstants.LAST_OFFSET; import static li.strolch.rest.StrolchRestfulConstants.LIMIT; import static li.strolch.rest.StrolchRestfulConstants.MSG; @@ -19,6 +20,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import li.strolch.service.api.ServiceResult; import li.strolch.utils.collections.Paging; import li.strolch.utils.helper.ExceptionHelper; import li.strolch.utils.helper.StringHelper; @@ -68,6 +70,42 @@ public class ResponseUtil { return Response.ok(json, MediaType.APPLICATION_JSON).build(); } + public static Response toResponse(String member, List jsonObjects) { + JsonObject response = new JsonObject(); + response.addProperty(MSG, StringHelper.DASH); + + JsonArray arrayJ = new JsonArray(); + for (JsonElement obj : jsonObjects) { + arrayJ.add(obj); + } + response.add(member, arrayJ); + + String json = new Gson().toJson(response); + + return Response.ok(json, MediaType.APPLICATION_JSON).build(); + } + + public static Response toResponse(ServiceResult svcResult) { + + String msg = StringHelper.DASH; + String exceptionMsg = StringHelper.DASH; + + if (!svcResult.isOk()) { + msg = svcResult.getMessage(); + Throwable t = svcResult.getThrowable(); + if (t != null) + exceptionMsg = StringHelper.formatExceptionMessage(t); + } + + JsonObject response = new JsonObject(); + response.addProperty(MSG, msg); + if (!exceptionMsg.equals(StringHelper.DASH)) + response.addProperty(EXCEPTION_MSG, exceptionMsg); + + String json = new Gson().toJson(response); + return Response.serverError().entity(json).type(MediaType.APPLICATION_JSON).build(); + } + public static Response toResponse(Throwable t) { JsonObject response = new JsonObject(); response.addProperty(MSG, ExceptionHelper.getExceptionMessageWithCauses(t));