From fe8b784cdf4ba457120a9eafc471971e10725701 Mon Sep 17 00:00:00 2001 From: uddhika Date: Fri, 20 Sep 2024 09:04:28 +0530 Subject: [PATCH] 204 status code and delete method json body handling --- .../ui/request/interceptor/InvokerHandler.java | 18 ++++++++++++++++++ .../request/interceptor/util/HandlerUtil.java | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/InvokerHandler.java b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/InvokerHandler.java index e8ffb2deef..d561ace090 100644 --- a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/InvokerHandler.java +++ b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/InvokerHandler.java @@ -27,8 +27,10 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hc.core5.http.ClassicHttpRequest; +import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpHeaders; import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.io.entity.StringEntity; import org.apache.hc.core5.http.io.support.ClassicRequestBuilder; import javax.servlet.annotation.MultipartConfig; @@ -37,6 +39,7 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import java.io.BufferedReader; import java.io.IOException; @MultipartConfig @@ -183,9 +186,24 @@ public class InvokerHandler extends HttpServlet { @Override protected void doDelete(HttpServletRequest req, HttpServletResponse resp) { try { + String jsonPayload = null; + if (req.getContentLength() > 0) { + StringBuilder jsonBuilder = new StringBuilder(); + String line; + try (BufferedReader reader = req.getReader()) { + while ((line = reader.readLine()) != null) { + jsonBuilder.append(line); + } + } + jsonPayload = jsonBuilder.toString(); + } if (validateRequest(req, resp)) { ClassicHttpRequest deleteRequest = ClassicRequestBuilder.delete(HandlerUtil.generateBackendRequestURL(req, apiEndpoint)).build(); + if (jsonPayload != null && !jsonPayload.isEmpty()) { + StringEntity entity = new StringEntity(jsonPayload, ContentType.APPLICATION_JSON); + deleteRequest.setEntity(entity); + } HandlerUtil.copyRequestHeaders(req, deleteRequest, false); deleteRequest.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BEARER + authData.getAccessToken()); ProxyResponse proxyResponse = HandlerUtil.execute(deleteRequest); diff --git a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/util/HandlerUtil.java b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/util/HandlerUtil.java index 26c4be4201..57ccd146ad 100644 --- a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/util/HandlerUtil.java +++ b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/util/HandlerUtil.java @@ -105,6 +105,13 @@ public class HandlerUtil { ProxyResponse handlerResponse = new ProxyResponse(); int statusCode = response.getCode(); if (responseEntity == null) { + if (statusCode == 204) { + handlerResponse.setCode(statusCode); + handlerResponse.setStatus(ProxyResponse.Status.SUCCESS); + handlerResponse.setExecutorResponse("SUCCESS"); + handlerResponse.setHeaders(response.getHeaders()); + return handlerResponse; + } log.error("Received null response for http request : " + httpRequest.getMethod() + " " + httpRequest.getRequestUri()); handlerResponse.setCode(HandlerConstants.INTERNAL_ERROR_CODE); handlerResponse.setStatus(ProxyResponse.Status.ERROR);