deleted the new module
This commit is contained in:
parent
29b0da9960
commit
553c26c1a2
2
pom.xml
2
pom.xml
|
@ -208,7 +208,7 @@
|
||||||
<module>spring-zuul</module>
|
<module>spring-zuul</module>
|
||||||
<module>spring-reactor</module>
|
<module>spring-reactor</module>
|
||||||
<module>spring-vertx</module>
|
<module>spring-vertx</module>
|
||||||
<module>spring-rest-logging</module>
|
|
||||||
|
|
||||||
<module>testing</module>
|
<module>testing</module>
|
||||||
<module>testng</module>
|
<module>testng</module>
|
||||||
|
|
|
@ -1,73 +0,0 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<groupId>com.baeldung</groupId>
|
|
||||||
<artifactId>spring-rest-logging</artifactId>
|
|
||||||
<version>0.1-SNAPSHOT</version>
|
|
||||||
<name>spring-rest-logging</name>
|
|
||||||
<packaging>war</packaging>
|
|
||||||
|
|
||||||
<parent>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
|
||||||
<version>1.4.3.RELEASE</version>
|
|
||||||
</parent>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<!-- Spring Boot Dependencies -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Test -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-test</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Spring -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-web</artifactId>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-logging</artifactId>
|
|
||||||
<groupId>commons-logging</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-webmvc</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- web -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.servlet</groupId>
|
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-test</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Apache common IO for utility -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>commons-io</groupId>
|
|
||||||
<artifactId>commons-io</artifactId>
|
|
||||||
<version>2.4</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
</project>
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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/**/");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,2 +0,0 @@
|
||||||
server.port= 9090
|
|
||||||
server.context-path=/rest-log
|
|
|
@ -1,21 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<configuration>
|
|
||||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
|
||||||
<encoder>
|
|
||||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
|
||||||
</pattern>
|
|
||||||
</encoder>
|
|
||||||
</appender>
|
|
||||||
|
|
||||||
<logger name="org.springframework">
|
|
||||||
<level value="ERROR" />
|
|
||||||
</logger>
|
|
||||||
|
|
||||||
<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter">
|
|
||||||
<level value="DEBUG" />
|
|
||||||
</logger>
|
|
||||||
|
|
||||||
<root level="DEBUG">
|
|
||||||
<appender-ref ref="STDOUT" />
|
|
||||||
</root>
|
|
||||||
</configuration>
|
|
|
@ -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<String> 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"));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue