Skip to content

Commit 29b0da9

Browse files
author
mokhan
committed
Web Logging code included in exisitng module namely "spring-rest"
1 parent 6c10596 commit 29b0da9

12 files changed

Lines changed: 626 additions & 318 deletions

File tree

‎spring-rest/pom.xml‎

Lines changed: 326 additions & 318 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.web.log.app;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.web.support.SpringBootServletInitializer;
7+
import org.springframework.context.annotation.ComponentScan;
8+
9+
@EnableAutoConfiguration
10+
@ComponentScan("com.baeldung.web.log")
11+
@SpringBootApplication
12+
public class Application extends SpringBootServletInitializer {
13+
14+
public static void main(final String[] args) {
15+
SpringApplication.run(Application.class, args);
16+
}
17+
18+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.web.log.app;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.stereotype.Component;
9+
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
10+
import org.springframework.web.util.ContentCachingRequestWrapper;
11+
12+
import com.baeldung.web.log.util.RequestLoggingUtil;
13+
14+
@Component
15+
public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter {
16+
17+
Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class);
18+
19+
@Override
20+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
21+
String postData = null;
22+
HttpServletRequest requestCacheWrapperObject = null;
23+
try {
24+
// Uncomment to produce the stream closed issue
25+
// postData = RequestLoggingUtil.getStringFromInputStream(request.getInputStream());
26+
27+
// To overcome request stream closed issue
28+
requestCacheWrapperObject = new ContentCachingRequestWrapper(request);
29+
requestCacheWrapperObject.getParameterMap();
30+
} catch (Exception exception) {
31+
exception.printStackTrace();
32+
} finally {
33+
postData = RequestLoggingUtil.readPayload(requestCacheWrapperObject);
34+
LOGGER.info("REQUEST DATA: " + postData);
35+
}
36+
return true;
37+
}
38+
39+
@Override
40+
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
41+
LOGGER.info("RESPONSE: " + response.getStatus());
42+
}
43+
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.web.log.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.filter.CommonsRequestLoggingFilter;
6+
7+
@Configuration
8+
public class RequestLoggingFilterConfig {
9+
10+
@Bean
11+
public CommonsRequestLoggingFilter logFilter() {
12+
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
13+
filter.setIncludeQueryString(true);
14+
filter.setIncludePayload(true);
15+
filter.setMaxPayloadLength(10000);
16+
filter.setIncludeHeaders(false);
17+
filter.setAfterMessagePrefix("REQUEST DATA : ");
18+
return filter;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.web.log.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
7+
8+
import com.baeldung.web.log.app.TaxiFareRequestInterceptor;
9+
10+
@Configuration
11+
public class TaxiFareMVCConfig extends WebMvcConfigurerAdapter {
12+
13+
@Autowired
14+
private TaxiFareRequestInterceptor taxiFareRequestInterceptor;
15+
16+
@Override
17+
public void addInterceptors(InterceptorRegistry registry) {
18+
registry.addInterceptor(taxiFareRequestInterceptor).addPathPatterns("/**/taxifare/**/");
19+
}
20+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.web.log.controller;
2+
3+
import javax.validation.Valid;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.web.bind.annotation.RequestBody;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.bind.annotation.ResponseBody;
13+
14+
import com.baeldung.web.log.data.RateCard;
15+
import com.baeldung.web.log.data.TaxiRide;
16+
import com.baeldung.web.log.service.TaxiFareCalculatorService;
17+
18+
@Controller
19+
public class TaxiFareController {
20+
21+
@Autowired
22+
private TaxiFareCalculatorService taxiFareCalculatorService;
23+
24+
private static final Logger LOGGER = LoggerFactory.getLogger(TaxiFareController.class);
25+
26+
@RequestMapping(method = RequestMethod.GET, value = "/taxifare/get/")
27+
@ResponseBody
28+
public RateCard getTaxiFare() {
29+
LOGGER.debug("getTaxiFare() - START");
30+
return new RateCard();
31+
}
32+
33+
@RequestMapping(method = RequestMethod.POST, value = "/taxifare/calculate/")
34+
@ResponseBody
35+
public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) {
36+
LOGGER.debug("calculateTaxiFare() - START");
37+
String totalFare = taxiFareCalculatorService.calculateFare(taxiRide);
38+
LOGGER.debug("calculateTaxiFare() - Total Fare : {}",totalFare);
39+
LOGGER.debug("calculateTaxiFare() - END");
40+
return totalFare;
41+
}
42+
43+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.web.log.data;
2+
3+
public class RateCard {
4+
5+
private String nightSurcharge;
6+
private String ratePerMile;
7+
8+
public RateCard(){
9+
nightSurcharge="Extra $ 100";
10+
ratePerMile="$ 10 Per Mile";
11+
}
12+
13+
14+
public String getNightSurcharge() {
15+
return nightSurcharge;
16+
}
17+
public void setNightSurcharge(String nightSurcharge) {
18+
this.nightSurcharge = nightSurcharge;
19+
}
20+
public String getRatePerMile() {
21+
return ratePerMile;
22+
}
23+
public void setRatePerMile(String ratePerMile) {
24+
this.ratePerMile = ratePerMile;
25+
}
26+
27+
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.web.log.data;
2+
3+
public class TaxiRide {
4+
5+
private Boolean isNightSurcharge;
6+
private Long distanceInMile;
7+
8+
public TaxiRide(){}
9+
10+
public TaxiRide(Boolean isNightSurcharge, Long distanceInMile){
11+
this.isNightSurcharge = isNightSurcharge;
12+
this.distanceInMile = distanceInMile;
13+
}
14+
15+
16+
public Boolean getIsNightSurcharge() {
17+
return isNightSurcharge;
18+
}
19+
20+
public void setIsNightSurcharge(Boolean isNightSurcharge) {
21+
this.isNightSurcharge = isNightSurcharge;
22+
}
23+
24+
public Long getDistanceInMile() {
25+
return distanceInMile;
26+
}
27+
28+
public void setDistanceInMile(Long distanceInMile) {
29+
this.distanceInMile = distanceInMile;
30+
}
31+
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.web.log.service;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
import com.baeldung.web.log.data.TaxiRide;
6+
7+
@Service
8+
public class TaxiFareCalculatorService {
9+
10+
public String calculateFare(TaxiRide taxiRide) {
11+
Long fare = 0l;
12+
if (taxiRide.getIsNightSurcharge()) {
13+
fare = taxiRide.getDistanceInMile() * 10 + 100;
14+
} else {
15+
fare = taxiRide.getDistanceInMile() * 10;
16+
}
17+
return String.valueOf(fare);
18+
}
19+
20+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.web.log.util;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.StringWriter;
6+
7+
import javax.servlet.http.HttpServletRequest;
8+
9+
import org.apache.commons.io.IOUtils;
10+
import org.springframework.web.util.ContentCachingRequestWrapper;
11+
import org.springframework.web.util.WebUtils;
12+
13+
public class RequestLoggingUtil {
14+
15+
public static String getStringFromInputStream(InputStream is) {
16+
StringWriter writer = new StringWriter();
17+
String encoding = "UTF-8";
18+
try {
19+
IOUtils.copy(is, writer, encoding);
20+
} catch (IOException e) {
21+
e.printStackTrace();
22+
}
23+
return writer.toString();
24+
}
25+
26+
public static String readPayload(final HttpServletRequest request) throws IOException {
27+
String payloadData = null;
28+
ContentCachingRequestWrapper contentCachingRequestWrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
29+
if (null != contentCachingRequestWrapper) {
30+
byte[] buf = contentCachingRequestWrapper.getContentAsByteArray();
31+
if (buf.length > 0) {
32+
payloadData = new String(buf, 0, buf.length, contentCachingRequestWrapper.getCharacterEncoding());
33+
}
34+
}
35+
return payloadData;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)