Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: aborted requests handling #129

Merged
merged 7 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
*******************************************************************************/
package iris.client_bff.events;

import static iris.client_bff.search_client.eps.LocationMapper.*;
import static iris.client_bff.search_client.eps.LocationMapper.map;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

import io.vavr.control.Option;
import iris.client_bff.events.EventDataRequest.DataRequestIdentifier;
import iris.client_bff.events.EventDataRequest.Status;
import iris.client_bff.events.eps.DataProviderClient;
Expand All @@ -28,7 +27,6 @@
import iris.client_bff.proxy.IRISAnnouncementException;
import iris.client_bff.proxy.ProxyServiceClient;
import iris.client_bff.search_client.SearchClient;
import iris.client_bff.search_client.eps.LocationMapper;
import iris.client_bff.search_client.exceptions.IRISSearchException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -37,7 +35,6 @@
import java.util.Optional;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -148,6 +145,14 @@ public EventDataRequest update(EventDataRequest dataRequest, EventUpdateDTO patc
if (patch.getStatus() != null) {
var status = Status.valueOf(patch.getStatus().name());
dataRequest.setStatus(status);

try {
proxyClient.abortAnnouncement(dataRequest.getAnnouncementToken());
epsDataRequestClient.abortGuestListDataRequest(dataRequest);
} catch (IRISAnnouncementException | IRISDataRequestException e) {
e.printStackTrace();
// TODO: Should we do something here?
}
}

return repository.save(dataRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
import iris.client_bff.events.exceptions.IRISDataRequestException;

public interface DataProviderClient {
void requestGuestListData(EventDataRequest request) throws IRISDataRequestException;
void requestGuestListData(EventDataRequest request) throws IRISDataRequestException;

void abortGuestListDataRequest(EventDataRequest request) throws IRISDataRequestException;
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
package iris.client_bff.events.eps;

import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import iris.client_bff.events.EventDataRequest;
import iris.client_bff.events.EventDataRequestService;
import iris.client_bff.events.EventDataSubmissionService;
import iris.client_bff.events.web.dto.GuestList;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.UUID;

import org.springframework.stereotype.Service;

import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;

@Slf4j
@Service
@AutoJsonRpcServiceImpl
@RequiredArgsConstructor
public class DataSubmissionRPCImpl implements DataSubmissionRPC {

private final EventDataRequestService requestService;
private final EventDataSubmissionService dataSubmissionService;
private final EventDataRequestService requestService;
private final EventDataSubmissionService dataSubmissionService;

public String submitGuestList(JsonRpcClientDto client, UUID dataAuthorizationToken, GuestList guestList) {
log.trace("Start submission {}", dataAuthorizationToken);

public String submitGuestList(JsonRpcClientDto client, UUID dataAuthorizationToken, GuestList guestList) {
log.trace("Start submission {}", dataAuthorizationToken);
// Todo check client.getName() vs. providerId

// Todo check client.getName() vs. providerId
return requestService.findById(dataAuthorizationToken).map(dataRequest -> {

return requestService.findById(dataAuthorizationToken).map(dataRequest -> {
if (dataRequest.getStatus().equals(EventDataRequest.Status.ABORTED)) {
log.trace("Submission {} for aborted event {}", dataAuthorizationToken, dataRequest.getId());
return "Error: Submission not allowed for " + dataAuthorizationToken.toString() + ". Request was aborted.";
}

dataSubmissionService.save(dataRequest, guestList);
dataSubmissionService.save(dataRequest, guestList);

log.trace("Done submission {}", dataAuthorizationToken);
log.trace("Done submission {}", dataAuthorizationToken);

return "OK";
return "OK";

}).orElseGet(() -> {
}).orElseGet(() -> {

// TODO sufficient?
// probably throw exception
// TODO sufficient?
// probably throw exception

log.error("Data submission for unknown data request occurred: {}", dataAuthorizationToken);
log.error("Data submission for unknown data request occurred: {}", dataAuthorizationToken);

return "Unknown dataAuthorizationToken: " + dataAuthorizationToken.toString();
});
}
return "Unknown dataAuthorizationToken: " + dataAuthorizationToken.toString();
});
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package iris.client_bff.events.eps;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import iris.client_bff.events.EventDataRequest;
import iris.client_bff.events.exceptions.IRISDataRequestException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;

@Slf4j
@Service
@AllArgsConstructor
Expand Down Expand Up @@ -47,6 +49,32 @@ public void requestGuestListData(EventDataRequest request) throws IRISDataReques
}
}

@Override
public void abortGuestListDataRequest(EventDataRequest request) throws IRISDataRequestException {
var requestId = request.getId().toString();
var methodName = request.getLocation().getProviderId() + ".abortDataRequest";

var payload = Map.of(
"dataRequest", SubmitPayload.builder()
.start(request.getRequestStart())
.end(request.getRequestEnd())
.dataAuthorizationToken(requestId)
.proxyEndpoint(request.getAnnouncementToken())
.locationId(request.getLocation().getLocationId())
.requestDetails(request.getRequestDetails())
.build());

try {
log.trace("abortGuestListDataRequest start: method {}; request {}", methodName, requestId);
epsRpcClient.invoke(methodName, payload);
log.debug("abortGuestListDataRequest done: method {}; request {};", methodName, requestId);

} catch (Throwable t) {
log.error("abortGuestListDataRequest error: method {}; request {}; error {}", methodName, requestId, t);
throw new IRISDataRequestException(t);
}
}

@Data
@Builder
public static class SubmitPayload {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package iris.client_bff.proxy;

import com.fasterxml.jackson.annotation.JsonFormat;
import iris.client_bff.config.ProxyServiceConfig;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -14,54 +13,67 @@

import org.springframework.stereotype.Service;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;

@Slf4j
@Service
@AllArgsConstructor
public class EPSProxyServiceServiceClient implements ProxyServiceClient {

private final ProxyServiceConfig config;

private final JsonRpcHttpClient proxyRpcClient;
private final ProxyServiceConfig config;

@Override
public String announce() throws IRISAnnouncementException {
private final JsonRpcHttpClient proxyRpcClient;

var domain = UUID.randomUUID()
+ "."
+ config.getTargetSubdomain();
private String sendAnnouncement(String domain, Instant expirationTime) throws IRISAnnouncementException {
var announcementDto = AnnouncementDto.builder()
.domain(domain)
.expiresAt(expirationTime)
.proxy(config.getTargetProxy())
.build();

var oneWeekFromNow = Instant.now().plus(7, ChronoUnit.DAYS);
var methodName = config.getEpsName()
+ "."
+ "announceConnection";

var announcementDto = AnnouncementDto.builder()
.domain(domain)
.expiresAt(oneWeekFromNow)
.proxy(config.getTargetProxy())
.build();
try {
proxyRpcClient.invoke(methodName, announcementDto);
if (expirationTime.isBefore(Instant.now())) {
log.debug("Aborted announcement {} to {}", announcementDto.getDomain(), announcementDto.getProxy());
} else {
log.debug("Announced {} to {} till {}", announcementDto.getDomain(), announcementDto.getProxy(),
announcementDto.getExpiresAt());
}
} catch (Throwable throwable) {
throw new IRISAnnouncementException(throwable);
}

var methodName = config.getEpsName()
+ "."
+ "announceConnection";
return domain;
}

try {
proxyRpcClient.invoke(methodName, announcementDto);
log.debug("Announced {} to {} till {}", announcementDto.getDomain(), announcementDto.getProxy(), announcementDto.getExpiresAt());
} catch (Throwable throwable) {
throw new IRISAnnouncementException(throwable);
}
@Override
public String announce() throws IRISAnnouncementException {
var domain = UUID.randomUUID()
+ "."
+ config.getTargetSubdomain();
var oneWeekFromNow = Instant.now().plus(7, ChronoUnit.DAYS);
return this.sendAnnouncement(domain, oneWeekFromNow);
}

return domain;
}
@Override
public String abortAnnouncement(String domain) throws IRISAnnouncementException {
var oneWeekAgo = Instant.now().minus(7, ChronoUnit.DAYS);
return this.sendAnnouncement(domain, oneWeekAgo);
}

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class AnnouncementDto {
private String domain;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "UTC")
private Instant expiresAt;
private String proxy;
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class AnnouncementDto {
private String domain;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "UTC")
private Instant expiresAt;
private String proxy;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package iris.client_bff.proxy;

import java.util.UUID;

public interface ProxyServiceClient {
String announce() throws IRISAnnouncementException;
String announce() throws IRISAnnouncementException;

String abortAnnouncement(String domain) throws IRISAnnouncementException;
}