From d34c6a2d47a73d50d7b12c8ff4f6931f56568115 Mon Sep 17 00:00:00 2001 From: Andreas Fankhauser <23085769+hiddenalpha@users.noreply.github.com> Date: Fri, 5 Jan 2024 09:45:51 +0100 Subject: [PATCH] [SDCISA-13736, SDCISA-10974] Fix bad habits in CopyResourceHandler. --- .../core/resource/CopyResourceHandler.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/gateleen-core/src/main/java/org/swisspush/gateleen/core/resource/CopyResourceHandler.java b/gateleen-core/src/main/java/org/swisspush/gateleen/core/resource/CopyResourceHandler.java index ef915d4ea..889703486 100644 --- a/gateleen-core/src/main/java/org/swisspush/gateleen/core/resource/CopyResourceHandler.java +++ b/gateleen-core/src/main/java/org/swisspush/gateleen/core/resource/CopyResourceHandler.java @@ -51,9 +51,10 @@ public boolean handle(final HttpServerRequest request) { if (task != null && validTask(request, task)) { performGETRequest(request, task); } else { - request.response().setStatusCode(StatusCode.BAD_REQUEST.getStatusCode()); - request.response().setStatusMessage(StatusCode.BAD_REQUEST.getStatusMessage()); - request.response().end(); + HttpServerResponse rsp = request.response(); + rsp.setStatusCode(StatusCode.BAD_REQUEST.getStatusCode()); + rsp.setStatusMessage(StatusCode.BAD_REQUEST.getStatusMessage()); + rsp.end(); } }); @@ -93,7 +94,7 @@ protected void performGETRequest(final HttpServerRequest request, final CopyTask selfClient.request(HttpMethod.GET, task.getSourceUri()).onComplete(event -> { if (event.failed()) { - log.warn("Failed request to {}: {}", request.uri(), event.cause()); + log.warn("Failed request to {}", request.uri(), new Exception("stacktrace", event.cause())); return; } HttpClientRequest selfRequest = event.result(); @@ -104,10 +105,16 @@ protected void performGETRequest(final HttpServerRequest request, final CopyTask selfRequest.setTimeout(DEFAULT_TIMEOUT); // add exception handler - selfRequest.exceptionHandler(exception -> log.warn("CopyResourceHandler: GET request failed: " + request.uri() + ": " + exception.getMessage())); + selfRequest.exceptionHandler( ex -> { + log.warn("CopyResourceHandler: GET request failed: {}", request.uri(), new Exception("stacktrace", ex)); + }); // fire selfRequest.send(asyncResult -> { + if( asyncResult.failed() ){ + log.warn("stacktrace", new Exception("stacktrace", asyncResult.cause())); + return; + } HttpClientResponse response = asyncResult.result(); // POST response is OK if (response.statusCode() == StatusCode.OK.getStatusCode()) { @@ -133,7 +140,7 @@ protected void performPUTRequest(final HttpServerRequest request, final HttpClie selfClient.request(HttpMethod.PUT, task.getDestinationUri()).onComplete(event -> { if (event.failed()) { - log.warn("Failed request to {}: {}", request.uri(), event.cause()); + log.warn("Failed request to {}", request.uri(), new Exception("stacktrace", event.cause())); return; } HttpClientRequest selfRequest = event.result(); @@ -149,6 +156,10 @@ protected void performPUTRequest(final HttpServerRequest request, final HttpClie // fire selfRequest.send(asyncResult -> { + if( asyncResult.failed() ){ + log.warn("stacktrace", new Exception("stacktrace", asyncResult.cause())); + return; + } HttpClientResponse response = asyncResult.result(); createResponse(request, response, task); }); @@ -176,8 +187,9 @@ private void createResponse(final HttpServerRequest request, final HttpClientRes request.response().setStatusCode(StatusCodeTranslator.translateStatusCode(response.statusCode(), request.headers())); request.response().setStatusMessage(response.statusMessage()); response.bodyHandler(buffer -> { - request.response().putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(buffer.length())); - request.response().end(buffer); + var rsp = request.response(); + rsp.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(buffer.length())); + rsp.end(buffer); }); }