From 101f6fd163f23cd825eb9d06dcda318ea620fbcc Mon Sep 17 00:00:00 2001 From: mokhan Date: Sun, 23 Jul 2017 14:44:52 +0530 Subject: [PATCH] Comment incorporation Missing file commit --- .../rest/log/util/RequestLoggingUtil.java | 38 +++++++++++++++++++ .../controller/TestTaxiFareController.java | 4 +- 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/util/RequestLoggingUtil.java diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/util/RequestLoggingUtil.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/util/RequestLoggingUtil.java new file mode 100644 index 0000000000..3c9eebf34e --- /dev/null +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/util/RequestLoggingUtil.java @@ -0,0 +1,38 @@ +package com.baeldung.rest.log.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.io.IOUtils; +import org.springframework.web.util.ContentCachingRequestWrapper; +import org.springframework.web.util.WebUtils; + +public class RequestLoggingUtil { + + public static String getStringFromInputStream(InputStream is) { + StringWriter writer = new StringWriter(); + String encoding = "UTF-8"; + try { + IOUtils.copy(is, writer, encoding); + } catch (IOException e) { + e.printStackTrace(); + } + return writer.toString(); + } + + public static String readPayload(final HttpServletRequest request) throws IOException { + String payloadData = null; + ContentCachingRequestWrapper contentCachingRequestWrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); + if (null != contentCachingRequestWrapper) { + byte[] buf = contentCachingRequestWrapper.getContentAsByteArray(); + if (buf.length > 0) { + payloadData = new String(buf, 0, buf.length, contentCachingRequestWrapper.getCharacterEncoding()); + } + } + return payloadData; + } + +} diff --git a/spring-rest-logging/src/test/java/com/baeldung/rest/log/controller/TestTaxiFareController.java b/spring-rest-logging/src/test/java/com/baeldung/rest/log/controller/TestTaxiFareController.java index 61d279a0c5..ed3cdda7ad 100644 --- a/spring-rest-logging/src/test/java/com/baeldung/rest/log/controller/TestTaxiFareController.java +++ b/spring-rest-logging/src/test/java/com/baeldung/rest/log/controller/TestTaxiFareController.java @@ -15,14 +15,14 @@ public class TestTaxiFareController { private static final String URL = "http://localhost:" + 9090 + "/rest-log/taxifare/"; @Test - public void given_taxi_fare_get() { + public void givenRequest_thenfetchTaxiFareRateCard() { TestRestTemplate testRestTemplate = new TestRestTemplate(); ResponseEntity response = testRestTemplate.getForEntity(URL + "get/", String.class); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); } @Test - public void given_taxi_ride_get_fare() { + public void givenTaxiRide_thenGetCalculatedFare() { TestRestTemplate testRestTemplate = new TestRestTemplate(); TaxiRide taxiRide = new TaxiRide(true,10l); String fare = testRestTemplate.postForObject(URL + "calculate/", taxiRide,String.class);