Removed redundent clases
This commit is contained in:
parent
1e9cc72570
commit
bbec00c4cd
|
@ -1,17 +0,0 @@
|
||||||
package com.baeldung.exclude_urls_filter;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
@ComponentScan(basePackages = "com.baeldung.exclude_urls_filter")
|
|
||||||
@Configuration
|
|
||||||
@SpringBootApplication
|
|
||||||
public class Application {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(Application.class, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,32 +0,0 @@
|
||||||
package com.baeldung.exclude_urls_filter.controller;
|
|
||||||
|
|
||||||
import com.baeldung.exclude_urls_filter.service.FAQService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
public class FAQController {
|
|
||||||
|
|
||||||
private final FAQService faqService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public FAQController(FAQService faqService) {
|
|
||||||
this.faqService = faqService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "/faq/helpline", method = RequestMethod.GET)
|
|
||||||
public ResponseEntity<String> getHelpLineNumber() {
|
|
||||||
String helplineNumber = faqService.getHelpLineNumber();
|
|
||||||
if (helplineNumber != null) {
|
|
||||||
return new ResponseEntity<String>(helplineNumber, HttpStatus.OK);
|
|
||||||
} else {
|
|
||||||
return new ResponseEntity<String>("Unavailable", HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,22 +0,0 @@
|
||||||
package com.baeldung.exclude_urls_filter.controller;
|
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
public class Ping {
|
|
||||||
|
|
||||||
@RequestMapping(value = "/health", method = RequestMethod.GET)
|
|
||||||
public ResponseEntity<String> pingGet() {
|
|
||||||
return new ResponseEntity<String>("pong", HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "/health", method = RequestMethod.POST)
|
|
||||||
public ResponseEntity<String> pingPost() {
|
|
||||||
return new ResponseEntity<String>("pong", HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,26 +0,0 @@
|
||||||
package com.baeldung.exclude_urls_filter.filter;
|
|
||||||
|
|
||||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class FilterRegistrationConfig {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public FilterRegistrationBean<LogFilter> logFilter() {
|
|
||||||
FilterRegistrationBean<LogFilter> registrationBean = new FilterRegistrationBean<>();
|
|
||||||
registrationBean.setFilter(new LogFilter());
|
|
||||||
registrationBean.addUrlPatterns("/health", "/faq/*");
|
|
||||||
return registrationBean;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public FilterRegistrationBean<HeaderValidatorFilter> headerValidatorFilter() {
|
|
||||||
FilterRegistrationBean<HeaderValidatorFilter> registrationBean = new FilterRegistrationBean<>();
|
|
||||||
registrationBean.setFilter(new HeaderValidatorFilter());
|
|
||||||
registrationBean.addUrlPatterns("*");
|
|
||||||
return registrationBean;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,33 +0,0 @@
|
||||||
package com.baeldung.exclude_urls_filter.filter;
|
|
||||||
|
|
||||||
import org.springframework.core.annotation.Order;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
|
||||||
|
|
||||||
import javax.servlet.*;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
@Order(1)
|
|
||||||
public class HeaderValidatorFilter extends OncePerRequestFilter {
|
|
||||||
@Override
|
|
||||||
protected void doFilterInternal(HttpServletRequest request,
|
|
||||||
HttpServletResponse response,
|
|
||||||
FilterChain filterChain)
|
|
||||||
throws ServletException,
|
|
||||||
IOException {
|
|
||||||
String countryCode = request.getHeader("X-Country-Code");
|
|
||||||
if (!"US".equals(countryCode)) {
|
|
||||||
response.sendError(HttpStatus.BAD_REQUEST.value(), "Invalid Locale");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
filterChain.doFilter(request, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
|
|
||||||
String path = request.getRequestURI();
|
|
||||||
return "/health".equals(path);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,25 +0,0 @@
|
||||||
package com.baeldung.exclude_urls_filter.filter;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.core.annotation.Order;
|
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
|
||||||
|
|
||||||
import javax.servlet.*;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
@Order(1)
|
|
||||||
public class LogFilter extends OncePerRequestFilter {
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(LogFilter.class);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
|
||||||
FilterChain filterChain) throws ServletException, IOException {
|
|
||||||
String path = request.getRequestURI();
|
|
||||||
String contentType = request.getContentType();
|
|
||||||
logger.info("Request URL path : {}, Request content type: {}", path, contentType);
|
|
||||||
filterChain.doFilter(request, response);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +0,0 @@
|
||||||
package com.baeldung.exclude_urls_filter.service;
|
|
||||||
|
|
||||||
public interface FAQService {
|
|
||||||
String getHelpLineNumber();
|
|
||||||
}
|
|
|
@ -1,15 +0,0 @@
|
||||||
package com.baeldung.exclude_urls_filter.service;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class FAQServiceImpl implements FAQService {
|
|
||||||
|
|
||||||
private static final String HELPLINE_NUMBER = "+1 888-777-66";
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getHelpLineNumber() {
|
|
||||||
return HELPLINE_NUMBER;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
package com.baeldung.form_submission;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
public class Application extends SpringBootServletInitializer {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(Application.class, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,42 +0,0 @@
|
||||||
package com.baeldung.form_submission.controllers;
|
|
||||||
|
|
||||||
import com.baeldung.form_submission.model.Feedback;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.ui.Model;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
@Controller
|
|
||||||
public class FeedbackForm {
|
|
||||||
|
|
||||||
@GetMapping(path = "/feedback")
|
|
||||||
public String getFeedbackForm(Model model) {
|
|
||||||
Feedback feedback = new Feedback();
|
|
||||||
model.addAttribute("feedback", feedback);
|
|
||||||
return "feedback";
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping(
|
|
||||||
path = "/web/feedback",
|
|
||||||
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
|
|
||||||
public String handleBrowserSubmissions(Feedback feedback) throws Exception {
|
|
||||||
// Save feedback data
|
|
||||||
return "redirect:/feedback/success";
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/feedback/success")
|
|
||||||
public ResponseEntity<String> getSuccess() {
|
|
||||||
return new ResponseEntity<String>("Thank you for submitting feedback.", HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping(
|
|
||||||
path = "/feedback",
|
|
||||||
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
|
|
||||||
public ResponseEntity<String> handleNonBrowserSubmissions(@RequestParam MultiValueMap paramMap) throws Exception {
|
|
||||||
// Save feedback data
|
|
||||||
return new ResponseEntity<String>("Thank you for submitting feedback", HttpStatus.OK);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,23 +0,0 @@
|
||||||
package com.baeldung.form_submission.model;
|
|
||||||
|
|
||||||
public class Feedback {
|
|
||||||
private String emailId;
|
|
||||||
private String comment;
|
|
||||||
|
|
||||||
public String getEmailId() {
|
|
||||||
return this.emailId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEmailId(String emailId) {
|
|
||||||
this.emailId = emailId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getComment() {
|
|
||||||
return this.comment;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setComment(String comment) {
|
|
||||||
this.comment = comment;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
package com.baeldung.spring.slash;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
public class Application implements WebMvcConfigurer {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(Application.class, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,31 +0,0 @@
|
||||||
package com.baeldung.spring.slash;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("slash")
|
|
||||||
public class SlashParsingController {
|
|
||||||
|
|
||||||
@GetMapping("mypaths/{anything}")
|
|
||||||
public String pathVariable(@PathVariable("anything") String anything) {
|
|
||||||
return anything;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("all/**")
|
|
||||||
public String allDirectories(HttpServletRequest request) {
|
|
||||||
return request.getRequestURI()
|
|
||||||
.split(request.getContextPath() + "/all/")[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("all")
|
|
||||||
public String queryParameter(@RequestParam("param") String param) {
|
|
||||||
return param;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,87 +0,0 @@
|
||||||
package com.baeldung.spring.slash;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
|
||||||
import org.springframework.test.web.servlet.MvcResult;
|
|
||||||
|
|
||||||
@AutoConfigureMockMvc
|
|
||||||
@ExtendWith(SpringExtension.class)
|
|
||||||
@SpringBootTest
|
|
||||||
public class SlashParsingControllerIntTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private MockMvc mockMvc;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenUsingPathVariablemWithoutSlashes_thenStatusOk() throws Exception {
|
|
||||||
final String stringWithoutSlashes = "noslash";
|
|
||||||
|
|
||||||
MvcResult mvcResult = mockMvc.perform(get("/slash/mypaths/" + stringWithoutSlashes))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andReturn();
|
|
||||||
|
|
||||||
assertEquals(stringWithoutSlashes, mvcResult.getResponse()
|
|
||||||
.getContentAsString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenUsingPathVariableWithSlashes_thenStatusNotFound() throws Exception {
|
|
||||||
final String stringWithSlashes = "url/with/slashes";
|
|
||||||
|
|
||||||
mockMvc.perform(get("/slash/mypaths/" + stringWithSlashes))
|
|
||||||
.andExpect(status().isNotFound());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenAllFallbackEndpoint_whenUsingPathWithSlashes_thenStatusOk() throws Exception {
|
|
||||||
final String stringWithSlashes = "url/for/testing/purposes";
|
|
||||||
|
|
||||||
MvcResult mvcResult = mockMvc.perform(get("/slash/all/" + stringWithSlashes))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andReturn();
|
|
||||||
|
|
||||||
assertEquals(stringWithSlashes, mvcResult.getResponse()
|
|
||||||
.getContentAsString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenAllFallbackEndpoint_whenUsingConsecutiveSlashes_thenPathNormalized() throws Exception {
|
|
||||||
final String stringWithSlashes = "http://myurl.com";
|
|
||||||
|
|
||||||
MvcResult mvcResult = mockMvc.perform(get("/slash/all/" + stringWithSlashes))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andReturn();
|
|
||||||
|
|
||||||
String stringWithSlashesNormalized = URI.create("/slash/all/" + stringWithSlashes)
|
|
||||||
.normalize()
|
|
||||||
.toString()
|
|
||||||
.split("/slash/all/")[1];
|
|
||||||
|
|
||||||
assertEquals(stringWithSlashesNormalized, mvcResult.getResponse()
|
|
||||||
.getContentAsString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenUsingSlashesInQueryParam_thenParameterAccepted() throws Exception {
|
|
||||||
final String stringWithSlashes = "url/for////testing/purposes";
|
|
||||||
|
|
||||||
MvcResult mvcResult = mockMvc.perform(get("/slash/all").param("param", stringWithSlashes))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andReturn();
|
|
||||||
|
|
||||||
assertEquals(stringWithSlashes, mvcResult.getResponse()
|
|
||||||
.getContentAsString());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue