Move some from adapter

This commit is contained in:
k.struzhkin 2019-05-28 18:42:14 +03:00
parent 465a70afe5
commit f2bf9dd830
10 changed files with 239 additions and 3 deletions

View File

@ -16,6 +16,7 @@
<damsel-utils.version>2.1.9</damsel-utils.version>
<damsel.version>1.256-2afe121</damsel.version>
<serializer.version>0.6.7</serializer.version>
<hellgate-adapter-client.version>2.1.9</hellgate-adapter-client.version>
</properties>
<dependencies>
@ -94,6 +95,12 @@
<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>
<groupId>junit</groupId>

View File

@ -0,0 +1,87 @@
package com.rbkmoney.adapter.bank.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;
}
}

View File

@ -0,0 +1,33 @@
package com.rbkmoney.adapter.bank.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;
@NotEmpty
private String callbackUrl;
@NotEmpty
private String pathCallbackUrl;
@NotEmpty
private String pathRecurrentCallbackUrl;
@NotEmpty
private String tagPrefix;
}

View File

@ -0,0 +1,64 @@
package com.rbkmoney.adapter.bank.spring.boot.starter.controller;
import com.rbkmoney.adapter.bank.spring.boot.starter.model.Callback;
import com.rbkmoney.adapter.bank.spring.boot.starter.serializer.CallbackSerializer;
import com.rbkmoney.adapter.helpers.hellgate.HellgateAdapterClient;
import com.rbkmoney.adapter.helpers.hellgate.exception.HellgateException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.function.BiFunction;
/**
* Handler callback 3DS
*/
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/${server.rest.endpoint}")
public class AdapterController {
private final HellgateAdapterClient hgClient;
private final CallbackSerializer callbackSerializer;
@PostMapping(value = "term_url")
public String receivePaymentIncomingParameters(HttpServletRequest request, HttpServletResponse servletResponse) throws IOException {
return processCallback(request, servletResponse, hgClient::processPaymentCallback);
}
@PostMapping(value = "rec_term_url")
public String receiveRecurrentIncomingParameters(HttpServletRequest request, HttpServletResponse servletResponse) throws IOException {
return processCallback(request, servletResponse, hgClient::processRecurrentTokenCallback);
}
private String processCallback(HttpServletRequest servletRequest, HttpServletResponse servletResponse, BiFunction<String, ByteBuffer, ByteBuffer> hgFunction) throws IOException {
String resp = "";
Callback callbackObj = callbackSerializer.read(servletRequest);
log.info("ProcessCallback {}", callbackObj);
try {
String tag = callbackObj.getMd();
ByteBuffer callback = ByteBuffer.wrap(callbackSerializer.writeByte(callbackObj));
ByteBuffer response = hgFunction.apply(tag, callback);
resp = new String(response.array(), StandardCharsets.UTF_8);
} catch (HellgateException e) {
log.warn("Failed handle callback for recurrent", e);
} catch (Exception e) {
log.error("Failed handle callback for recurrent", e);
}
if (StringUtils.hasText(callbackObj.getTermUrl())) {
servletResponse.sendRedirect(callbackObj.getTermUrl());
}
log.info("ProcessCallback response {}", resp);
return resp;
}
}

View File

@ -29,7 +29,7 @@ public class DefaultStepResolverImpl implements StepResolver<StateModel, ExitSta
return Step.FINISH_THREE_DS;
} else if (stateModel.isMakeRecurrent()) {
return Step.AUTH_RECURRENT;
} else if (stateModel.getRecurrentContext() != null && stateModel.getRecurrentContext().getRecToken() != null) {
} else if (stateModel.getRecToken() != null && !stateModel.getRecToken().isEmpty()) {
return Step.RECURRENT;
}
return Step.AUTH;

View File

@ -1,6 +1,5 @@
package com.rbkmoney.adapter.bank.spring.boot.starter.model;
import com.rbkmoney.damsel.cds.ExpDate;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@ -24,9 +23,13 @@ public class OperationModel {
@ToString.Exclude
private String cardHolder;
private String cryptogram;
private String eci;
private Long amount;
private Short currencyCode;
private String currencySymbolCode;
private String createdAt;
private String ip;
private String email;

View File

@ -11,7 +11,6 @@ public class StateModel {
private String recToken;
private TargetStatus targetStatus;
private boolean makeRecurrent;
private RecurrentContext recurrentContext;
private AdapterContext adapterContext;
private Step step;

View File

@ -0,0 +1,11 @@
package com.rbkmoney.adapter.bank.spring.boot.starter.service;
import com.rbkmoney.adapter.bank.spring.boot.starter.model.ExitStateModel;
import java.util.Map;
public interface ThreeDsPropertiesService {
Map<String, String> initProperties(ExitStateModel exitStateModel);
}

View File

@ -0,0 +1,28 @@
package com.rbkmoney.adapter.bank.spring.boot.starter.service;
import com.rbkmoney.adapter.bank.spring.boot.starter.config.properties.AdapterProperties;
import com.rbkmoney.adapter.bank.spring.boot.starter.constants.ThreeDsFields;
import com.rbkmoney.adapter.bank.spring.boot.starter.model.ExitStateModel;
import com.rbkmoney.adapter.bank.spring.boot.starter.utils.RedirectUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
@RequiredArgsConstructor
public class ThreeDsPropertiesServiceImpl implements ThreeDsPropertiesService {
private final AdapterProperties adapterProperties;
@Override
public Map<String, String> initProperties(ExitStateModel exitStateModel) {
Map<String, String> params = new HashMap<>();
params.put(ThreeDsFields.PA_REQ, exitStateModel.getPaReq());
params.put(ThreeDsFields.MD, exitStateModel.getMd());
params.put(ThreeDsFields.TERM_URL, RedirectUtils.getCallbackUrl(adapterProperties.getCallbackUrl(), adapterProperties.getPathCallbackUrl()));
return params;
}
}

View File

@ -1,8 +1,12 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.rbkmoney.adapter.bank.spring.boot.starter.config.ErrorMappingConfiguration,\
com.rbkmoney.adapter.bank.spring.boot.starter.config.properties.TimerProperties,\
com.rbkmoney.adapter.bank.spring.boot.starter.config.properties.AdapterProperties,\
com.rbkmoney.adapter.bank.spring.boot.starter.config.FilterConfiguration,\
com.rbkmoney.adapter.bank.spring.boot.starter.flow.TargetStatusResolver,\
com.rbkmoney.adapter.bank.spring.boot.starter.serializer.AdapterSerializer,\
com.rbkmoney.adapter.bank.spring.boot.starter.serializer.CallbackSerializer,\
com.rbkmoney.adapter.bank.spring.boot.starter.serializer.RecurrentContextSerializer,\
com.rbkmoney.adapter.bank.spring.boot.starter.controller.AdapterController,\
com.rbkmoney.adapter.bank.spring.boot.starter.service.ThreeDsPropertiesServiceImpl,\
com.rbkmoney.adapter.bank.spring.boot.starter.flow.DefaultStepResolverImpl