From ec360d3049e279bf58476af2dadaf3fd231e40bc Mon Sep 17 00:00:00 2001 From: Vignesh-Kalyanasundaram Date: Wed, 16 Oct 2024 12:38:47 +0530 Subject: [PATCH] EDGPATRON-149 Formatting response --- .../org/folio/edge/patron/PatronHandler.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/folio/edge/patron/PatronHandler.java b/src/main/java/org/folio/edge/patron/PatronHandler.java index 81600a2..2863f17 100644 --- a/src/main/java/org/folio/edge/patron/PatronHandler.java +++ b/src/main/java/org/folio/edge/patron/PatronHandler.java @@ -266,7 +266,7 @@ public void handleGetPatronRegistrationStatus(RoutingContext ctx) { String alternateTenantId = ctx.request().getParam("alternateTenantId", client.tenant); final PatronOkapiClient patronClient = new PatronOkapiClient(client, alternateTenantId); patronClient.getExtPatronAccountByEmail(params.get(PARAM_EMAIL_ID), - resp -> handleProxyResponse(ctx, resp), + resp -> handleRegistrationStatusResponse(ctx, resp), t -> handleProxyException(ctx, t)); }); } @@ -343,6 +343,32 @@ protected void handleProxyResponse(RoutingContext ctx, HttpResponse resp } } + protected void handleRegistrationStatusResponse(RoutingContext ctx, HttpResponse resp) { + HttpServerResponse serverResponse = ctx.response(); + + int statusCode = resp.statusCode(); + serverResponse.setStatusCode(statusCode); + + String respBody = resp.bodyAsString(); + if (logger.isDebugEnabled() ) { + logger.debug("handleRegistrationStatusResponse:: response {} ", respBody); + } + + String contentType = resp.getHeader(HttpHeaders.CONTENT_TYPE.toString()); + + if (resp.statusCode() < 400 && Objects.nonNull(respBody)){ + setContentType(serverResponse, contentType); + serverResponse.end(respBody); //not an error case, pass on the response body as received + } + else { + String errorMsg = (statusCode == 404 || statusCode == 400 || statusCode == 422) + ? get422ErrorMsg(statusCode, respBody) + : getStructuredErrorMessage(statusCode, respBody); + setContentType(serverResponse, APPLICATION_JSON); + serverResponse.end(errorMsg); + } + } + @Override protected void handleProxyException(RoutingContext ctx, Throwable t) { logger.error("Exception retrieving data from mod-patron:", t);