io.github.lyc8503.spring.starter.incantation.config.SnakeCaseApplicationConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opening-incantation-spring-boot-starter Show documentation
Show all versions of opening-incantation-spring-boot-starter Show documentation
Some magic incantation for Spring Boot
The newest version!
package io.github.lyc8503.spring.starter.incantation.config;
import com.google.common.base.CaseFormat;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.NonNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Configuration
public class SnakeCaseApplicationConfiguration {
@Bean
public OncePerRequestFilter snakeCaseConverterFilter() {
return new OncePerRequestFilter() {
@Override
protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull FilterChain filterChain) throws ServletException, IOException {
final Map parameters = new ConcurrentHashMap<>();
for (String param : request.getParameterMap().keySet()) {
String camelCaseParam = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, param);
parameters.put(camelCaseParam, request.getParameterValues(param));
parameters.put(param, request.getParameterValues(param));
}
filterChain.doFilter(new HttpServletRequestWrapper(request) {
@Override
public String getParameter(String name) {
return parameters.containsKey(name) ? parameters.get(name)[0] : null;
}
@Override
public Enumeration getParameterNames() {
return Collections.enumeration(parameters.keySet());
}
@Override
public String[] getParameterValues(String name) {
return parameters.get(name);
}
@Override
public Map getParameterMap() {
return parameters;
}
}, response);
}
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy