Add 204 status code and delete method json body handling in ui-request-handle

no-details
Lasantha Dharmakeerthi 2 months ago
commit 94371ce586

@ -27,8 +27,10 @@ import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.hc.core5.http.ClassicHttpRequest; 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.HttpHeaders;
import org.apache.hc.core5.http.HttpStatus; 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 org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.MultipartConfig;
@ -37,6 +39,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
@MultipartConfig @MultipartConfig
@ -183,9 +186,24 @@ public class InvokerHandler extends HttpServlet {
@Override @Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) { protected void doDelete(HttpServletRequest req, HttpServletResponse resp) {
try { 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)) { if (validateRequest(req, resp)) {
ClassicHttpRequest deleteRequest = ClassicRequestBuilder.delete(HandlerUtil.generateBackendRequestURL(req, ClassicHttpRequest deleteRequest = ClassicRequestBuilder.delete(HandlerUtil.generateBackendRequestURL(req,
apiEndpoint)).build(); apiEndpoint)).build();
if (jsonPayload != null && !jsonPayload.isEmpty()) {
StringEntity entity = new StringEntity(jsonPayload, ContentType.APPLICATION_JSON);
deleteRequest.setEntity(entity);
}
HandlerUtil.copyRequestHeaders(req, deleteRequest, false); HandlerUtil.copyRequestHeaders(req, deleteRequest, false);
deleteRequest.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BEARER + authData.getAccessToken()); deleteRequest.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BEARER + authData.getAccessToken());
ProxyResponse proxyResponse = HandlerUtil.execute(deleteRequest); ProxyResponse proxyResponse = HandlerUtil.execute(deleteRequest);

@ -104,6 +104,13 @@ public class HandlerUtil {
final HttpEntity responseEntity = response.getEntity(); final HttpEntity responseEntity = response.getEntity();
ProxyResponse handlerResponse = new ProxyResponse(); ProxyResponse handlerResponse = new ProxyResponse();
int statusCode = response.getCode(); int statusCode = response.getCode();
if (statusCode == 204) {
handlerResponse.setCode(statusCode);
handlerResponse.setStatus(ProxyResponse.Status.SUCCESS);
handlerResponse.setExecutorResponse("SUCCESS");
handlerResponse.setHeaders(response.getHeaders());
return handlerResponse;
}
if (responseEntity == null) { if (responseEntity == null) {
log.error("Received null response for http request : " + httpRequest.getMethod() + " " + httpRequest.getRequestUri()); log.error("Received null response for http request : " + httpRequest.getMethod() + " " + httpRequest.getRequestUri());
handlerResponse.setCode(HandlerConstants.INTERNAL_ERROR_CODE); handlerResponse.setCode(HandlerConstants.INTERNAL_ERROR_CODE);

Loading…
Cancel
Save