mirror of
https://github.com/valitydev/adapter-bank-payout-spring-boot-starter.git
synced 2024-11-06 00:45:21 +00:00
Refactoring
This commit is contained in:
parent
8e458169bd
commit
e13741ac42
31
pom.xml
31
pom.xml
@ -5,7 +5,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.rbkmoney</groupId>
|
||||
<artifactId>adapter-bank-payout-spring-boot-starter</artifactId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
<version>0.0.3-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Adapter-bank-payout-spring-boot-starter</name>
|
||||
@ -14,7 +14,7 @@
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>8</java.version>
|
||||
<spring-boot.version>2.1.0.RELEASE</spring-boot.version>
|
||||
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
|
||||
<damsel-utils.version>2.1.9</damsel-utils.version>
|
||||
<damsel.version>1.256-2afe121</damsel.version>
|
||||
<serializer.version>0.6.7</serializer.version>
|
||||
@ -71,14 +71,13 @@
|
||||
<!--rbk-->
|
||||
<dependency>
|
||||
<groupId>com.rbkmoney</groupId>
|
||||
<artifactId>error-mapping-java</artifactId>
|
||||
<version>${error-mapping.version}</version>
|
||||
<scope>provided</scope>
|
||||
<artifactId>adapter-common-lib</artifactId>
|
||||
<version>0.0.4-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.rbkmoney.woody</groupId>
|
||||
<artifactId>woody-thrift</artifactId>
|
||||
<version>1.1.15</version>
|
||||
<groupId>com.rbkmoney</groupId>
|
||||
<artifactId>error-mapping-java</artifactId>
|
||||
<version>${error-mapping.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@ -87,22 +86,6 @@
|
||||
<version>${damsel.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.rbkmoney</groupId>
|
||||
<artifactId>damsel-utils</artifactId>
|
||||
<version>${damsel-utils.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.rbkmoney.geck</groupId>
|
||||
<artifactId>serializer</artifactId>
|
||||
<version>${serializer.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.rbkmoney.proxy-libs</groupId>
|
||||
<artifactId>hellgate-adapter-client</artifactId>
|
||||
<version>${hellgate-adapter-client.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test -->
|
||||
<dependency>
|
||||
|
@ -1,7 +1,8 @@
|
||||
package com.rbkmoney.adapter.bank.payout.spring.boot.starter.config;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.rbkmoney.adapter.common.mapper.SimpleErrorMapping;
|
||||
import com.rbkmoney.adapter.common.mapper.SimpleObjectMapper;
|
||||
import com.rbkmoney.error.mapping.ErrorMapping;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@ -10,23 +11,25 @@ import org.springframework.core.io.Resource;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class ErrorMappingConfiguration {
|
||||
public class AppConfig {
|
||||
|
||||
@Value("${error-mapping.file}")
|
||||
private Resource filePath;
|
||||
private Resource errorMappingFilePath;
|
||||
|
||||
@Value("${error-mapping.patternReason:\"'%s' - '%s'\"}")
|
||||
private String patternReason;
|
||||
private String errorMappingPattern;
|
||||
|
||||
@Bean
|
||||
ErrorMapping errorMapping() throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
|
||||
|
||||
ErrorMapping errorMapping = new ErrorMapping(filePath.getInputStream(), patternReason, mapper);
|
||||
errorMapping.validateMappingFormat();
|
||||
return errorMapping;
|
||||
public ErrorMapping errorMapping() throws IOException {
|
||||
return new SimpleErrorMapping(errorMappingFilePath, errorMappingPattern).getErrorMapping();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ObjectMapper objectMapper(){
|
||||
return new SimpleObjectMapper().getSimpleObjectMapper();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
package com.rbkmoney.adapter.bank.payout.spring.boot.starter.config;
|
||||
|
||||
import com.rbkmoney.woody.api.flow.WFlow;
|
||||
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 javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@Configuration
|
||||
public class FilterConfiguration {
|
||||
|
||||
public static final String HEALTH = "/actuator/health";
|
||||
|
||||
@Value("${server.rest.port}")
|
||||
private int restPort;
|
||||
|
||||
@Value("/${server.rest.endpoint}/")
|
||||
private String restEndpoint;
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean externalPortRestrictingFilter() {
|
||||
Filter filter = new OncePerRequestFilter() {
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||
FilterChain filterChain) throws ServletException, IOException {
|
||||
String servletPath = request.getServletPath();
|
||||
if ((request.getLocalPort() == restPort)
|
||||
&& !(servletPath.startsWith(restEndpoint) || servletPath.startsWith(HEALTH))) {
|
||||
response.sendError(404, "Unknown address");
|
||||
return;
|
||||
}
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
};
|
||||
|
||||
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
|
||||
filterRegistrationBean.setFilter(filter);
|
||||
filterRegistrationBean.setOrder(-100);
|
||||
filterRegistrationBean.setName("httpPortFilter");
|
||||
filterRegistrationBean.addUrlPatterns("/*");
|
||||
return filterRegistrationBean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean woodyFilter() {
|
||||
WFlow wFlow = new WFlow();
|
||||
Filter 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;
|
||||
}
|
||||
};
|
||||
|
||||
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
|
||||
filterRegistrationBean.setFilter(filter);
|
||||
filterRegistrationBean.setOrder(-50);
|
||||
filterRegistrationBean.setName("woodyFilter");
|
||||
filterRegistrationBean.addUrlPatterns(restEndpoint + "*");
|
||||
return filterRegistrationBean;
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.rbkmoney.adapter.bank.payout.spring.boot.starter.config.properties;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Configuration
|
||||
@ConfigurationProperties("adapter")
|
||||
@Validated
|
||||
public class AdapterProperties {
|
||||
|
||||
@NotEmpty
|
||||
private String url;
|
||||
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.rbkmoney.adapter.bank.payout.spring.boot.starter.config.properties;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties("time.config")
|
||||
@Validated
|
||||
@Getter
|
||||
@Setter
|
||||
public class TimerProperties {
|
||||
|
||||
@NotNull
|
||||
private int redirectTimeout;
|
||||
|
||||
@NotNull
|
||||
private int maxTimePolling;
|
||||
|
||||
@NotNull
|
||||
private int pollingDelay;
|
||||
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package com.rbkmoney.adapter.bank.payout.spring.boot.starter.utils;
|
||||
|
||||
import com.rbkmoney.damsel.cds.ExpDate;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class CardDateConverter {
|
||||
|
||||
public static String getYearFromExpDate(ExpDate expDate) {
|
||||
return String.format("%1$02d", expDate.getYear() % 100);
|
||||
}
|
||||
|
||||
public static String getYearFromShort(short year) {
|
||||
return String.format("%1$02d", year % 100);
|
||||
}
|
||||
|
||||
public static String getMonthFromExpDate(ExpDate expDate) {
|
||||
return String.format("%02d", expDate.getMonth());
|
||||
}
|
||||
|
||||
public static String getMonthFromByte(byte month) {
|
||||
return String.format("%02d", month);
|
||||
}
|
||||
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
package com.rbkmoney.adapter.bank.payout.spring.boot.starter.utils;
|
||||
|
||||
import org.apache.commons.codec.binary.Hex;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class SignGenerator {
|
||||
|
||||
public static String prepareDataForHmac(String[] fields, MultiValueMap<String, String> params) {
|
||||
StringBuilder dataHmac = new StringBuilder();
|
||||
Arrays.asList(fields)
|
||||
.forEach(field -> {
|
||||
List<String> fieldParameters = params.get(field);
|
||||
if (fieldParameters != null
|
||||
&& !fieldParameters.isEmpty()
|
||||
&& fieldParameters.get(0) != null
|
||||
&& !fieldParameters.get(0).isEmpty()
|
||||
) {
|
||||
dataHmac.append(fieldParameters.get(0).length());
|
||||
dataHmac.append(fieldParameters.get(0));
|
||||
} else {
|
||||
dataHmac.append("-");
|
||||
}
|
||||
}
|
||||
);
|
||||
return dataHmac.toString();
|
||||
}
|
||||
|
||||
public static String sign(String[] fieldsForSign, MultiValueMap<String, String> params, String key, String algorithm) {
|
||||
String dataHmac = prepareDataForHmac(fieldsForSign, params);
|
||||
String pSign = sign(dataHmac, key, algorithm);
|
||||
return pSign.toUpperCase();
|
||||
}
|
||||
|
||||
public static String sign(String data, String key, String algorithm) {
|
||||
try {
|
||||
byte[] decodedKey = Hex.decodeHex(key.toCharArray());
|
||||
SecretKeySpec keySpec = new SecretKeySpec(decodedKey, algorithm);
|
||||
Mac mac = Mac.getInstance(algorithm);
|
||||
mac.init(keySpec);
|
||||
|
||||
byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] signatureBytes = mac.doFinal(dataBytes);
|
||||
|
||||
return new String(new Hex().encode(signatureBytes));
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user