From f05345112f95e9e282dc4ce169787fbf941d676b Mon Sep 17 00:00:00 2001 From: Sarf Khan Date: Sun, 16 Jul 2017 21:39:35 +0530 Subject: [PATCH 1/7] Spring rest logging Log incoming request --- spring-rest-logging/pom.xml | 64 +++++++++++++++++++ .../baeldung/rest/log/app/Application.java | 18 ++++++ .../log/app/TaxiFareRequestInterceptor.java | 27 ++++++++ .../config/RequestLoggingFilterConfig.java | 23 +++++++ .../rest/log/config/TaxiFareMVCConfig.java | 20 ++++++ .../log/controller/TaxiFareController.java | 40 ++++++++++++ .../com/baeldung/rest/log/data/RateCard.java | 28 ++++++++ .../com/baeldung/rest/log/data/TaxiRide.java | 32 ++++++++++ .../service/TaxiFareCalculatorService.java | 20 ++++++ .../src/main/resources/application.properties | 2 + .../src/main/resources/logback.xml | 21 ++++++ .../controller/TestTaxiFareController.java | 33 ++++++++++ 12 files changed, 328 insertions(+) create mode 100644 spring-rest-logging/pom.xml create mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/app/Application.java create mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java create mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java create mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/config/TaxiFareMVCConfig.java create mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java create mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/data/RateCard.java create mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/data/TaxiRide.java create mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/service/TaxiFareCalculatorService.java create mode 100644 spring-rest-logging/src/main/resources/application.properties create mode 100644 spring-rest-logging/src/main/resources/logback.xml create mode 100644 spring-rest-logging/src/test/java/com/baeldung/rest/log/controller/TestTaxiFareController.java diff --git a/spring-rest-logging/pom.xml b/spring-rest-logging/pom.xml new file mode 100644 index 0000000000..5924650e5f --- /dev/null +++ b/spring-rest-logging/pom.xml @@ -0,0 +1,64 @@ + + 4.0.0 + com.baeldung + spring-rest-logging + 0.1-SNAPSHOT + spring-rest-logging + war + + + org.springframework.boot + spring-boot-starter-parent + 1.4.3.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + + org.springframework.boot + spring-boot-test + + + + + org.springframework + spring-web + + + commons-logging + commons-logging + + + + + org.springframework + spring-webmvc + + + + + + javax.servlet + javax.servlet-api + provided + + + + junit + junit + test + + + + org.springframework + spring-test + + + diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/Application.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/Application.java new file mode 100644 index 0000000000..9eb79ee1b1 --- /dev/null +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/Application.java @@ -0,0 +1,18 @@ +package com.baeldung.rest.log.app; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.support.SpringBootServletInitializer; +import org.springframework.context.annotation.ComponentScan; + +@EnableAutoConfiguration +@ComponentScan("com.baeldung") +@SpringBootApplication +public class Application extends SpringBootServletInitializer { + + public static void main(final String[] args) { + SpringApplication.run(Application.class, args); + } + +} \ No newline at end of file diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java new file mode 100644 index 0000000000..bc011a4db7 --- /dev/null +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java @@ -0,0 +1,27 @@ +package com.baeldung.rest.log.app; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +@Component +public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter { + + Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class); + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + LOGGER.info("REQUEST URI: " + request.getRequestURI()); + return true; + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + LOGGER.info("RESPONSE: " + response.getStatus()); + } + +} diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java new file mode 100644 index 0000000000..158ba51f77 --- /dev/null +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java @@ -0,0 +1,23 @@ +package com.baeldung.rest.log.config; + +import javax.servlet.annotation.WebFilter; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.filter.CommonsRequestLoggingFilter; + +@Configuration +@WebFilter +public class RequestLoggingFilterConfig { + + @Bean + public CommonsRequestLoggingFilter logFilter() { + CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter(); + filter.setIncludeQueryString(true); + filter.setIncludePayload(true); + filter.setMaxPayloadLength(10000); + filter.setIncludeHeaders(false); + filter.setAfterMessagePrefix("REQUEST DATA : "); + return filter; + } +} diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/TaxiFareMVCConfig.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/TaxiFareMVCConfig.java new file mode 100644 index 0000000000..71d0fd379f --- /dev/null +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/TaxiFareMVCConfig.java @@ -0,0 +1,20 @@ +package com.baeldung.rest.log.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +import com.baeldung.rest.log.app.TaxiFareRequestInterceptor; + +@Configuration +public class TaxiFareMVCConfig extends WebMvcConfigurerAdapter { + + @Autowired + private TaxiFareRequestInterceptor taxiFareRequestInterceptor; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(taxiFareRequestInterceptor).addPathPatterns("/**/taxifare/**/"); + } +} diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java new file mode 100644 index 0000000000..0d8e4dafa4 --- /dev/null +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java @@ -0,0 +1,40 @@ +package com.baeldung.rest.log.controller; + +import javax.validation.Valid; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.baeldung.rest.log.data.RateCard; +import com.baeldung.rest.log.data.TaxiRide; +import com.baeldung.rest.log.service.TaxiFareCalculatorService; + +@Controller +public class TaxiFareController { + + @Autowired + private TaxiFareCalculatorService taxiFareCalculatorService; + + private static final Logger LOGGER = LoggerFactory.getLogger(TaxiFareController.class); + + @RequestMapping(method = RequestMethod.GET, value = "/taxifare/get/") + @ResponseBody + public RateCard getTaxiFare() { + LOGGER.debug("getTaxiFare() - START"); + return new RateCard(); + } + + @RequestMapping(method = RequestMethod.POST, value = "/taxifare/calculate/") + @ResponseBody + public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) { + LOGGER.debug("calculateTaxiFare() - START"); + return taxiFareCalculatorService.calculateFare(taxiRide); + } + +} diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/data/RateCard.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/data/RateCard.java new file mode 100644 index 0000000000..6e0c6d0e38 --- /dev/null +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/data/RateCard.java @@ -0,0 +1,28 @@ +package com.baeldung.rest.log.data; + +public class RateCard { + + private String nightSurcharge; + private String ratePerMile; + + public RateCard(){ + nightSurcharge="Extra $ 100"; + ratePerMile="$ 10 Per Mile"; + } + + + public String getNightSurcharge() { + return nightSurcharge; + } + public void setNightSurcharge(String nightSurcharge) { + this.nightSurcharge = nightSurcharge; + } + public String getRatePerMile() { + return ratePerMile; + } + public void setRatePerMile(String ratePerMile) { + this.ratePerMile = ratePerMile; + } + + +} diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/data/TaxiRide.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/data/TaxiRide.java new file mode 100644 index 0000000000..bcf102e49d --- /dev/null +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/data/TaxiRide.java @@ -0,0 +1,32 @@ +package com.baeldung.rest.log.data; + +public class TaxiRide { + + private Boolean isNightSurcharge; + private Long distanceInMile; + + public TaxiRide(){} + + public TaxiRide(Boolean isNightSurcharge, Long distanceInMile){ + this.isNightSurcharge = isNightSurcharge; + this.distanceInMile = distanceInMile; + } + + + public Boolean getIsNightSurcharge() { + return isNightSurcharge; + } + + public void setIsNightSurcharge(Boolean isNightSurcharge) { + this.isNightSurcharge = isNightSurcharge; + } + + public Long getDistanceInMile() { + return distanceInMile; + } + + public void setDistanceInMile(Long distanceInMile) { + this.distanceInMile = distanceInMile; + } + +} diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/service/TaxiFareCalculatorService.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/service/TaxiFareCalculatorService.java new file mode 100644 index 0000000000..31dfd7ac50 --- /dev/null +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/service/TaxiFareCalculatorService.java @@ -0,0 +1,20 @@ +package com.baeldung.rest.log.service; + +import org.springframework.stereotype.Service; + +import com.baeldung.rest.log.data.TaxiRide; + +@Service +public class TaxiFareCalculatorService { + + public String calculateFare(TaxiRide taxiRide) { + Long fare = 0l; + if (taxiRide.getIsNightSurcharge()) { + fare = taxiRide.getDistanceInMile() * 10 + 100; + } else { + fare = taxiRide.getDistanceInMile() * 10; + } + return String.valueOf(fare); + } + +} diff --git a/spring-rest-logging/src/main/resources/application.properties b/spring-rest-logging/src/main/resources/application.properties new file mode 100644 index 0000000000..ff8a818e66 --- /dev/null +++ b/spring-rest-logging/src/main/resources/application.properties @@ -0,0 +1,2 @@ +server.port= 9090 +server.context-path=/rest-log diff --git a/spring-rest-logging/src/main/resources/logback.xml b/spring-rest-logging/src/main/resources/logback.xml new file mode 100644 index 0000000000..08117752c7 --- /dev/null +++ b/spring-rest-logging/src/main/resources/logback.xml @@ -0,0 +1,21 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + + \ No newline at end of file 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 new file mode 100644 index 0000000000..61d279a0c5 --- /dev/null +++ b/spring-rest-logging/src/test/java/com/baeldung/rest/log/controller/TestTaxiFareController.java @@ -0,0 +1,33 @@ +package com.baeldung.rest.log.controller; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.Test; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import com.baeldung.rest.log.data.TaxiRide; + +public class TestTaxiFareController { + + private static final String URL = "http://localhost:" + 9090 + "/rest-log/taxifare/"; + + @Test + public void given_taxi_fare_get() { + 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() { + TestRestTemplate testRestTemplate = new TestRestTemplate(); + TaxiRide taxiRide = new TaxiRide(true,10l); + String fare = testRestTemplate.postForObject(URL + "calculate/", taxiRide,String.class); + assertThat(fare, equalTo("200")); + } + + +} From 75ac7de5cde3ac424af9e9f145f258fb4a237703 Mon Sep 17 00:00:00 2001 From: mokhan Date: Fri, 21 Jul 2017 22:12:27 +0530 Subject: [PATCH 2/7] Updated custom logging implementation --- spring-rest-logging/pom.xml | 11 ++++++++++- .../log/app/TaxiFareRequestInterceptor.java | 19 ++++++++++++++++++- .../config/RequestLoggingFilterConfig.java | 3 --- .../log/controller/TaxiFareController.java | 5 ++++- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/spring-rest-logging/pom.xml b/spring-rest-logging/pom.xml index 5924650e5f..799a746b63 100644 --- a/spring-rest-logging/pom.xml +++ b/spring-rest-logging/pom.xml @@ -12,7 +12,7 @@ spring-boot-starter-parent 1.4.3.RELEASE - + @@ -60,5 +60,14 @@ org.springframework spring-test + + + + commons-io + commons-io + 2.4 + + + diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java index bc011a4db7..4c187772cc 100644 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java @@ -7,6 +7,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; +import org.springframework.web.util.ContentCachingRequestWrapper; + +import com.baeldung.rest.log.util.RequestLoggingUtil; @Component public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter { @@ -15,7 +18,21 @@ public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - LOGGER.info("REQUEST URI: " + request.getRequestURI()); + String postData = null; + HttpServletRequest requestCacheWrapperObject = null; + try { + // Uncomment to produce the stream closed issue + // postData = RequestLoggingUtil.getStringFromInputStream(request.getInputStream()); + + // To overcome request stream closed issue + requestCacheWrapperObject = new ContentCachingRequestWrapper(request); + requestCacheWrapperObject.getParameterMap(); + } catch (Exception exception) { + exception.printStackTrace(); + } finally { + postData = RequestLoggingUtil.readPayload(requestCacheWrapperObject); + LOGGER.info("REQUEST DATA: " + postData); + } return true; } diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java index 158ba51f77..7afb34dd11 100644 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java @@ -1,13 +1,10 @@ package com.baeldung.rest.log.config; -import javax.servlet.annotation.WebFilter; - import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.CommonsRequestLoggingFilter; @Configuration -@WebFilter public class RequestLoggingFilterConfig { @Bean diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java index 0d8e4dafa4..1ef9ece0b2 100644 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java @@ -34,7 +34,10 @@ public class TaxiFareController { @ResponseBody public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) { LOGGER.debug("calculateTaxiFare() - START"); - return taxiFareCalculatorService.calculateFare(taxiRide); + String totalFare = taxiFareCalculatorService.calculateFare(taxiRide); + LOGGER.debug("calculateTaxiFare() - Total Fare : {}",totalFare); + LOGGER.debug("calculateTaxiFare() - END"); + return totalFare; } } From 101f6fd163f23cd825eb9d06dcda318ea620fbcc Mon Sep 17 00:00:00 2001 From: mokhan Date: Sun, 23 Jul 2017 14:44:52 +0530 Subject: [PATCH 3/7] 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); From 879bedf205e7609cd5d792d21d0ba680d681f545 Mon Sep 17 00:00:00 2001 From: mokhan Date: Sun, 23 Jul 2017 14:44:52 +0530 Subject: [PATCH 4/7] Comment incorporation Missing file commit --- pom.xml | 1 + .../rest/log/util/RequestLoggingUtil.java | 38 +++++++++++++++++++ .../controller/TestTaxiFareController.java | 4 +- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/util/RequestLoggingUtil.java diff --git a/pom.xml b/pom.xml index 818d131359..9bfadc01a5 100644 --- a/pom.xml +++ b/pom.xml @@ -208,6 +208,7 @@ spring-zuul spring-reactor spring-vertx + spring-rest-logging testing testng 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); From 29b0da9960184906702a49ce5ee2aadde4b3c681 Mon Sep 17 00:00:00 2001 From: mokhan Date: Mon, 24 Jul 2017 23:03:24 +0530 Subject: [PATCH 5/7] Web Logging code included in exisitng module namely "spring-rest" --- spring-rest/pom.xml | 558 +++++++++--------- .../com/baeldung/web/log/app/Application.java | 18 + .../log/app/TaxiFareRequestInterceptor.java | 44 ++ .../config/RequestLoggingFilterConfig.java | 20 + .../web/log/config/TaxiFareMVCConfig.java | 20 + .../log/controller/TaxiFareController.java | 43 ++ .../com/baeldung/web/log/data/RateCard.java | 28 + .../com/baeldung/web/log/data/TaxiRide.java | 32 + .../service/TaxiFareCalculatorService.java | 20 + .../web/log/util/RequestLoggingUtil.java | 38 ++ spring-rest/src/main/resources/logback.xml | 4 + .../web/log/test/TestTaxiFareController.java | 33 ++ 12 files changed, 583 insertions(+), 275 deletions(-) create mode 100644 spring-rest/src/main/java/com/baeldung/web/log/app/Application.java create mode 100644 spring-rest/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java create mode 100644 spring-rest/src/main/java/com/baeldung/web/log/config/RequestLoggingFilterConfig.java create mode 100644 spring-rest/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java create mode 100644 spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java create mode 100644 spring-rest/src/main/java/com/baeldung/web/log/data/RateCard.java create mode 100644 spring-rest/src/main/java/com/baeldung/web/log/data/TaxiRide.java create mode 100644 spring-rest/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java create mode 100644 spring-rest/src/main/java/com/baeldung/web/log/util/RequestLoggingUtil.java create mode 100644 spring-rest/src/test/java/com/baeldung/web/log/test/TestTaxiFareController.java diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index c12028149d..6065d7e529 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -1,321 +1,329 @@ - 4.0.0 - com.baeldung - spring-rest - 0.1-SNAPSHOT - spring-rest - war + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.baeldung + spring-rest + 0.1-SNAPSHOT + spring-rest + war - - parent-boot-4 - com.baeldung - 0.0.1-SNAPSHOT - ../parent-boot-4 - + + parent-boot-4 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-4 + - + - + - - org.springframework.boot - spring-boot-starter-thymeleaf - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-devtools - - - org.springframework.boot - spring-boot-test - + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-devtools + + + org.springframework.boot + spring-boot-test + - + - - org.springframework - spring-web - - - commons-logging - commons-logging - - - - - org.springframework - spring-webmvc - - - org.springframework - spring-oxm - + + org.springframework + spring-web + + + commons-logging + commons-logging + + + + + org.springframework + spring-webmvc + + + org.springframework + spring-oxm + - - commons-fileupload - commons-fileupload - ${commons-fileupload.version} - - + + commons-fileupload + commons-fileupload + ${commons-fileupload.version} + + - - javax.servlet - javax.servlet-api - provided - + + javax.servlet + javax.servlet-api + provided + - - javax.servlet - jstl - runtime - + + javax.servlet + jstl + runtime + - + - - com.fasterxml.jackson.core - jackson-databind - + + com.fasterxml.jackson.core + jackson-databind + - - com.fasterxml.jackson.dataformat - jackson-dataformat-xml - + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + - - com.thoughtworks.xstream - xstream - ${xstream.version} - + + com.thoughtworks.xstream + xstream + ${xstream.version} + - + - - com.google.guava - guava - ${guava.version} - + + com.google.guava + guava + ${guava.version} + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - + - - com.squareup.okhttp3 - okhttp - ${com.squareup.okhttp3.version} - + + com.squareup.okhttp3 + okhttp + ${com.squareup.okhttp3.version} + - + - - org.hamcrest - hamcrest-core - test - - - org.hamcrest - hamcrest-library - test - + + org.hamcrest + hamcrest-core + test + + + org.hamcrest + hamcrest-library + test + - - org.mockito - mockito-core - test - + + org.mockito + mockito-core + test + - - org.springframework - spring-test - + + org.springframework + spring-test + - - - com.google.protobuf - protobuf-java - ${protobuf-java.version} - - - com.googlecode.protobuf-java-format - protobuf-java-format - ${protobuf-java-format.version} - + + + com.google.protobuf + protobuf-java + ${protobuf-java.version} + + + com.googlecode.protobuf-java-format + protobuf-java-format + ${protobuf-java-format.version} + - - com.esotericsoftware - kryo - ${kryo.version} - + + com.esotericsoftware + kryo + ${kryo.version} + - - com.jayway.jsonpath - json-path - + + com.jayway.jsonpath + json-path + - - - spring-rest - - - src/main/resources - true - - + + + commons-io + commons-io + 2.4 + - - - org.springframework.boot - spring-boot-maven-plugin - - true - - - - org.apache.maven.plugins - maven-war-plugin - + - - org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} - - true - - tomcat8x - embedded - - - - - - - 8082 - - - - + + spring-rest + + + src/main/resources + true + + - + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + org.apache.maven.plugins + maven-war-plugin + - + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + tomcat8x + embedded + + + + + + + 8082 + + + + - + - - integration - - - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - none - - - **/*IntegrationTest.java - - - - - + - - - + - - live - - - - org.codehaus.cargo - cargo-maven2-plugin - - - start-server - pre-integration-test - - start - - - - stop-server - post-integration-test - - stop - - - - + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + none + + + **/*IntegrationTest.java + + + + + - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - none - - - **/*LiveTest.java - - - cargo - - - - - + + + - - - + + live + + + + org.codehaus.cargo + cargo-maven2-plugin + + + start-server + pre-integration-test + + start + + + + stop-server + post-integration-test + + stop + + + + - + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + none + + + **/*LiveTest.java + + + cargo + + + + + - - 1.3.2 - 4.0.0 - 1.4 - 3.1.0 - 3.5 - 1.4.9 + + + - - 20.0 + - - 1.6.0 - 3.0.4 + + 1.3.2 + 4.0.0 + 1.4 + 3.1.0 + 3.5 + 1.4.9 - - 3.4.1 + + 20.0 - 2.2.0 - + + 1.6.0 + 3.0.4 + + + 3.4.1 + + 2.2.0 + diff --git a/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java b/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java new file mode 100644 index 0000000000..9bdbbd0d9f --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java @@ -0,0 +1,18 @@ +package com.baeldung.web.log.app; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.support.SpringBootServletInitializer; +import org.springframework.context.annotation.ComponentScan; + +@EnableAutoConfiguration +@ComponentScan("com.baeldung.web.log") +@SpringBootApplication +public class Application extends SpringBootServletInitializer { + + public static void main(final String[] args) { + SpringApplication.run(Application.class, args); + } + +} \ No newline at end of file diff --git a/spring-rest/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java b/spring-rest/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java new file mode 100644 index 0000000000..831d236edd --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java @@ -0,0 +1,44 @@ +package com.baeldung.web.log.app; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; +import org.springframework.web.util.ContentCachingRequestWrapper; + +import com.baeldung.web.log.util.RequestLoggingUtil; + +@Component +public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter { + + Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class); + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + String postData = null; + HttpServletRequest requestCacheWrapperObject = null; + try { + // Uncomment to produce the stream closed issue + // postData = RequestLoggingUtil.getStringFromInputStream(request.getInputStream()); + + // To overcome request stream closed issue + requestCacheWrapperObject = new ContentCachingRequestWrapper(request); + requestCacheWrapperObject.getParameterMap(); + } catch (Exception exception) { + exception.printStackTrace(); + } finally { + postData = RequestLoggingUtil.readPayload(requestCacheWrapperObject); + LOGGER.info("REQUEST DATA: " + postData); + } + return true; + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + LOGGER.info("RESPONSE: " + response.getStatus()); + } + +} diff --git a/spring-rest/src/main/java/com/baeldung/web/log/config/RequestLoggingFilterConfig.java b/spring-rest/src/main/java/com/baeldung/web/log/config/RequestLoggingFilterConfig.java new file mode 100644 index 0000000000..bc9ad1cf84 --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/log/config/RequestLoggingFilterConfig.java @@ -0,0 +1,20 @@ +package com.baeldung.web.log.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.filter.CommonsRequestLoggingFilter; + +@Configuration +public class RequestLoggingFilterConfig { + + @Bean + public CommonsRequestLoggingFilter logFilter() { + CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter(); + filter.setIncludeQueryString(true); + filter.setIncludePayload(true); + filter.setMaxPayloadLength(10000); + filter.setIncludeHeaders(false); + filter.setAfterMessagePrefix("REQUEST DATA : "); + return filter; + } +} diff --git a/spring-rest/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java b/spring-rest/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java new file mode 100644 index 0000000000..fa26706ff0 --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java @@ -0,0 +1,20 @@ +package com.baeldung.web.log.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +import com.baeldung.web.log.app.TaxiFareRequestInterceptor; + +@Configuration +public class TaxiFareMVCConfig extends WebMvcConfigurerAdapter { + + @Autowired + private TaxiFareRequestInterceptor taxiFareRequestInterceptor; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(taxiFareRequestInterceptor).addPathPatterns("/**/taxifare/**/"); + } +} diff --git a/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java b/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java new file mode 100644 index 0000000000..28bf07e8a6 --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java @@ -0,0 +1,43 @@ +package com.baeldung.web.log.controller; + +import javax.validation.Valid; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.baeldung.web.log.data.RateCard; +import com.baeldung.web.log.data.TaxiRide; +import com.baeldung.web.log.service.TaxiFareCalculatorService; + +@Controller +public class TaxiFareController { + + @Autowired + private TaxiFareCalculatorService taxiFareCalculatorService; + + private static final Logger LOGGER = LoggerFactory.getLogger(TaxiFareController.class); + + @RequestMapping(method = RequestMethod.GET, value = "/taxifare/get/") + @ResponseBody + public RateCard getTaxiFare() { + LOGGER.debug("getTaxiFare() - START"); + return new RateCard(); + } + + @RequestMapping(method = RequestMethod.POST, value = "/taxifare/calculate/") + @ResponseBody + public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) { + LOGGER.debug("calculateTaxiFare() - START"); + String totalFare = taxiFareCalculatorService.calculateFare(taxiRide); + LOGGER.debug("calculateTaxiFare() - Total Fare : {}",totalFare); + LOGGER.debug("calculateTaxiFare() - END"); + return totalFare; + } + +} diff --git a/spring-rest/src/main/java/com/baeldung/web/log/data/RateCard.java b/spring-rest/src/main/java/com/baeldung/web/log/data/RateCard.java new file mode 100644 index 0000000000..35ae38fd11 --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/log/data/RateCard.java @@ -0,0 +1,28 @@ +package com.baeldung.web.log.data; + +public class RateCard { + + private String nightSurcharge; + private String ratePerMile; + + public RateCard(){ + nightSurcharge="Extra $ 100"; + ratePerMile="$ 10 Per Mile"; + } + + + public String getNightSurcharge() { + return nightSurcharge; + } + public void setNightSurcharge(String nightSurcharge) { + this.nightSurcharge = nightSurcharge; + } + public String getRatePerMile() { + return ratePerMile; + } + public void setRatePerMile(String ratePerMile) { + this.ratePerMile = ratePerMile; + } + + +} diff --git a/spring-rest/src/main/java/com/baeldung/web/log/data/TaxiRide.java b/spring-rest/src/main/java/com/baeldung/web/log/data/TaxiRide.java new file mode 100644 index 0000000000..0877cdced4 --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/log/data/TaxiRide.java @@ -0,0 +1,32 @@ +package com.baeldung.web.log.data; + +public class TaxiRide { + + private Boolean isNightSurcharge; + private Long distanceInMile; + + public TaxiRide(){} + + public TaxiRide(Boolean isNightSurcharge, Long distanceInMile){ + this.isNightSurcharge = isNightSurcharge; + this.distanceInMile = distanceInMile; + } + + + public Boolean getIsNightSurcharge() { + return isNightSurcharge; + } + + public void setIsNightSurcharge(Boolean isNightSurcharge) { + this.isNightSurcharge = isNightSurcharge; + } + + public Long getDistanceInMile() { + return distanceInMile; + } + + public void setDistanceInMile(Long distanceInMile) { + this.distanceInMile = distanceInMile; + } + +} diff --git a/spring-rest/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java b/spring-rest/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java new file mode 100644 index 0000000000..ec9c81187a --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java @@ -0,0 +1,20 @@ +package com.baeldung.web.log.service; + +import org.springframework.stereotype.Service; + +import com.baeldung.web.log.data.TaxiRide; + +@Service +public class TaxiFareCalculatorService { + + public String calculateFare(TaxiRide taxiRide) { + Long fare = 0l; + if (taxiRide.getIsNightSurcharge()) { + fare = taxiRide.getDistanceInMile() * 10 + 100; + } else { + fare = taxiRide.getDistanceInMile() * 10; + } + return String.valueOf(fare); + } + +} diff --git a/spring-rest/src/main/java/com/baeldung/web/log/util/RequestLoggingUtil.java b/spring-rest/src/main/java/com/baeldung/web/log/util/RequestLoggingUtil.java new file mode 100644 index 0000000000..c13d35b55c --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/log/util/RequestLoggingUtil.java @@ -0,0 +1,38 @@ +package com.baeldung.web.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/src/main/resources/logback.xml b/spring-rest/src/main/resources/logback.xml index ec0dc2469a..3496a4a03c 100644 --- a/spring-rest/src/main/resources/logback.xml +++ b/spring-rest/src/main/resources/logback.xml @@ -6,6 +6,10 @@ + + + + diff --git a/spring-rest/src/test/java/com/baeldung/web/log/test/TestTaxiFareController.java b/spring-rest/src/test/java/com/baeldung/web/log/test/TestTaxiFareController.java new file mode 100644 index 0000000000..935275983f --- /dev/null +++ b/spring-rest/src/test/java/com/baeldung/web/log/test/TestTaxiFareController.java @@ -0,0 +1,33 @@ +package com.baeldung.web.log.test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.Test; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import com.baeldung.web.log.data.TaxiRide; + +public class TestTaxiFareController { + + private static final String URL = "http://localhost:" + 8082 + "/spring-rest/taxifare/"; + + @Test + 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 givenTaxiRide_thenGetCalculatedFare() { + TestRestTemplate testRestTemplate = new TestRestTemplate(); + TaxiRide taxiRide = new TaxiRide(true,10l); + String fare = testRestTemplate.postForObject(URL + "calculate/", taxiRide,String.class); + assertThat(fare, equalTo("200")); + } + + +} From 553c26c1a2a96b80012cfa8e9cd64a6e35af8762 Mon Sep 17 00:00:00 2001 From: mokhan Date: Mon, 24 Jul 2017 23:08:43 +0530 Subject: [PATCH 6/7] deleted the new module --- pom.xml | 2 +- spring-rest-logging/pom.xml | 73 ------------------- .../baeldung/rest/log/app/Application.java | 18 ----- .../log/app/TaxiFareRequestInterceptor.java | 44 ----------- .../config/RequestLoggingFilterConfig.java | 20 ----- .../rest/log/config/TaxiFareMVCConfig.java | 20 ----- .../log/controller/TaxiFareController.java | 43 ----------- .../com/baeldung/rest/log/data/RateCard.java | 28 ------- .../com/baeldung/rest/log/data/TaxiRide.java | 32 -------- .../service/TaxiFareCalculatorService.java | 20 ----- .../rest/log/util/RequestLoggingUtil.java | 38 ---------- .../src/main/resources/application.properties | 2 - .../src/main/resources/logback.xml | 21 ------ .../controller/TestTaxiFareController.java | 33 --------- 14 files changed, 1 insertion(+), 393 deletions(-) delete mode 100644 spring-rest-logging/pom.xml delete mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/app/Application.java delete mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java delete mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java delete mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/config/TaxiFareMVCConfig.java delete mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java delete mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/data/RateCard.java delete mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/data/TaxiRide.java delete mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/service/TaxiFareCalculatorService.java delete mode 100644 spring-rest-logging/src/main/java/com/baeldung/rest/log/util/RequestLoggingUtil.java delete mode 100644 spring-rest-logging/src/main/resources/application.properties delete mode 100644 spring-rest-logging/src/main/resources/logback.xml delete mode 100644 spring-rest-logging/src/test/java/com/baeldung/rest/log/controller/TestTaxiFareController.java diff --git a/pom.xml b/pom.xml index 9bfadc01a5..7e87d4fd8c 100644 --- a/pom.xml +++ b/pom.xml @@ -208,7 +208,7 @@ spring-zuul spring-reactor spring-vertx - spring-rest-logging + testing testng diff --git a/spring-rest-logging/pom.xml b/spring-rest-logging/pom.xml deleted file mode 100644 index 799a746b63..0000000000 --- a/spring-rest-logging/pom.xml +++ /dev/null @@ -1,73 +0,0 @@ - - 4.0.0 - com.baeldung - spring-rest-logging - 0.1-SNAPSHOT - spring-rest-logging - war - - - org.springframework.boot - spring-boot-starter-parent - 1.4.3.RELEASE - - - - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - - - org.springframework.boot - spring-boot-test - - - - - org.springframework - spring-web - - - commons-logging - commons-logging - - - - - org.springframework - spring-webmvc - - - - - - javax.servlet - javax.servlet-api - provided - - - - junit - junit - test - - - - org.springframework - spring-test - - - - - commons-io - commons-io - 2.4 - - - - - diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/Application.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/Application.java deleted file mode 100644 index 9eb79ee1b1..0000000000 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/Application.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baeldung.rest.log.app; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.web.support.SpringBootServletInitializer; -import org.springframework.context.annotation.ComponentScan; - -@EnableAutoConfiguration -@ComponentScan("com.baeldung") -@SpringBootApplication -public class Application extends SpringBootServletInitializer { - - public static void main(final String[] args) { - SpringApplication.run(Application.class, args); - } - -} \ No newline at end of file diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java deleted file mode 100644 index 4c187772cc..0000000000 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.baeldung.rest.log.app; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; -import org.springframework.web.util.ContentCachingRequestWrapper; - -import com.baeldung.rest.log.util.RequestLoggingUtil; - -@Component -public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter { - - Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class); - - @Override - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - String postData = null; - HttpServletRequest requestCacheWrapperObject = null; - try { - // Uncomment to produce the stream closed issue - // postData = RequestLoggingUtil.getStringFromInputStream(request.getInputStream()); - - // To overcome request stream closed issue - requestCacheWrapperObject = new ContentCachingRequestWrapper(request); - requestCacheWrapperObject.getParameterMap(); - } catch (Exception exception) { - exception.printStackTrace(); - } finally { - postData = RequestLoggingUtil.readPayload(requestCacheWrapperObject); - LOGGER.info("REQUEST DATA: " + postData); - } - return true; - } - - @Override - public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { - LOGGER.info("RESPONSE: " + response.getStatus()); - } - -} diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java deleted file mode 100644 index 7afb34dd11..0000000000 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.rest.log.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.filter.CommonsRequestLoggingFilter; - -@Configuration -public class RequestLoggingFilterConfig { - - @Bean - public CommonsRequestLoggingFilter logFilter() { - CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter(); - filter.setIncludeQueryString(true); - filter.setIncludePayload(true); - filter.setMaxPayloadLength(10000); - filter.setIncludeHeaders(false); - filter.setAfterMessagePrefix("REQUEST DATA : "); - return filter; - } -} diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/TaxiFareMVCConfig.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/TaxiFareMVCConfig.java deleted file mode 100644 index 71d0fd379f..0000000000 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/TaxiFareMVCConfig.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.rest.log.config; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; - -import com.baeldung.rest.log.app.TaxiFareRequestInterceptor; - -@Configuration -public class TaxiFareMVCConfig extends WebMvcConfigurerAdapter { - - @Autowired - private TaxiFareRequestInterceptor taxiFareRequestInterceptor; - - @Override - public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(taxiFareRequestInterceptor).addPathPatterns("/**/taxifare/**/"); - } -} diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java deleted file mode 100644 index 1ef9ece0b2..0000000000 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.baeldung.rest.log.controller; - -import javax.validation.Valid; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; - -import com.baeldung.rest.log.data.RateCard; -import com.baeldung.rest.log.data.TaxiRide; -import com.baeldung.rest.log.service.TaxiFareCalculatorService; - -@Controller -public class TaxiFareController { - - @Autowired - private TaxiFareCalculatorService taxiFareCalculatorService; - - private static final Logger LOGGER = LoggerFactory.getLogger(TaxiFareController.class); - - @RequestMapping(method = RequestMethod.GET, value = "/taxifare/get/") - @ResponseBody - public RateCard getTaxiFare() { - LOGGER.debug("getTaxiFare() - START"); - return new RateCard(); - } - - @RequestMapping(method = RequestMethod.POST, value = "/taxifare/calculate/") - @ResponseBody - public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) { - LOGGER.debug("calculateTaxiFare() - START"); - String totalFare = taxiFareCalculatorService.calculateFare(taxiRide); - LOGGER.debug("calculateTaxiFare() - Total Fare : {}",totalFare); - LOGGER.debug("calculateTaxiFare() - END"); - return totalFare; - } - -} diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/data/RateCard.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/data/RateCard.java deleted file mode 100644 index 6e0c6d0e38..0000000000 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/data/RateCard.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.rest.log.data; - -public class RateCard { - - private String nightSurcharge; - private String ratePerMile; - - public RateCard(){ - nightSurcharge="Extra $ 100"; - ratePerMile="$ 10 Per Mile"; - } - - - public String getNightSurcharge() { - return nightSurcharge; - } - public void setNightSurcharge(String nightSurcharge) { - this.nightSurcharge = nightSurcharge; - } - public String getRatePerMile() { - return ratePerMile; - } - public void setRatePerMile(String ratePerMile) { - this.ratePerMile = ratePerMile; - } - - -} diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/data/TaxiRide.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/data/TaxiRide.java deleted file mode 100644 index bcf102e49d..0000000000 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/data/TaxiRide.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.rest.log.data; - -public class TaxiRide { - - private Boolean isNightSurcharge; - private Long distanceInMile; - - public TaxiRide(){} - - public TaxiRide(Boolean isNightSurcharge, Long distanceInMile){ - this.isNightSurcharge = isNightSurcharge; - this.distanceInMile = distanceInMile; - } - - - public Boolean getIsNightSurcharge() { - return isNightSurcharge; - } - - public void setIsNightSurcharge(Boolean isNightSurcharge) { - this.isNightSurcharge = isNightSurcharge; - } - - public Long getDistanceInMile() { - return distanceInMile; - } - - public void setDistanceInMile(Long distanceInMile) { - this.distanceInMile = distanceInMile; - } - -} diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/service/TaxiFareCalculatorService.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/service/TaxiFareCalculatorService.java deleted file mode 100644 index 31dfd7ac50..0000000000 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/service/TaxiFareCalculatorService.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.rest.log.service; - -import org.springframework.stereotype.Service; - -import com.baeldung.rest.log.data.TaxiRide; - -@Service -public class TaxiFareCalculatorService { - - public String calculateFare(TaxiRide taxiRide) { - Long fare = 0l; - if (taxiRide.getIsNightSurcharge()) { - fare = taxiRide.getDistanceInMile() * 10 + 100; - } else { - fare = taxiRide.getDistanceInMile() * 10; - } - return String.valueOf(fare); - } - -} 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 deleted file mode 100644 index 3c9eebf34e..0000000000 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/util/RequestLoggingUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -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/main/resources/application.properties b/spring-rest-logging/src/main/resources/application.properties deleted file mode 100644 index ff8a818e66..0000000000 --- a/spring-rest-logging/src/main/resources/application.properties +++ /dev/null @@ -1,2 +0,0 @@ -server.port= 9090 -server.context-path=/rest-log diff --git a/spring-rest-logging/src/main/resources/logback.xml b/spring-rest-logging/src/main/resources/logback.xml deleted file mode 100644 index 08117752c7..0000000000 --- a/spring-rest-logging/src/main/resources/logback.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - web - %date [%thread] %-5level %logger{36} - %message%n - - - - - - - - - - - - - - - - \ No newline at end of file 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 deleted file mode 100644 index ed3cdda7ad..0000000000 --- a/spring-rest-logging/src/test/java/com/baeldung/rest/log/controller/TestTaxiFareController.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.rest.log.controller; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; - -import org.junit.Test; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; - -import com.baeldung.rest.log.data.TaxiRide; - -public class TestTaxiFareController { - - private static final String URL = "http://localhost:" + 9090 + "/rest-log/taxifare/"; - - @Test - 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 givenTaxiRide_thenGetCalculatedFare() { - TestRestTemplate testRestTemplate = new TestRestTemplate(); - TaxiRide taxiRide = new TaxiRide(true,10l); - String fare = testRestTemplate.postForObject(URL + "calculate/", taxiRide,String.class); - assertThat(fare, equalTo("200")); - } - - -} From 7fdab0e191fb709e4481198caad1ad73baba9479 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Tue, 25 Jul 2017 15:42:57 +0100 Subject: [PATCH 7/7] BAEL-554 - Logging Spring Web Request --- .../web/log/test/TestTaxiFareController.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spring-rest/src/test/java/com/baeldung/web/log/test/TestTaxiFareController.java b/spring-rest/src/test/java/com/baeldung/web/log/test/TestTaxiFareController.java index 935275983f..398e3c04e9 100644 --- a/spring-rest/src/test/java/com/baeldung/web/log/test/TestTaxiFareController.java +++ b/spring-rest/src/test/java/com/baeldung/web/log/test/TestTaxiFareController.java @@ -13,21 +13,21 @@ import com.baeldung.web.log.data.TaxiRide; public class TestTaxiFareController { private static final String URL = "http://localhost:" + 8082 + "/spring-rest/taxifare/"; - + @Test - public void givenRequest_thenfetchTaxiFareRateCard() { + public void givenRequest_whenFetchTaxiFareRateCard_thanOK() { TestRestTemplate testRestTemplate = new TestRestTemplate(); ResponseEntity response = testRestTemplate.getForEntity(URL + "get/", String.class); + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); } - + @Test - public void givenTaxiRide_thenGetCalculatedFare() { + public void givenTaxiRide_whenCalculatedFare_thanStatus200() { TestRestTemplate testRestTemplate = new TestRestTemplate(); - TaxiRide taxiRide = new TaxiRide(true,10l); - String fare = testRestTemplate.postForObject(URL + "calculate/", taxiRide,String.class); + TaxiRide taxiRide = new TaxiRide(true, 10l); + String fare = testRestTemplate.postForObject(URL + "calculate/", taxiRide, String.class); + assertThat(fare, equalTo("200")); } - - }