refactor webconfig

This commit is contained in:
Anatoly Karlov 2024-10-18 22:24:56 +07:00
parent fd684507a7
commit b934fe2569
4 changed files with 190 additions and 66 deletions

View File

@ -0,0 +1,94 @@
package dev.vality.disputes.config;
import dev.vality.woody.api.flow.WFlow;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
@Configuration
public class NetworkConfig {
@Value("${server.port}")
private int restPort;
@Value("${openapi.valityDisputes.base-path:/disputes}/")
private String restEndpoint;
public static final String HEALTH = "/actuator/health";
public static final String MERCHANT = "/disputes-api/v1/merchant";
public static final String MANUAL = "/disputes-api/v1/manual-parsing";
public static final String CALLBACK = "/disputes-api/v1/callback";
@Bean
public FilterRegistrationBean externalPortRestrictingFilter() {
var filter = new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
var servletPath = request.getServletPath();
var enabledPaths = servletPath.startsWith(restEndpoint)
|| servletPath.startsWith(HEALTH)
|| servletPath.startsWith(MERCHANT)
|| servletPath.startsWith(MANUAL)
|| servletPath.startsWith(CALLBACK);
if ((request.getLocalPort() == restPort) && !enabledPaths) {
response.sendError(404, "Unknown address");
return;
}
filterChain.doFilter(request, response);
}
};
var filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(filter);
filterRegistrationBean.setOrder(-100);
filterRegistrationBean.setName("httpPortFilter");
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
@Bean
@SuppressWarnings("LocalVariableName")
public FilterRegistrationBean woodyFilter() {
var wFlow = new WFlow();
var filter = new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
if ((request.getLocalPort() == restPort)
&& request.getServletPath().startsWith(restEndpoint)) {
wFlow.createServiceFork(() -> {
try {
filterChain.doFilter(request, response);
} catch (IOException | ServletException e) {
sneakyThrow(e);
}
}).run();
return;
}
filterChain.doFilter(request, response);
}
private <E extends Throwable, T> T sneakyThrow(Throwable t) throws E {
throw (E) t;
}
};
var filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(filter);
filterRegistrationBean.setOrder(-50);
filterRegistrationBean.setName("woodyFilter");
filterRegistrationBean.addUrlPatterns(restEndpoint + "*");
return filterRegistrationBean;
}
}

View File

@ -1,65 +0,0 @@
package dev.vality.disputes.config;
import dev.vality.woody.api.flow.WFlow;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.io.IOException;
@Configuration
@SuppressWarnings({"ParameterName", "LocalVariableName"})
public class WebConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*");
}
};
}
@Bean
public FilterRegistrationBean woodyFilter() {
var woodyFlow = new WFlow();
var filter = new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) {
woodyFlow.createServiceFork(
() -> {
try {
filterChain.doFilter(request, response);
} catch (IOException | ServletException e) {
sneakyThrow(e);
}
}
)
.run();
}
private <E extends Throwable, T> T sneakyThrow(Throwable t) throws E {
throw (E) t;
}
};
var filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(filter);
filterRegistrationBean.setOrder(-50);
filterRegistrationBean.setName("woodyFilter");
filterRegistrationBean.addUrlPatterns("*");
return filterRegistrationBean;
}
}

View File

@ -26,7 +26,7 @@ public class ManualParsingTopic {
if (!enabled) {
return;
}
var contextMap = MDC.getCopyOfContextMap();
var contextMap = MDC.getCopyOfContextMap() == null ? new HashMap<String, String>() : MDC.getCopyOfContextMap();
contextMap.put("dispute_id", dispute.getId().toString());
var attachmentsCollect = attachments.stream().map(Attachment::toString).collect(Collectors.joining(", "));
contextMap.put("dispute_attachments", attachmentsCollect);

View File

@ -0,0 +1,95 @@
package dev.vality.disputes.api;
import dev.vality.damsel.payment_processing.InvoicingSrv;
import dev.vality.disputes.admin.CancelParamsRequest;
import dev.vality.disputes.admin.ManualParsingServiceSrv;
import dev.vality.disputes.callback.DisputeCallbackParams;
import dev.vality.disputes.callback.ProviderDisputesCallbackServiceSrv;
import dev.vality.disputes.config.WireMockSpringBootITest;
import dev.vality.disputes.merchant.DisputeParams;
import dev.vality.disputes.merchant.MerchantDisputesServiceSrv;
import dev.vality.disputes.schedule.service.config.WiremockAddressesHolder;
import dev.vality.disputes.util.DamselUtil;
import dev.vality.woody.api.flow.error.WRuntimeException;
import dev.vality.woody.thrift.impl.http.THSpawnClientBuilder;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.TestPropertySource;
import java.net.URI;
import static dev.vality.disputes.config.NetworkConfig.*;
@WireMockSpringBootITest
@SuppressWarnings({"ParameterName", "LineLength"})
@Import(WiremockAddressesHolder.class)
@TestPropertySource(properties = {
"server.port=${local.server.port}",
})
public class ServletTest {
@MockBean
private InvoicingSrv.Iface invoicingClient;
@LocalServerPort
private int serverPort;
@Test
@SneakyThrows
public void merchantServletTest() {
var iface = new THSpawnClientBuilder()
.withAddress(new URI("http://127.0.0.1:" + serverPort + MERCHANT))
.withNetworkTimeout(5000)
.build(MerchantDisputesServiceSrv.Iface.class);
var request = DamselUtil.fillRequiredTBaseObject(
new DisputeParams(),
DisputeParams.class
);
Assertions.assertThrows(WRuntimeException.class, () -> iface.createDispute(request));
}
@Test
@SneakyThrows
public void manualServletTest() {
var iface = new THSpawnClientBuilder()
.withAddress(new URI("http://127.0.0.1:" + serverPort + MANUAL))
.withNetworkTimeout(5000)
.build(ManualParsingServiceSrv.Iface.class);
var request = DamselUtil.fillRequiredTBaseObject(
new CancelParamsRequest(),
CancelParamsRequest.class
);
iface.cancelPending(request);
}
@Test
@SneakyThrows
public void callbackServletTest() {
var iface = new THSpawnClientBuilder()
.withAddress(new URI("http://127.0.0.1:" + serverPort + CALLBACK))
.withNetworkTimeout(5000)
.build(ProviderDisputesCallbackServiceSrv.Iface.class);
var request = DamselUtil.fillRequiredTBaseObject(
new DisputeCallbackParams(),
DisputeCallbackParams.class
);
iface.changeStatus(request);
}
@Test
@SneakyThrows
public void wrongPathServletTest() {
var iface = new THSpawnClientBuilder()
.withAddress(new URI("http://127.0.0.1:" + serverPort + "/wrong_path"))
.withNetworkTimeout(5000)
.build(MerchantDisputesServiceSrv.Iface.class);
var request = DamselUtil.fillRequiredTBaseObject(
new DisputeParams(),
DisputeParams.class
);
Assertions.assertThrows(WRuntimeException.class, () -> iface.createDispute(request));
}
}